159721Sbostic /*- 259721Sbostic * Copyright (c) 1993 The Regents of the University of California. 359721Sbostic * All rights reserved. 459721Sbostic * 559721Sbostic * This code is derived from software contributed to Berkeley by 659721Sbostic * Sean Eric Fagan of Cygnus Support. 759721Sbostic * 859721Sbostic * %sccs.include.redist.c% 959721Sbostic */ 1059721Sbostic 1159721Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*60273Sbostic static char sccsid[] = "@(#)sysconf.c 5.2 (Berkeley) 05/24/93"; 1359721Sbostic #endif /* LIBC_SCCS and not lint */ 1459721Sbostic 1559721Sbostic #include <sys/param.h> 1659721Sbostic #include <sys/sysctl.h> 1759721Sbostic 1859721Sbostic #include <errno.h> 1959721Sbostic #include <unistd.h> 2059721Sbostic 21*60273Sbostic /* 22*60273Sbostic * sysconf -- 23*60273Sbostic * get configurable system variables. 24*60273Sbostic * 25*60273Sbostic * XXX 26*60273Sbostic * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values 27*60273Sbostic * not change during the lifetime of the calling process. This would seem 28*60273Sbostic * to require that any change to system limits kill all running processes. 29*60273Sbostic * A workaround might be to cache the values when they are first retrieved 30*60273Sbostic * and then simply return the cached value on subsequent calls. This is 31*60273Sbostic * less useful than returning up-to-date values, however. 32*60273Sbostic */ 3359721Sbostic long 3459721Sbostic sysconf(name) 3559721Sbostic int name; 3659721Sbostic { 3759721Sbostic struct clockinfo clk; 3859721Sbostic size_t len; 3959721Sbostic int mib[2], value; 4059721Sbostic 41*60273Sbostic len = sizeof(value); 42*60273Sbostic 4359721Sbostic switch (name) { 44*60273Sbostic /* 1003.1 */ 4559721Sbostic case _SC_ARG_MAX: 4659721Sbostic mib[0] = CTL_KERN; 4759721Sbostic mib[1] = KERN_ARGMAX; 4859721Sbostic break; 4959721Sbostic case _SC_CHILD_MAX: 5059721Sbostic mib[0] = CTL_KERN; 51*60273Sbostic mib[1] = KERN_MAXUPROC; 5259721Sbostic break; 5359721Sbostic case _SC_CLK_TCK: 54*60273Sbostic return (CLK_TCK); 55*60273Sbostic case _SC_JOB_CONTROL: 5659721Sbostic mib[0] = CTL_KERN; 57*60273Sbostic mib[1] = KERN_JOB_CONTROL; 58*60273Sbostic goto yesno; 5959721Sbostic case _SC_NGROUPS_MAX: 6059721Sbostic mib[0] = CTL_KERN; 6159721Sbostic mib[1] = KERN_NGROUPS; 6259721Sbostic break; 6359721Sbostic case _SC_OPEN_MAX: 6459721Sbostic mib[0] = CTL_KERN; 65*60273Sbostic mib[1] = KERN_MAXUFILES; 6659721Sbostic break; 67*60273Sbostic case _SC_STREAM_MAX: 68*60273Sbostic mib[0] = CTL_USER; 69*60273Sbostic mib[1] = USER_STREAM_MAX; 7059721Sbostic break; 71*60273Sbostic case _SC_TZNAME_MAX: 72*60273Sbostic mib[0] = CTL_USER; 73*60273Sbostic mib[1] = USER_TZNAME_MAX; 74*60273Sbostic break; 7559721Sbostic case _SC_SAVED_IDS: 7659721Sbostic mib[0] = CTL_KERN; 7759721Sbostic mib[1] = KERN_SAVED_IDS; 78*60273Sbostic goto yesno; 7959721Sbostic case _SC_VERSION: 8059721Sbostic mib[0] = CTL_KERN; 8159721Sbostic mib[1] = KERN_POSIX1; 8259721Sbostic break; 83*60273Sbostic 84*60273Sbostic /* 1003.2 */ 8559721Sbostic case _SC_BC_BASE_MAX: 8659721Sbostic mib[0] = CTL_USER; 8759721Sbostic mib[1] = USER_BC_BASE_MAX; 8859721Sbostic break; 8959721Sbostic case _SC_BC_DIM_MAX: 9059721Sbostic mib[0] = CTL_USER; 9159721Sbostic mib[1] = USER_BC_DIM_MAX; 9259721Sbostic break; 9359721Sbostic case _SC_BC_SCALE_MAX: 9459721Sbostic mib[0] = CTL_USER; 9559721Sbostic mib[1] = USER_BC_SCALE_MAX; 9659721Sbostic break; 9759721Sbostic case _SC_BC_STRING_MAX: 9859721Sbostic mib[0] = CTL_USER; 9959721Sbostic mib[1] = USER_BC_STRING_MAX; 10059721Sbostic break; 10159721Sbostic case _SC_COLL_WEIGHTS_MAX: 10259721Sbostic mib[0] = CTL_USER; 10359721Sbostic mib[1] = USER_COLL_WEIGHTS_MAX; 10459721Sbostic break; 10559721Sbostic case _SC_EXPR_NEST_MAX: 10659721Sbostic mib[0] = CTL_USER; 10759721Sbostic mib[1] = USER_EXPR_NEST_MAX; 10859721Sbostic break; 10959721Sbostic case _SC_LINE_MAX: 11059721Sbostic mib[0] = CTL_USER; 11159721Sbostic mib[1] = USER_LINE_MAX; 11259721Sbostic break; 11359721Sbostic case _SC_RE_DUP_MAX: 11459721Sbostic mib[0] = CTL_USER; 11559721Sbostic mib[1] = USER_RE_DUP_MAX; 11659721Sbostic break; 11759721Sbostic case _SC_2_VERSION: 11859721Sbostic mib[0] = CTL_USER; 11959721Sbostic mib[1] = USER_POSIX2_VERSION; 12059721Sbostic break; 12159721Sbostic case _SC_2_C_BIND: 12259721Sbostic mib[0] = CTL_USER; 12359721Sbostic mib[1] = USER_POSIX2_C_BIND; 124*60273Sbostic goto yesno; 12559721Sbostic case _SC_2_C_DEV: 12659721Sbostic mib[0] = CTL_USER; 12759721Sbostic mib[1] = USER_POSIX2_C_DEV; 128*60273Sbostic goto yesno; 129*60273Sbostic case _SC_2_CHAR_TERM: 130*60273Sbostic mib[0] = CTL_USER; 131*60273Sbostic mib[1] = USER_POSIX2_CHAR_TERM; 132*60273Sbostic goto yesno; 13359721Sbostic case _SC_2_FORT_DEV: 13459721Sbostic mib[0] = CTL_USER; 13559721Sbostic mib[1] = USER_POSIX2_FORT_DEV; 136*60273Sbostic goto yesno; 13759721Sbostic case _SC_2_FORT_RUN: 13859721Sbostic mib[0] = CTL_USER; 13959721Sbostic mib[1] = USER_POSIX2_FORT_RUN; 140*60273Sbostic goto yesno; 14159721Sbostic case _SC_2_LOCALEDEF: 14259721Sbostic mib[0] = CTL_USER; 14359721Sbostic mib[1] = USER_POSIX2_LOCALEDEF; 144*60273Sbostic goto yesno; 14559721Sbostic case _SC_2_SW_DEV: 14659721Sbostic mib[0] = CTL_USER; 14759721Sbostic mib[1] = USER_POSIX2_SW_DEV; 148*60273Sbostic goto yesno; 149*60273Sbostic case _SC_2_UPE: 150*60273Sbostic mib[0] = CTL_USER; 151*60273Sbostic mib[1] = USER_POSIX2_UPE; 152*60273Sbostic yesno: if (sysctl(mib, 2, &value, &len, NULL, 0) == -1) 153*60273Sbostic return (-1); 154*60273Sbostic if (value == 0) 155*60273Sbostic return (-1); 156*60273Sbostic return (value); 15759721Sbostic break; 15859721Sbostic default: 15959721Sbostic errno = EINVAL; 16059721Sbostic return (-1); 16159721Sbostic } 16259721Sbostic return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value); 16359721Sbostic } 164