xref: /netbsd-src/lib/libc/gen/sysctl.c (revision 73704c4ce4ee2a60eb617e693ce7e9f03902613e)
1 /*	$NetBSD: sysctl.c,v 1.13 2003/08/07 16:42:57 agc 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.13 2003/08/07 16:42:57 agc Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 
45 #include <assert.h>
46 #include <errno.h>
47 #include <limits.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 int
59 sysctl(name, namelen, oldp, oldlenp, newp, newlen)
60 	int *name;
61 	unsigned int namelen;
62 	void *oldp;
63 	const void *newp;
64 	size_t *oldlenp, newlen;
65 {
66 
67 	_DIAGASSERT(name != NULL);
68 
69 	if (name[0] != CTL_USER)
70 		/* LINTED will fix when sysctl interface gets corrected */
71 		return (__sysctl(name, namelen, oldp, oldlenp,
72 		    (void *)newp, newlen));
73 
74 	if (newp != NULL) {
75 		errno = EPERM;
76 		return (-1);
77 	}
78 	if (namelen != 2) {
79 		errno = EINVAL;
80 		return (-1);
81 	}
82 
83 	switch (name[1]) {
84 	case USER_CS_PATH:
85 		if (oldp && *oldlenp < sizeof(_PATH_STDPATH))
86 			return (ENOMEM);
87 		*oldlenp = sizeof(_PATH_STDPATH);
88 		if (oldp != NULL)
89 			memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH));
90 		return (0);
91 	}
92 
93 	if (oldp && *oldlenp < sizeof(int))
94 		return (ENOMEM);
95 	*oldlenp = sizeof(int);
96 	if (oldp == NULL)
97 		return (0);
98 
99 	switch (name[1]) {
100 	case USER_BC_BASE_MAX:
101 		*(int *)oldp = BC_BASE_MAX;
102 		return (0);
103 	case USER_BC_DIM_MAX:
104 		*(int *)oldp = BC_DIM_MAX;
105 		return (0);
106 	case USER_BC_SCALE_MAX:
107 		*(int *)oldp = BC_SCALE_MAX;
108 		return (0);
109 	case USER_BC_STRING_MAX:
110 		*(int *)oldp = BC_STRING_MAX;
111 		return (0);
112 	case USER_COLL_WEIGHTS_MAX:
113 		*(int *)oldp = COLL_WEIGHTS_MAX;
114 		return (0);
115 	case USER_EXPR_NEST_MAX:
116 		*(int *)oldp = EXPR_NEST_MAX;
117 		return (0);
118 	case USER_LINE_MAX:
119 		*(int *)oldp = LINE_MAX;
120 		return (0);
121 	case USER_RE_DUP_MAX:
122 		*(int *)oldp = RE_DUP_MAX;
123 		return (0);
124 	case USER_POSIX2_VERSION:
125 		*(int *)oldp = _POSIX2_VERSION;
126 		return (0);
127 	case USER_POSIX2_C_BIND:
128 #ifdef POSIX2_C_BIND
129 		*(int *)oldp = 1;
130 #else
131 		*(int *)oldp = 0;
132 #endif
133 		return (0);
134 	case USER_POSIX2_C_DEV:
135 #ifdef	POSIX2_C_DEV
136 		*(int *)oldp = 1;
137 #else
138 		*(int *)oldp = 0;
139 #endif
140 		return (0);
141 	case USER_POSIX2_CHAR_TERM:
142 #ifdef	POSIX2_CHAR_TERM
143 		*(int *)oldp = 1;
144 #else
145 		*(int *)oldp = 0;
146 #endif
147 		return (0);
148 	case USER_POSIX2_FORT_DEV:
149 #ifdef	POSIX2_FORT_DEV
150 		*(int *)oldp = 1;
151 #else
152 		*(int *)oldp = 0;
153 #endif
154 		return (0);
155 	case USER_POSIX2_FORT_RUN:
156 #ifdef	POSIX2_FORT_RUN
157 		*(int *)oldp = 1;
158 #else
159 		*(int *)oldp = 0;
160 #endif
161 		return (0);
162 	case USER_POSIX2_LOCALEDEF:
163 #ifdef	POSIX2_LOCALEDEF
164 		*(int *)oldp = 1;
165 #else
166 		*(int *)oldp = 0;
167 #endif
168 		return (0);
169 	case USER_POSIX2_SW_DEV:
170 #ifdef	POSIX2_SW_DEV
171 		*(int *)oldp = 1;
172 #else
173 		*(int *)oldp = 0;
174 #endif
175 		return (0);
176 	case USER_POSIX2_UPE:
177 #ifdef	POSIX2_UPE
178 		*(int *)oldp = 1;
179 #else
180 		*(int *)oldp = 0;
181 #endif
182 		return (0);
183 	case USER_STREAM_MAX:
184 		*(int *)oldp = FOPEN_MAX;
185 		return (0);
186 	case USER_TZNAME_MAX:
187 		*(int *)oldp = NAME_MAX;
188 		return (0);
189 	case USER_ATEXIT_MAX:
190 		*(int *)oldp = -1; /* ANSI C minimum provided; not limited */
191 		return (0);
192 	default:
193 		errno = EINVAL;
194 		return (-1);
195 	}
196 	/* NOTREACHED */
197 }
198