linux32_sysctl.c revision 1.10 1 /* $NetBSD: linux32_sysctl.c,v 1.10 2008/11/12 12:36:10 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: linux32_sysctl.c,v 1.10 2008/11/12 12:36:10 ad Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/proc.h>
40 #include <sys/mount.h>
41 #include <sys/sysctl.h>
42 #include <sys/syscallargs.h>
43 #include <sys/ktrace.h>
44
45 #include <compat/netbsd32/netbsd32.h>
46
47 #include <compat/linux/common/linux_types.h>
48 #include <compat/linux/common/linux_signal.h>
49 #include <compat/linux/common/linux_sysctl.h>
50
51 #include <compat/linux/linux_syscallargs.h>
52
53 #include <compat/linux32/common/linux32_types.h>
54 #include <compat/linux32/common/linux32_signal.h>
55 #include <compat/linux32/common/linux32_sysctl.h>
56
57 #include <compat/linux32/linux32_syscallargs.h>
58
59 char linux32_sysname[128] = "Linux";
60 char linux32_release[128] = "2.4.18";
61 char linux32_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
62
63
64 SYSCTL_SETUP(sysctl_emul_linux32_setup, "sysctl emul.linux32 subtree setup")
65 {
66
67 sysctl_createv(clog, 0, NULL, NULL,
68 CTLFLAG_PERMANENT,
69 CTLTYPE_NODE, "emul", NULL,
70 NULL, 0, NULL, 0,
71 CTL_EMUL, CTL_EOL);
72 sysctl_createv(clog, 0, NULL, NULL,
73 CTLFLAG_PERMANENT,
74 CTLTYPE_NODE, "linux32",
75 SYSCTL_DESCR("Linux 32 bit emulation settings"),
76 NULL, 0, NULL, 0,
77 CTL_EMUL, EMUL_LINUX32, CTL_EOL);
78 sysctl_createv(clog, 0, NULL, NULL,
79 CTLFLAG_PERMANENT,
80 CTLTYPE_NODE, "kern",
81 SYSCTL_DESCR("Linux 32 bit kernel emulation settings"),
82 NULL, 0, NULL, 0,
83 CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN, CTL_EOL);
84
85 sysctl_createv(clog, 0, NULL, NULL,
86 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
87 CTLTYPE_STRING, "ostype",
88 SYSCTL_DESCR("Linux 32 bit operating system type"),
89 NULL, 0, linux32_sysname, sizeof(linux32_sysname),
90 CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
91 EMUL_LINUX32_KERN_OSTYPE, CTL_EOL);
92 sysctl_createv(clog, 0, NULL, NULL,
93 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
94 CTLTYPE_STRING, "osrelease",
95 SYSCTL_DESCR("Linux 32 bit operating system release"),
96 NULL, 0, linux32_release, sizeof(linux32_release),
97 CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
98 EMUL_LINUX32_KERN_OSRELEASE, CTL_EOL);
99 sysctl_createv(clog, 0, NULL, NULL,
100 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
101 CTLTYPE_STRING, "osversion",
102 SYSCTL_DESCR("Linux 32 bit operating system revision"),
103 NULL, 0, linux32_version, sizeof(linux32_version),
104 CTL_EMUL, EMUL_LINUX32, EMUL_LINUX32_KERN,
105 EMUL_LINUX32_KERN_VERSION, CTL_EOL);
106 }
107
108 struct sysctlnode linux32_sysctl_root = {
109 .sysctl_flags = SYSCTL_VERSION|
110 CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
111 .sysctl_num = 0,
112 .sysctl_name = "(linux32_root)",
113 sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
114 };
115
116 SYSCTL_SETUP(linux32_sysctl_setup, "linux32 emulated sysctl subtree setup")
117 {
118 const struct sysctlnode *node = &linux32_sysctl_root;
119
120 sysctl_createv(clog, 0, &node, &node,
121 CTLFLAG_PERMANENT,
122 CTLTYPE_NODE, "kern", NULL,
123 NULL, 0, NULL, 0,
124 LINUX_CTL_KERN, CTL_EOL);
125
126 sysctl_createv(clog, 0, &node, NULL,
127 CTLFLAG_PERMANENT,
128 CTLTYPE_STRING, "ostype", NULL,
129 NULL, 0, linux32_sysname, sizeof(linux32_sysname),
130 LINUX_KERN_OSTYPE, CTL_EOL);
131 sysctl_createv(clog, 0, &node, NULL,
132 CTLFLAG_PERMANENT,
133 CTLTYPE_STRING, "osrelease", NULL,
134 NULL, 0, linux32_release, sizeof(linux32_release),
135 LINUX_KERN_OSRELEASE, CTL_EOL);
136 sysctl_createv(clog, 0, &node, NULL,
137 CTLFLAG_PERMANENT,
138 CTLTYPE_STRING, "version", NULL,
139 NULL, 0, linux32_version, sizeof(linux32_version),
140 LINUX_KERN_VERSION, CTL_EOL);
141
142 linux32_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
143 }
144
145 int
146 linux32_sys___sysctl(struct lwp *l, const struct linux32_sys___sysctl_args *uap, register_t *retval)
147 {
148 /* {
149 syscallarg(linux32___sysctlp_t) lsp;
150 } */
151 struct linux32_sysctl ls32;
152 int name[CTL_MAXNAME];
153 size_t savelen;
154 netbsd32_size_t oldlen32;
155 size_t oldlen;
156 int error;
157
158 /*
159 * Read sysctl arguments
160 */
161 if ((error = copyin(SCARG_P32(uap, lsp), &ls32, sizeof(ls32))) != 0)
162 return error;
163
164 /*
165 * Read oldlen
166 */
167 if (NETBSD32PTR64(ls32.oldlenp) != NULL) {
168 if ((error = copyin(NETBSD32PTR64(ls32.oldlenp),
169 &oldlen32, sizeof(oldlen32))) != 0)
170 return error;
171 } else {
172 oldlen32 = 0;
173 }
174
175 savelen = (size_t)oldlen32;
176
177 /*
178 * Sanity check nlen
179 */
180 if ((ls32.nlen > CTL_MAXNAME) || (ls32.nlen < 1))
181 return EINVAL;
182
183 /*
184 * Read the sysctl name
185 */
186 if ((error = copyin(NETBSD32PTR64(ls32.name), &name,
187 ls32.nlen * sizeof(int))) != 0)
188 return error;
189
190 ktrmib(name, ls32.nlen);
191 /*
192 * First try linux32 tree, then linux tree
193 */
194 oldlen = (size_t)oldlen32;
195 sysctl_lock(NETBSD32PTR64(ls32.newval) != NULL);
196 error = sysctl_dispatch(name, ls32.nlen,
197 NETBSD32PTR64(ls32.oldval), &oldlen,
198 NETBSD32PTR64(ls32.newval), ls32.newlen,
199 name, l, &linux32_sysctl_root);
200 oldlen32 = (netbsd32_size_t)oldlen;
201 sysctl_unlock();
202
203 /*
204 * Check for oldlen overflow (not likely, but who knows...)
205 */
206 if (oldlen != oldlen32) {
207 #ifdef DEBUG_LINUX
208 printf("%s: oldlen32 = %d, oldlen = %ld\n",
209 __func__, oldlen32, oldlen);
210 #endif
211 return EINVAL;
212 }
213
214 /*
215 * set caller's oldlen, even if we got an error
216 */
217 if (NETBSD32PTR64(ls32.oldlenp)) {
218 int nerror;
219
220 nerror = copyout(&oldlen32,
221 NETBSD32PTR64(ls32.oldlenp), sizeof(oldlen32));
222
223 if (error == 0)
224 error = nerror;
225 }
226
227 /*
228 * oldlen was too short
229 */
230 if ((error == 0) &&
231 (NETBSD32PTR64(ls32.oldval) != NULL) &&
232 (savelen < oldlen32))
233 error = ENOMEM;
234
235 return error;
236 }
237