Page MenuHomeFreeBSD

New implementation of getlocalbase()
ClosedPublic

Authored by se on Nov 25 2020, 10:47 AM.
Tags
None
Referenced Files
Unknown Object (File)
Sun, Apr 21, 1:12 PM
Unknown Object (File)
Feb 24 2024, 12:22 AM
Unknown Object (File)
Feb 24 2024, 12:22 AM
Unknown Object (File)
Feb 24 2024, 12:22 AM
Unknown Object (File)
Feb 23 2024, 11:47 AM
Unknown Object (File)
Jan 14 2024, 5:38 AM
Unknown Object (File)
Jan 8 2024, 2:50 PM
Unknown Object (File)
Dec 20 2023, 6:00 AM
Subscribers

Details

Summary

This implementation of getlocalbase() is based on the first version that I had submitted for review in D27236.
It uses a static buffer to retrieve the sysctl("user.localbase") value.
The size of this buffer can be reduced, but this will not have any effect on the execution size of this library, AFAICT.
The data segment size is slightly lower than 7KB without and 8KB with a buffer of size MAXPATHLEN, resulting in 8KB VM space being allocated in either case.

  • If the function is compiled with LOCALBASE_CTL_LEN=0, then no sysctl() calls will be made and the functionality is identical to the previously existing getenv("LOCALBASE") in programs like "pkg" or "mailwrapper".
  • If LOCALBASE_CTL_LEN is not overridden, then the buffer will be large enough to hold a string of length MAXPATHLEN, the maximum value supported by the sysctl variable "user.localbase".
  • If LOCALBASE_CTL_LEN is between 1 and MAXPATHLEN-1, then a sysctl value too large to fit in the internal buffer has been set, then a string not suitable for use as a path name ("/\177", a slash followed by a <DEL> character) is returned (under the assumption, that a directory named "<DEL>" will never exist in the systems root directory and thus any look-up of a path with that prefix will fail).

In order to keep calling programs simple I want to always return a valid string.

When built with a default value of LOCALBASE_CTL_LEN, no error case exists.
If LOCALBASE_CTL_LEN is reduced and the kernel buffer for the user.localbase sysctl variable is reduced to the same value (e.g. 64 characters, which should be a reasonable upper limit for the LOCALBASE prefix), there is no error case.
I'd rather not complicate all programs that call this function to prevent an error case and thus propose the inline signaling of this error case by returning a valid string that is not a valid file name prefix.

Test Plan

Build and install libutil with this patch.
Test programs that use getlocalbase() e.g. "pkg" or "mailwrapper" with LOCALBASE set in the environment or the user.localbase sysctl variable set to a non-default value.

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

se requested review of this revision.Nov 25 2020, 10:47 AM
getlocalbase.c
75 ↗(On Diff #79987)

Instead of "/\177" I could also imagine "/dev/null/" or any other path that is known to not possibly exist in FreeBSD.

0mp added inline comments.
getlocalbase.c
75 ↗(On Diff #79987)

We use /nonexistent for user homes. Perhaphs this one could be a candidate?

This revision was not accepted when it landed; it landed in state Needs Review.Dec 12 2020, 11:24 AM
This revision was automatically updated to reflect the committed changes.