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