git: af7d105379a6 - main - sysctlbyname(): restore access to user variables

From: Stefan Eßer <se_at_FreeBSD.org>
Date: Wed, 09 Feb 2022 22:10:42 UTC
The branch main has been updated by se:

URL: https://cgit.FreeBSD.org/src/commit/?id=af7d105379a649b7af4bffd15fbeab692bb52b69

commit af7d105379a649b7af4bffd15fbeab692bb52b69
Author:     Stefan Eßer <se@FreeBSD.org>
AuthorDate: 2022-02-09 21:56:00 +0000
Commit:     Stefan Eßer <se@FreeBSD.org>
CommitDate: 2022-02-09 22:10:31 +0000

    sysctlbyname(): restore access to user variables
    
    The optimization of sysctlbyname() in commit d05b53e0baee7 had the
    side-effect of not going through the fix-up for the user.* variables
    in the previously called sysctl() function.
    
    This lead to 0 or an empty strings being returned by sysctlbyname()
    for all user.* variables.
    
    An alternate implementation would store the user variables in the
    kernel during system start-up. That would allow to remove the fix-up
    code in the C library that is currently required to provide the actual
    values.
    
    This update restores the previous code path for the user.* variables
    and keeps the performance optimization intact for all other variables.
    
    Approved by:    mjg
    Reviewed by:    kaktus
    Differential Revision:  https://reviews.freebsd.org/D34171
---
 lib/libc/gen/sysctlbyname.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/lib/libc/gen/sysctlbyname.c b/lib/libc/gen/sysctlbyname.c
index 9b4ffc0ca4ae..5086cc4b7d81 100644
--- a/lib/libc/gen/sysctlbyname.c
+++ b/lib/libc/gen/sysctlbyname.c
@@ -41,8 +41,17 @@ sysctlbyname(const char *name, void *oldp, size_t *oldlenp,
     const void *newp, size_t newlen)
 {
 	size_t len;
+	int oid[2];
 
-	len = strlen(name);
-	return (__sysctlbyname(name, len, oldp, oldlenp, newp,
-	    newlen));
+	if (__predict_true(strncmp(name, "user.", 5) != 0)) {
+		len = strlen(name);
+		return (__sysctlbyname(name, len, oldp, oldlenp, newp,
+			newlen));
+	} else {
+		len = nitems(oid);
+		if (sysctlnametomib(name, oid, &len) == -1)
+			return (-1);
+		return (sysctl(oid, (u_int)len, oldp, oldlenp, newp,
+		    newlen));
+	}
 }