xref: /netbsd-src/sys/compat/netbsd32/netbsd32_sysctl.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: netbsd32_sysctl.c,v 1.31 2010/04/23 15:19:20 rmind 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.31 2010/04/23 15:19:20 rmind 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/mount.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/vnode.h>
48 #include <sys/syscallargs.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51 #include <sys/dirent.h>
52 #include <sys/ktrace.h>
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <compat/netbsd32/netbsd32.h>
57 #include <compat/netbsd32/netbsd32_syscall.h>
58 #include <compat/netbsd32/netbsd32_syscallargs.h>
59 #include <compat/netbsd32/netbsd32_conv.h>
60 #include <compat/netbsd32/netbsd32_sysctl.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 static struct sysctllog *netbsd32_clog;
74 
75 /*
76  * sysctl helper routine for netbsd32's kern.boottime node
77  */
78 static int
79 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
80 {
81 	struct sysctlnode node;
82 	struct netbsd32_timespec bt32;
83 
84 	netbsd32_from_timespec(&boottime, &bt32);
85 
86 	node = *rnode;
87 	node.sysctl_data = &bt32;
88 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
89 }
90 
91 /*
92  * sysctl helper routine for netbsd32's vm.loadavg node
93  */
94 static int
95 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
96 {
97 	struct sysctlnode node;
98 	struct netbsd32_loadavg av32;
99 
100 	netbsd32_from_loadavg(&av32, &averunnable);
101 
102 	node = *rnode;
103 	node.sysctl_data = &av32;
104 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
105 }
106 
107 void
108 netbsd32_sysctl_init(void)
109 {
110 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
111 	extern const char machine_arch32[];
112 	extern const char machine32[];
113 
114 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
115 		       CTLFLAG_PERMANENT,
116 		       CTLTYPE_NODE, "kern", NULL,
117 		       NULL, 0, NULL, 0,
118 		       CTL_KERN, CTL_EOL);
119 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
120 		       CTLFLAG_PERMANENT,
121 		       CTLTYPE_STRUCT, "boottime", NULL,
122 		       netbsd32_sysctl_kern_boottime, 0, NULL,
123 		       sizeof(struct netbsd32_timeval),
124 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
125 
126 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
127 		       CTLFLAG_PERMANENT,
128 		       CTLTYPE_NODE, "vm", NULL,
129 		       NULL, 0, NULL, 0,
130 		       CTL_VM, CTL_EOL);
131 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
132 		       CTLFLAG_PERMANENT,
133 		       CTLTYPE_STRUCT, "loadavg", NULL,
134 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
135 		       sizeof(struct netbsd32_loadavg),
136 		       CTL_VM, VM_LOADAVG, CTL_EOL);
137 
138 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
139 		       CTLFLAG_PERMANENT,
140 		       CTLTYPE_NODE, "hw", NULL,
141 		       NULL, 0, NULL, 0,
142 		       CTL_HW, CTL_EOL);
143 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
144 		       CTLFLAG_PERMANENT,
145 		       CTLTYPE_STRING, "machine", NULL,
146 		       NULL, 0, &machine32, 0,
147 		       CTL_HW, HW_MACHINE, CTL_EOL);
148 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
149 		       CTLFLAG_PERMANENT,
150 		       CTLTYPE_STRING, "machine_arch", NULL,
151 		       NULL, 0, &machine_arch32, 0,
152 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
153 }
154 
155 void
156 netbsd32_sysctl_fini(void)
157 {
158 
159 	sysctl_teardown(&netbsd32_clog);
160 	sysctl_free(&netbsd32_sysctl_root);
161 }
162 
163 int
164 netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
165 {
166 	/* {
167 		syscallarg(netbsd32_intp) name;
168 		syscallarg(u_int) namelen;
169 		syscallarg(netbsd32_voidp) old;
170 		syscallarg(netbsd32_size_tp) oldlenp;
171 		syscallarg(netbsd32_voidp) new;
172 		syscallarg(netbsd32_size_t) newlen;
173 	} */
174 	const struct sysctlnode *pnode;
175 	netbsd32_size_t netbsd32_oldlen;
176 	size_t oldlen, *oldlenp, savelen;
177 	int name[CTL_MAXNAME], error, nerror, *namep;
178 	void *newp, *oldp;
179 
180 	/*
181 	 * get and convert 32 bit size_t to native size_t
182 	 */
183 	namep = SCARG_P32(uap, name);
184 	oldp = SCARG_P32(uap, old);
185 	newp = SCARG_P32(uap, new);
186 	oldlenp = SCARG_P32(uap, oldlenp);
187 	oldlen = 0;
188 	if (oldlenp != NULL) {
189 		error = copyin(oldlenp, &netbsd32_oldlen,
190 			       sizeof(netbsd32_oldlen));
191 		if (error)
192 			return (error);
193 		oldlen = netbsd32_oldlen;
194 	}
195 	savelen = oldlen;
196 
197 	/*
198 	 * retrieve name and see if we need to dispatch this query to
199 	 * the shadow tree.  if we find it in the shadow tree,
200 	 * dispatch to there, otherwise NULL means use the built-in
201 	 * default main tree.
202 	 */
203 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
204 		return (EINVAL);
205 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
206         if (error)
207                 return (error);
208 
209 	ktrmib(name, SCARG(uap, namelen));
210 
211 	sysctl_lock(newp != NULL);
212 	pnode = &netbsd32_sysctl_root;
213 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
214 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
215 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
216 				oldp, &oldlen,
217 				newp, SCARG(uap, newlen),
218 				&name[0], l, pnode);
219 	sysctl_unlock();
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