Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_sysctl.c revision 1.21.22.1
      1  1.21.22.1      yamt /*	$NetBSD: netbsd32_sysctl.c,v 1.21.22.1 2006/10/22 06:05:27 yamt Exp $	*/
      2        1.1       mrg 
      3        1.1       mrg /*
      4       1.12    atatat  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5        1.1       mrg  * All rights reserved.
      6        1.1       mrg  *
      7       1.12    atatat  * This code is derived from software contributed to The NetBSD Foundation
      8       1.12    atatat  * by Andrew Brown.
      9       1.12    atatat  *
     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.21.22.1      yamt __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.21.22.1 2006/10/22 06:05:27 yamt Exp $");
     36        1.1       mrg 
     37        1.3       mrg #if defined(_KERNEL_OPT)
     38        1.1       mrg #include "opt_ddb.h"
     39  1.21.22.1      yamt #include "opt_ktrace.h"
     40        1.1       mrg #endif
     41        1.1       mrg 
     42        1.1       mrg #include <sys/param.h>
     43        1.1       mrg #include <sys/systm.h>
     44        1.1       mrg #include <sys/kernel.h>
     45        1.1       mrg #include <sys/malloc.h>
     46        1.1       mrg #include <sys/mount.h>
     47        1.1       mrg #include <sys/stat.h>
     48        1.1       mrg #include <sys/time.h>
     49        1.1       mrg #include <sys/vnode.h>
     50        1.8   thorpej #include <sys/sa.h>
     51        1.1       mrg #include <sys/syscallargs.h>
     52        1.1       mrg #include <sys/proc.h>
     53        1.1       mrg #include <sys/sysctl.h>
     54       1.20  christos #include <sys/dirent.h>
     55  1.21.22.1      yamt #ifdef KTRACE
     56  1.21.22.1      yamt #include <sys/ktrace.h>
     57  1.21.22.1      yamt #endif
     58        1.1       mrg 
     59        1.1       mrg #include <uvm/uvm_extern.h>
     60        1.1       mrg 
     61        1.1       mrg #include <compat/netbsd32/netbsd32.h>
     62        1.1       mrg #include <compat/netbsd32/netbsd32_syscall.h>
     63        1.1       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     64        1.1       mrg #include <compat/netbsd32/netbsd32_conv.h>
     65        1.1       mrg 
     66        1.1       mrg #if defined(DDB)
     67        1.1       mrg #include <ddb/ddbvar.h>
     68        1.1       mrg #endif
     69        1.1       mrg 
     70       1.12    atatat struct sysctlnode netbsd32_sysctl_root = {
     71       1.14    atatat 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
     72       1.12    atatat 	.sysctl_num = 0,
     73       1.12    atatat 	.sysctl_name = "(netbsd32_root)",
     74       1.15    atatat 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
     75       1.12    atatat };
     76        1.1       mrg 
     77        1.1       mrg /*
     78       1.12    atatat  * sysctl helper routine for netbsd32's kern.boottime node
     79        1.1       mrg  */
     80       1.12    atatat static int
     81       1.12    atatat netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
     82        1.1       mrg {
     83       1.12    atatat 	struct sysctlnode node;
     84       1.12    atatat 	struct netbsd32_timeval bt32;
     85        1.1       mrg 
     86       1.12    atatat 	netbsd32_from_timeval(&boottime, &bt32);
     87        1.1       mrg 
     88       1.12    atatat 	node = *rnode;
     89       1.12    atatat 	node.sysctl_data = &bt32;
     90       1.12    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
     91        1.1       mrg }
     92        1.1       mrg 
     93        1.1       mrg /*
     94       1.12    atatat  * sysctl helper routine for netbsd32's vm.loadavg node
     95        1.1       mrg  */
     96       1.12    atatat static int
     97       1.12    atatat netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
     98        1.1       mrg {
     99       1.12    atatat 	struct sysctlnode node;
    100       1.12    atatat 	struct netbsd32_loadavg av32;
    101        1.1       mrg 
    102       1.12    atatat 	netbsd32_from_loadavg(&av32, &averunnable);
    103        1.1       mrg 
    104       1.12    atatat 	node = *rnode;
    105       1.12    atatat 	node.sysctl_data = &av32;
    106       1.12    atatat 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    107        1.1       mrg }
    108        1.1       mrg 
    109       1.12    atatat SYSCTL_SETUP(netbsd32_sysctl_emul_setup, "sysctl netbsd32 shadow tree setup")
    110        1.6       eeh {
    111       1.16    atatat 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
    112       1.19      cube 	extern const char machine_arch32[];
    113       1.19      cube 	extern const char machine32[];
    114        1.6       eeh 
    115       1.14    atatat 	sysctl_createv(clog, 0, &_root, NULL,
    116       1.14    atatat 		       CTLFLAG_PERMANENT,
    117       1.14    atatat 		       CTLTYPE_NODE, "kern", NULL,
    118       1.12    atatat 		       NULL, 0, NULL, 0,
    119       1.12    atatat 		       CTL_KERN, CTL_EOL);
    120       1.14    atatat 	sysctl_createv(clog, 0, &_root, NULL,
    121       1.14    atatat 		       CTLFLAG_PERMANENT,
    122       1.14    atatat 		       CTLTYPE_STRUCT, "boottime", NULL,
    123       1.12    atatat 		       netbsd32_sysctl_kern_boottime, 0, NULL,
    124       1.12    atatat 		       sizeof(struct netbsd32_timeval),
    125       1.12    atatat 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
    126       1.12    atatat 
    127       1.14    atatat 	sysctl_createv(clog, 0, &_root, NULL,
    128       1.14    atatat 		       CTLFLAG_PERMANENT,
    129       1.14    atatat 		       CTLTYPE_NODE, "vm", NULL,
    130       1.12    atatat 		       NULL, 0, NULL, 0,
    131       1.12    atatat 		       CTL_VM, CTL_EOL);
    132       1.14    atatat 	sysctl_createv(clog, 0, &_root, NULL,
    133       1.14    atatat 		       CTLFLAG_PERMANENT,
    134       1.14    atatat 		       CTLTYPE_STRUCT, "loadavg", NULL,
    135       1.12    atatat 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
    136       1.12    atatat 		       sizeof(struct netbsd32_loadavg),
    137       1.12    atatat 		       CTL_VM, VM_LOADAVG, CTL_EOL);
    138       1.12    atatat 
    139       1.14    atatat 	sysctl_createv(clog, 0, &_root, NULL,
    140       1.14    atatat 		       CTLFLAG_PERMANENT,
    141       1.14    atatat 		       CTLTYPE_NODE, "hw", NULL,
    142       1.12    atatat 		       NULL, 0, NULL, 0,
    143       1.12    atatat 		       CTL_HW, CTL_EOL);
    144       1.14    atatat 	sysctl_createv(clog, 0, &_root, NULL,
    145       1.14    atatat 		       CTLFLAG_PERMANENT,
    146       1.18      cube 		       CTLTYPE_STRING, "machine", NULL,
    147       1.19      cube 		       NULL, 0, &machine32, 0,
    148       1.18      cube 		       CTL_HW, HW_MACHINE, CTL_EOL);
    149       1.18      cube 	sysctl_createv(clog, 0, &_root, NULL,
    150       1.18      cube 		       CTLFLAG_PERMANENT,
    151       1.14    atatat 		       CTLTYPE_STRING, "machine_arch", NULL,
    152       1.19      cube 		       NULL, 0, &machine_arch32, 0,
    153       1.12    atatat 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
    154        1.6       eeh }
    155        1.6       eeh 
    156        1.1       mrg int
    157        1.8   thorpej netbsd32___sysctl(l, v, retval)
    158        1.8   thorpej 	struct lwp *l;
    159        1.1       mrg 	void *v;
    160        1.1       mrg 	register_t *retval;
    161        1.1       mrg {
    162        1.1       mrg 	struct netbsd32___sysctl_args /* {
    163        1.1       mrg 		syscallarg(netbsd32_intp) name;
    164        1.1       mrg 		syscallarg(u_int) namelen;
    165        1.1       mrg 		syscallarg(netbsd32_voidp) old;
    166        1.1       mrg 		syscallarg(netbsd32_size_tp) oldlenp;
    167        1.1       mrg 		syscallarg(netbsd32_voidp) new;
    168        1.1       mrg 		syscallarg(netbsd32_size_t) newlen;
    169        1.1       mrg 	} */ *uap = v;
    170       1.17    atatat 	const struct sysctlnode *pnode;
    171       1.12    atatat 	netbsd32_size_t netbsd32_oldlen;
    172       1.12    atatat 	size_t oldlen, *oldlenp, savelen;
    173       1.12    atatat 	int name[CTL_MAXNAME], error, nerror, *namep;
    174       1.12    atatat 	void *newp, *oldp;
    175        1.1       mrg 
    176       1.12    atatat 	/*
    177       1.12    atatat 	 * get and convert 32 bit size_t to native size_t
    178       1.12    atatat 	 */
    179       1.12    atatat 	namep = NETBSD32PTR64(SCARG(uap, name));
    180       1.12    atatat 	oldp = NETBSD32PTR64(SCARG(uap, old));
    181       1.12    atatat 	newp = NETBSD32PTR64(SCARG(uap, new));
    182       1.12    atatat 	oldlenp = NETBSD32PTR64(SCARG(uap, oldlenp));
    183       1.12    atatat 	oldlen = 0;
    184       1.12    atatat 	if (oldlenp != NULL) {
    185       1.12    atatat 		error = copyin(oldlenp, &netbsd32_oldlen,
    186       1.12    atatat 			       sizeof(netbsd32_oldlen));
    187       1.12    atatat 		if (error)
    188       1.12    atatat 			return (error);
    189       1.12    atatat 		oldlen = netbsd32_oldlen;
    190       1.12    atatat 	}
    191       1.12    atatat 	savelen = oldlen;
    192        1.1       mrg 
    193        1.1       mrg 	/*
    194       1.12    atatat 	 * retrieve name and see if we need to dispatch this query to
    195       1.12    atatat 	 * the shadow tree.  if we find it in the shadow tree,
    196       1.12    atatat 	 * dispatch to there, otherwise NULL means use the built-in
    197       1.12    atatat 	 * default main tree.
    198        1.1       mrg 	 */
    199       1.12    atatat 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
    200        1.1       mrg 		return (EINVAL);
    201       1.12    atatat 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
    202       1.12    atatat         if (error)
    203       1.12    atatat                 return (error);
    204       1.12    atatat 
    205  1.21.22.1      yamt #ifdef KTRACE
    206  1.21.22.1      yamt 	if (KTRPOINT(l->l_proc, KTR_MIB))
    207  1.21.22.1      yamt 		ktrmib(l, name, SCARG(uap, namelen));
    208  1.21.22.1      yamt #endif
    209  1.21.22.1      yamt 
    210       1.12    atatat 	/*
    211       1.12    atatat 	 * wire old so that copyout() is less likely to fail?
    212       1.12    atatat 	 */
    213       1.12    atatat 	error = sysctl_lock(l, oldp, savelen);
    214        1.1       mrg 	if (error)
    215        1.1       mrg 		return (error);
    216        1.1       mrg 
    217       1.12    atatat 	pnode = &netbsd32_sysctl_root;
    218       1.12    atatat 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
    219       1.12    atatat 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
    220       1.12    atatat 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
    221       1.12    atatat 				oldp, &oldlen,
    222       1.12    atatat 				newp, SCARG(uap, newlen),
    223       1.12    atatat 				&name[0], l, pnode);
    224       1.12    atatat 
    225       1.12    atatat 	/*
    226       1.12    atatat 	 * release the sysctl lock
    227       1.12    atatat 	 */
    228       1.12    atatat 	sysctl_unlock(l);
    229       1.12    atatat 
    230       1.12    atatat 	/*
    231       1.12    atatat 	 * reset caller's oldlen, even if we got an error
    232       1.12    atatat 	 */
    233       1.12    atatat 	if (oldlenp) {
    234       1.12    atatat 		netbsd32_oldlen = oldlen;
    235       1.12    atatat                 nerror = copyout(&netbsd32_oldlen, oldlenp,
    236       1.12    atatat 				 sizeof(netbsd32_oldlen));
    237       1.12    atatat                 if (error == 0)
    238       1.12    atatat                         error = nerror;
    239        1.1       mrg 	}
    240        1.1       mrg 
    241        1.1       mrg 	/*
    242       1.12    atatat 	 * if the only problem is that we weren't given enough space,
    243       1.12    atatat 	 * that's an ENOMEM error
    244        1.1       mrg 	 */
    245       1.12    atatat 	if (error == 0 && oldp != NULL && savelen < oldlen)
    246       1.12    atatat 		error = ENOMEM;
    247        1.1       mrg 
    248        1.1       mrg 	return (error);
    249        1.1       mrg }
    250