Re-arrange some of the code to separate writable user tree variables from

R/O variables.

While here fix some nearby style. No functional change intended.

MFC after:	1 month
This commit is contained in:
se 2020-11-02 18:48:06 +00:00
parent 3119959468
commit 6d510ac169

View File

@ -53,26 +53,42 @@ sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
int retval;
size_t orig_oldlen;
orig_oldlen = oldlenp ? *oldlenp : 0;
orig_oldlen = oldlenp != NULL ? *oldlenp : 0;
retval = __sysctl(name, namelen, oldp, oldlenp, newp, newlen);
/*
* All valid names under CTL_USER have a dummy entry in the sysctl
* tree (to support name lookups and enumerations) with an
* empty/zero value, and the true value is supplied by this routine.
* For all such names, __sysctl() is used solely to validate the
* name.
* Valid names under CTL_USER except USER_LOCALBASE have a dummy entry
* in the sysctl tree (to support name lookups and enumerations) with
* an empty/zero value, and the true value is supplied by this routine.
* For all such names, __sysctl() is used solely to validate the name.
*
* Return here unless there was a successful lookup for a CTL_USER
* name.
* Return here unless there was a successful lookup for a CTL_USER name.
*/
if (retval || name[0] != CTL_USER)
if (retval != 0 || name[0] != CTL_USER)
return (retval);
if (namelen != 2) {
errno = EINVAL;
return (-1);
}
if (newp != NULL && name[1] != USER_LOCALBASE) {
/* Variables under CLT_USER that may be overridden by kernel values */
switch (name[1]) {
case USER_LOCALBASE:
if (oldlenp == NULL || *oldlenp != 1)
return (0);
if (oldp != NULL) {
if (orig_oldlen < sizeof(_PATH_LOCALBASE)) {
errno = ENOMEM;
return (-1);
}
memmove(oldp, _PATH_LOCALBASE, sizeof(_PATH_LOCALBASE));
}
*oldlenp = sizeof(_PATH_LOCALBASE);
return (0);
}
/* Variables under CLT_USER whose values are immutably defined below */
if (newp != NULL) {
errno = EPERM;
return (-1);
}
@ -87,26 +103,9 @@ sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
if (oldp != NULL)
memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH));
return (0);
case USER_LOCALBASE:
if (oldlenp != NULL) {
if (oldp == NULL) {
if (*oldlenp == 1)
*oldlenp = sizeof(_PATH_LOCALBASE);
} else {
if (*oldlenp != 1)
return (retval);
if (orig_oldlen < sizeof(_PATH_LOCALBASE)) {
errno = ENOMEM;
return (-1);
}
*oldlenp = sizeof(_PATH_LOCALBASE);
memmove(oldp, _PATH_LOCALBASE, sizeof(_PATH_LOCALBASE));
}
}
return (0);
}
if (oldp && *oldlenp < sizeof(int)) {
if (oldp != NULL && *oldlenp < sizeof(int)) {
errno = ENOMEM;
return (-1);
}