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