Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_sysctl.c revision 1.1.4.5
      1 /*	$NetBSD: netbsd32_sysctl.c,v 1.1.4.5 2002/05/29 21:32:50 nathanw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.1.4.5 2002/05/29 21:32:50 nathanw Exp $");
     33 
     34 #if defined(_KERNEL_OPT)
     35 #include "opt_ddb.h"
     36 #endif
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mount.h>
     43 #include <sys/stat.h>
     44 #include <sys/time.h>
     45 #include <sys/vnode.h>
     46 #include <sys/sa.h>
     47 #include <sys/syscallargs.h>
     48 #include <sys/proc.h>
     49 #define	__SYSCTL_PRIVATE
     50 #include <sys/sysctl.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <compat/netbsd32/netbsd32.h>
     55 #include <compat/netbsd32/netbsd32_syscall.h>
     56 #include <compat/netbsd32/netbsd32_syscallargs.h>
     57 #include <compat/netbsd32/netbsd32_conv.h>
     58 
     59 #if defined(DDB)
     60 #include <ddb/ddbvar.h>
     61 #endif
     62 
     63 int uvm_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
     64 int kern_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
     65 
     66 /*
     67  * uvm_sysctl32: sysctl hook into UVM system, handling special 32-bit
     68  * sensitive calls.
     69  */
     70 int
     71 uvm_sysctl32(name, namelen, oldp, oldlenp, newp, newlen, p)
     72 	int *name;
     73 	u_int namelen;
     74 	void *oldp;
     75 	size_t *oldlenp;
     76 	void *newp;
     77 	size_t newlen;
     78 	struct proc *p;
     79 {
     80 	struct netbsd32_loadavg av32;
     81 
     82 	/* all sysctl names at this level are terminal */
     83 	if (namelen != 1)
     84 		return (ENOTDIR);		/* overloaded */
     85 
     86 	switch (name[0]) {
     87 	case VM_LOADAVG:
     88 		netbsd32_from_loadavg(&av32, &averunnable);
     89 		return (sysctl_rdstruct(oldp, oldlenp, newp, &av32,
     90 		    sizeof(av32)));
     91 
     92 	default:
     93 		return (EOPNOTSUPP);
     94 	}
     95 	/* NOTREACHED */
     96 }
     97 
     98 /*
     99  * kern_sysctl32: sysctl hook into KERN system, handling special 32-bit
    100  * sensitive calls.
    101  */
    102 int
    103 kern_sysctl32(name, namelen, oldp, oldlenp, newp, newlen, p)
    104 	int *name;
    105 	u_int namelen;
    106 	void *oldp;
    107 	size_t *oldlenp;
    108 	void *newp;
    109 	size_t newlen;
    110 	struct proc *p;
    111 {
    112 	struct netbsd32_timeval bt32;
    113 
    114 	/* All sysctl names at this level, except for a few, are terminal. */
    115 	switch (name[0]) {
    116 #if 0
    117 	case KERN_PROC:
    118 	case KERN_PROC2:
    119 	case KERN_PROF:
    120 	case KERN_MBUF:
    121 	case KERN_PROC_ARGS:
    122 	case KERN_SYSVIPC_INFO:
    123 		/* Not terminal. */
    124 		break;
    125 #endif
    126 	default:
    127 		if (namelen != 1)
    128 			return (ENOTDIR);	/* overloaded */
    129 	}
    130 
    131 	switch (name[0]) {
    132 	case KERN_BOOTTIME:
    133 		netbsd32_from_timeval(&boottime, &bt32);
    134 		return (sysctl_rdstruct(oldp, oldlenp, newp, &bt32,
    135 		    sizeof(struct netbsd32_timeval)));
    136 
    137 	default:
    138 		return (EOPNOTSUPP);
    139 	}
    140 	/* NOTREACHED */
    141 }
    142 
    143 int
    144 netbsd32___sysctl(p, v, retval)
    145 	struct proc *p;
    146 	void *v;
    147 	register_t *retval;
    148 {
    149 	struct netbsd32___sysctl_args /* {
    150 		syscallarg(netbsd32_intp) name;
    151 		syscallarg(u_int) namelen;
    152 		syscallarg(netbsd32_voidp) old;
    153 		syscallarg(netbsd32_size_tp) oldlenp;
    154 		syscallarg(netbsd32_voidp) new;
    155 		syscallarg(netbsd32_size_t) newlen;
    156 	} */ *uap = v;
    157 	int error;
    158 	netbsd32_size_t savelen = 0;
    159 	size_t oldlen = 0;
    160 	sysctlfn *fn;
    161 	int name[CTL_MAXNAME];
    162 
    163 /*
    164  * Some of these sysctl functions do their own copyin/copyout.
    165  * We need to disable or emulate the ones that need their
    166  * arguments converted.
    167  */
    168 
    169 	if (SCARG(uap, new) != NULL &&
    170 	    (error = suser(p->p_ucred, &p->p_acflag)))
    171 		return (error);
    172 	/*
    173 	 * all top-level sysctl names are non-terminal
    174 	 */
    175 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
    176 		return (EINVAL);
    177 	error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
    178 		       SCARG(uap, namelen) * sizeof(int));
    179 	if (error)
    180 		return (error);
    181 
    182 	switch (name[0]) {
    183 	case CTL_KERN:
    184 		switch (name[1]) {
    185 #if 0
    186 		case KERN_FILE:
    187 		case KERN_NTPTIME:
    188 		case KERN_SYSVIPC_INFO:
    189 #endif
    190 		case KERN_BOOTTIME:
    191 			fn = kern_sysctl32;
    192 			break;
    193 		default:
    194 			fn = kern_sysctl;
    195 			break;
    196 		}
    197 		break;
    198 	case CTL_HW:
    199 		fn = hw_sysctl;
    200 		break;
    201 	case CTL_VM:
    202 		switch (name[1]) {
    203 		case VM_LOADAVG:
    204 			fn = uvm_sysctl32;	/* need to convert a `long' */
    205 			break;
    206 		default:
    207 			fn = uvm_sysctl;
    208 			break;
    209 		}
    210 		break;
    211 	case CTL_NET:
    212 		fn = net_sysctl;
    213 		break;
    214 	case CTL_VFS:
    215 		fn = vfs_sysctl;
    216 		break;
    217 	case CTL_MACHDEP:
    218 		fn = cpu_sysctl;
    219 		break;
    220 #ifdef DEBUG
    221 	case CTL_DEBUG:
    222 		fn = debug_sysctl;
    223 		break;
    224 #endif
    225 #ifdef DDB
    226 	case CTL_DDB:
    227 		fn = ddb_sysctl;
    228 		break;
    229 #endif
    230 	case CTL_PROC:
    231 		fn = proc_sysctl;
    232 		break;
    233 	default:
    234 		return (EOPNOTSUPP);
    235 	}
    236 
    237 	/*
    238 	 * XXX Hey, we wire `old', but what about `new'?
    239 	 */
    240 
    241 	if (SCARG(uap, oldlenp) &&
    242 	    (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen,
    243 	     sizeof(savelen))))
    244 		return (error);
    245 	if (SCARG(uap, old) != NULL) {
    246 		error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
    247 		if (error)
    248 			return (error);
    249 		error = uvm_vslock(p, (void *)(vaddr_t)SCARG(uap, old), savelen,
    250 		    VM_PROT_WRITE);
    251 		if (error) {
    252 			(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
    253 			return error;
    254 		}
    255 		oldlen = savelen;
    256 	}
    257 	error = (*fn)(name + 1, SCARG(uap, namelen) - 1,
    258 		      (void *)(u_long)SCARG(uap, old), &oldlen,
    259 		      (void *)(u_long)SCARG(uap, new), SCARG(uap, newlen), p);
    260 	if (SCARG(uap, old) != NULL) {
    261 		uvm_vsunlock(p, (void *)(u_long)SCARG(uap, old), savelen);
    262 		(void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
    263 	}
    264 	savelen = oldlen;
    265 	if (error)
    266 		return (error);
    267 	if (SCARG(uap, oldlenp))
    268 		error = copyout(&savelen,
    269 		    (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
    270 	return (error);
    271 }
    272