Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libc / gen / sysconf.c
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)sysconf.c   8.2 (Berkeley) 3/20/94";
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <sys/_posix.h>
42 #include <sys/param.h>
43 #include <sys/time.h>
44 #include <sys/sysctl.h>
45 #include <sys/resource.h>
46
47 #include <errno.h>
48 #include <time.h>
49 #include <unistd.h>
50
51 /*
52  * sysconf --
53  *      get configurable system variables.
54  *
55  * XXX
56  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
57  * not change during the lifetime of the calling process.  This would seem
58  * to require that any change to system limits kill all running processes.
59  * A workaround might be to cache the values when they are first retrieved
60  * and then simply return the cached value on subsequent calls.  This is
61  * less useful than returning up-to-date values, however.
62  */
63 long
64 sysconf(name)
65         int name;
66 {
67         struct rlimit rl;
68         size_t len;
69         int mib[2], value;
70         long defaultresult;
71
72         len = sizeof(value);
73         defaultresult = -1;
74
75         switch (name) {
76 /* 1003.1 */
77         case _SC_ARG_MAX:
78                 mib[0] = CTL_KERN;
79                 mib[1] = KERN_ARGMAX;
80                 break;
81         case _SC_CHILD_MAX:
82                 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
83         case _SC_CLK_TCK:
84                 return (CLK_TCK);
85         case _SC_JOB_CONTROL:
86                 mib[0] = CTL_KERN;
87                 mib[1] = KERN_JOB_CONTROL;
88                 goto yesno;
89         case _SC_NGROUPS_MAX:
90                 mib[0] = CTL_KERN;
91                 mib[1] = KERN_NGROUPS;
92                 break;
93         case _SC_OPEN_MAX:
94                 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
95         case _SC_STREAM_MAX:
96                 mib[0] = CTL_USER;
97                 mib[1] = USER_STREAM_MAX;
98                 break;
99         case _SC_TZNAME_MAX:
100                 mib[0] = CTL_USER;
101                 mib[1] = USER_TZNAME_MAX;
102                 break;
103         case _SC_SAVED_IDS:
104                 mib[0] = CTL_KERN;
105                 mib[1] = KERN_SAVED_IDS;
106                 goto yesno;
107         case _SC_VERSION:
108                 mib[0] = CTL_KERN;
109                 mib[1] = KERN_POSIX1;
110                 break;
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 #ifdef _P1003_1B_VISIBLE
183         /* POSIX.1B */
184
185         case _SC_ASYNCHRONOUS_IO:
186                 mib[0] = CTL_P1003_1B;
187                 mib[1] = CTL_P1003_1B_ASYNCHRONOUS_IO;
188                 goto yesno;
189         case _SC_MAPPED_FILES:
190                 mib[0] = CTL_P1003_1B;
191                 mib[1] = CTL_P1003_1B_MAPPED_FILES;
192                 goto yesno;
193         case _SC_MEMLOCK:
194                 mib[0] = CTL_P1003_1B;
195                 mib[1] = CTL_P1003_1B_MEMLOCK;
196                 goto yesno;
197         case _SC_MEMLOCK_RANGE:
198                 mib[0] = CTL_P1003_1B;
199                 mib[1] = CTL_P1003_1B_MEMLOCK_RANGE;
200                 goto yesno;
201         case _SC_MEMORY_PROTECTION:
202                 mib[0] = CTL_P1003_1B;
203                 mib[1] = CTL_P1003_1B_MEMORY_PROTECTION;
204                 goto yesno;
205         case _SC_MESSAGE_PASSING:
206                 mib[0] = CTL_P1003_1B;
207                 mib[1] = CTL_P1003_1B_MESSAGE_PASSING;
208                 goto yesno;
209         case _SC_PRIORITIZED_IO:
210                 mib[0] = CTL_P1003_1B;
211                 mib[1] = CTL_P1003_1B_PRIORITIZED_IO;
212                 goto yesno;
213         case _SC_PRIORITY_SCHEDULING:
214                 mib[0] = CTL_P1003_1B;
215                 mib[1] = CTL_P1003_1B_PRIORITY_SCHEDULING;
216                 goto yesno;
217         case _SC_REALTIME_SIGNALS:
218                 mib[0] = CTL_P1003_1B;
219                 mib[1] = CTL_P1003_1B_REALTIME_SIGNALS;
220                 goto yesno;
221         case _SC_SEMAPHORES:
222                 mib[0] = CTL_P1003_1B;
223                 mib[1] = CTL_P1003_1B_SEMAPHORES;
224                 goto yesno;
225         case _SC_FSYNC:
226                 mib[0] = CTL_P1003_1B;
227                 mib[1] = CTL_P1003_1B_FSYNC;
228                 goto yesno;
229         case _SC_SHARED_MEMORY_OBJECTS:
230                 mib[0] = CTL_P1003_1B;
231                 mib[1] = CTL_P1003_1B_SHARED_MEMORY_OBJECTS;
232                 goto yesno;
233         case _SC_SYNCHRONIZED_IO:
234                 mib[0] = CTL_P1003_1B;
235                 mib[1] = CTL_P1003_1B_SYNCHRONIZED_IO;
236                 goto yesno;
237         case _SC_TIMERS:
238                 mib[0] = CTL_P1003_1B;
239                 mib[1] = CTL_P1003_1B_TIMERS;
240                 goto yesno;
241         case _SC_AIO_LISTIO_MAX:
242                 mib[0] = CTL_P1003_1B;
243                 mib[1] = CTL_P1003_1B_AIO_LISTIO_MAX;
244                 goto yesno;
245         case _SC_AIO_MAX:
246                 mib[0] = CTL_P1003_1B;
247                 mib[1] = CTL_P1003_1B_AIO_MAX;
248                 goto yesno;
249         case _SC_AIO_PRIO_DELTA_MAX:
250                 mib[0] = CTL_P1003_1B;
251                 mib[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX;
252                 goto yesno;
253         case _SC_DELAYTIMER_MAX:
254                 mib[0] = CTL_P1003_1B;
255                 mib[1] = CTL_P1003_1B_DELAYTIMER_MAX;
256                 goto yesno;
257         case _SC_MQ_OPEN_MAX:
258                 mib[0] = CTL_P1003_1B;
259                 mib[1] = CTL_P1003_1B_MQ_OPEN_MAX;
260                 goto yesno;
261         case _SC_PAGESIZE:
262                 defaultresult = getpagesize();
263                 mib[0] = CTL_P1003_1B;
264                 mib[1] = CTL_P1003_1B_PAGESIZE;
265                 goto yesno;
266         case _SC_RTSIG_MAX:
267                 mib[0] = CTL_P1003_1B;
268                 mib[1] = CTL_P1003_1B_RTSIG_MAX;
269                 goto yesno;
270         case _SC_SEM_NSEMS_MAX:
271                 mib[0] = CTL_P1003_1B;
272                 mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX;
273                 goto yesno;
274         case _SC_SEM_VALUE_MAX:
275                 mib[0] = CTL_P1003_1B;
276                 mib[1] = CTL_P1003_1B_SEM_VALUE_MAX;
277                 goto yesno;
278         case _SC_SIGQUEUE_MAX:
279                 mib[0] = CTL_P1003_1B;
280                 mib[1] = CTL_P1003_1B_SIGQUEUE_MAX;
281                 goto yesno;
282         case _SC_TIMER_MAX:
283                 mib[0] = CTL_P1003_1B;
284                 mib[1] = CTL_P1003_1B_TIMER_MAX;
285                 goto yesno;
286 #endif /* _P1003_1B_VISIBLE */
287
288 yesno:          if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
289                         return (-1);
290                 if (value == 0)
291                         return (defaultresult);
292                 return (value);
293                 break;
294         default:
295                 errno = EINVAL;
296                 return (-1);
297         }
298         return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
299 }