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