xref: /freebsd/lib/libc/gen/sysctlbyname.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  */
10 
11 #include <sys/cdefs.h>
12 __FBSDID("$FreeBSD$");
13 
14 #include <sys/types.h>
15 #include <sys/sysctl.h>
16 #include <string.h>
17 
18 int
19 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
20 	     size_t newlen)
21 {
22 	int name2oid_oid[2];
23 	int real_oid[CTL_MAXNAME+2];
24 	int error;
25 	size_t oidlen;
26 
27 	name2oid_oid[0] = 0;	/* This is magic & undocumented! */
28 	name2oid_oid[1] = 3;
29 
30 	oidlen = sizeof(real_oid);
31 	error = sysctl(name2oid_oid, 2, real_oid, &oidlen, (void *)name,
32 		       strlen(name));
33 	if (error < 0)
34 		return error;
35 	oidlen /= sizeof (int);
36 	error = sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen);
37 	return (error);
38 }
39 
40