Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_sysctl.c revision 1.10.2.4
      1  1.10.2.4    skrll /*	$NetBSD: netbsd32_sysctl.c,v 1.10.2.4 2005/11/10 14:01:21 skrll Exp $	*/
      2       1.1      mrg 
      3       1.1      mrg /*
      4  1.10.2.1    skrll  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5       1.1      mrg  * All rights reserved.
      6       1.1      mrg  *
      7  1.10.2.1    skrll  * This code is derived from software contributed to The NetBSD Foundation
      8  1.10.2.1    skrll  * by Andrew Brown.
      9  1.10.2.1    skrll  *
     10       1.1      mrg  * Redistribution and use in source and binary forms, with or without
     11       1.1      mrg  * modification, are permitted provided that the following conditions
     12       1.1      mrg  * are met:
     13       1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     14       1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     15       1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      mrg  *    documentation and/or other materials provided with the distribution.
     18       1.1      mrg  * 3. The name of the author may not be used to endorse or promote products
     19       1.1      mrg  *    derived from this software without specific prior written permission.
     20       1.1      mrg  *
     21       1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1      mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.1      mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.1      mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.1      mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     26       1.1      mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27       1.1      mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     28       1.1      mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29       1.1      mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30       1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31       1.1      mrg  * SUCH DAMAGE.
     32       1.1      mrg  */
     33       1.4    lukem 
     34       1.4    lukem #include <sys/cdefs.h>
     35  1.10.2.4    skrll __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.10.2.4 2005/11/10 14:01:21 skrll Exp $");
     36       1.1      mrg 
     37       1.3      mrg #if defined(_KERNEL_OPT)
     38       1.1      mrg #include "opt_ddb.h"
     39       1.1      mrg #endif
     40       1.1      mrg 
     41       1.1      mrg #include <sys/param.h>
     42       1.1      mrg #include <sys/systm.h>
     43       1.1      mrg #include <sys/kernel.h>
     44       1.1      mrg #include <sys/malloc.h>
     45       1.1      mrg #include <sys/mount.h>
     46       1.1      mrg #include <sys/stat.h>
     47       1.1      mrg #include <sys/time.h>
     48       1.1      mrg #include <sys/vnode.h>
     49       1.8  thorpej #include <sys/sa.h>
     50       1.1      mrg #include <sys/syscallargs.h>
     51       1.1      mrg #include <sys/proc.h>
     52       1.1      mrg #include <sys/sysctl.h>
     53  1.10.2.4    skrll #include <sys/dirent.h>
     54       1.1      mrg 
     55       1.1      mrg #include <uvm/uvm_extern.h>
     56       1.1      mrg 
     57       1.1      mrg #include <compat/netbsd32/netbsd32.h>
     58       1.1      mrg #include <compat/netbsd32/netbsd32_syscall.h>
     59       1.1      mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     60       1.1      mrg #include <compat/netbsd32/netbsd32_conv.h>
     61       1.1      mrg 
     62       1.1      mrg #if defined(DDB)
     63       1.1      mrg #include <ddb/ddbvar.h>
     64       1.1      mrg #endif
     65       1.1      mrg 
     66  1.10.2.1    skrll struct sysctlnode netbsd32_sysctl_root = {
     67  1.10.2.1    skrll 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
     68  1.10.2.1    skrll 	.sysctl_num = 0,
     69  1.10.2.1    skrll 	.sysctl_name = "(netbsd32_root)",
     70  1.10.2.1    skrll 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
     71  1.10.2.1    skrll };
     72       1.1      mrg 
     73       1.1      mrg /*
     74  1.10.2.1    skrll  * sysctl helper routine for netbsd32's kern.boottime node
     75       1.1      mrg  */
     76  1.10.2.1    skrll static int
     77  1.10.2.1    skrll netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
     78       1.1      mrg {
     79  1.10.2.1    skrll 	struct sysctlnode node;
     80  1.10.2.1    skrll 	struct netbsd32_timeval bt32;
     81       1.1      mrg 
     82  1.10.2.1    skrll 	netbsd32_from_timeval(&boottime, &bt32);
     83       1.1      mrg 
     84  1.10.2.1    skrll 	node = *rnode;
     85  1.10.2.1    skrll 	node.sysctl_data = &bt32;
     86  1.10.2.1    skrll 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
     87       1.1      mrg }
     88       1.1      mrg 
     89       1.1      mrg /*
     90  1.10.2.1    skrll  * sysctl helper routine for netbsd32's vm.loadavg node
     91       1.1      mrg  */
     92  1.10.2.1    skrll static int
     93  1.10.2.1    skrll netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
     94       1.1      mrg {
     95  1.10.2.1    skrll 	struct sysctlnode node;
     96  1.10.2.1    skrll 	struct netbsd32_loadavg av32;
     97       1.1      mrg 
     98  1.10.2.1    skrll 	netbsd32_from_loadavg(&av32, &averunnable);
     99       1.1      mrg 
    100  1.10.2.1    skrll 	node = *rnode;
    101  1.10.2.1    skrll 	node.sysctl_data = &av32;
    102  1.10.2.1    skrll 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    103       1.1      mrg }
    104       1.1      mrg 
    105  1.10.2.1    skrll SYSCTL_SETUP(netbsd32_sysctl_emul_setup, "sysctl netbsd32 shadow tree setup")
    106       1.6      eeh {
    107  1.10.2.4    skrll 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
    108  1.10.2.4    skrll 	extern const char machine_arch32[];
    109  1.10.2.4    skrll 	extern const char machine32[];
    110       1.6      eeh 
    111  1.10.2.1    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    112  1.10.2.1    skrll 		       CTLFLAG_PERMANENT,
    113  1.10.2.1    skrll 		       CTLTYPE_NODE, "kern", NULL,
    114  1.10.2.1    skrll 		       NULL, 0, NULL, 0,
    115  1.10.2.1    skrll 		       CTL_KERN, CTL_EOL);
    116  1.10.2.1    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    117  1.10.2.1    skrll 		       CTLFLAG_PERMANENT,
    118  1.10.2.1    skrll 		       CTLTYPE_STRUCT, "boottime", NULL,
    119  1.10.2.1    skrll 		       netbsd32_sysctl_kern_boottime, 0, NULL,
    120  1.10.2.1    skrll 		       sizeof(struct netbsd32_timeval),
    121  1.10.2.1    skrll 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    122  1.10.2.1    skrll 
    123  1.10.2.1    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    124  1.10.2.1    skrll 		       CTLFLAG_PERMANENT,
    125  1.10.2.1    skrll 		       CTLTYPE_NODE, "vm", NULL,
    126  1.10.2.1    skrll 		       NULL, 0, NULL, 0,
    127  1.10.2.1    skrll 		       CTL_VM, CTL_EOL);
    128  1.10.2.1    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    129  1.10.2.1    skrll 		       CTLFLAG_PERMANENT,
    130  1.10.2.1    skrll 		       CTLTYPE_STRUCT, "loadavg", NULL,
    131  1.10.2.1    skrll 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
    132  1.10.2.1    skrll 		       sizeof(struct netbsd32_loadavg),
    133  1.10.2.1    skrll 		       CTL_VM, VM_LOADAVG, CTL_EOL);
    134  1.10.2.1    skrll 
    135  1.10.2.1    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    136  1.10.2.1    skrll 		       CTLFLAG_PERMANENT,
    137  1.10.2.1    skrll 		       CTLTYPE_NODE, "hw", NULL,
    138  1.10.2.1    skrll 		       NULL, 0, NULL, 0,
    139  1.10.2.1    skrll 		       CTL_HW, CTL_EOL);
    140  1.10.2.1    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    141  1.10.2.1    skrll 		       CTLFLAG_PERMANENT,
    142  1.10.2.4    skrll 		       CTLTYPE_STRING, "machine", NULL,
    143  1.10.2.4    skrll 		       NULL, 0, &machine32, 0,
    144  1.10.2.4    skrll 		       CTL_HW, HW_MACHINE, CTL_EOL);
    145  1.10.2.4    skrll 	sysctl_createv(clog, 0, &_root, NULL,
    146  1.10.2.4    skrll 		       CTLFLAG_PERMANENT,
    147  1.10.2.1    skrll 		       CTLTYPE_STRING, "machine_arch", NULL,
    148  1.10.2.4    skrll 		       NULL, 0, &machine_arch32, 0,
    149  1.10.2.1    skrll 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    150       1.6      eeh }
    151       1.6      eeh 
    152       1.1      mrg int
    153       1.8  thorpej netbsd32___sysctl(l, v, retval)
    154       1.8  thorpej 	struct lwp *l;
    155       1.1      mrg 	void *v;
    156       1.1      mrg 	register_t *retval;
    157       1.1      mrg {
    158       1.1      mrg 	struct netbsd32___sysctl_args /* {
    159       1.1      mrg 		syscallarg(netbsd32_intp) name;
    160       1.1      mrg 		syscallarg(u_int) namelen;
    161       1.1      mrg 		syscallarg(netbsd32_voidp) old;
    162       1.1      mrg 		syscallarg(netbsd32_size_tp) oldlenp;
    163       1.1      mrg 		syscallarg(netbsd32_voidp) new;
    164       1.1      mrg 		syscallarg(netbsd32_size_t) newlen;
    165       1.1      mrg 	} */ *uap = v;
    166  1.10.2.4    skrll 	const struct sysctlnode *pnode;
    167  1.10.2.1    skrll 	netbsd32_size_t netbsd32_oldlen;
    168  1.10.2.1    skrll 	size_t oldlen, *oldlenp, savelen;
    169  1.10.2.1    skrll 	int name[CTL_MAXNAME], error, nerror, *namep;
    170  1.10.2.1    skrll 	void *newp, *oldp;
    171       1.1      mrg 
    172  1.10.2.1    skrll 	/*
    173  1.10.2.1    skrll 	 * get and convert 32 bit size_t to native size_t
    174  1.10.2.1    skrll 	 */
    175  1.10.2.1    skrll 	namep = NETBSD32PTR64(SCARG(uap, name));
    176  1.10.2.1    skrll 	oldp = NETBSD32PTR64(SCARG(uap, old));
    177  1.10.2.1    skrll 	newp = NETBSD32PTR64(SCARG(uap, new));
    178  1.10.2.1    skrll 	oldlenp = NETBSD32PTR64(SCARG(uap, oldlenp));
    179  1.10.2.1    skrll 	oldlen = 0;
    180  1.10.2.1    skrll 	if (oldlenp != NULL) {
    181  1.10.2.1    skrll 		error = copyin(oldlenp, &netbsd32_oldlen,
    182  1.10.2.1    skrll 			       sizeof(netbsd32_oldlen));
    183  1.10.2.1    skrll 		if (error)
    184  1.10.2.1    skrll 			return (error);
    185  1.10.2.1    skrll 		oldlen = netbsd32_oldlen;
    186  1.10.2.1    skrll 	}
    187  1.10.2.1    skrll 	savelen = oldlen;
    188       1.1      mrg 
    189       1.1      mrg 	/*
    190  1.10.2.1    skrll 	 * retrieve name and see if we need to dispatch this query to
    191  1.10.2.1    skrll 	 * the shadow tree.  if we find it in the shadow tree,
    192  1.10.2.1    skrll 	 * dispatch to there, otherwise NULL means use the built-in
    193  1.10.2.1    skrll 	 * default main tree.
    194       1.1      mrg 	 */
    195  1.10.2.1    skrll 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
    196       1.1      mrg 		return (EINVAL);
    197  1.10.2.1    skrll 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
    198  1.10.2.1    skrll         if (error)
    199  1.10.2.1    skrll                 return (error);
    200  1.10.2.1    skrll 
    201  1.10.2.1    skrll 	/*
    202  1.10.2.1    skrll 	 * wire old so that copyout() is less likely to fail?
    203  1.10.2.1    skrll 	 */
    204  1.10.2.1    skrll 	error = sysctl_lock(l, oldp, savelen);
    205       1.1      mrg 	if (error)
    206       1.1      mrg 		return (error);
    207       1.1      mrg 
    208  1.10.2.1    skrll 	pnode = &netbsd32_sysctl_root;
    209  1.10.2.1    skrll 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
    210  1.10.2.1    skrll 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
    211  1.10.2.1    skrll 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
    212  1.10.2.1    skrll 				oldp, &oldlen,
    213  1.10.2.1    skrll 				newp, SCARG(uap, newlen),
    214  1.10.2.1    skrll 				&name[0], l, pnode);
    215       1.1      mrg 
    216       1.1      mrg 	/*
    217  1.10.2.1    skrll 	 * release the sysctl lock
    218       1.1      mrg 	 */
    219  1.10.2.1    skrll 	sysctl_unlock(l);
    220       1.1      mrg 
    221  1.10.2.1    skrll 	/*
    222  1.10.2.1    skrll 	 * reset caller's oldlen, even if we got an error
    223  1.10.2.1    skrll 	 */
    224  1.10.2.1    skrll 	if (oldlenp) {
    225  1.10.2.1    skrll 		netbsd32_oldlen = oldlen;
    226  1.10.2.1    skrll                 nerror = copyout(&netbsd32_oldlen, oldlenp,
    227  1.10.2.1    skrll 				 sizeof(netbsd32_oldlen));
    228  1.10.2.1    skrll                 if (error == 0)
    229  1.10.2.1    skrll                         error = nerror;
    230       1.1      mrg 	}
    231  1.10.2.1    skrll 
    232  1.10.2.1    skrll 	/*
    233  1.10.2.1    skrll 	 * if the only problem is that we weren't given enough space,
    234  1.10.2.1    skrll 	 * that's an ENOMEM error
    235  1.10.2.1    skrll 	 */
    236  1.10.2.1    skrll 	if (error == 0 && oldp != NULL && savelen < oldlen)
    237  1.10.2.1    skrll 		error = ENOMEM;
    238  1.10.2.1    skrll 
    239       1.1      mrg 	return (error);
    240       1.1      mrg }
    241