Home | History | Annotate | Line # | Download | only in gen
      1 /*	$NetBSD: sysctl.c,v 1.43 2026/07/10 21:54:30 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char sccsid[] = "@(#)sysctl.c	8.2 (Berkeley) 1/4/94";
     36 #else
     37 __RCSID("$NetBSD: sysctl.c,v 1.43 2026/07/10 21:54:30 kre Exp $");
     38 #endif
     39 #endif /* LIBC_SCCS and not lint */
     40 
     41 #include "namespace.h"
     42 #include <sys/param.h>
     43 #define __COMPAT_SYSCTL
     44 #include <sys/sysctl.h>
     45 
     46 #include <assert.h>
     47 #include <errno.h>
     48 #include <paths.h>
     49 #include <stdio.h>
     50 #include <string.h>
     51 #include <time.h>
     52 #include <unistd.h>
     53 #include "extern.h"
     54 
     55 #ifdef __weak_alias
     56 __weak_alias(sysctl,_sysctl)
     57 #endif
     58 
     59 /*
     60  * handles requests off the user subtree
     61  */
     62 static int user_sysctl(const int *, u_int, void *, size_t *,
     63 			const void *, size_t);
     64 
     65 /*
     66  * copies out individual nodes taking target version into account
     67  */
     68 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
     69 			     size_t *);
     70 
     71 #include <stdlib.h>
     72 
     73 int
     74 sysctl(const int *name, unsigned int namelen,
     75 	void *oldp, size_t *oldlenp,
     76 	const void *newp, size_t newlen)
     77 {
     78 	size_t oldlen, savelen;
     79 	int error;
     80 
     81 	if (name[0] != CTL_USER)
     82 		return (__sysctl(name, namelen, oldp, oldlenp,
     83 				 newp, newlen));
     84 
     85 	oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
     86 	savelen = oldlen;
     87 	error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
     88 
     89 	if (error != 0) {
     90 		errno = error;
     91 		return (-1);
     92 	}
     93 
     94 	if (oldlenp != NULL) {
     95 		*oldlenp = oldlen;
     96 		if (oldp != NULL && oldlen > savelen) {
     97 			errno = ENOMEM;
     98 			return (-1);
     99 		}
    100 	}
    101 
    102 	return (0);
    103 }
    104 
    105 static int
    106 user_sysctl(const int *name, unsigned int namelen,
    107 	void *oldp, size_t *oldlenp,
    108 	const void *newp, size_t newlen)
    109 {
    110 #ifndef SMALL
    111 #define X(a)	a
    112 #else
    113 #define	X(a)	""
    114 #endif
    115 #define _INT(s, n, v, d) {					\
    116 	.sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|	\
    117 			CTLTYPE_INT|SYSCTL_VERSION,		\
    118 	.sysctl_size = sizeof(int),				\
    119 	.sysctl_name = (s),					\
    120 	.sysctl_num = (n),					\
    121 	.sysctl_un.scu_idata = (v),				\
    122 	.sysctl_desc = X(d),					\
    123 	}
    124 
    125 #define _STRING(s, n, v, d) {					\
    126 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|CTLTYPE_STRING, \
    127 	.sysctl_size = sizeof(v),				\
    128 	.sysctl_name = (s),					\
    129 	.sysctl_num = (n),					\
    130 	.sysctl_data = __UNCONST(v),				\
    131 	.sysctl_desc = X(d),					\
    132 	}
    133 
    134 	/*
    135 	 * the nodes under the "user" node
    136 	 */
    137 	static const struct sysctlnode sysctl_usermib[] = {
    138 		_STRING("cs_path", USER_CS_PATH, _PATH_STDPATH,
    139 			"A value for the PATH environment variable "
    140 			"that finds all the standard utilities"),
    141 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX,
    142 		     "The maximum ibase/obase values in the bc(1) utility"),
    143 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX,
    144 		     "The maximum array size in the bc(1) utility"),
    145 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX,
    146 		     "The maximum scale value in the bc(1) utility"),
    147 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX,
    148 		     "The maximum string length in the bc(1) utility"),
    149 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
    150 		     COLL_WEIGHTS_MAX, "The maximum number of weights that can "
    151 		     "be assigned to any entry of the LC_COLLATE order keyword "
    152 		     "in the locale definition file"),
    153 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX,
    154 		     "The maximum number of expressions that can be nested "
    155 		     "within parenthesis by the expr(1) utility"),
    156 		_INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length "
    157 		     "in bytes of a text-processing utility's input line"),
    158 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum "
    159 		     "number of repeated occurrences of a regular expression "
    160 		     "permitted when using interval notation"),
    161 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION,
    162 		     "The version of POSIX 1003.2 with which the system "
    163 		     "attempts to comply"),
    164 #ifdef _POSIX2_C_BIND
    165 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1,
    166 		     "Whether the system's C-language development facilities "
    167 		     "support the C-Language Bindings Option"),
    168 #else
    169 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0,
    170 		     "Whether the system's C-language development facilities "
    171 		     "support the C-Language Bindings Option"),
    172 #endif
    173 #ifdef POSIX2_C_DEV
    174 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1,
    175 		     "Whether the system supports the C-Language Development "
    176 		     "Utilities Option"),
    177 #else
    178 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0,
    179 		     "Whether the system supports the C-Language Development "
    180 		     "Utilities Option"),
    181 #endif
    182 #ifdef POSIX2_CHAR_TERM
    183 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1,
    184 		     "Whether the system supports at least one terminal type "
    185 		     "capable of all operations described in POSIX 1003.2"),
    186 #else
    187 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0,
    188 		     "Whether the system supports at least one terminal type "
    189 		     "capable of all operations described in POSIX 1003.2"),
    190 #endif
    191 #ifdef POSIX2_FORT_DEV
    192 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1,
    193 		     "Whether the system supports the FORTRAN Development "
    194 		     "Utilities Option"),
    195 #else
    196 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0,
    197 		     "Whether the system supports the FORTRAN Development "
    198 		     "Utilities Option"),
    199 #endif
    200 #ifdef POSIX2_FORT_RUN
    201 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1,
    202 		     "Whether the system supports the FORTRAN Runtime "
    203 		     "Utilities Option"),
    204 #else
    205 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0,
    206 		     "Whether the system supports the FORTRAN Runtime "
    207 		     "Utilities Option"),
    208 #endif
    209 #ifdef POSIX2_LOCALEDEF
    210 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1,
    211 		     "Whether the system supports the creation of locales"),
    212 #else
    213 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0,
    214 		     "Whether the system supports the creation of locales"),
    215 #endif
    216 #ifdef POSIX2_SW_DEV
    217 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1,
    218 		     "Whether the system supports the Software Development "
    219 		     "Utilities Option"),
    220 #else
    221 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0,
    222 		     "Whether the system supports the Software Development "
    223 		     "Utilities Option"),
    224 #endif
    225 #ifdef POSIX2_UPE
    226 		_INT("posix2_upe", USER_POSIX2_UPE, 1,
    227 		     "Whether the system supports the User Portability "
    228 		     "Utilities Option"),
    229 #else
    230 		_INT("posix2_upe", USER_POSIX2_UPE, 0,
    231 		     "Whether the system supports the User Portability "
    232 		     "Utilities Option"),
    233 #endif
    234 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX,
    235 		     "The minimum maximum number of streams that a process "
    236 		     "may have open at any one time"),
    237 		_INT("tzname_max", USER_TZNAME_MAX, _TZNAME_MAXIMUM,
    238 		     "The minimum maximum number of bytes supported for the "
    239 		     "name of a timezone abbreviation"),
    240 		_INT("atexit_max", USER_ATEXIT_MAX, -1,
    241 		     "The maximum number of functions that may be registered "
    242 		     "with atexit(3)"),
    243 
    244 #ifdef USER_OSTYPE_VALUE
    245 		_STRING("ostype", USER_OSTYPE, USER_OSTYPE_VALUE,
    246 		     "The name of the operating system"),
    247 #endif
    248 #ifdef USER_OSREVISION_VALUE
    249 		_INT("osrevision", USER_OSREVISION, USER_OSREVISION_VALUE,
    250 		     "A numeric representation of the OS version"),
    251 #endif
    252 #ifdef USER_OSRELEASE_VALUE
    253 		_STRING("osrelease", USER_OSRELEASE, USER_OSRELEASE_VALUE,
    254 		     "The system version of the libraries & utilities"),
    255 #endif
    256 #ifdef USER_BUILDTIME_VALUE
    257 		_STRING("built", USER_BUILDTIME, USER_BUILDTIME_VALUE,
    258 		     "The date & time (UTC) at which libc was last built "
    259 		     "- if with a trailing '!' the reproducible build time"),
    260 #endif
    261 	};
    262 #undef X
    263 #undef _INT
    264 #undef _STRING
    265 
    266 	static const int clen = sizeof(sysctl_usermib) /
    267 		sizeof(sysctl_usermib[0]);
    268 
    269 	const struct sysctlnode *node;
    270 	int ni;
    271 	size_t l, sz;
    272 
    273 	/*
    274 	 * none of these nodes are writable and they're all terminal (for now)
    275 	 */
    276 	if (namelen != 1)
    277 		return (EINVAL);
    278 
    279 	l = *oldlenp;
    280 	if (name[0] == CTL_QUERY) {
    281 		uint v;
    282 		node = newp;
    283 		if (node == NULL)
    284 			return (EINVAL);
    285 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
    286 			 newlen == sizeof(struct sysctlnode))
    287 			v = SYSCTL_VERS_1;
    288 		else
    289 			return (EINVAL);
    290 
    291 		sz = 0;
    292 		for (ni = 0; ni < clen; ni++)
    293 			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
    294 		*oldlenp = sz;
    295 		return (0);
    296 	}
    297 
    298 	if (name[0] == CTL_DESCRIBE) {
    299 		/*
    300 		 * XXX make sure this is larger than the largest
    301 		 * "user" description
    302 		 */
    303 		char buf[192];
    304 		struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
    305 		size_t d;
    306 
    307 		node = newp;
    308 		if (node != NULL &&
    309 		    (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
    310 		     newlen != sizeof(struct sysctlnode)))
    311 			return (EINVAL);
    312 
    313 		sz = 0;
    314 		for (ni = 0; ni < clen; ni++) {
    315 			memset(&buf[0], 0, sizeof(buf));
    316 			if (node != NULL &&
    317 			    node->sysctl_num != sysctl_usermib[ni].sysctl_num)
    318 				continue;
    319 			d1->descr_num = sysctl_usermib[ni].sysctl_num;
    320 			d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
    321 			if (sysctl_usermib[ni].sysctl_desc == NULL)
    322 				d1->descr_len = 1;
    323 			else {
    324 				size_t dlen;
    325 				(void)strlcpy(d1->descr_str,
    326 					sysctl_usermib[ni].sysctl_desc,
    327 					sizeof(buf) - sizeof(*d1));
    328 				dlen = strlen(d1->descr_str) + 1;
    329 				_DIAGASSERT(__type_fit(uint32_t, dlen));
    330 				d1->descr_len = (uint32_t)dlen;
    331 			}
    332 			d = (size_t)__sysc_desc_len(d1->descr_len);
    333 			if (d2 != NULL)
    334 				memcpy(d2, d1, d);
    335 			sz += d;
    336 			d2 = (void *)((char *)(void *)d2 + d);
    337 			if (node != NULL)
    338 				break;
    339 		}
    340 		*oldlenp = sz;
    341 		if (sz == 0 && node != NULL)
    342 			return (ENOENT);
    343 		return (0);
    344 
    345 	}
    346 
    347 	/*
    348 	 * none of these nodes are writable
    349 	 */
    350 	if (newp != NULL || newlen != 0)
    351 		return (EPERM);
    352 
    353 	node = &sysctl_usermib[0];
    354 	for (ni = 0; ni	< clen; ni++)
    355 		if (name[0] == node[ni].sysctl_num)
    356 			break;
    357 	if (ni == clen)
    358 		return (EOPNOTSUPP);
    359 
    360 	node = &node[ni];
    361 	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
    362 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
    363 		case CTLTYPE_INT:
    364 			newp = &node->sysctl_idata;
    365 			break;
    366 		case CTLTYPE_QUAD:
    367 			newp = &node->sysctl_qdata;
    368 			break;
    369 		default:
    370 			return (EINVAL);
    371 		}
    372 	}
    373 	else
    374 		newp = node->sysctl_data;
    375 
    376 	l = MIN(l, node->sysctl_size);
    377 	if (oldp != NULL)
    378 		memcpy(oldp, newp, l);
    379 	*oldlenp = node->sysctl_size;
    380 
    381 	return (0);
    382 }
    383 
    384 static size_t
    385 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
    386 {
    387 	const void *src = n;
    388 	size_t sz;
    389 
    390 	switch (v) {
    391 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
    392 #error __cvt_node_out: no support for SYSCTL_VERSION
    393 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
    394 
    395 	case SYSCTL_VERSION:
    396 		sz = sizeof(struct sysctlnode);
    397 		break;
    398 
    399 	default:
    400 		sz = 0;
    401 		break;
    402 	}
    403 
    404 	if (sz > 0 && *o != NULL && *l >= sz) {
    405 		memcpy(*o, src, sz);
    406 		*o = sz + (caddr_t)*o;
    407 		*l -= sz;
    408 	}
    409 
    410 	return(sz);
    411 }
    412