Merge from vendor branch NTPD:
[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  * @(#)sysconf.c        8.2 (Berkeley) 3/20/94
37  * $DragonFly: src/lib/libc/gen/sysconf.c,v 1.3 2004/11/09 17:52:45 joerg Exp $
38  */
39
40 #include <sys/_posix.h>
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/sysctl.h>
44 #include <sys/resource.h>
45
46 #include <errno.h>
47 #include <time.h>
48 #include <unistd.h>
49
50 /*
51  * sysconf --
52  *      get configurable system variables.
53  *
54  * XXX
55  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
56  * not change during the lifetime of the calling process.  This would seem
57  * to require that any change to system limits kill all running processes.
58  * A workaround might be to cache the values when they are first retrieved
59  * and then simply return the cached value on subsequent calls.  This is
60  * less useful than returning up-to-date values, however.
61  */
62 long
63 sysconf(name)
64         int name;
65 {
66         struct rlimit rl;
67         size_t len;
68         int mib[2], value;
69         long defaultresult;
70
71         len = sizeof(value);
72         defaultresult = -1;
73
74         switch (name) {
75 /* 1003.1 */
76         case _SC_ARG_MAX:
77                 mib[0] = CTL_KERN;
78                 mib[1] = KERN_ARGMAX;
79                 break;
80         case _SC_CHILD_MAX:
81                 return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
82         case _SC_CLK_TCK:
83                 return (CLK_TCK);
84         case _SC_JOB_CONTROL:
85                 mib[0] = CTL_KERN;
86                 mib[1] = KERN_JOB_CONTROL;
87                 goto yesno;
88         case _SC_NGROUPS_MAX:
89                 mib[0] = CTL_KERN;
90                 mib[1] = KERN_NGROUPS;
91                 break;
92         case _SC_OPEN_MAX:
93                 return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
94         case _SC_STREAM_MAX:
95                 mib[0] = CTL_USER;
96                 mib[1] = USER_STREAM_MAX;
97                 break;
98         case _SC_TZNAME_MAX:
99                 mib[0] = CTL_USER;
100                 mib[1] = USER_TZNAME_MAX;
101                 break;
102         case _SC_SAVED_IDS:
103                 mib[0] = CTL_KERN;
104                 mib[1] = KERN_SAVED_IDS;
105                 goto yesno;
106         case _SC_VERSION:
107                 mib[0] = CTL_KERN;
108                 mib[1] = KERN_POSIX1;
109                 break;
110         case _SC_IOV_MAX:
111                 mib[0] = CTL_KERN;
112                 mib[1] = KERN_IOV_MAX;
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                 goto yesno;
184
185 #ifdef _P1003_1B_VISIBLE
186         /* POSIX.1B */
187
188         case _SC_ASYNCHRONOUS_IO:
189                 mib[0] = CTL_P1003_1B;
190                 mib[1] = CTL_P1003_1B_ASYNCHRONOUS_IO;
191                 goto yesno;
192         case _SC_MAPPED_FILES:
193                 mib[0] = CTL_P1003_1B;
194                 mib[1] = CTL_P1003_1B_MAPPED_FILES;
195                 goto yesno;
196         case _SC_MEMLOCK:
197                 mib[0] = CTL_P1003_1B;
198                 mib[1] = CTL_P1003_1B_MEMLOCK;
199                 goto yesno;
200         case _SC_MEMLOCK_RANGE:
201                 mib[0] = CTL_P1003_1B;
202                 mib[1] = CTL_P1003_1B_MEMLOCK_RANGE;
203                 goto yesno;
204         case _SC_MEMORY_PROTECTION:
205                 mib[0] = CTL_P1003_1B;
206                 mib[1] = CTL_P1003_1B_MEMORY_PROTECTION;
207                 goto yesno;
208         case _SC_MESSAGE_PASSING:
209                 mib[0] = CTL_P1003_1B;
210                 mib[1] = CTL_P1003_1B_MESSAGE_PASSING;
211                 goto yesno;
212         case _SC_PRIORITIZED_IO:
213                 mib[0] = CTL_P1003_1B;
214                 mib[1] = CTL_P1003_1B_PRIORITIZED_IO;
215                 goto yesno;
216         case _SC_PRIORITY_SCHEDULING:
217                 mib[0] = CTL_P1003_1B;
218                 mib[1] = CTL_P1003_1B_PRIORITY_SCHEDULING;
219                 goto yesno;
220         case _SC_REALTIME_SIGNALS:
221                 mib[0] = CTL_P1003_1B;
222                 mib[1] = CTL_P1003_1B_REALTIME_SIGNALS;
223                 goto yesno;
224         case _SC_SEMAPHORES:
225                 mib[0] = CTL_P1003_1B;
226                 mib[1] = CTL_P1003_1B_SEMAPHORES;
227                 goto yesno;
228         case _SC_FSYNC:
229                 mib[0] = CTL_P1003_1B;
230                 mib[1] = CTL_P1003_1B_FSYNC;
231                 goto yesno;
232         case _SC_SHARED_MEMORY_OBJECTS:
233                 mib[0] = CTL_P1003_1B;
234                 mib[1] = CTL_P1003_1B_SHARED_MEMORY_OBJECTS;
235                 goto yesno;
236         case _SC_SYNCHRONIZED_IO:
237                 mib[0] = CTL_P1003_1B;
238                 mib[1] = CTL_P1003_1B_SYNCHRONIZED_IO;
239                 goto yesno;
240         case _SC_TIMERS:
241                 mib[0] = CTL_P1003_1B;
242                 mib[1] = CTL_P1003_1B_TIMERS;
243                 goto yesno;
244         case _SC_AIO_LISTIO_MAX:
245                 mib[0] = CTL_P1003_1B;
246                 mib[1] = CTL_P1003_1B_AIO_LISTIO_MAX;
247                 goto yesno;
248         case _SC_AIO_MAX:
249                 mib[0] = CTL_P1003_1B;
250                 mib[1] = CTL_P1003_1B_AIO_MAX;
251                 goto yesno;
252         case _SC_AIO_PRIO_DELTA_MAX:
253                 mib[0] = CTL_P1003_1B;
254                 mib[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX;
255                 goto yesno;
256         case _SC_DELAYTIMER_MAX:
257                 mib[0] = CTL_P1003_1B;
258                 mib[1] = CTL_P1003_1B_DELAYTIMER_MAX;
259                 goto yesno;
260         case _SC_MQ_OPEN_MAX:
261                 mib[0] = CTL_P1003_1B;
262                 mib[1] = CTL_P1003_1B_MQ_OPEN_MAX;
263                 goto yesno;
264         case _SC_PAGESIZE:
265                 defaultresult = getpagesize();
266                 mib[0] = CTL_P1003_1B;
267                 mib[1] = CTL_P1003_1B_PAGESIZE;
268                 goto yesno;
269         case _SC_RTSIG_MAX:
270                 mib[0] = CTL_P1003_1B;
271                 mib[1] = CTL_P1003_1B_RTSIG_MAX;
272                 goto yesno;
273         case _SC_SEM_NSEMS_MAX:
274                 mib[0] = CTL_P1003_1B;
275                 mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX;
276                 goto yesno;
277         case _SC_SEM_VALUE_MAX:
278                 mib[0] = CTL_P1003_1B;
279                 mib[1] = CTL_P1003_1B_SEM_VALUE_MAX;
280                 goto yesno;
281         case _SC_SIGQUEUE_MAX:
282                 mib[0] = CTL_P1003_1B;
283                 mib[1] = CTL_P1003_1B_SIGQUEUE_MAX;
284                 goto yesno;
285         case _SC_TIMER_MAX:
286                 mib[0] = CTL_P1003_1B;
287                 mib[1] = CTL_P1003_1B_TIMER_MAX;
288                 goto yesno;
289 #endif /* _P1003_1B_VISIBLE */
290
291 yesno:          if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
292                         return (-1);
293                 if (value == 0)
294                         return (defaultresult);
295                 return (value);
296                 break;
297         default:
298                 errno = EINVAL;
299                 return (-1);
300         }
301         return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
302 }