]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/sysconf.c
Libc-339.tar.gz
[apple/libc.git] / gen / FreeBSD / sysconf.c
CommitLineData
59e0d9fe
A
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)
38static char sccsid[] = "@(#)sysconf.c 8.2 (Berkeley) 3/20/94";
39#endif /* LIBC_SCCS and not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/lib/libc/gen/sysconf.c,v 1.20 2002/11/17 08:54:29 dougb Exp $");
42
43#include <sys/param.h>
44#include <sys/time.h>
45#include <sys/sysctl.h>
46#include <sys/resource.h>
47#include <sys/socket.h>
48
49#include <errno.h>
50#include <limits.h>
51#include <paths.h>
52#include <pthread.h> /* we just need the limits */
53#include <time.h>
54#include <unistd.h>
55
56#include "../stdlib/atexit.h"
57#include "../stdtime/tzfile.h"
58
59#define _PATH_ZONEINFO TZDIR /* from tzfile.h */
60
61/*
62 * sysconf --
63 * get configurable system variables.
64 *
65 * XXX
66 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
67 * not change during the lifetime of the calling process. This would seem
68 * to require that any change to system limits kill all running processes.
69 * A workaround might be to cache the values when they are first retrieved
70 * and then simply return the cached value on subsequent calls. This is
71 * less useful than returning up-to-date values, however.
72 */
73long
74sysconf(name)
75 int name;
76{
77 struct rlimit rl;
78 size_t len;
79 int mib[2], sverrno, value;
80 long defaultresult;
81 const char *path;
82
83 len = sizeof(value);
84 defaultresult = -1;
85
86 switch (name) {
87 case _SC_ARG_MAX:
88 mib[0] = CTL_KERN;
89 mib[1] = KERN_ARGMAX;
90 break;
91 case _SC_CHILD_MAX:
92 if (getrlimit(RLIMIT_NPROC, &rl) != 0)
93 return (-1);
94 if (rl.rlim_cur == RLIM_INFINITY)
95 return (-1);
96 if (rl.rlim_cur > LONG_MAX) {
97 errno = EOVERFLOW;
98 return (-1);
99 }
100 return ((long)rl.rlim_cur);
101 case _SC_CLK_TCK:
102 return (CLK_TCK);
103 case _SC_NGROUPS_MAX:
104 mib[0] = CTL_KERN;
105 mib[1] = KERN_NGROUPS;
106 break;
107 case _SC_OPEN_MAX:
108 case _SC_STREAM_MAX: /* assume fds run out before memory does */
109 if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
110 return (-1);
111 if (rl.rlim_cur == RLIM_INFINITY)
112 return (-1);
113 if (rl.rlim_cur > LONG_MAX) {
114 errno = EOVERFLOW;
115 return (-1);
116 }
117 return ((long)rl.rlim_cur);
118 case _SC_JOB_CONTROL:
119 return (_POSIX_JOB_CONTROL);
120 case _SC_SAVED_IDS:
121 /* XXX - must be 1 */
122 mib[0] = CTL_KERN;
123 mib[1] = KERN_SAVED_IDS;
124 goto yesno;
125 case _SC_VERSION:
126 mib[0] = CTL_KERN;
127 mib[1] = KERN_POSIX1;
128 break;
129 case _SC_BC_BASE_MAX:
130 return (BC_BASE_MAX);
131 case _SC_BC_DIM_MAX:
132 return (BC_DIM_MAX);
133 case _SC_BC_SCALE_MAX:
134 return (BC_SCALE_MAX);
135 case _SC_BC_STRING_MAX:
136 return (BC_STRING_MAX);
137 case _SC_COLL_WEIGHTS_MAX:
138 return (COLL_WEIGHTS_MAX);
139 case _SC_EXPR_NEST_MAX:
140 return (EXPR_NEST_MAX);
141 case _SC_LINE_MAX:
142 return (LINE_MAX);
143 case _SC_RE_DUP_MAX:
144 return (RE_DUP_MAX);
145 case _SC_2_VERSION:
146 /*
147 * This is something of a lie, but it would be silly at
148 * this point to try to deduce this from the contents
149 * of the filesystem.
150 */
151 return (_POSIX2_VERSION);
152 case _SC_2_C_BIND:
153 return (_POSIX2_C_BIND);
154 case _SC_2_C_DEV:
155 return (_POSIX2_C_DEV);
156 case _SC_2_CHAR_TERM:
157 return (_POSIX2_CHAR_TERM);
158 case _SC_2_FORT_DEV:
159 return (_POSIX2_FORT_DEV);
160 case _SC_2_FORT_RUN:
161 return (_POSIX2_FORT_RUN);
162 case _SC_2_LOCALEDEF:
163 return (_POSIX2_LOCALEDEF);
164 case _SC_2_SW_DEV:
165 return (_POSIX2_SW_DEV);
166 case _SC_2_UPE:
167 return (_POSIX2_UPE);
168 case _SC_TZNAME_MAX:
169 path = _PATH_ZONEINFO;
170do_NAME_MAX:
171 sverrno = errno;
172 errno = 0;
173 value = pathconf(path, _PC_NAME_MAX);
174 if (value == -1 && errno != 0)
175 return (-1);
176 errno = sverrno;
177 return (value);
178
179 case _SC_ASYNCHRONOUS_IO:
180#if _POSIX_ASYNCHRONOUS_IO == 0
181 mib[0] = CTL_P1003_1B;
182 mib[1] = CTL_P1003_1B_ASYNCHRONOUS_IO;
183 break;
184#else
185 return (_POSIX_ASYNCHRONOUS_IO);
186#endif
187 case _SC_MAPPED_FILES:
188 return (_POSIX_MAPPED_FILES);
189 case _SC_MEMLOCK:
190 return (_POSIX_MEMLOCK);
191 case _SC_MEMLOCK_RANGE:
192 return (_POSIX_MEMLOCK_RANGE);
193 case _SC_MEMORY_PROTECTION:
194 return (_POSIX_MEMORY_PROTECTION);
195 case _SC_MESSAGE_PASSING:
196#if _POSIX_MESSAGE_PASSING == 0
197 mib[0] = CTL_P1003_1B;
198 mib[1] = CTL_P1003_1B_MESSAGE_PASSING;
199 goto yesno;
200#else
201 return (_POSIX_MESSAGE_PASSING);
202#endif
203 case _SC_PRIORITIZED_IO:
204#if _POSIX_PRIORITIZED_IO == 0
205 mib[0] = CTL_P1003_1B;
206 mib[1] = CTL_P1003_1B_PRIORITIZED_IO;
207 goto yesno;
208#else
209 return (_POSIX_PRIORITIZED_IO);
210#endif
211 case _SC_PRIORITY_SCHEDULING:
212#if _POSIX_PRIORITY_SCHEDULING == 0
213 mib[0] = CTL_P1003_1B;
214 mib[1] = CTL_P1003_1B_PRIORITY_SCHEDULING;
215 goto yesno;
216#else
217 return (_POSIX_PRIORITY_SCHEDULING);
218#endif
219 case _SC_REALTIME_SIGNALS:
220#if _POSIX_REALTIME_SIGNALS == 0
221 mib[0] = CTL_P1003_1B;
222 mib[1] = CTL_P1003_1B_REALTIME_SIGNALS;
223 goto yesno;
224#else
225 return (_POSIX_REALTIME_SIGNALS);
226#endif
227 case _SC_SEMAPHORES:
228#if _POSIX_SEMAPHORES == 0
229 mib[0] = CTL_P1003_1B;
230 mib[1] = CTL_P1003_1B_SEMAPHORES;
231 goto yesno;
232#else
233 return (_POSIX_SEMAPHORES);
234#endif
235 case _SC_FSYNC:
236 return (_POSIX_FSYNC);
237
238 case _SC_SHARED_MEMORY_OBJECTS:
239 return (_POSIX_SHARED_MEMORY_OBJECTS);
240 case _SC_SYNCHRONIZED_IO:
241#if _POSIX_SYNCHRONIZED_IO == 0
242 mib[0] = CTL_P1003_1B;
243 mib[1] = CTL_P1003_1B_SYNCHRONIZED_IO;
244 goto yesno;
245#else
246 return (_POSIX_SYNCHRONIZED_IO);
247#endif
248 case _SC_TIMERS:
249#if _POSIX_TIMERS == 0
250 mib[0] = CTL_P1003_1B;
251 mib[1] = CTL_P1003_1B_TIMERS;
252 goto yesno;
253#else
254 return (_POSIX_TIMERS);
255#endif
256 case _SC_AIO_LISTIO_MAX:
257 mib[0] = CTL_P1003_1B;
258 mib[1] = CTL_P1003_1B_AIO_LISTIO_MAX;
259 break;
260 case _SC_AIO_MAX:
261 mib[0] = CTL_P1003_1B;
262 mib[1] = CTL_P1003_1B_AIO_MAX;
263 break;
264 case _SC_AIO_PRIO_DELTA_MAX:
265 mib[0] = CTL_P1003_1B;
266 mib[1] = CTL_P1003_1B_AIO_PRIO_DELTA_MAX;
267 break;
268 case _SC_DELAYTIMER_MAX:
269 mib[0] = CTL_P1003_1B;
270 mib[1] = CTL_P1003_1B_DELAYTIMER_MAX;
271 goto yesno;
272 case _SC_MQ_OPEN_MAX:
273 mib[0] = CTL_P1003_1B;
274 mib[1] = CTL_P1003_1B_MQ_OPEN_MAX;
275 goto yesno;
276 case _SC_PAGESIZE:
277 defaultresult = getpagesize();
278 mib[0] = CTL_P1003_1B;
279 mib[1] = CTL_P1003_1B_PAGESIZE;
280 goto yesno;
281 case _SC_RTSIG_MAX:
282 mib[0] = CTL_P1003_1B;
283 mib[1] = CTL_P1003_1B_RTSIG_MAX;
284 goto yesno;
285 case _SC_SEM_NSEMS_MAX:
286 mib[0] = CTL_P1003_1B;
287 mib[1] = CTL_P1003_1B_SEM_NSEMS_MAX;
288 goto yesno;
289 case _SC_SEM_VALUE_MAX:
290 mib[0] = CTL_P1003_1B;
291 mib[1] = CTL_P1003_1B_SEM_VALUE_MAX;
292 goto yesno;
293 case _SC_SIGQUEUE_MAX:
294 mib[0] = CTL_P1003_1B;
295 mib[1] = CTL_P1003_1B_SIGQUEUE_MAX;
296 goto yesno;
297 case _SC_TIMER_MAX:
298 mib[0] = CTL_P1003_1B;
299 mib[1] = CTL_P1003_1B_TIMER_MAX;
300
301yesno: if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
302 return (-1);
303 if (value == 0)
304 return (defaultresult);
305 return (value);
306
307 case _SC_2_PBS:
308 case _SC_2_PBS_ACCOUNTING:
309 case _SC_2_PBS_CHECKPOINT:
310 case _SC_2_PBS_LOCATE:
311 case _SC_2_PBS_MESSAGE:
312 case _SC_2_PBS_TRACK:
313#if _POSIX2_PBS == 0
314#error "don't know how to determine _SC_2_PBS"
315 /*
316 * This probably requires digging through the filesystem
317 * to see if the appropriate package has been installed.
318 * Since we don't currently support this option at all,
319 * it's not worth the effort to write the code now.
320 * Figuring out which of the sub-options are supported
321 * would be even more difficult, so it's probably easier
322 * to always say ``no''.
323 */
324#else
325 return (_POSIX2_PBS);
326#endif
327 case _SC_ADVISORY_INFO:
328#if _POSIX_ADVISORY_INFO == 0
329#error "_POSIX_ADVISORY_INFO"
330#else
331 return (_POSIX_ADVISORY_INFO);
332#endif
333 case _SC_BARRIERS:
334#if _POSIX_BARRIERS == 0
335#error "_POSIX_BARRIERS"
336#else
337 return (_POSIX_BARRIERS);
338#endif
339 case _SC_CLOCK_SELECTION:
340#if _POSIX_CLOCK_SELECTION == 0
341#error "_POSIX_CLOCK_SELECTION"
342#else
343 return (_POSIX_CLOCK_SELECTION);
344#endif
345 case _SC_CPUTIME:
346#if _POSIX_CPUTIME == 0
347#error "_POSIX_CPUTIME"
348#else
349 return (_POSIX_CPUTIME);
350#endif
351#ifdef notdef
352 case _SC_FILE_LOCKING:
353 /*
354 * XXX - The standard doesn't tell us how to define
355 * _POSIX_FILE_LOCKING, so we can't answer this one.
356 */
357#endif
358#if _POSIX_THREAD_SAFE_FUNCTIONS > -1
359 case _SC_GETGR_R_SIZE_MAX:
360 case _SC_GETPW_R_SIZE_MAX:
361#error "somebody needs to implement this"
362#endif
363 case _SC_HOST_NAME_MAX:
364 return (MAXHOSTNAMELEN - 1); /* does not include \0 */
365 case _SC_LOGIN_NAME_MAX:
366 return (MAXLOGNAME);
367 case _SC_MONOTONIC_CLOCK:
368#if _POSIX_MONOTONIC_CLOCK == 0
369#error "_POSIX_MONOTONIC_CLOCK"
370#else
371 return (_POSIX_MONOTONIC_CLOCK);
372#endif
373#if _POSIX_MESSAGE_PASSING > -1
374 case _SC_MQ_PRIO_MAX:
375 return (MQ_PRIO_MAX);
376#endif
377 case _SC_READER_WRITER_LOCKS:
378 return (_POSIX_READER_WRITER_LOCKS);
379 case _SC_REGEXP:
380 return (_POSIX_REGEXP);
381 case _SC_SHELL:
382 return (_POSIX_SHELL);
383 case _SC_SPAWN:
384 return (_POSIX_SPAWN);
385 case _SC_SPIN_LOCKS:
386 return (_POSIX_SPIN_LOCKS);
387 case _SC_SPORADIC_SERVER:
388#if _POSIX_SPORADIC_SERVER == 0
389#error "_POSIX_SPORADIC_SERVER"
390#else
391 return (_POSIX_SPORADIC_SERVER);
392#endif
393 case _SC_THREAD_ATTR_STACKADDR:
394 return (_POSIX_THREAD_ATTR_STACKADDR);
395 case _SC_THREAD_ATTR_STACKSIZE:
396 return (_POSIX_THREAD_ATTR_STACKSIZE);
397 case _SC_THREAD_CPUTIME:
398 return (_POSIX_THREAD_CPUTIME);
399 case _SC_THREAD_DESTRUCTOR_ITERATIONS:
400 return (PTHREAD_DESTRUCTOR_ITERATIONS);
401 case _SC_THREAD_KEYS_MAX:
402 return (PTHREAD_KEYS_MAX);
403 case _SC_THREAD_PRIO_INHERIT:
404 return (_POSIX_THREAD_PRIO_INHERIT);
405 case _SC_THREAD_PRIO_PROTECT:
406 return (_POSIX_THREAD_PRIO_PROTECT);
407 case _SC_THREAD_PRIORITY_SCHEDULING:
408 return (_POSIX_THREAD_PRIORITY_SCHEDULING);
409 case _SC_THREAD_PROCESS_SHARED:
410 return (_POSIX_THREAD_PROCESS_SHARED);
411 case _SC_THREAD_SAFE_FUNCTIONS:
412 return (_POSIX_THREAD_SAFE_FUNCTIONS);
413 case _SC_THREAD_STACK_MIN:
414 return (PTHREAD_STACK_MIN);
415 case _SC_THREAD_THREADS_MAX:
416 return (PTHREAD_THREADS_MAX); /* XXX wrong type! */
417 case _SC_TIMEOUTS:
418 return (_POSIX_TIMEOUTS);
419 case _SC_THREADS:
420 return (_POSIX_THREADS);
421 case _SC_TRACE:
422#if _POSIX_TRACE == 0
423#error "_POSIX_TRACE"
424 /* While you're implementing this, also do the ones below. */
425#else
426 return (_POSIX_TRACE);
427#endif
428#if _POSIX_TRACE > -1
429 case _SC_TRACE_EVENT_FILTER:
430 return (_POSIX_TRACE_EVENT_FILTER);
431 case _SC_TRACE_INHERIT:
432 return (_POSIX_TRACE_INHERIT);
433 case _SC_TRACE_LOG:
434 return (_POSIX_TRACE_LOG);
435#endif
436 case _SC_TTY_NAME_MAX:
437 path = _PATH_DEV;
438 goto do_NAME_MAX;
439 case _SC_TYPED_MEMORY_OBJECTS:
440#if _POSIX_TYPED_MEMORY_OBJECTS == 0
441#error "_POSIX_TYPED_MEMORY_OBJECTS"
442#else
443 return (_POSIX_TYPED_MEMORY_OBJECTS);
444#endif
445 case _SC_V6_ILP32_OFF32:
446#if _V6_ILP32_OFF32 == 0
447 if (sizeof(int) * CHAR_BIT == 32 &&
448 sizeof(int) == sizeof(long) &&
449 sizeof(long) == sizeof(void *) &&
450 sizeof(void *) == sizeof(off_t))
451 return 1;
452 else
453 return -1;
454#else
455 return (_V6_ILP32_OFF32);
456#endif
457 case _SC_V6_ILP32_OFFBIG:
458#if _V6_ILP32_OFFBIG == 0
459 if (sizeof(int) * CHAR_BIT == 32 &&
460 sizeof(int) == sizeof(long) &&
461 sizeof(long) == sizeof(void *) &&
462 sizeof(off_t) * CHAR_BIT >= 64)
463 return 1;
464 else
465 return -1;
466#else
467 return (_V6_ILP32_OFFBIG);
468#endif
469 case _SC_V6_LP64_OFF64:
470#if _V6_LP64_OFF64 == 0
471 if (sizeof(int) * CHAR_BIT == 32 &&
472 sizeof(long) * CHAR_BIT == 64 &&
473 sizeof(long) == sizeof(void *) &&
474 sizeof(void *) == sizeof(off_t))
475 return 1;
476 else
477 return -1;
478#else
479 return (_V6_LP64_OFF64);
480#endif
481 case _SC_V6_LPBIG_OFFBIG:
482#if _V6_LPBIG_OFFBIG == 0
483 if (sizeof(int) * CHAR_BIT >= 32 &&
484 sizeof(long) * CHAR_BIT >= 64 &&
485 sizeof(void *) * CHAR_BIT >= 64 &&
486 sizeof(off_t) * CHAR_BIT >= 64)
487 return 1;
488 else
489 return -1;
490#else
491 return (_V6_LPBIG_OFFBIG);
492#endif
493 case _SC_ATEXIT_MAX:
494 return (ATEXIT_SIZE);
495 case _SC_IOV_MAX:
496 mib[0] = CTL_KERN;
497 mib[1] = KERN_IOV_MAX;
498 break;
499 case _SC_XOPEN_CRYPT:
500 return (_XOPEN_CRYPT);
501 case _SC_XOPEN_ENH_I18N:
502 return (_XOPEN_ENH_I18N);
503 case _SC_XOPEN_LEGACY:
504 return (_XOPEN_LEGACY);
505 case _SC_XOPEN_REALTIME:
506#if _XOPEN_REALTIME == 0
507 sverrno = errno;
508 value = sysconf(_SC_ASYNCHRONOUS_IO) > 0 &&
509 sysconf(_SC_MEMLOCK) > 0 &&
510 sysconf(_SC_MEMLOCK_RANGE) > 0 &&
511 sysconf(_SC_MESSAGE_PASSING) > 0 &&
512 sysconf(_SC_PRIORITY_SCHEDULING) > 0 &&
513 sysconf(_SC_REALTIME_SIGNALS) > 0 &&
514 sysconf(_SC_SEMAPHORES) > 0 &&
515 sysconf(_SC_SHARED_MEMORY_OBJECTS) > 0 &&
516 sysconf(_SC_SYNCHRONIZED_IO) > 0 &&
517 sysconf(_SC_TIMERS) > 0;
518 errno = sverrno;
519 if (value)
520 return (200112L);
521 else
522 return (-1);
523#else
524 return (_XOPEN_REALTIME);
525#endif
526 case _SC_XOPEN_REALTIME_THREADS:
527#if _XOPEN_REALTIME_THREADS == 0
528#error "_XOPEN_REALTIME_THREADS"
529#else
530 return (_XOPEN_REALTIME_THREADS);
531#endif
532 case _SC_XOPEN_SHM:
533 sverrno = errno;
534 if (sysctlbyname("kern.ipc.shmmin", &value, &len, NULL,
535 0) == -1) {
536 errno = sverrno;
537 return (-1);
538 }
539 errno = sverrno;
540 return (1);
541 case _SC_XOPEN_STREAMS:
542 return (_XOPEN_STREAMS);
543 case _SC_XOPEN_UNIX:
544 return (_XOPEN_UNIX);
545#ifdef _XOPEN_VERSION
546 case _SC_XOPEN_VERSION:
547 return (_XOPEN_VERSION);
548#endif
549#ifdef _XOPEN_XCU_VERSION
550 case _SC_XOPEN_XCU_VERSION:
551 return (_XOPEN_XCU_VERSION);
552#endif
553 case _SC_SYMLOOP_MAX:
554 return (MAXSYMLINKS);
555 case _SC_RAW_SOCKETS:
556 return (_POSIX_RAW_SOCKETS);
557 case _SC_IPV6:
558#if _POSIX_IPV6 == 0
559 sverrno = errno;
560 value = socket(PF_INET6, SOCK_DGRAM, 0);
561 errno = sverrno;
562 if (value >= 0) {
563 close(value);
564 return (200112L);
565 } else
566 return (0);
567#else
568 return (_POSIX_IPV6);
569#endif
570
571 case _SC_NPROCESSORS_CONF:
572 case _SC_NPROCESSORS_ONLN:
573 mib[0] = CTL_HW;
574 mib[1] = HW_NCPU;
575 break;
576
577 default:
578 errno = EINVAL;
579 return (-1);
580 }
581 return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
582}