Author: se Date: Mon Nov 2 18:48:06 2020 New Revision: 367280 URL: https://svnweb.freebsd.org/changeset/base/367280
Log: 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 Modified: head/lib/libc/gen/sysctl.c Modified: head/lib/libc/gen/sysctl.c ============================================================================== --- head/lib/libc/gen/sysctl.c Mon Nov 2 18:45:43 2020 (r367279) +++ head/lib/libc/gen/sysctl.c Mon Nov 2 18:48:06 2020 (r367280) @@ -53,26 +53,42 @@ sysctl(const int *name, u_int namelen, void *oldp, siz 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, siz 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); } _______________________________________________ svn-src-head@freebsd.org mailing list https://lists.freebsd.org/mailman/listinfo/svn-src-head To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"