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