xref: /csrg-svn/lib/libc/gen/sysconf.c (revision 66407)
159721Sbostic /*-
261111Sbostic  * Copyright (c) 1993
361111Sbostic  *	The Regents of the University of California.  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*66407Sbostic static char sccsid[] = "@(#)sysconf.c	8.2 (Berkeley) 03/20/94";
1359721Sbostic #endif /* LIBC_SCCS and not lint */
1459721Sbostic 
1559721Sbostic #include <sys/param.h>
1659721Sbostic #include <sys/sysctl.h>
1760295Sbostic #include <sys/time.h>
1860295Sbostic #include <sys/resource.h>
1959721Sbostic 
2059721Sbostic #include <errno.h>
2159721Sbostic #include <unistd.h>
2259721Sbostic 
2360273Sbostic /*
2460273Sbostic  * sysconf --
2560273Sbostic  *	get configurable system variables.
2660273Sbostic  *
2760273Sbostic  * XXX
2860273Sbostic  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
2960273Sbostic  * not change during the lifetime of the calling process.  This would seem
3060273Sbostic  * to require that any change to system limits kill all running processes.
3160273Sbostic  * A workaround might be to cache the values when they are first retrieved
3260273Sbostic  * and then simply return the cached value on subsequent calls.  This is
3360273Sbostic  * less useful than returning up-to-date values, however.
3460273Sbostic  */
3559721Sbostic long
sysconf(name)3659721Sbostic sysconf(name)
3759721Sbostic 	int name;
3859721Sbostic {
3959721Sbostic 	struct clockinfo clk;
4060295Sbostic 	struct rlimit rl;
4159721Sbostic 	size_t len;
4259721Sbostic 	int mib[2], value;
4359721Sbostic 
4460273Sbostic 	len = sizeof(value);
4560273Sbostic 
4659721Sbostic 	switch (name) {
4760273Sbostic /* 1003.1 */
4859721Sbostic 	case _SC_ARG_MAX:
4959721Sbostic 		mib[0] = CTL_KERN;
5059721Sbostic 		mib[1] = KERN_ARGMAX;
5159721Sbostic 		break;
5259721Sbostic 	case _SC_CHILD_MAX:
53*66407Sbostic 		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
5459721Sbostic 	case _SC_CLK_TCK:
5560273Sbostic 		return (CLK_TCK);
5660273Sbostic 	case _SC_JOB_CONTROL:
5759721Sbostic 		mib[0] = CTL_KERN;
5860273Sbostic 		mib[1] = KERN_JOB_CONTROL;
5960273Sbostic 		goto yesno;
6059721Sbostic 	case _SC_NGROUPS_MAX:
6159721Sbostic 		mib[0] = CTL_KERN;
6259721Sbostic 		mib[1] = KERN_NGROUPS;
6359721Sbostic 		break;
6459721Sbostic 	case _SC_OPEN_MAX:
65*66407Sbostic 		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
6660273Sbostic 	case _SC_STREAM_MAX:
6760273Sbostic 		mib[0] = CTL_USER;
6860273Sbostic 		mib[1] = USER_STREAM_MAX;
6959721Sbostic 		break;
7060273Sbostic 	case _SC_TZNAME_MAX:
7160273Sbostic 		mib[0] = CTL_USER;
7260273Sbostic 		mib[1] = USER_TZNAME_MAX;
7360273Sbostic 		break;
7459721Sbostic 	case _SC_SAVED_IDS:
7559721Sbostic 		mib[0] = CTL_KERN;
7659721Sbostic 		mib[1] = KERN_SAVED_IDS;
7760273Sbostic 		goto yesno;
7859721Sbostic 	case _SC_VERSION:
7959721Sbostic 		mib[0] = CTL_KERN;
8059721Sbostic 		mib[1] = KERN_POSIX1;
8159721Sbostic 		break;
8260273Sbostic 
8360273Sbostic /* 1003.2 */
8459721Sbostic 	case _SC_BC_BASE_MAX:
8559721Sbostic 		mib[0] = CTL_USER;
8659721Sbostic 		mib[1] = USER_BC_BASE_MAX;
8759721Sbostic 		break;
8859721Sbostic 	case _SC_BC_DIM_MAX:
8959721Sbostic 		mib[0] = CTL_USER;
9059721Sbostic 		mib[1] = USER_BC_DIM_MAX;
9159721Sbostic 		break;
9259721Sbostic 	case _SC_BC_SCALE_MAX:
9359721Sbostic 		mib[0] = CTL_USER;
9459721Sbostic 		mib[1] = USER_BC_SCALE_MAX;
9559721Sbostic 		break;
9659721Sbostic 	case _SC_BC_STRING_MAX:
9759721Sbostic 		mib[0] = CTL_USER;
9859721Sbostic 		mib[1] = USER_BC_STRING_MAX;
9959721Sbostic 		break;
10059721Sbostic 	case _SC_COLL_WEIGHTS_MAX:
10159721Sbostic 		mib[0] = CTL_USER;
10259721Sbostic 		mib[1] = USER_COLL_WEIGHTS_MAX;
10359721Sbostic 		break;
10459721Sbostic 	case _SC_EXPR_NEST_MAX:
10559721Sbostic 		mib[0] = CTL_USER;
10659721Sbostic 		mib[1] = USER_EXPR_NEST_MAX;
10759721Sbostic 		break;
10859721Sbostic 	case _SC_LINE_MAX:
10959721Sbostic 		mib[0] = CTL_USER;
11059721Sbostic 		mib[1] = USER_LINE_MAX;
11159721Sbostic 		break;
11259721Sbostic 	case _SC_RE_DUP_MAX:
11359721Sbostic 		mib[0] = CTL_USER;
11459721Sbostic 		mib[1] = USER_RE_DUP_MAX;
11559721Sbostic 		break;
11659721Sbostic 	case _SC_2_VERSION:
11759721Sbostic 		mib[0] = CTL_USER;
11859721Sbostic 		mib[1] = USER_POSIX2_VERSION;
11959721Sbostic 		break;
12059721Sbostic 	case _SC_2_C_BIND:
12159721Sbostic 		mib[0] = CTL_USER;
12259721Sbostic 		mib[1] = USER_POSIX2_C_BIND;
12360273Sbostic 		goto yesno;
12459721Sbostic 	case _SC_2_C_DEV:
12559721Sbostic 		mib[0] = CTL_USER;
12659721Sbostic 		mib[1] = USER_POSIX2_C_DEV;
12760273Sbostic 		goto yesno;
12860273Sbostic 	case _SC_2_CHAR_TERM:
12960273Sbostic 		mib[0] = CTL_USER;
13060273Sbostic 		mib[1] = USER_POSIX2_CHAR_TERM;
13160273Sbostic 		goto yesno;
13259721Sbostic 	case _SC_2_FORT_DEV:
13359721Sbostic 		mib[0] = CTL_USER;
13459721Sbostic 		mib[1] = USER_POSIX2_FORT_DEV;
13560273Sbostic 		goto yesno;
13659721Sbostic 	case _SC_2_FORT_RUN:
13759721Sbostic 		mib[0] = CTL_USER;
13859721Sbostic 		mib[1] = USER_POSIX2_FORT_RUN;
13960273Sbostic 		goto yesno;
14059721Sbostic 	case _SC_2_LOCALEDEF:
14159721Sbostic 		mib[0] = CTL_USER;
14259721Sbostic 		mib[1] = USER_POSIX2_LOCALEDEF;
14360273Sbostic 		goto yesno;
14459721Sbostic 	case _SC_2_SW_DEV:
14559721Sbostic 		mib[0] = CTL_USER;
14659721Sbostic 		mib[1] = USER_POSIX2_SW_DEV;
14760273Sbostic 		goto yesno;
14860273Sbostic 	case _SC_2_UPE:
14960273Sbostic 		mib[0] = CTL_USER;
15060273Sbostic 		mib[1] = USER_POSIX2_UPE;
15160273Sbostic yesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
15260273Sbostic 			return (-1);
15360273Sbostic 		if (value == 0)
15460273Sbostic 			return (-1);
15560273Sbostic 		return (value);
15659721Sbostic 		break;
15759721Sbostic 	default:
15859721Sbostic 		errno = EINVAL;
15959721Sbostic 		return (-1);
16059721Sbostic 	}
16159721Sbostic 	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
16259721Sbostic }
163