xref: /netbsd-src/lib/libc/gen/sysconf.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: sysconf.c,v 1.3 1995/03/04 01:56:09 cgd 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 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)sysconf.c	8.2 (Berkeley) 3/20/94";
42 #else
43 static char rcsid[] = "$NetBSD: sysconf.c,v 1.3 1995/03/04 01:56:09 cgd Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 #include <sys/param.h>
48 #include <sys/sysctl.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 
52 #include <errno.h>
53 #include <unistd.h>
54 
55 /*
56  * sysconf --
57  *	get configurable system variables.
58  *
59  * XXX
60  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
61  * not change during the lifetime of the calling process.  This would seem
62  * to require that any change to system limits kill all running processes.
63  * A workaround might be to cache the values when they are first retrieved
64  * and then simply return the cached value on subsequent calls.  This is
65  * less useful than returning up-to-date values, however.
66  */
67 long
68 sysconf(name)
69 	int name;
70 {
71 	struct clockinfo clk;
72 	struct rlimit rl;
73 	size_t len;
74 	int mib[2], value;
75 
76 	len = sizeof(value);
77 
78 	switch (name) {
79 /* 1003.1 */
80 	case _SC_ARG_MAX:
81 		mib[0] = CTL_KERN;
82 		mib[1] = KERN_ARGMAX;
83 		break;
84 	case _SC_CHILD_MAX:
85 		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
86 	case _SC_CLK_TCK:
87 		return (CLK_TCK);
88 	case _SC_JOB_CONTROL:
89 		mib[0] = CTL_KERN;
90 		mib[1] = KERN_JOB_CONTROL;
91 		goto yesno;
92 	case _SC_NGROUPS_MAX:
93 		mib[0] = CTL_KERN;
94 		mib[1] = KERN_NGROUPS;
95 		break;
96 	case _SC_OPEN_MAX:
97 		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
98 	case _SC_STREAM_MAX:
99 		mib[0] = CTL_USER;
100 		mib[1] = USER_STREAM_MAX;
101 		break;
102 	case _SC_TZNAME_MAX:
103 		mib[0] = CTL_USER;
104 		mib[1] = USER_TZNAME_MAX;
105 		break;
106 	case _SC_SAVED_IDS:
107 		mib[0] = CTL_KERN;
108 		mib[1] = KERN_SAVED_IDS;
109 		goto yesno;
110 	case _SC_VERSION:
111 		mib[0] = CTL_KERN;
112 		mib[1] = KERN_POSIX1;
113 		break;
114 
115 /* 1003.2 */
116 	case _SC_BC_BASE_MAX:
117 		mib[0] = CTL_USER;
118 		mib[1] = USER_BC_BASE_MAX;
119 		break;
120 	case _SC_BC_DIM_MAX:
121 		mib[0] = CTL_USER;
122 		mib[1] = USER_BC_DIM_MAX;
123 		break;
124 	case _SC_BC_SCALE_MAX:
125 		mib[0] = CTL_USER;
126 		mib[1] = USER_BC_SCALE_MAX;
127 		break;
128 	case _SC_BC_STRING_MAX:
129 		mib[0] = CTL_USER;
130 		mib[1] = USER_BC_STRING_MAX;
131 		break;
132 	case _SC_COLL_WEIGHTS_MAX:
133 		mib[0] = CTL_USER;
134 		mib[1] = USER_COLL_WEIGHTS_MAX;
135 		break;
136 	case _SC_EXPR_NEST_MAX:
137 		mib[0] = CTL_USER;
138 		mib[1] = USER_EXPR_NEST_MAX;
139 		break;
140 	case _SC_LINE_MAX:
141 		mib[0] = CTL_USER;
142 		mib[1] = USER_LINE_MAX;
143 		break;
144 	case _SC_RE_DUP_MAX:
145 		mib[0] = CTL_USER;
146 		mib[1] = USER_RE_DUP_MAX;
147 		break;
148 	case _SC_2_VERSION:
149 		mib[0] = CTL_USER;
150 		mib[1] = USER_POSIX2_VERSION;
151 		break;
152 	case _SC_2_C_BIND:
153 		mib[0] = CTL_USER;
154 		mib[1] = USER_POSIX2_C_BIND;
155 		goto yesno;
156 	case _SC_2_C_DEV:
157 		mib[0] = CTL_USER;
158 		mib[1] = USER_POSIX2_C_DEV;
159 		goto yesno;
160 	case _SC_2_CHAR_TERM:
161 		mib[0] = CTL_USER;
162 		mib[1] = USER_POSIX2_CHAR_TERM;
163 		goto yesno;
164 	case _SC_2_FORT_DEV:
165 		mib[0] = CTL_USER;
166 		mib[1] = USER_POSIX2_FORT_DEV;
167 		goto yesno;
168 	case _SC_2_FORT_RUN:
169 		mib[0] = CTL_USER;
170 		mib[1] = USER_POSIX2_FORT_RUN;
171 		goto yesno;
172 	case _SC_2_LOCALEDEF:
173 		mib[0] = CTL_USER;
174 		mib[1] = USER_POSIX2_LOCALEDEF;
175 		goto yesno;
176 	case _SC_2_SW_DEV:
177 		mib[0] = CTL_USER;
178 		mib[1] = USER_POSIX2_SW_DEV;
179 		goto yesno;
180 	case _SC_2_UPE:
181 		mib[0] = CTL_USER;
182 		mib[1] = USER_POSIX2_UPE;
183 yesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
184 			return (-1);
185 		if (value == 0)
186 			return (-1);
187 		return (value);
188 		break;
189 	default:
190 		errno = EINVAL;
191 		return (-1);
192 	}
193 	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
194 }
195