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