xref: /freebsd/lib/libc/gen/sysctl.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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/param.h>
33 #include <sys/sysctl.h>
34 
35 #include <errno.h>
36 #include <limits.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <string.h>
41 
42 extern int __sysctl(const int *name, u_int namelen, void *oldp,
43     size_t *oldlenp, const void *newp, size_t newlen);
44 
45 static int
46 set_user_str(void *dstp, size_t *dstlenp, const char *src, size_t len,
47     size_t maxlen)
48 {
49 	int retval;
50 
51 	retval = 0;
52 	if (dstp != NULL) {
53 		if (len > maxlen) {
54 			len = maxlen;
55 			errno = ENOMEM;
56 			retval = -1;
57 		}
58 		memcpy(dstp, src, len);
59 	}
60 	*dstlenp = len;
61 	return (retval);
62 }
63 
64 int
65 sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
66     const void *newp, size_t newlen)
67 {
68 	int retval;
69 	size_t orig_oldlen;
70 
71 	orig_oldlen = oldlenp != NULL ? *oldlenp : 0;
72 	retval = __sysctl(name, namelen, oldp, oldlenp, newp, newlen);
73 	/*
74 	 * Valid names under CTL_USER except USER_LOCALBASE have a dummy entry
75 	 * in the sysctl tree (to support name lookups and enumerations) with
76 	 * an empty/zero value, and the true value is supplied by this routine.
77 	 * For all such names, __sysctl() is used solely to validate the name.
78 	 *
79 	 * Return here unless there was a successful lookup for a CTL_USER name.
80 	 */
81 	if (retval != 0 || name[0] != CTL_USER)
82 		return (retval);
83 
84 	if (namelen != 2) {
85 		errno = EINVAL;
86 		return (-1);
87 	}
88 
89 	/* Variables under CLT_USER that may be overridden by kernel values */
90 	switch (name[1]) {
91 	case USER_LOCALBASE:
92 		if (oldlenp == NULL || *oldlenp > sizeof(""))
93 			return (0);
94 		return (set_user_str(oldp, oldlenp, _PATH_LOCALBASE,
95 			sizeof(_PATH_LOCALBASE), orig_oldlen));
96 	}
97 
98 	/* Variables under CLT_USER whose values are immutably defined below */
99 	if (newp != NULL) {
100 		errno = EPERM;
101 		return (-1);
102 	}
103 
104 	switch (name[1]) {
105 	case USER_CS_PATH:
106 		return (set_user_str(oldp, oldlenp, _PATH_STDPATH,
107 		    sizeof(_PATH_STDPATH), orig_oldlen));
108 	}
109 
110 	if (oldp != NULL && *oldlenp < sizeof(int)) {
111 		errno = ENOMEM;
112 		return (-1);
113 	}
114 	*oldlenp = sizeof(int);
115 	if (oldp == NULL)
116 		return (0);
117 
118 	switch (name[1]) {
119 	case USER_BC_BASE_MAX:
120 		*(int *)oldp = BC_BASE_MAX;
121 		return (0);
122 	case USER_BC_DIM_MAX:
123 		*(int *)oldp = BC_DIM_MAX;
124 		return (0);
125 	case USER_BC_SCALE_MAX:
126 		*(int *)oldp = BC_SCALE_MAX;
127 		return (0);
128 	case USER_BC_STRING_MAX:
129 		*(int *)oldp = BC_STRING_MAX;
130 		return (0);
131 	case USER_COLL_WEIGHTS_MAX:
132 		*(int *)oldp = COLL_WEIGHTS_MAX;
133 		return (0);
134 	case USER_EXPR_NEST_MAX:
135 		*(int *)oldp = EXPR_NEST_MAX;
136 		return (0);
137 	case USER_LINE_MAX:
138 		*(int *)oldp = LINE_MAX;
139 		return (0);
140 	case USER_RE_DUP_MAX:
141 		*(int *)oldp = RE_DUP_MAX;
142 		return (0);
143 	case USER_POSIX2_VERSION:
144 		*(int *)oldp = _POSIX2_VERSION;
145 		return (0);
146 	case USER_POSIX2_C_BIND:
147 #ifdef POSIX2_C_BIND
148 		*(int *)oldp = 1;
149 #else
150 		*(int *)oldp = 0;
151 #endif
152 		return (0);
153 	case USER_POSIX2_C_DEV:
154 #ifdef	POSIX2_C_DEV
155 		*(int *)oldp = 1;
156 #else
157 		*(int *)oldp = 0;
158 #endif
159 		return (0);
160 	case USER_POSIX2_CHAR_TERM:
161 #ifdef	POSIX2_CHAR_TERM
162 		*(int *)oldp = 1;
163 #else
164 		*(int *)oldp = 0;
165 #endif
166 		return (0);
167 	case USER_POSIX2_FORT_DEV:
168 #ifdef	POSIX2_FORT_DEV
169 		*(int *)oldp = 1;
170 #else
171 		*(int *)oldp = 0;
172 #endif
173 		return (0);
174 	case USER_POSIX2_FORT_RUN:
175 #ifdef	POSIX2_FORT_RUN
176 		*(int *)oldp = 1;
177 #else
178 		*(int *)oldp = 0;
179 #endif
180 		return (0);
181 	case USER_POSIX2_LOCALEDEF:
182 #ifdef	POSIX2_LOCALEDEF
183 		*(int *)oldp = 1;
184 #else
185 		*(int *)oldp = 0;
186 #endif
187 		return (0);
188 	case USER_POSIX2_SW_DEV:
189 #ifdef	POSIX2_SW_DEV
190 		*(int *)oldp = 1;
191 #else
192 		*(int *)oldp = 0;
193 #endif
194 		return (0);
195 	case USER_POSIX2_UPE:
196 #ifdef	POSIX2_UPE
197 		*(int *)oldp = 1;
198 #else
199 		*(int *)oldp = 0;
200 #endif
201 		return (0);
202 	case USER_STREAM_MAX:
203 		*(int *)oldp = FOPEN_MAX;
204 		return (0);
205 	case USER_TZNAME_MAX:
206 		*(int *)oldp = NAME_MAX;
207 		return (0);
208 	default:
209 		errno = EINVAL;
210 		return (-1);
211 	}
212 	/* NOTREACHED */
213 }
214