158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
458f0484fSRodney W. Grimes * Copyright (c) 1993
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes * are met:
1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes * without specific prior written permission.
1858f0484fSRodney W. Grimes *
1958f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes * SUCH DAMAGE.
3058f0484fSRodney W. Grimes */
3158f0484fSRodney W. Grimes
3258f0484fSRodney W. Grimes #include <sys/param.h>
3358f0484fSRodney W. Grimes #include <sys/sysctl.h>
3458f0484fSRodney W. Grimes
3558f0484fSRodney W. Grimes #include <errno.h>
3658f0484fSRodney W. Grimes #include <limits.h>
3758f0484fSRodney W. Grimes #include <paths.h>
3858f0484fSRodney W. Grimes #include <stdio.h>
3958f0484fSRodney W. Grimes #include <unistd.h>
408b102407SPoul-Henning Kamp #include <string.h>
4158f0484fSRodney W. Grimes
4262399df2SEd Schouten extern int __sysctl(const int *name, u_int namelen, void *oldp,
4362399df2SEd Schouten size_t *oldlenp, const void *newp, size_t newlen);
44e0554a53SJacques Vidrine
45*9535d9f1SStefan Eßer static int
set_user_str(void * dstp,size_t * dstlenp,const char * src,size_t len,size_t maxlen)46*9535d9f1SStefan Eßer set_user_str(void *dstp, size_t *dstlenp, const char *src, size_t len,
47*9535d9f1SStefan Eßer size_t maxlen)
48*9535d9f1SStefan Eßer {
49*9535d9f1SStefan Eßer int retval;
50*9535d9f1SStefan Eßer
51*9535d9f1SStefan Eßer retval = 0;
52*9535d9f1SStefan Eßer if (dstp != NULL) {
53*9535d9f1SStefan Eßer if (len > maxlen) {
54*9535d9f1SStefan Eßer len = maxlen;
55*9535d9f1SStefan Eßer errno = ENOMEM;
56*9535d9f1SStefan Eßer retval = -1;
57*9535d9f1SStefan Eßer }
58*9535d9f1SStefan Eßer memcpy(dstp, src, len);
59*9535d9f1SStefan Eßer }
60*9535d9f1SStefan Eßer *dstlenp = len;
61*9535d9f1SStefan Eßer return (retval);
62*9535d9f1SStefan Eßer }
63*9535d9f1SStefan Eßer
6458f0484fSRodney W. Grimes int
sysctl(const int * name,u_int namelen,void * oldp,size_t * oldlenp,const void * newp,size_t newlen)6562399df2SEd Schouten sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
6662399df2SEd Schouten const void *newp, size_t newlen)
6758f0484fSRodney W. Grimes {
684d8ed60cSTom Rhodes int retval;
694959a176SPatrick Kelsey size_t orig_oldlen;
704d8ed60cSTom Rhodes
7123b40928SStefan Eßer orig_oldlen = oldlenp != NULL ? *oldlenp : 0;
724d8ed60cSTom Rhodes retval = __sysctl(name, namelen, oldp, oldlenp, newp, newlen);
734959a176SPatrick Kelsey /*
7423b40928SStefan Eßer * Valid names under CTL_USER except USER_LOCALBASE have a dummy entry
7523b40928SStefan Eßer * in the sysctl tree (to support name lookups and enumerations) with
7623b40928SStefan Eßer * an empty/zero value, and the true value is supplied by this routine.
7723b40928SStefan Eßer * For all such names, __sysctl() is used solely to validate the name.
784959a176SPatrick Kelsey *
7923b40928SStefan Eßer * Return here unless there was a successful lookup for a CTL_USER name.
804959a176SPatrick Kelsey */
8123b40928SStefan Eßer if (retval != 0 || name[0] != CTL_USER)
824d8ed60cSTom Rhodes return (retval);
8358f0484fSRodney W. Grimes
8458f0484fSRodney W. Grimes if (namelen != 2) {
8558f0484fSRodney W. Grimes errno = EINVAL;
8658f0484fSRodney W. Grimes return (-1);
8758f0484fSRodney W. Grimes }
8823b40928SStefan Eßer
8923b40928SStefan Eßer /* Variables under CLT_USER that may be overridden by kernel values */
9023b40928SStefan Eßer switch (name[1]) {
9123b40928SStefan Eßer case USER_LOCALBASE:
92*9535d9f1SStefan Eßer if (oldlenp == NULL || *oldlenp > sizeof(""))
93*9535d9f1SStefan Eßer return (0);
94*9535d9f1SStefan Eßer return (set_user_str(oldp, oldlenp, _PATH_LOCALBASE,
95*9535d9f1SStefan Eßer sizeof(_PATH_LOCALBASE), orig_oldlen));
9623b40928SStefan Eßer }
9723b40928SStefan Eßer
9823b40928SStefan Eßer /* Variables under CLT_USER whose values are immutably defined below */
9923b40928SStefan Eßer if (newp != NULL) {
1001ebef477SStefan Eßer errno = EPERM;
1011ebef477SStefan Eßer return (-1);
1021ebef477SStefan Eßer }
10358f0484fSRodney W. Grimes
10458f0484fSRodney W. Grimes switch (name[1]) {
10558f0484fSRodney W. Grimes case USER_CS_PATH:
106*9535d9f1SStefan Eßer return (set_user_str(oldp, oldlenp, _PATH_STDPATH,
107*9535d9f1SStefan Eßer sizeof(_PATH_STDPATH), orig_oldlen));
10858f0484fSRodney W. Grimes }
10958f0484fSRodney W. Grimes
11023b40928SStefan Eßer if (oldp != NULL && *oldlenp < sizeof(int)) {
111eec1dbe6SDag-Erling Smørgrav errno = ENOMEM;
112eec1dbe6SDag-Erling Smørgrav return (-1);
113eec1dbe6SDag-Erling Smørgrav }
11458f0484fSRodney W. Grimes *oldlenp = sizeof(int);
11558f0484fSRodney W. Grimes if (oldp == NULL)
11658f0484fSRodney W. Grimes return (0);
11758f0484fSRodney W. Grimes
11858f0484fSRodney W. Grimes switch (name[1]) {
11958f0484fSRodney W. Grimes case USER_BC_BASE_MAX:
12058f0484fSRodney W. Grimes *(int *)oldp = BC_BASE_MAX;
12158f0484fSRodney W. Grimes return (0);
12258f0484fSRodney W. Grimes case USER_BC_DIM_MAX:
12358f0484fSRodney W. Grimes *(int *)oldp = BC_DIM_MAX;
12458f0484fSRodney W. Grimes return (0);
12558f0484fSRodney W. Grimes case USER_BC_SCALE_MAX:
12658f0484fSRodney W. Grimes *(int *)oldp = BC_SCALE_MAX;
12758f0484fSRodney W. Grimes return (0);
12858f0484fSRodney W. Grimes case USER_BC_STRING_MAX:
12958f0484fSRodney W. Grimes *(int *)oldp = BC_STRING_MAX;
13058f0484fSRodney W. Grimes return (0);
13158f0484fSRodney W. Grimes case USER_COLL_WEIGHTS_MAX:
13258f0484fSRodney W. Grimes *(int *)oldp = COLL_WEIGHTS_MAX;
13358f0484fSRodney W. Grimes return (0);
13458f0484fSRodney W. Grimes case USER_EXPR_NEST_MAX:
13558f0484fSRodney W. Grimes *(int *)oldp = EXPR_NEST_MAX;
13658f0484fSRodney W. Grimes return (0);
13758f0484fSRodney W. Grimes case USER_LINE_MAX:
13858f0484fSRodney W. Grimes *(int *)oldp = LINE_MAX;
13958f0484fSRodney W. Grimes return (0);
14058f0484fSRodney W. Grimes case USER_RE_DUP_MAX:
14158f0484fSRodney W. Grimes *(int *)oldp = RE_DUP_MAX;
14258f0484fSRodney W. Grimes return (0);
14358f0484fSRodney W. Grimes case USER_POSIX2_VERSION:
14458f0484fSRodney W. Grimes *(int *)oldp = _POSIX2_VERSION;
14558f0484fSRodney W. Grimes return (0);
14658f0484fSRodney W. Grimes case USER_POSIX2_C_BIND:
14758f0484fSRodney W. Grimes #ifdef POSIX2_C_BIND
14858f0484fSRodney W. Grimes *(int *)oldp = 1;
14958f0484fSRodney W. Grimes #else
15058f0484fSRodney W. Grimes *(int *)oldp = 0;
15158f0484fSRodney W. Grimes #endif
15258f0484fSRodney W. Grimes return (0);
15358f0484fSRodney W. Grimes case USER_POSIX2_C_DEV:
15458f0484fSRodney W. Grimes #ifdef POSIX2_C_DEV
15558f0484fSRodney W. Grimes *(int *)oldp = 1;
15658f0484fSRodney W. Grimes #else
15758f0484fSRodney W. Grimes *(int *)oldp = 0;
15858f0484fSRodney W. Grimes #endif
15958f0484fSRodney W. Grimes return (0);
16058f0484fSRodney W. Grimes case USER_POSIX2_CHAR_TERM:
16158f0484fSRodney W. Grimes #ifdef POSIX2_CHAR_TERM
16258f0484fSRodney W. Grimes *(int *)oldp = 1;
16358f0484fSRodney W. Grimes #else
16458f0484fSRodney W. Grimes *(int *)oldp = 0;
16558f0484fSRodney W. Grimes #endif
16658f0484fSRodney W. Grimes return (0);
16758f0484fSRodney W. Grimes case USER_POSIX2_FORT_DEV:
16858f0484fSRodney W. Grimes #ifdef POSIX2_FORT_DEV
16958f0484fSRodney W. Grimes *(int *)oldp = 1;
17058f0484fSRodney W. Grimes #else
17158f0484fSRodney W. Grimes *(int *)oldp = 0;
17258f0484fSRodney W. Grimes #endif
17358f0484fSRodney W. Grimes return (0);
17458f0484fSRodney W. Grimes case USER_POSIX2_FORT_RUN:
17558f0484fSRodney W. Grimes #ifdef POSIX2_FORT_RUN
17658f0484fSRodney W. Grimes *(int *)oldp = 1;
17758f0484fSRodney W. Grimes #else
17858f0484fSRodney W. Grimes *(int *)oldp = 0;
17958f0484fSRodney W. Grimes #endif
18058f0484fSRodney W. Grimes return (0);
18158f0484fSRodney W. Grimes case USER_POSIX2_LOCALEDEF:
18258f0484fSRodney W. Grimes #ifdef POSIX2_LOCALEDEF
18358f0484fSRodney W. Grimes *(int *)oldp = 1;
18458f0484fSRodney W. Grimes #else
18558f0484fSRodney W. Grimes *(int *)oldp = 0;
18658f0484fSRodney W. Grimes #endif
18758f0484fSRodney W. Grimes return (0);
18858f0484fSRodney W. Grimes case USER_POSIX2_SW_DEV:
18958f0484fSRodney W. Grimes #ifdef POSIX2_SW_DEV
19058f0484fSRodney W. Grimes *(int *)oldp = 1;
19158f0484fSRodney W. Grimes #else
19258f0484fSRodney W. Grimes *(int *)oldp = 0;
19358f0484fSRodney W. Grimes #endif
19458f0484fSRodney W. Grimes return (0);
19558f0484fSRodney W. Grimes case USER_POSIX2_UPE:
19658f0484fSRodney W. Grimes #ifdef POSIX2_UPE
19758f0484fSRodney W. Grimes *(int *)oldp = 1;
19858f0484fSRodney W. Grimes #else
19958f0484fSRodney W. Grimes *(int *)oldp = 0;
20058f0484fSRodney W. Grimes #endif
20158f0484fSRodney W. Grimes return (0);
20258f0484fSRodney W. Grimes case USER_STREAM_MAX:
20358f0484fSRodney W. Grimes *(int *)oldp = FOPEN_MAX;
20458f0484fSRodney W. Grimes return (0);
20558f0484fSRodney W. Grimes case USER_TZNAME_MAX:
20658f0484fSRodney W. Grimes *(int *)oldp = NAME_MAX;
20758f0484fSRodney W. Grimes return (0);
20858f0484fSRodney W. Grimes default:
20958f0484fSRodney W. Grimes errno = EINVAL;
21058f0484fSRodney W. Grimes return (-1);
21158f0484fSRodney W. Grimes }
21258f0484fSRodney W. Grimes /* NOTREACHED */
21358f0484fSRodney W. Grimes }
214