No empty .Rs/.Re
[netbsd-mini2440.git] / sys / compat / netbsd32 / netbsd32_sysctl.c
blob6573cebf578ee82d88a91907f0d56f0e304a8ddd
1 /* $NetBSD: netbsd32_sysctl.c,v 1.29.4.1 2009/01/04 01:56:02 christos Exp $ */
3 /*
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown.
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.
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.
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.29.4.1 2009/01/04 01:56:02 christos Exp $");
37 #if defined(_KERNEL_OPT)
38 #include "opt_ddb.h"
39 #endif
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/syscallargs.h>
50 #include <sys/proc.h>
51 #include <sys/sysctl.h>
52 #include <sys/dirent.h>
53 #include <sys/ktrace.h>
55 #include <uvm/uvm_extern.h>
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 #include <compat/netbsd32/netbsd32_sysctl.h>
63 #if defined(DDB)
64 #include <ddb/ddbvar.h>
65 #endif
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)),
74 static struct sysctllog *netbsd32_clog;
77 * sysctl helper routine for netbsd32's kern.boottime node
79 static int
80 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
82 struct sysctlnode node;
83 struct netbsd32_timespec bt32;
85 netbsd32_from_timespec(&boottime, &bt32);
87 node = *rnode;
88 node.sysctl_data = &bt32;
89 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
93 * sysctl helper routine for netbsd32's vm.loadavg node
95 static int
96 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
98 struct sysctlnode node;
99 struct netbsd32_loadavg av32;
101 netbsd32_from_loadavg(&av32, &averunnable);
103 node = *rnode;
104 node.sysctl_data = &av32;
105 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
108 void
109 netbsd32_sysctl_init(void)
111 const struct sysctlnode *_root = &netbsd32_sysctl_root;
112 extern const char machine_arch32[];
113 extern const char machine32[];
115 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
116 CTLFLAG_PERMANENT,
117 CTLTYPE_NODE, "kern", NULL,
118 NULL, 0, NULL, 0,
119 CTL_KERN, CTL_EOL);
120 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
121 CTLFLAG_PERMANENT,
122 CTLTYPE_STRUCT, "boottime", NULL,
123 netbsd32_sysctl_kern_boottime, 0, NULL,
124 sizeof(struct netbsd32_timeval),
125 CTL_KERN, KERN_BOOTTIME, CTL_EOL);
127 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
128 CTLFLAG_PERMANENT,
129 CTLTYPE_NODE, "vm", NULL,
130 NULL, 0, NULL, 0,
131 CTL_VM, CTL_EOL);
132 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
133 CTLFLAG_PERMANENT,
134 CTLTYPE_STRUCT, "loadavg", NULL,
135 netbsd32_sysctl_vm_loadavg, 0, NULL,
136 sizeof(struct netbsd32_loadavg),
137 CTL_VM, VM_LOADAVG, CTL_EOL);
139 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
140 CTLFLAG_PERMANENT,
141 CTLTYPE_NODE, "hw", NULL,
142 NULL, 0, NULL, 0,
143 CTL_HW, CTL_EOL);
144 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
145 CTLFLAG_PERMANENT,
146 CTLTYPE_STRING, "machine", NULL,
147 NULL, 0, &machine32, 0,
148 CTL_HW, HW_MACHINE, CTL_EOL);
149 sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
150 CTLFLAG_PERMANENT,
151 CTLTYPE_STRING, "machine_arch", NULL,
152 NULL, 0, &machine_arch32, 0,
153 CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
156 void
157 netbsd32_sysctl_fini(void)
160 sysctl_teardown(&netbsd32_clog);
161 sysctl_free(&netbsd32_sysctl_root);
165 netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
167 /* {
168 syscallarg(netbsd32_intp) name;
169 syscallarg(u_int) namelen;
170 syscallarg(netbsd32_voidp) old;
171 syscallarg(netbsd32_size_tp) oldlenp;
172 syscallarg(netbsd32_voidp) new;
173 syscallarg(netbsd32_size_t) newlen;
174 } */
175 const struct sysctlnode *pnode;
176 netbsd32_size_t netbsd32_oldlen;
177 size_t oldlen, *oldlenp, savelen;
178 int name[CTL_MAXNAME], error, nerror, *namep;
179 void *newp, *oldp;
182 * get and convert 32 bit size_t to native size_t
184 namep = SCARG_P32(uap, name);
185 oldp = SCARG_P32(uap, old);
186 newp = SCARG_P32(uap, new);
187 oldlenp = SCARG_P32(uap, oldlenp);
188 oldlen = 0;
189 if (oldlenp != NULL) {
190 error = copyin(oldlenp, &netbsd32_oldlen,
191 sizeof(netbsd32_oldlen));
192 if (error)
193 return (error);
194 oldlen = netbsd32_oldlen;
196 savelen = oldlen;
199 * retrieve name and see if we need to dispatch this query to
200 * the shadow tree. if we find it in the shadow tree,
201 * dispatch to there, otherwise NULL means use the built-in
202 * default main tree.
204 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
205 return (EINVAL);
206 error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
207 if (error)
208 return (error);
210 ktrmib(name, SCARG(uap, namelen));
212 sysctl_lock(newp != NULL);
213 pnode = &netbsd32_sysctl_root;
214 error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
215 pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
216 error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
217 oldp, &oldlen,
218 newp, SCARG(uap, newlen),
219 &name[0], l, pnode);
220 sysctl_unlock();
223 * reset caller's oldlen, even if we got an error
225 if (oldlenp) {
226 netbsd32_oldlen = oldlen;
227 nerror = copyout(&netbsd32_oldlen, oldlenp,
228 sizeof(netbsd32_oldlen));
229 if (error == 0)
230 error = nerror;
234 * if the only problem is that we weren't given enough space,
235 * that's an ENOMEM error
237 if (error == 0 && oldp != NULL && savelen < oldlen)
238 error = ENOMEM;
240 return (error);