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