Home | History | Annotate | Line # | Download | only in freebsd
freebsd_sysctl.c revision 1.9
      1 /*	$NetBSD: freebsd_sysctl.c,v 1.9 2007/08/15 12:07:28 ad 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.9 2007/08/15 12:07:28 ad Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/proc.h>
     49 #include <sys/mount.h>
     50 #include <sys/signal.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/malloc.h>
     53 #include <sys/mman.h>
     54 #include <sys/sysctl.h>
     55 #include <sys/ktrace.h>
     56 
     57 #include <sys/syscallargs.h>
     58 
     59 #include <compat/freebsd/freebsd_syscallargs.h>
     60 #include <compat/common/compat_util.h>
     61 #include <compat/freebsd/freebsd_rtprio.h>
     62 #include <compat/freebsd/freebsd_timex.h>
     63 #include <compat/freebsd/freebsd_signal.h>
     64 #include <compat/freebsd/freebsd_mman.h>
     65 
     66 static int freebsd_sysctl_name2oid(char *, int *, int *);
     67 
     68 SYSCTL_SETUP_PROTO(freebsd_sysctl_setup);
     69 SYSCTL_SETUP(freebsd_sysctl_setup, "freebsd emulated sysctl setup")
     70 {
     71 	sysctl_createv(clog, 0, NULL, NULL,
     72 			CTLFLAG_PERMANENT,
     73 			CTLTYPE_NODE, "kern", NULL,
     74 			NULL, 0, NULL, 0,
     75 			CTL_KERN, CTL_EOL);
     76         sysctl_createv(clog, 0, NULL, NULL,
     77 			CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
     78 			CTLTYPE_INT, "osreldate",
     79 			SYSCTL_DESCR("Operating system revision"),
     80 			NULL, __NetBSD_Version__, NULL, 0,
     81 			CTL_KERN, CTL_CREATE, CTL_EOL);
     82 }
     83 
     84 int
     85 freebsd_sys_sysctl(l, v, retval)
     86 	struct lwp *l;
     87 	void *v;
     88 	register_t *retval;
     89 {
     90 	struct freebsd_sys_sysctl_args /* {
     91 		syscallarg(int *) name;
     92 		syscallarg(u_int) namelen;
     93 		syscallarg(void *) old;
     94 		syscallarg(size_t *) oldlenp;
     95 		syscallarg(void *) new;
     96 		syscallarg(size_t) newlen;
     97 	} */ *uap = v;
     98 	int error;
     99 	int name[CTL_MAXNAME];
    100 	size_t newlen, *oldlenp;
    101 	u_int namelen;
    102 	void *new, *old;
    103 
    104 	namelen = SCARG(uap, namelen);
    105 
    106 	if ((namelen > CTL_MAXNAME) || (namelen < 1))
    107 		return EINVAL;
    108 
    109 	if ((error = copyin(SCARG(uap, name), name,
    110 	    namelen * sizeof(*name))) != 0)
    111 		return error;
    112 
    113 	if (namelen > 0 && name[0] != 0)
    114 		return(sys___sysctl(l, v, retval));
    115 
    116 	ktrmib(name, namelen);
    117 
    118 	/*
    119 	 * FreeBSD sysctl uses an undocumented set of special OIDs in it's
    120 	 * sysctl MIB whose tree is rooted at oid 0.  These OIDs are
    121 	 * interpretted by their sysctl to implement functions that NetBSD
    122 	 * performs in libc, such as sysctlgetmibinfo.
    123 	 *
    124 	 * From the FreeBSD kern_sysctl.c, these OIDs are:
    125 	 * {0,0}        printf the entire MIB-tree.
    126 	 * {0,1,...}    return the name of the "..." OID.
    127 	 * {0,2,...}    return the next OID.
    128 	 * {0,3}        return the OID of the name in "new"
    129 	 * {0,4,...}    return the kind & format info for the "..." OID.
    130 	 * {0,5,...}    return the description the "..." OID.
    131 	 *
    132 	 * Only implement {0,3} for now.
    133 	 */
    134 
    135 	if (namelen < 2 || namelen > CTL_MAXNAME)
    136 		return(EINVAL);
    137 
    138 	if (name[1] == 3) {
    139 		char *locnew;
    140 		int oid[CTL_MAXNAME];
    141 		u_int oidlen = CTL_MAXNAME;
    142 
    143 		new = SCARG(uap, new);
    144 		newlen = SCARG(uap, newlen);
    145 		if (new == NULL || newlen < 1 ||
    146 		    newlen > (SYSCTL_NAMELEN * CTL_MAXNAME))
    147 			return(EINVAL);
    148 
    149 		old = SCARG(uap, old);
    150 		oldlenp = SCARG(uap, oldlenp);
    151 		if (old == NULL || oldlenp == NULL || *oldlenp < sizeof(int))
    152 			return(EINVAL);
    153 
    154 		if ((locnew =
    155 		     (char *) malloc(newlen + 1, M_TEMP, M_WAITOK)) == NULL)
    156 			return(ENOMEM);
    157 
    158 		if ((error = copyinstr(new, locnew, newlen + 1, NULL)) ||
    159 		    (error = sysctl_lock(l, old, *oldlenp))) {
    160 			free(locnew, M_TEMP);
    161 			return(error);
    162 		}
    163 
    164 		ktrmibio(-1, UIO_WRITE, new, newlen + 1, error);
    165 		error = freebsd_sysctl_name2oid(locnew, oid, &oidlen);
    166 		sysctl_unlock(l);
    167 		free(locnew, M_TEMP);
    168 		if (error)
    169 			return(error);
    170 
    171 		oidlen *= sizeof(int);
    172 		error = copyout(oid, SCARG(uap, old),
    173 				MIN(oidlen, *SCARG(uap, oldlenp)));
    174 		if (error)
    175 			return(error);
    176 		ktrmibio(-1, UIO_READ, SCARG(uap, old),
    177 		    MIN(oidlen, *SCARG(uap, oldlenp)),  0);
    178 
    179 		error = copyout(&oidlen, SCARG(uap, oldlenp), sizeof(u_int));
    180 
    181 		return(error);
    182 	}
    183 
    184 	return(EOPNOTSUPP);
    185 }
    186 
    187 static int
    188 freebsd_sysctl_name2oid(char *name, int *oid, int *oidlen)
    189 {
    190 	char *dot;
    191 	int oi, ci;
    192 	struct sysctlnode *node, *pnode;
    193 
    194 	pnode = &sysctl_root;
    195 	node = sysctl_root.sysctl_child;
    196 	oi = 0;
    197 	if ((dot = strchr(name, (int)'.')) != NULL)
    198 		*dot++ = '\0';
    199 
    200 next:
    201 	while (*name != '\0' && node != NULL) {
    202 		for (ci = 0; ci < pnode->sysctl_clen; ci++) {
    203 			if (strcmp(name, node[ci].sysctl_name) == 0) {
    204 				oid[oi++] = node[ci].sysctl_num;
    205 				if ((name = dot) == NULL) {
    206 					*oidlen = oi;
    207 					return(0);
    208 				}
    209 				if ((dot = strchr(name, (int)'.')) != NULL)
    210 					*dot++ = '\0';
    211 
    212 				if (SYSCTL_TYPE(node[ci].sysctl_flags) !=
    213 				    CTLTYPE_NODE)
    214 					return(ENOTDIR);
    215 
    216 				pnode = &node[ci];
    217 				node = pnode->sysctl_child;
    218 				goto next;
    219 			}
    220 		}
    221 
    222 		/* no more nodes, it must not exist */
    223 		break;
    224 	}
    225 
    226 	return(ENOENT);
    227 }
    228