sysctl.c revision 111010
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
351573Srgrimesstatic char sccsid[] = "@(#)sysctl.c	8.2 (Berkeley) 1/4/94";
361573Srgrimes#endif /* LIBC_SCCS and not lint */
3792986Sobrien#include <sys/cdefs.h>
3892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/sysctl.c 111010 2003-02-16 17:29:11Z nectar $");
391573Srgrimes
401573Srgrimes#include <sys/param.h>
411573Srgrimes#include <sys/sysctl.h>
421573Srgrimes
431573Srgrimes#include <errno.h>
441573Srgrimes#include <limits.h>
451573Srgrimes#include <paths.h>
461573Srgrimes#include <stdio.h>
471573Srgrimes#include <unistd.h>
4811659Sphk#include <string.h>
491573Srgrimes
50111010Snectarextern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
51111010Snectar    void *newp, size_t newlen);
52111010Snectar
531573Srgrimesint
541573Srgrimessysctl(name, namelen, oldp, oldlenp, newp, newlen)
551573Srgrimes	int *name;
561573Srgrimes	u_int namelen;
571573Srgrimes	void *oldp, *newp;
581573Srgrimes	size_t *oldlenp, newlen;
591573Srgrimes{
601573Srgrimes	if (name[0] != CTL_USER)
611573Srgrimes		return (__sysctl(name, namelen, oldp, oldlenp, newp, newlen));
621573Srgrimes
631573Srgrimes	if (newp != NULL) {
641573Srgrimes		errno = EPERM;
651573Srgrimes		return (-1);
661573Srgrimes	}
671573Srgrimes	if (namelen != 2) {
681573Srgrimes		errno = EINVAL;
691573Srgrimes		return (-1);
701573Srgrimes	}
711573Srgrimes
721573Srgrimes	switch (name[1]) {
731573Srgrimes	case USER_CS_PATH:
7442353Sdes		if (oldp && *oldlenp < sizeof(_PATH_STDPATH)) {
7542353Sdes			errno = ENOMEM;
7642353Sdes			return -1;
7742353Sdes		}
781573Srgrimes		*oldlenp = sizeof(_PATH_STDPATH);
791573Srgrimes		if (oldp != NULL)
801573Srgrimes			memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH));
811573Srgrimes		return (0);
821573Srgrimes	}
831573Srgrimes
8442353Sdes	if (oldp && *oldlenp < sizeof(int)) {
8542353Sdes		errno = ENOMEM;
8642353Sdes		return (-1);
8742353Sdes	}
881573Srgrimes	*oldlenp = sizeof(int);
891573Srgrimes	if (oldp == NULL)
901573Srgrimes		return (0);
911573Srgrimes
921573Srgrimes	switch (name[1]) {
931573Srgrimes	case USER_BC_BASE_MAX:
941573Srgrimes		*(int *)oldp = BC_BASE_MAX;
951573Srgrimes		return (0);
961573Srgrimes	case USER_BC_DIM_MAX:
971573Srgrimes		*(int *)oldp = BC_DIM_MAX;
981573Srgrimes		return (0);
991573Srgrimes	case USER_BC_SCALE_MAX:
1001573Srgrimes		*(int *)oldp = BC_SCALE_MAX;
1011573Srgrimes		return (0);
1021573Srgrimes	case USER_BC_STRING_MAX:
1031573Srgrimes		*(int *)oldp = BC_STRING_MAX;
1041573Srgrimes		return (0);
1051573Srgrimes	case USER_COLL_WEIGHTS_MAX:
1061573Srgrimes		*(int *)oldp = COLL_WEIGHTS_MAX;
1071573Srgrimes		return (0);
1081573Srgrimes	case USER_EXPR_NEST_MAX:
1091573Srgrimes		*(int *)oldp = EXPR_NEST_MAX;
1101573Srgrimes		return (0);
1111573Srgrimes	case USER_LINE_MAX:
1121573Srgrimes		*(int *)oldp = LINE_MAX;
1131573Srgrimes		return (0);
1141573Srgrimes	case USER_RE_DUP_MAX:
1151573Srgrimes		*(int *)oldp = RE_DUP_MAX;
1161573Srgrimes		return (0);
1171573Srgrimes	case USER_POSIX2_VERSION:
1181573Srgrimes		*(int *)oldp = _POSIX2_VERSION;
1191573Srgrimes		return (0);
1201573Srgrimes	case USER_POSIX2_C_BIND:
1211573Srgrimes#ifdef POSIX2_C_BIND
1221573Srgrimes		*(int *)oldp = 1;
1231573Srgrimes#else
1241573Srgrimes		*(int *)oldp = 0;
1251573Srgrimes#endif
1261573Srgrimes		return (0);
1271573Srgrimes	case USER_POSIX2_C_DEV:
1281573Srgrimes#ifdef	POSIX2_C_DEV
1291573Srgrimes		*(int *)oldp = 1;
1301573Srgrimes#else
1311573Srgrimes		*(int *)oldp = 0;
1321573Srgrimes#endif
1331573Srgrimes		return (0);
1341573Srgrimes	case USER_POSIX2_CHAR_TERM:
1351573Srgrimes#ifdef	POSIX2_CHAR_TERM
1361573Srgrimes		*(int *)oldp = 1;
1371573Srgrimes#else
1381573Srgrimes		*(int *)oldp = 0;
1391573Srgrimes#endif
1401573Srgrimes		return (0);
1411573Srgrimes	case USER_POSIX2_FORT_DEV:
1421573Srgrimes#ifdef	POSIX2_FORT_DEV
1431573Srgrimes		*(int *)oldp = 1;
1441573Srgrimes#else
1451573Srgrimes		*(int *)oldp = 0;
1461573Srgrimes#endif
1471573Srgrimes		return (0);
1481573Srgrimes	case USER_POSIX2_FORT_RUN:
1491573Srgrimes#ifdef	POSIX2_FORT_RUN
1501573Srgrimes		*(int *)oldp = 1;
1511573Srgrimes#else
1521573Srgrimes		*(int *)oldp = 0;
1531573Srgrimes#endif
1541573Srgrimes		return (0);
1551573Srgrimes	case USER_POSIX2_LOCALEDEF:
1561573Srgrimes#ifdef	POSIX2_LOCALEDEF
1571573Srgrimes		*(int *)oldp = 1;
1581573Srgrimes#else
1591573Srgrimes		*(int *)oldp = 0;
1601573Srgrimes#endif
1611573Srgrimes		return (0);
1621573Srgrimes	case USER_POSIX2_SW_DEV:
1631573Srgrimes#ifdef	POSIX2_SW_DEV
1641573Srgrimes		*(int *)oldp = 1;
1651573Srgrimes#else
1661573Srgrimes		*(int *)oldp = 0;
1671573Srgrimes#endif
1681573Srgrimes		return (0);
1691573Srgrimes	case USER_POSIX2_UPE:
1701573Srgrimes#ifdef	POSIX2_UPE
1711573Srgrimes		*(int *)oldp = 1;
1721573Srgrimes#else
1731573Srgrimes		*(int *)oldp = 0;
1741573Srgrimes#endif
1751573Srgrimes		return (0);
1761573Srgrimes	case USER_STREAM_MAX:
1771573Srgrimes		*(int *)oldp = FOPEN_MAX;
1781573Srgrimes		return (0);
1791573Srgrimes	case USER_TZNAME_MAX:
1801573Srgrimes		*(int *)oldp = NAME_MAX;
1811573Srgrimes		return (0);
1821573Srgrimes	default:
1831573Srgrimes		errno = EINVAL;
1841573Srgrimes		return (-1);
1851573Srgrimes	}
1861573Srgrimes	/* NOTREACHED */
1871573Srgrimes}
188