xref: /netbsd-src/lib/libc/gen/sysconf.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: sysconf.c,v 1.5 1997/07/21 14:07:35 jtc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Sean Eric Fagan of Cygnus Support.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 #if 0
42 static char sccsid[] = "@(#)sysconf.c	8.2 (Berkeley) 3/20/94";
43 #else
44 __RCSID("$NetBSD: sysconf.c,v 1.5 1997/07/21 14:07:35 jtc Exp $");
45 #endif
46 #endif /* LIBC_SCCS and not lint */
47 
48 #include "namespace.h"
49 #include <sys/param.h>
50 #include <sys/sysctl.h>
51 #include <sys/time.h>
52 #include <sys/resource.h>
53 
54 #include <errno.h>
55 #include <unistd.h>
56 
57 #ifdef __weak_alias
58 __weak_alias(sysconf,_sysconf);
59 #endif
60 
61 /*
62  * sysconf --
63  *	get configurable system variables.
64  *
65  * XXX
66  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
67  * not change during the lifetime of the calling process.  This would seem
68  * to require that any change to system limits kill all running processes.
69  * A workaround might be to cache the values when they are first retrieved
70  * and then simply return the cached value on subsequent calls.  This is
71  * less useful than returning up-to-date values, however.
72  */
73 long
74 sysconf(name)
75 	int name;
76 {
77 	struct rlimit rl;
78 	size_t len;
79 	int mib[2], value;
80 
81 	len = sizeof(value);
82 
83 	switch (name) {
84 /* 1003.1 */
85 	case _SC_ARG_MAX:
86 		mib[0] = CTL_KERN;
87 		mib[1] = KERN_ARGMAX;
88 		break;
89 	case _SC_CHILD_MAX:
90 		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
91 	case _SC_CLK_TCK:
92 		return (CLK_TCK);
93 	case _SC_JOB_CONTROL:
94 		mib[0] = CTL_KERN;
95 		mib[1] = KERN_JOB_CONTROL;
96 		goto yesno;
97 	case _SC_NGROUPS_MAX:
98 		mib[0] = CTL_KERN;
99 		mib[1] = KERN_NGROUPS;
100 		break;
101 	case _SC_OPEN_MAX:
102 		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
103 	case _SC_STREAM_MAX:
104 		mib[0] = CTL_USER;
105 		mib[1] = USER_STREAM_MAX;
106 		break;
107 	case _SC_TZNAME_MAX:
108 		mib[0] = CTL_USER;
109 		mib[1] = USER_TZNAME_MAX;
110 		break;
111 	case _SC_SAVED_IDS:
112 		mib[0] = CTL_KERN;
113 		mib[1] = KERN_SAVED_IDS;
114 		goto yesno;
115 	case _SC_VERSION:
116 		mib[0] = CTL_KERN;
117 		mib[1] = KERN_POSIX1;
118 		break;
119 
120 /* 1003.2 */
121 	case _SC_BC_BASE_MAX:
122 		mib[0] = CTL_USER;
123 		mib[1] = USER_BC_BASE_MAX;
124 		break;
125 	case _SC_BC_DIM_MAX:
126 		mib[0] = CTL_USER;
127 		mib[1] = USER_BC_DIM_MAX;
128 		break;
129 	case _SC_BC_SCALE_MAX:
130 		mib[0] = CTL_USER;
131 		mib[1] = USER_BC_SCALE_MAX;
132 		break;
133 	case _SC_BC_STRING_MAX:
134 		mib[0] = CTL_USER;
135 		mib[1] = USER_BC_STRING_MAX;
136 		break;
137 	case _SC_COLL_WEIGHTS_MAX:
138 		mib[0] = CTL_USER;
139 		mib[1] = USER_COLL_WEIGHTS_MAX;
140 		break;
141 	case _SC_EXPR_NEST_MAX:
142 		mib[0] = CTL_USER;
143 		mib[1] = USER_EXPR_NEST_MAX;
144 		break;
145 	case _SC_LINE_MAX:
146 		mib[0] = CTL_USER;
147 		mib[1] = USER_LINE_MAX;
148 		break;
149 	case _SC_RE_DUP_MAX:
150 		mib[0] = CTL_USER;
151 		mib[1] = USER_RE_DUP_MAX;
152 		break;
153 	case _SC_2_VERSION:
154 		mib[0] = CTL_USER;
155 		mib[1] = USER_POSIX2_VERSION;
156 		break;
157 	case _SC_2_C_BIND:
158 		mib[0] = CTL_USER;
159 		mib[1] = USER_POSIX2_C_BIND;
160 		goto yesno;
161 	case _SC_2_C_DEV:
162 		mib[0] = CTL_USER;
163 		mib[1] = USER_POSIX2_C_DEV;
164 		goto yesno;
165 	case _SC_2_CHAR_TERM:
166 		mib[0] = CTL_USER;
167 		mib[1] = USER_POSIX2_CHAR_TERM;
168 		goto yesno;
169 	case _SC_2_FORT_DEV:
170 		mib[0] = CTL_USER;
171 		mib[1] = USER_POSIX2_FORT_DEV;
172 		goto yesno;
173 	case _SC_2_FORT_RUN:
174 		mib[0] = CTL_USER;
175 		mib[1] = USER_POSIX2_FORT_RUN;
176 		goto yesno;
177 	case _SC_2_LOCALEDEF:
178 		mib[0] = CTL_USER;
179 		mib[1] = USER_POSIX2_LOCALEDEF;
180 		goto yesno;
181 	case _SC_2_SW_DEV:
182 		mib[0] = CTL_USER;
183 		mib[1] = USER_POSIX2_SW_DEV;
184 		goto yesno;
185 	case _SC_2_UPE:
186 		mib[0] = CTL_USER;
187 		mib[1] = USER_POSIX2_UPE;
188 yesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
189 			return (-1);
190 		if (value == 0)
191 			return (-1);
192 		return (value);
193 		break;
194 	default:
195 		errno = EINVAL;
196 		return (-1);
197 	}
198 	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
199 }
200