1 /* $NetBSD: netbsd32_sysctl.c,v 1.29.4.1 2009/01/04 01:56:02 christos Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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)
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>
48 #include <sys/vnode.h>
49 #include <sys/syscallargs.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>
64 #include <ddb/ddbvar.h>
67 struct sysctlnode netbsd32_sysctl_root
= {
68 .sysctl_flags
= SYSCTL_VERSION
|CTLFLAG_ROOT
|CTLTYPE_NODE
,
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
80 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS
)
82 struct sysctlnode node
;
83 struct netbsd32_timespec bt32
;
85 netbsd32_from_timespec(&boottime
, &bt32
);
88 node
.sysctl_data
= &bt32
;
89 return (sysctl_lookup(SYSCTLFN_CALL(&node
)));
93 * sysctl helper routine for netbsd32's vm.loadavg node
96 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS
)
98 struct sysctlnode node
;
99 struct netbsd32_loadavg av32
;
101 netbsd32_from_loadavg(&av32
, &averunnable
);
104 node
.sysctl_data
= &av32
;
105 return (sysctl_lookup(SYSCTLFN_CALL(&node
)));
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
,
117 CTLTYPE_NODE
, "kern", NULL
,
120 sysctl_createv(&netbsd32_clog
, 0, &_root
, NULL
,
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
,
129 CTLTYPE_NODE
, "vm", NULL
,
132 sysctl_createv(&netbsd32_clog
, 0, &_root
, NULL
,
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
,
141 CTLTYPE_NODE
, "hw", NULL
,
144 sysctl_createv(&netbsd32_clog
, 0, &_root
, NULL
,
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
,
151 CTLTYPE_STRING
, "machine_arch", NULL
,
152 NULL
, 0, &machine_arch32
, 0,
153 CTL_HW
, HW_MACHINE_ARCH
, CTL_EOL
);
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
)
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;
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
;
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
);
189 if (oldlenp
!= NULL
) {
190 error
= copyin(oldlenp
, &netbsd32_oldlen
,
191 sizeof(netbsd32_oldlen
));
194 oldlen
= netbsd32_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
204 if (SCARG(uap
, namelen
) > CTL_MAXNAME
|| SCARG(uap
, namelen
) < 1)
206 error
= copyin(namep
, &name
[0], SCARG(uap
, namelen
) * sizeof(int));
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
),
218 newp
, SCARG(uap
, newlen
),
223 * reset caller's oldlen, even if we got an error
226 netbsd32_oldlen
= oldlen
;
227 nerror
= copyout(&netbsd32_oldlen
, oldlenp
,
228 sizeof(netbsd32_oldlen
));
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
)