Home | History | Annotate | Line # | Download | only in freebsd
freebsd_sysctl.c revision 1.15.16.1
      1  1.15.16.1   yamt /*	$NetBSD: freebsd_sysctl.c,v 1.15.16.1 2014/05/22 11:40:15 yamt Exp $	*/
      2        1.1   heas 
      3        1.1   heas /*-
      4        1.1   heas  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5        1.1   heas  * All rights reserved.
      6        1.1   heas  *
      7        1.1   heas  * Redistribution and use in source and binary forms, with or without
      8        1.1   heas  * modification, are permitted provided that the following conditions
      9        1.1   heas  * are met:
     10        1.1   heas  * 1. Redistributions of source code must retain the above copyright
     11        1.1   heas  *    notice, this list of conditions and the following disclaimer.
     12        1.1   heas  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1   heas  *    notice, this list of conditions and the following disclaimer in the
     14        1.1   heas  *    documentation and/or other materials provided with the distribution.
     15        1.1   heas  *
     16        1.1   heas  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17        1.1   heas  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18        1.1   heas  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19        1.1   heas  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20        1.1   heas  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21        1.1   heas  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22        1.1   heas  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23        1.1   heas  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24        1.1   heas  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25        1.1   heas  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26        1.1   heas  * POSSIBILITY OF SUCH DAMAGE.
     27        1.1   heas  */
     28        1.1   heas 
     29        1.1   heas /*
     30        1.1   heas  * FreeBSD compatibility module. Try to deal with various FreeBSD sysctl calls.
     31        1.1   heas  */
     32        1.1   heas 
     33        1.1   heas #include <sys/cdefs.h>
     34  1.15.16.1   yamt __KERNEL_RCSID(0, "$NetBSD: freebsd_sysctl.c,v 1.15.16.1 2014/05/22 11:40:15 yamt Exp $");
     35        1.1   heas 
     36        1.1   heas #include <sys/param.h>
     37        1.1   heas #include <sys/systm.h>
     38        1.1   heas #include <sys/proc.h>
     39        1.1   heas #include <sys/mount.h>
     40        1.1   heas #include <sys/signal.h>
     41        1.1   heas #include <sys/signalvar.h>
     42        1.1   heas #include <sys/malloc.h>
     43        1.1   heas #include <sys/mman.h>
     44        1.1   heas #include <sys/sysctl.h>
     45        1.5   manu #include <sys/ktrace.h>
     46        1.1   heas 
     47        1.1   heas #include <sys/syscallargs.h>
     48        1.1   heas 
     49        1.1   heas #include <compat/freebsd/freebsd_syscallargs.h>
     50        1.1   heas #include <compat/common/compat_util.h>
     51        1.1   heas #include <compat/freebsd/freebsd_rtprio.h>
     52        1.1   heas #include <compat/freebsd/freebsd_timex.h>
     53        1.1   heas #include <compat/freebsd/freebsd_signal.h>
     54        1.1   heas #include <compat/freebsd/freebsd_mman.h>
     55       1.15     ad #include <compat/freebsd/freebsd_sysctl.h>
     56        1.1   heas 
     57        1.1   heas static int freebsd_sysctl_name2oid(char *, int *, int *);
     58        1.1   heas 
     59       1.15     ad static struct sysctllog *freebsd_clog;
     60       1.15     ad 
     61       1.15     ad void
     62       1.15     ad freebsd_sysctl_init(void)
     63        1.1   heas {
     64       1.15     ad 
     65       1.15     ad         sysctl_createv(&freebsd_clog, 0, NULL, NULL,
     66        1.1   heas 			CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
     67        1.1   heas 			CTLTYPE_INT, "osreldate",
     68        1.1   heas 			SYSCTL_DESCR("Operating system revision"),
     69        1.1   heas 			NULL, __NetBSD_Version__, NULL, 0,
     70        1.1   heas 			CTL_KERN, CTL_CREATE, CTL_EOL);
     71        1.1   heas }
     72        1.1   heas 
     73       1.15     ad void
     74       1.15     ad freebsd_sysctl_fini(void)
     75       1.15     ad {
     76       1.15     ad 
     77       1.15     ad 	sysctl_teardown(&freebsd_clog);
     78       1.15     ad }
     79       1.15     ad 
     80        1.1   heas int
     81       1.11    dsl freebsd_sys_sysctl(struct lwp *l, const struct freebsd_sys_sysctl_args *uap, register_t *retval)
     82        1.1   heas {
     83       1.11    dsl 	/* {
     84        1.1   heas 		syscallarg(int *) name;
     85        1.1   heas 		syscallarg(u_int) namelen;
     86        1.1   heas 		syscallarg(void *) old;
     87        1.1   heas 		syscallarg(size_t *) oldlenp;
     88        1.1   heas 		syscallarg(void *) new;
     89        1.1   heas 		syscallarg(size_t) newlen;
     90       1.11    dsl 	} */
     91        1.5   manu 	int error;
     92        1.5   manu 	int name[CTL_MAXNAME];
     93        1.1   heas 	size_t newlen, *oldlenp;
     94        1.1   heas 	u_int namelen;
     95        1.1   heas 	void *new, *old;
     96        1.1   heas 
     97        1.1   heas 	namelen = SCARG(uap, namelen);
     98        1.5   manu 
     99        1.5   manu 	if ((namelen > CTL_MAXNAME) || (namelen < 1))
    100        1.5   manu 		return EINVAL;
    101        1.5   manu 
    102        1.5   manu 	if ((error = copyin(SCARG(uap, name), name,
    103        1.5   manu 	    namelen * sizeof(*name))) != 0)
    104        1.5   manu 		return error;
    105        1.5   manu 
    106        1.1   heas 	if (namelen > 0 && name[0] != 0)
    107       1.11    dsl 		return(sys___sysctl(l, (const void *)uap, retval));
    108        1.1   heas 
    109        1.9     ad 	ktrmib(name, namelen);
    110        1.5   manu 
    111        1.1   heas 	/*
    112        1.1   heas 	 * FreeBSD sysctl uses an undocumented set of special OIDs in it's
    113        1.1   heas 	 * sysctl MIB whose tree is rooted at oid 0.  These OIDs are
    114        1.1   heas 	 * interpretted by their sysctl to implement functions that NetBSD
    115        1.1   heas 	 * performs in libc, such as sysctlgetmibinfo.
    116        1.1   heas 	 *
    117        1.1   heas 	 * From the FreeBSD kern_sysctl.c, these OIDs are:
    118        1.1   heas 	 * {0,0}        printf the entire MIB-tree.
    119        1.1   heas 	 * {0,1,...}    return the name of the "..." OID.
    120        1.1   heas 	 * {0,2,...}    return the next OID.
    121        1.1   heas 	 * {0,3}        return the OID of the name in "new"
    122        1.1   heas 	 * {0,4,...}    return the kind & format info for the "..." OID.
    123        1.1   heas 	 * {0,5,...}    return the description the "..." OID.
    124        1.1   heas 	 *
    125        1.1   heas 	 * Only implement {0,3} for now.
    126        1.1   heas 	 */
    127        1.1   heas 
    128        1.1   heas 	if (namelen < 2 || namelen > CTL_MAXNAME)
    129        1.1   heas 		return(EINVAL);
    130        1.1   heas 
    131        1.1   heas 	if (name[1] == 3) {
    132        1.1   heas 		char *locnew;
    133        1.1   heas 		int oid[CTL_MAXNAME];
    134        1.1   heas 		u_int oidlen = CTL_MAXNAME;
    135        1.1   heas 
    136        1.1   heas 		new = SCARG(uap, new);
    137        1.1   heas 		newlen = SCARG(uap, newlen);
    138        1.1   heas 		if (new == NULL || newlen < 1 ||
    139        1.1   heas 		    newlen > (SYSCTL_NAMELEN * CTL_MAXNAME))
    140        1.1   heas 			return(EINVAL);
    141        1.1   heas 
    142        1.1   heas 		old = SCARG(uap, old);
    143        1.1   heas 		oldlenp = SCARG(uap, oldlenp);
    144        1.1   heas 		if (old == NULL || oldlenp == NULL || *oldlenp < sizeof(int))
    145        1.1   heas 			return(EINVAL);
    146        1.1   heas 
    147        1.1   heas 		if ((locnew =
    148        1.1   heas 		     (char *) malloc(newlen + 1, M_TEMP, M_WAITOK)) == NULL)
    149        1.1   heas 			return(ENOMEM);
    150        1.1   heas 
    151       1.12     ad 		if ((error = copyinstr(new, locnew, newlen + 1, NULL))) {
    152        1.1   heas 			free(locnew, M_TEMP);
    153        1.1   heas 			return(error);
    154        1.1   heas 		}
    155        1.1   heas 
    156        1.9     ad 		ktrmibio(-1, UIO_WRITE, new, newlen + 1, error);
    157       1.12     ad 		sysctl_lock(new != NULL);
    158        1.1   heas 		error = freebsd_sysctl_name2oid(locnew, oid, &oidlen);
    159       1.12     ad 		sysctl_unlock();
    160        1.1   heas 		free(locnew, M_TEMP);
    161        1.1   heas 		if (error)
    162        1.1   heas 			return(error);
    163        1.1   heas 
    164        1.1   heas 		oidlen *= sizeof(int);
    165        1.1   heas 		error = copyout(oid, SCARG(uap, old),
    166        1.1   heas 				MIN(oidlen, *SCARG(uap, oldlenp)));
    167        1.1   heas 		if (error)
    168        1.1   heas 			return(error);
    169        1.9     ad 		ktrmibio(-1, UIO_READ, SCARG(uap, old),
    170        1.9     ad 		    MIN(oidlen, *SCARG(uap, oldlenp)),  0);
    171        1.9     ad 
    172        1.1   heas 		error = copyout(&oidlen, SCARG(uap, oldlenp), sizeof(u_int));
    173        1.2  perry 
    174        1.1   heas 		return(error);
    175        1.1   heas 	}
    176        1.1   heas 
    177        1.1   heas 	return(EOPNOTSUPP);
    178        1.1   heas }
    179        1.1   heas 
    180        1.1   heas static int
    181        1.1   heas freebsd_sysctl_name2oid(char *name, int *oid, int *oidlen)
    182        1.1   heas {
    183        1.1   heas 	char *dot;
    184        1.1   heas 	int oi, ci;
    185        1.1   heas 	struct sysctlnode *node, *pnode;
    186        1.1   heas 
    187        1.1   heas 	pnode = &sysctl_root;
    188        1.1   heas 	node = sysctl_root.sysctl_child;
    189        1.1   heas 	oi = 0;
    190        1.1   heas 	if ((dot = strchr(name, (int)'.')) != NULL)
    191        1.1   heas 		*dot++ = '\0';
    192        1.1   heas 
    193        1.1   heas next:
    194        1.1   heas 	while (*name != '\0' && node != NULL) {
    195        1.1   heas 		for (ci = 0; ci < pnode->sysctl_clen; ci++) {
    196        1.1   heas 			if (strcmp(name, node[ci].sysctl_name) == 0) {
    197        1.1   heas 				oid[oi++] = node[ci].sysctl_num;
    198        1.1   heas 				if ((name = dot) == NULL) {
    199        1.1   heas 					*oidlen = oi;
    200        1.1   heas 					return(0);
    201        1.1   heas 				}
    202        1.1   heas 				if ((dot = strchr(name, (int)'.')) != NULL)
    203        1.1   heas 					*dot++ = '\0';
    204        1.1   heas 
    205        1.1   heas 				if (SYSCTL_TYPE(node[ci].sysctl_flags) !=
    206        1.1   heas 				    CTLTYPE_NODE)
    207        1.1   heas 					return(ENOTDIR);
    208        1.1   heas 
    209        1.1   heas 				pnode = &node[ci];
    210        1.1   heas 				node = pnode->sysctl_child;
    211        1.1   heas 				goto next;
    212        1.1   heas 			}
    213        1.1   heas 		}
    214        1.1   heas 
    215        1.1   heas 		/* no more nodes, it must not exist */
    216        1.1   heas 		break;
    217        1.1   heas 	}
    218        1.1   heas 
    219        1.1   heas 	return(ENOENT);
    220        1.1   heas }
    221