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 * 4. Neither the name of the University nor the names of its contributors
141573Srgrimes *    may be used to endorse or promote products derived from this software
151573Srgrimes *    without specific prior written permission.
161573Srgrimes *
171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271573Srgrimes * SUCH DAMAGE.
281573Srgrimes */
291573Srgrimes
301573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
311573Srgrimesstatic char sccsid[] = "@(#)sysctl.c	8.2 (Berkeley) 1/4/94";
321573Srgrimes#endif /* LIBC_SCCS and not lint */
3392986Sobrien#include <sys/cdefs.h>
3492986Sobrien__FBSDID("$FreeBSD$");
351573Srgrimes
361573Srgrimes#include <sys/param.h>
371573Srgrimes#include <sys/sysctl.h>
381573Srgrimes
391573Srgrimes#include <errno.h>
401573Srgrimes#include <limits.h>
411573Srgrimes#include <paths.h>
421573Srgrimes#include <stdio.h>
431573Srgrimes#include <unistd.h>
4411659Sphk#include <string.h>
451573Srgrimes
46204170Sedextern int __sysctl(const int *name, u_int namelen, void *oldp,
47204170Sed    size_t *oldlenp, const void *newp, size_t newlen);
48111010Snectar
491573Srgrimesint
50204170Sedsysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
51204170Sed    const void *newp, size_t newlen)
521573Srgrimes{
53240176Strhodes	int retval;
54285188Spkelsey	size_t orig_oldlen;
551573Srgrimes
56285188Spkelsey	orig_oldlen = oldlenp ? *oldlenp : 0;
57240176Strhodes	retval = __sysctl(name, namelen, oldp, oldlenp, newp, newlen);
58285188Spkelsey	/*
59285188Spkelsey	 * All valid names under CTL_USER have a dummy entry in the sysctl
60285188Spkelsey	 * tree (to support name lookups and enumerations) with an
61285188Spkelsey	 * empty/zero value, and the true value is supplied by this routine.
62285188Spkelsey	 * For all such names, __sysctl() is used solely to validate the
63285188Spkelsey	 * name.
64285188Spkelsey	 *
65285188Spkelsey	 * Return here unless there was a successful lookup for a CTL_USER
66285188Spkelsey	 * name.
67285188Spkelsey	 */
68285188Spkelsey	if (retval || name[0] != CTL_USER)
69240176Strhodes		return (retval);
70240176Strhodes
711573Srgrimes	if (newp != NULL) {
721573Srgrimes		errno = EPERM;
731573Srgrimes		return (-1);
741573Srgrimes	}
751573Srgrimes	if (namelen != 2) {
761573Srgrimes		errno = EINVAL;
771573Srgrimes		return (-1);
781573Srgrimes	}
791573Srgrimes
801573Srgrimes	switch (name[1]) {
811573Srgrimes	case USER_CS_PATH:
82285188Spkelsey		if (oldp && orig_oldlen < sizeof(_PATH_STDPATH)) {
8342353Sdes			errno = ENOMEM;
8442353Sdes			return -1;
8542353Sdes		}
861573Srgrimes		*oldlenp = sizeof(_PATH_STDPATH);
871573Srgrimes		if (oldp != NULL)
881573Srgrimes			memmove(oldp, _PATH_STDPATH, sizeof(_PATH_STDPATH));
891573Srgrimes		return (0);
901573Srgrimes	}
911573Srgrimes
9242353Sdes	if (oldp && *oldlenp < sizeof(int)) {
9342353Sdes		errno = ENOMEM;
9442353Sdes		return (-1);
9542353Sdes	}
961573Srgrimes	*oldlenp = sizeof(int);
971573Srgrimes	if (oldp == NULL)
981573Srgrimes		return (0);
991573Srgrimes
1001573Srgrimes	switch (name[1]) {
1011573Srgrimes	case USER_BC_BASE_MAX:
1021573Srgrimes		*(int *)oldp = BC_BASE_MAX;
1031573Srgrimes		return (0);
1041573Srgrimes	case USER_BC_DIM_MAX:
1051573Srgrimes		*(int *)oldp = BC_DIM_MAX;
1061573Srgrimes		return (0);
1071573Srgrimes	case USER_BC_SCALE_MAX:
1081573Srgrimes		*(int *)oldp = BC_SCALE_MAX;
1091573Srgrimes		return (0);
1101573Srgrimes	case USER_BC_STRING_MAX:
1111573Srgrimes		*(int *)oldp = BC_STRING_MAX;
1121573Srgrimes		return (0);
1131573Srgrimes	case USER_COLL_WEIGHTS_MAX:
1141573Srgrimes		*(int *)oldp = COLL_WEIGHTS_MAX;
1151573Srgrimes		return (0);
1161573Srgrimes	case USER_EXPR_NEST_MAX:
1171573Srgrimes		*(int *)oldp = EXPR_NEST_MAX;
1181573Srgrimes		return (0);
1191573Srgrimes	case USER_LINE_MAX:
1201573Srgrimes		*(int *)oldp = LINE_MAX;
1211573Srgrimes		return (0);
1221573Srgrimes	case USER_RE_DUP_MAX:
1231573Srgrimes		*(int *)oldp = RE_DUP_MAX;
1241573Srgrimes		return (0);
1251573Srgrimes	case USER_POSIX2_VERSION:
1261573Srgrimes		*(int *)oldp = _POSIX2_VERSION;
1271573Srgrimes		return (0);
1281573Srgrimes	case USER_POSIX2_C_BIND:
1291573Srgrimes#ifdef POSIX2_C_BIND
1301573Srgrimes		*(int *)oldp = 1;
1311573Srgrimes#else
1321573Srgrimes		*(int *)oldp = 0;
1331573Srgrimes#endif
1341573Srgrimes		return (0);
1351573Srgrimes	case USER_POSIX2_C_DEV:
1361573Srgrimes#ifdef	POSIX2_C_DEV
1371573Srgrimes		*(int *)oldp = 1;
1381573Srgrimes#else
1391573Srgrimes		*(int *)oldp = 0;
1401573Srgrimes#endif
1411573Srgrimes		return (0);
1421573Srgrimes	case USER_POSIX2_CHAR_TERM:
1431573Srgrimes#ifdef	POSIX2_CHAR_TERM
1441573Srgrimes		*(int *)oldp = 1;
1451573Srgrimes#else
1461573Srgrimes		*(int *)oldp = 0;
1471573Srgrimes#endif
1481573Srgrimes		return (0);
1491573Srgrimes	case USER_POSIX2_FORT_DEV:
1501573Srgrimes#ifdef	POSIX2_FORT_DEV
1511573Srgrimes		*(int *)oldp = 1;
1521573Srgrimes#else
1531573Srgrimes		*(int *)oldp = 0;
1541573Srgrimes#endif
1551573Srgrimes		return (0);
1561573Srgrimes	case USER_POSIX2_FORT_RUN:
1571573Srgrimes#ifdef	POSIX2_FORT_RUN
1581573Srgrimes		*(int *)oldp = 1;
1591573Srgrimes#else
1601573Srgrimes		*(int *)oldp = 0;
1611573Srgrimes#endif
1621573Srgrimes		return (0);
1631573Srgrimes	case USER_POSIX2_LOCALEDEF:
1641573Srgrimes#ifdef	POSIX2_LOCALEDEF
1651573Srgrimes		*(int *)oldp = 1;
1661573Srgrimes#else
1671573Srgrimes		*(int *)oldp = 0;
1681573Srgrimes#endif
1691573Srgrimes		return (0);
1701573Srgrimes	case USER_POSIX2_SW_DEV:
1711573Srgrimes#ifdef	POSIX2_SW_DEV
1721573Srgrimes		*(int *)oldp = 1;
1731573Srgrimes#else
1741573Srgrimes		*(int *)oldp = 0;
1751573Srgrimes#endif
1761573Srgrimes		return (0);
1771573Srgrimes	case USER_POSIX2_UPE:
1781573Srgrimes#ifdef	POSIX2_UPE
1791573Srgrimes		*(int *)oldp = 1;
1801573Srgrimes#else
1811573Srgrimes		*(int *)oldp = 0;
1821573Srgrimes#endif
1831573Srgrimes		return (0);
1841573Srgrimes	case USER_STREAM_MAX:
1851573Srgrimes		*(int *)oldp = FOPEN_MAX;
1861573Srgrimes		return (0);
1871573Srgrimes	case USER_TZNAME_MAX:
1881573Srgrimes		*(int *)oldp = NAME_MAX;
1891573Srgrimes		return (0);
1901573Srgrimes	default:
1911573Srgrimes		errno = EINVAL;
1921573Srgrimes		return (-1);
1931573Srgrimes	}
1941573Srgrimes	/* NOTREACHED */
1951573Srgrimes}
196