Home | History | Annotate | Line # | Download | only in gen
      1 /*	$NetBSD: sysctl.c,v 1.39 2025/09/27 21:00:41 christos 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.39 2025/09/27 21:00:41 christos 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 <unistd.h>
     52 #include "extern.h"
     53 
     54 #ifdef __weak_alias
     55 __weak_alias(sysctl,_sysctl)
     56 #endif
     57 
     58 /*
     59  * handles requests off the user subtree
     60  */
     61 static int user_sysctl(const int *, u_int, void *, size_t *,
     62 			const void *, size_t);
     63 
     64 /*
     65  * copies out individual nodes taking target version into account
     66  */
     67 static size_t __cvt_node_out(uint, const struct sysctlnode *, void **,
     68 			     size_t *);
     69 
     70 #include <stdlib.h>
     71 
     72 int
     73 sysctl(const int *name, unsigned int namelen,
     74 	void *oldp, size_t *oldlenp,
     75 	const void *newp, size_t newlen)
     76 {
     77 	size_t oldlen, savelen;
     78 	int error;
     79 
     80 	if (name[0] != CTL_USER)
     81 		return (__sysctl(name, namelen, oldp, oldlenp,
     82 				 newp, newlen));
     83 
     84 	oldlen = (oldlenp == NULL) ? 0 : *oldlenp;
     85 	savelen = oldlen;
     86 	error = user_sysctl(name + 1, namelen - 1, oldp, &oldlen, newp, newlen);
     87 
     88 	if (error != 0) {
     89 		errno = error;
     90 		return (-1);
     91 	}
     92 
     93 	if (oldlenp != NULL) {
     94 		*oldlenp = oldlen;
     95 		if (oldp != NULL && oldlen > savelen) {
     96 			errno = ENOMEM;
     97 			return (-1);
     98 		}
     99 	}
    100 
    101 	return (0);
    102 }
    103 
    104 static int
    105 user_sysctl(const int *name, unsigned int namelen,
    106 	void *oldp, size_t *oldlenp,
    107 	const void *newp, size_t newlen)
    108 {
    109 #ifndef SMALL
    110 #define X(a)	a
    111 #else
    112 #define	X(a)	""
    113 #endif
    114 #define _INT(s, n, v, d) {					\
    115 	.sysctl_flags = CTLFLAG_IMMEDIATE|CTLFLAG_PERMANENT|	\
    116 			CTLTYPE_INT|SYSCTL_VERSION,		\
    117 	.sysctl_size = sizeof(int),				\
    118 	.sysctl_name = (s),					\
    119 	.sysctl_num = (n),					\
    120 	.sysctl_un.scu_idata = (v),				\
    121 	.sysctl_desc = X(d),					\
    122 	}
    123 
    124 	/*
    125 	 * the nodes under the "user" node
    126 	 */
    127 	static const struct sysctlnode sysctl_usermib[] = {
    128 		{
    129 			.sysctl_flags = SYSCTL_VERSION|CTLFLAG_PERMANENT|
    130 				CTLTYPE_STRING,
    131 			.sysctl_size = sizeof(_PATH_STDPATH),
    132 			.sysctl_name = "cs_path",
    133 			.sysctl_num = USER_CS_PATH,
    134 			.sysctl_data = __UNCONST(_PATH_STDPATH),
    135 			.sysctl_desc = __UNCONST(
    136 				"A value for the PATH environment variable "
    137 				"that finds all the standard utilities"),
    138 		},
    139 		_INT("bc_base_max", USER_BC_BASE_MAX, BC_BASE_MAX,
    140 		     "The maximum ibase/obase values in the bc(1) utility"),
    141 		_INT("bc_dim_max", USER_BC_DIM_MAX, BC_DIM_MAX,
    142 		     "The maximum array size in the bc(1) utility"),
    143 		_INT("bc_scale_max", USER_BC_SCALE_MAX, BC_SCALE_MAX,
    144 		     "The maximum scale value in the bc(1) utility"),
    145 		_INT("bc_string_max", USER_BC_STRING_MAX, BC_STRING_MAX,
    146 		     "The maximum string length in the bc(1) utility"),
    147 		_INT("coll_weights_max", USER_COLL_WEIGHTS_MAX,
    148 		     COLL_WEIGHTS_MAX, "The maximum number of weights that can "
    149 		     "be assigned to any entry of the LC_COLLATE order keyword "
    150 		     "in the locale definition file"),
    151 		_INT("expr_nest_max", USER_EXPR_NEST_MAX, EXPR_NEST_MAX,
    152 		     "The maximum number of expressions that can be nested "
    153 		     "within parenthesis by the expr(1) utility"),
    154 		_INT("line_max", USER_LINE_MAX, LINE_MAX, "The maximum length "
    155 		     "in bytes of a text-processing utility's input line"),
    156 		_INT("re_dup_max", USER_RE_DUP_MAX, RE_DUP_MAX, "The maximum "
    157 		     "number of repeated occurrences of a regular expression "
    158 		     "permitted when using interval notation"),
    159 		_INT("posix2_version", USER_POSIX2_VERSION, _POSIX2_VERSION,
    160 		     "The version of POSIX 1003.2 with which the system "
    161 		     "attempts to comply"),
    162 #ifdef _POSIX2_C_BIND
    163 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 1,
    164 		     "Whether the system's C-language development facilities "
    165 		     "support the C-Language Bindings Option"),
    166 #else
    167 		_INT("posix2_c_bind", USER_POSIX2_C_BIND, 0,
    168 		     "Whether the system's C-language development facilities "
    169 		     "support the C-Language Bindings Option"),
    170 #endif
    171 #ifdef POSIX2_C_DEV
    172 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 1,
    173 		     "Whether the system supports the C-Language Development "
    174 		     "Utilities Option"),
    175 #else
    176 		_INT("posix2_c_dev", USER_POSIX2_C_DEV, 0,
    177 		     "Whether the system supports the C-Language Development "
    178 		     "Utilities Option"),
    179 #endif
    180 #ifdef POSIX2_CHAR_TERM
    181 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 1,
    182 		     "Whether the system supports at least one terminal type "
    183 		     "capable of all operations described in POSIX 1003.2"),
    184 #else
    185 		_INT("posix2_char_term", USER_POSIX2_CHAR_TERM, 0,
    186 		     "Whether the system supports at least one terminal type "
    187 		     "capable of all operations described in POSIX 1003.2"),
    188 #endif
    189 #ifdef POSIX2_FORT_DEV
    190 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 1,
    191 		     "Whether the system supports the FORTRAN Development "
    192 		     "Utilities Option"),
    193 #else
    194 		_INT("posix2_fort_dev", USER_POSIX2_FORT_DEV, 0,
    195 		     "Whether the system supports the FORTRAN Development "
    196 		     "Utilities Option"),
    197 #endif
    198 #ifdef POSIX2_FORT_RUN
    199 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 1,
    200 		     "Whether the system supports the FORTRAN Runtime "
    201 		     "Utilities Option"),
    202 #else
    203 		_INT("posix2_fort_run", USER_POSIX2_FORT_RUN, 0,
    204 		     "Whether the system supports the FORTRAN Runtime "
    205 		     "Utilities Option"),
    206 #endif
    207 #ifdef POSIX2_LOCALEDEF
    208 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 1,
    209 		     "Whether the system supports the creation of locales"),
    210 #else
    211 		_INT("posix2_localedef", USER_POSIX2_LOCALEDEF, 0,
    212 		     "Whether the system supports the creation of locales"),
    213 #endif
    214 #ifdef POSIX2_SW_DEV
    215 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 1,
    216 		     "Whether the system supports the Software Development "
    217 		     "Utilities Option"),
    218 #else
    219 		_INT("posix2_sw_dev", USER_POSIX2_SW_DEV, 0,
    220 		     "Whether the system supports the Software Development "
    221 		     "Utilities Option"),
    222 #endif
    223 #ifdef POSIX2_UPE
    224 		_INT("posix2_upe", USER_POSIX2_UPE, 1,
    225 		     "Whether the system supports the User Portability "
    226 		     "Utilities Option"),
    227 #else
    228 		_INT("posix2_upe", USER_POSIX2_UPE, 0,
    229 		     "Whether the system supports the User Portability "
    230 		     "Utilities Option"),
    231 #endif
    232 		_INT("stream_max", USER_STREAM_MAX, FOPEN_MAX,
    233 		     "The minimum maximum number of streams that a process "
    234 		     "may have open at any one time"),
    235 		_INT("tzname_max", USER_TZNAME_MAX, NAME_MAX,
    236 		     "The minimum maximum number of types supported for the "
    237 		     "name of a timezone"),
    238 		_INT("atexit_max", USER_ATEXIT_MAX, -1,
    239 		     "The maximum number of functions that may be registered "
    240 		     "with atexit(3)"),
    241 	};
    242 #undef _INT
    243 
    244 	static const int clen = sizeof(sysctl_usermib) /
    245 		sizeof(sysctl_usermib[0]);
    246 
    247 	const struct sysctlnode *node;
    248 	int ni;
    249 	size_t l, sz;
    250 
    251 	/*
    252 	 * none of these nodes are writable and they're all terminal (for now)
    253 	 */
    254 	if (namelen != 1)
    255 		return (EINVAL);
    256 
    257 	l = *oldlenp;
    258 	if (name[0] == CTL_QUERY) {
    259 		uint v;
    260 		node = newp;
    261 		if (node == NULL)
    262 			return (EINVAL);
    263 		else if (SYSCTL_VERS(node->sysctl_flags) == SYSCTL_VERS_1 &&
    264 			 newlen == sizeof(struct sysctlnode))
    265 			v = SYSCTL_VERS_1;
    266 		else
    267 			return (EINVAL);
    268 
    269 		sz = 0;
    270 		for (ni = 0; ni < clen; ni++)
    271 			sz += __cvt_node_out(v, &sysctl_usermib[ni], &oldp, &l);
    272 		*oldlenp = sz;
    273 		return (0);
    274 	}
    275 
    276 	if (name[0] == CTL_DESCRIBE) {
    277 		/*
    278 		 * XXX make sure this is larger than the largest
    279 		 * "user" description
    280 		 */
    281 		char buf[192];
    282 		struct sysctldesc *d1 = (void *)&buf[0], *d2 = oldp;
    283 		size_t d;
    284 
    285 		node = newp;
    286 		if (node != NULL &&
    287 		    (SYSCTL_VERS(node->sysctl_flags) < SYSCTL_VERS_1 ||
    288 		     newlen != sizeof(struct sysctlnode)))
    289 			return (EINVAL);
    290 
    291 		sz = 0;
    292 		for (ni = 0; ni < clen; ni++) {
    293 			memset(&buf[0], 0, sizeof(buf));
    294 			if (node != NULL &&
    295 			    node->sysctl_num != sysctl_usermib[ni].sysctl_num)
    296 				continue;
    297 			d1->descr_num = sysctl_usermib[ni].sysctl_num;
    298 			d1->descr_ver = sysctl_usermib[ni].sysctl_ver;
    299 			if (sysctl_usermib[ni].sysctl_desc == NULL)
    300 				d1->descr_len = 1;
    301 			else {
    302 				size_t dlen;
    303 				(void)strlcpy(d1->descr_str,
    304 					sysctl_usermib[ni].sysctl_desc,
    305 					sizeof(buf) - sizeof(*d1));
    306 				dlen = strlen(d1->descr_str) + 1;
    307 				_DIAGASSERT(__type_fit(uint32_t, dlen));
    308 				d1->descr_len = (uint32_t)dlen;
    309 			}
    310 			d = (size_t)__sysc_desc_len(d1->descr_len);
    311 			if (d2 != NULL)
    312 				memcpy(d2, d1, d);
    313 			sz += d;
    314 			d2 = (void *)((char *)(void *)d2 + d);
    315 			if (node != NULL)
    316 				break;
    317 		}
    318 		*oldlenp = sz;
    319 		if (sz == 0 && node != NULL)
    320 			return (ENOENT);
    321 		return (0);
    322 
    323 	}
    324 
    325 	/*
    326 	 * none of these nodes are writable
    327 	 */
    328 	if (newp != NULL || newlen != 0)
    329 		return (EPERM);
    330 
    331 	node = &sysctl_usermib[0];
    332 	for (ni = 0; ni	< clen; ni++)
    333 		if (name[0] == node[ni].sysctl_num)
    334 			break;
    335 	if (ni == clen)
    336 		return (EOPNOTSUPP);
    337 
    338 	node = &node[ni];
    339 	if (node->sysctl_flags & CTLFLAG_IMMEDIATE) {
    340 		switch (SYSCTL_TYPE(node->sysctl_flags)) {
    341 		case CTLTYPE_INT:
    342 			newp = &node->sysctl_idata;
    343 			break;
    344 		case CTLTYPE_QUAD:
    345 			newp = &node->sysctl_qdata;
    346 			break;
    347 		default:
    348 			return (EINVAL);
    349 		}
    350 	}
    351 	else
    352 		newp = node->sysctl_data;
    353 
    354 	l = MIN(l, node->sysctl_size);
    355 	if (oldp != NULL)
    356 		memcpy(oldp, newp, l);
    357 	*oldlenp = node->sysctl_size;
    358 
    359 	return (0);
    360 }
    361 
    362 static size_t
    363 __cvt_node_out(uint v, const struct sysctlnode *n, void **o, size_t *l)
    364 {
    365 	const void *src = n;
    366 	size_t sz;
    367 
    368 	switch (v) {
    369 #if (SYSCTL_VERSION != SYSCTL_VERS_1)
    370 #error __cvt_node_out: no support for SYSCTL_VERSION
    371 #endif /* (SYSCTL_VERSION != SYSCTL_VERS_1) */
    372 
    373 	case SYSCTL_VERSION:
    374 		sz = sizeof(struct sysctlnode);
    375 		break;
    376 
    377 	default:
    378 		sz = 0;
    379 		break;
    380 	}
    381 
    382 	if (sz > 0 && *o != NULL && *l >= sz) {
    383 		memcpy(*o, src, sz);
    384 		*o = sz + (caddr_t)*o;
    385 		*l -= sz;
    386 	}
    387 
    388 	return(sz);
    389 }
    390