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