xref: /netbsd-src/sys/compat/netbsd32/netbsd32_sysctl.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: netbsd32_sysctl.c,v 1.29 2008/11/19 18:36:05 ad 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.29 2008/11/19 18:36:05 ad 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/syscallargs.h>
50 #include <sys/proc.h>
51 #include <sys/sysctl.h>
52 #include <sys/dirent.h>
53 #include <sys/ktrace.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 #include <compat/netbsd32/netbsd32_sysctl.h>
62 
63 #if defined(DDB)
64 #include <ddb/ddbvar.h>
65 #endif
66 
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)),
72 };
73 
74 static struct sysctllog *netbsd32_clog;
75 
76 /*
77  * sysctl helper routine for netbsd32's kern.boottime node
78  */
79 static int
80 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
81 {
82 	struct sysctlnode node;
83 	struct netbsd32_timeval bt32;
84 
85 	netbsd32_from_timeval(&boottime, &bt32);
86 
87 	node = *rnode;
88 	node.sysctl_data = &bt32;
89 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
90 }
91 
92 /*
93  * sysctl helper routine for netbsd32's vm.loadavg node
94  */
95 static int
96 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
97 {
98 	struct sysctlnode node;
99 	struct netbsd32_loadavg av32;
100 
101 	netbsd32_from_loadavg(&av32, &averunnable);
102 
103 	node = *rnode;
104 	node.sysctl_data = &av32;
105 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
106 }
107 
108 void
109 netbsd32_sysctl_init(void)
110 {
111 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
112 	extern const char machine_arch32[];
113 	extern const char machine32[];
114 
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);
126 
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);
138 
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);
154 }
155 
156 void
157 netbsd32_sysctl_fini(void)
158 {
159 
160 	sysctl_teardown(&netbsd32_clog);
161 	sysctl_free(&netbsd32_sysctl_root);
162 }
163 
164 int
165 netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
166 {
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;
180 
181 	/*
182 	 * get and convert 32 bit size_t to native size_t
183 	 */
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;
195 	}
196 	savelen = oldlen;
197 
198 	/*
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.
203 	 */
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);
209 
210 	ktrmib(name, SCARG(uap, namelen));
211 
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();
221 
222 	/*
223 	 * reset caller's oldlen, even if we got an error
224 	 */
225 	if (oldlenp) {
226 		netbsd32_oldlen = oldlen;
227                 nerror = copyout(&netbsd32_oldlen, oldlenp,
228 				 sizeof(netbsd32_oldlen));
229                 if (error == 0)
230                         error = nerror;
231 	}
232 
233 	/*
234 	 * if the only problem is that we weren't given enough space,
235 	 * that's an ENOMEM error
236 	 */
237 	if (error == 0 && oldp != NULL && savelen < oldlen)
238 		error = ENOMEM;
239 
240 	return (error);
241 }
242