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