sysconf.c revision 6171
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Sean Eric Fagan of Cygnus Support.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
381573Srgrimesstatic char sccsid[] = "@(#)sysconf.c	8.2 (Berkeley) 3/20/94";
391573Srgrimes#endif /* LIBC_SCCS and not lint */
401573Srgrimes
411573Srgrimes#include <sys/param.h>
421573Srgrimes#include <sys/sysctl.h>
431573Srgrimes#include <sys/resource.h>
441573Srgrimes
451573Srgrimes#include <errno.h>
466171Sbde#include <time.h>
471573Srgrimes#include <unistd.h>
481573Srgrimes
491573Srgrimes/*
501573Srgrimes * sysconf --
511573Srgrimes *	get configurable system variables.
521573Srgrimes *
531573Srgrimes * XXX
541573Srgrimes * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
551573Srgrimes * not change during the lifetime of the calling process.  This would seem
561573Srgrimes * to require that any change to system limits kill all running processes.
571573Srgrimes * A workaround might be to cache the values when they are first retrieved
581573Srgrimes * and then simply return the cached value on subsequent calls.  This is
591573Srgrimes * less useful than returning up-to-date values, however.
601573Srgrimes */
611573Srgrimeslong
621573Srgrimessysconf(name)
631573Srgrimes	int name;
641573Srgrimes{
651573Srgrimes	struct clockinfo clk;
661573Srgrimes	struct rlimit rl;
671573Srgrimes	size_t len;
681573Srgrimes	int mib[2], value;
691573Srgrimes
701573Srgrimes	len = sizeof(value);
711573Srgrimes
721573Srgrimes	switch (name) {
731573Srgrimes/* 1003.1 */
741573Srgrimes	case _SC_ARG_MAX:
751573Srgrimes		mib[0] = CTL_KERN;
761573Srgrimes		mib[1] = KERN_ARGMAX;
771573Srgrimes		break;
781573Srgrimes	case _SC_CHILD_MAX:
791573Srgrimes		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
801573Srgrimes	case _SC_CLK_TCK:
811573Srgrimes		return (CLK_TCK);
821573Srgrimes	case _SC_JOB_CONTROL:
831573Srgrimes		mib[0] = CTL_KERN;
841573Srgrimes		mib[1] = KERN_JOB_CONTROL;
851573Srgrimes		goto yesno;
861573Srgrimes	case _SC_NGROUPS_MAX:
871573Srgrimes		mib[0] = CTL_KERN;
881573Srgrimes		mib[1] = KERN_NGROUPS;
891573Srgrimes		break;
901573Srgrimes	case _SC_OPEN_MAX:
911573Srgrimes		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
921573Srgrimes	case _SC_STREAM_MAX:
931573Srgrimes		mib[0] = CTL_USER;
941573Srgrimes		mib[1] = USER_STREAM_MAX;
951573Srgrimes		break;
961573Srgrimes	case _SC_TZNAME_MAX:
971573Srgrimes		mib[0] = CTL_USER;
981573Srgrimes		mib[1] = USER_TZNAME_MAX;
991573Srgrimes		break;
1001573Srgrimes	case _SC_SAVED_IDS:
1011573Srgrimes		mib[0] = CTL_KERN;
1021573Srgrimes		mib[1] = KERN_SAVED_IDS;
1031573Srgrimes		goto yesno;
1041573Srgrimes	case _SC_VERSION:
1051573Srgrimes		mib[0] = CTL_KERN;
1061573Srgrimes		mib[1] = KERN_POSIX1;
1071573Srgrimes		break;
1081573Srgrimes
1091573Srgrimes/* 1003.2 */
1101573Srgrimes	case _SC_BC_BASE_MAX:
1111573Srgrimes		mib[0] = CTL_USER;
1121573Srgrimes		mib[1] = USER_BC_BASE_MAX;
1131573Srgrimes		break;
1141573Srgrimes	case _SC_BC_DIM_MAX:
1151573Srgrimes		mib[0] = CTL_USER;
1161573Srgrimes		mib[1] = USER_BC_DIM_MAX;
1171573Srgrimes		break;
1181573Srgrimes	case _SC_BC_SCALE_MAX:
1191573Srgrimes		mib[0] = CTL_USER;
1201573Srgrimes		mib[1] = USER_BC_SCALE_MAX;
1211573Srgrimes		break;
1221573Srgrimes	case _SC_BC_STRING_MAX:
1231573Srgrimes		mib[0] = CTL_USER;
1241573Srgrimes		mib[1] = USER_BC_STRING_MAX;
1251573Srgrimes		break;
1261573Srgrimes	case _SC_COLL_WEIGHTS_MAX:
1271573Srgrimes		mib[0] = CTL_USER;
1281573Srgrimes		mib[1] = USER_COLL_WEIGHTS_MAX;
1291573Srgrimes		break;
1301573Srgrimes	case _SC_EXPR_NEST_MAX:
1311573Srgrimes		mib[0] = CTL_USER;
1321573Srgrimes		mib[1] = USER_EXPR_NEST_MAX;
1331573Srgrimes		break;
1341573Srgrimes	case _SC_LINE_MAX:
1351573Srgrimes		mib[0] = CTL_USER;
1361573Srgrimes		mib[1] = USER_LINE_MAX;
1371573Srgrimes		break;
1381573Srgrimes	case _SC_RE_DUP_MAX:
1391573Srgrimes		mib[0] = CTL_USER;
1401573Srgrimes		mib[1] = USER_RE_DUP_MAX;
1411573Srgrimes		break;
1421573Srgrimes	case _SC_2_VERSION:
1431573Srgrimes		mib[0] = CTL_USER;
1441573Srgrimes		mib[1] = USER_POSIX2_VERSION;
1451573Srgrimes		break;
1461573Srgrimes	case _SC_2_C_BIND:
1471573Srgrimes		mib[0] = CTL_USER;
1481573Srgrimes		mib[1] = USER_POSIX2_C_BIND;
1491573Srgrimes		goto yesno;
1501573Srgrimes	case _SC_2_C_DEV:
1511573Srgrimes		mib[0] = CTL_USER;
1521573Srgrimes		mib[1] = USER_POSIX2_C_DEV;
1531573Srgrimes		goto yesno;
1541573Srgrimes	case _SC_2_CHAR_TERM:
1551573Srgrimes		mib[0] = CTL_USER;
1561573Srgrimes		mib[1] = USER_POSIX2_CHAR_TERM;
1571573Srgrimes		goto yesno;
1581573Srgrimes	case _SC_2_FORT_DEV:
1591573Srgrimes		mib[0] = CTL_USER;
1601573Srgrimes		mib[1] = USER_POSIX2_FORT_DEV;
1611573Srgrimes		goto yesno;
1621573Srgrimes	case _SC_2_FORT_RUN:
1631573Srgrimes		mib[0] = CTL_USER;
1641573Srgrimes		mib[1] = USER_POSIX2_FORT_RUN;
1651573Srgrimes		goto yesno;
1661573Srgrimes	case _SC_2_LOCALEDEF:
1671573Srgrimes		mib[0] = CTL_USER;
1681573Srgrimes		mib[1] = USER_POSIX2_LOCALEDEF;
1691573Srgrimes		goto yesno;
1701573Srgrimes	case _SC_2_SW_DEV:
1711573Srgrimes		mib[0] = CTL_USER;
1721573Srgrimes		mib[1] = USER_POSIX2_SW_DEV;
1731573Srgrimes		goto yesno;
1741573Srgrimes	case _SC_2_UPE:
1751573Srgrimes		mib[0] = CTL_USER;
1761573Srgrimes		mib[1] = USER_POSIX2_UPE;
1771573Srgrimesyesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
1781573Srgrimes			return (-1);
1791573Srgrimes		if (value == 0)
1801573Srgrimes			return (-1);
1811573Srgrimes		return (value);
1821573Srgrimes		break;
1831573Srgrimes	default:
1841573Srgrimes		errno = EINVAL;
1851573Srgrimes		return (-1);
1861573Srgrimes	}
1871573Srgrimes	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
1881573Srgrimes}
189