linux_sysctl.c revision 1.25.2.2 1 /* $NetBSD: linux_sysctl.c,v 1.25.2.2 2007/05/07 10:55:13 yamt 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * sysctl system call.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: linux_sysctl.c,v 1.25.2.2 2007/05/07 10:55:13 yamt Exp $");
45
46 #if defined (_KERNEL_OPT)
47 #include "opt_ktrace.h"
48 #endif
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/errno.h>
53 #include <sys/proc.h>
54 #include <sys/mount.h>
55 #include <sys/sysctl.h>
56 #include <sys/syscallargs.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60
61 #include <compat/linux/common/linux_types.h>
62 #include <compat/linux/common/linux_signal.h>
63
64 #include <compat/linux/linux_syscallargs.h>
65 #include <compat/linux/common/linux_sysctl.h>
66 #include <compat/linux/common/linux_exec.h>
67 #include <compat/linux/common/linux_machdep.h>
68
69 char linux_sysname[128] = "Linux";
70 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc__)
71 char linux_release[128] = "2.4.18";
72 char linux_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
73 #else
74 char linux_release[128] = "2.0.38";
75 char linux_version[128] = "#0 Sun Nov 11 11:11:11 MET 2000";
76 #endif
77 char linux_machine[128] = "";
78
79 #ifndef _LKM
80 static
81 #endif
82 struct sysctlnode linux_sysctl_root = {
83 .sysctl_flags = SYSCTL_VERSION|
84 CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
85 .sysctl_num = 0,
86 .sysctl_name = "(linux_root)",
87 sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
88 };
89
90 /*
91 * setup for small sysctl tree used by emulation
92 */
93 SYSCTL_SETUP(linux_sysctl_setup, "linux emulated sysctl subtree setup")
94 {
95 const struct sysctlnode *node = &linux_sysctl_root;
96
97 sysctl_createv(clog, 0, &node, &node,
98 CTLFLAG_PERMANENT,
99 CTLTYPE_NODE, "kern", NULL,
100 NULL, 0, NULL, 0,
101 LINUX_CTL_KERN, CTL_EOL);
102
103 sysctl_createv(clog, 0, &node, NULL,
104 CTLFLAG_PERMANENT,
105 CTLTYPE_STRING, "ostype", NULL,
106 NULL, 0, linux_sysname, sizeof(linux_sysname),
107 LINUX_KERN_OSTYPE, CTL_EOL);
108 sysctl_createv(clog, 0, &node, NULL,
109 CTLFLAG_PERMANENT,
110 CTLTYPE_STRING, "osrelease", NULL,
111 NULL, 0, linux_release, sizeof(linux_release),
112 LINUX_KERN_OSRELEASE, CTL_EOL);
113 sysctl_createv(clog, 0, &node, NULL,
114 CTLFLAG_PERMANENT,
115 CTLTYPE_STRING, "version", NULL,
116 NULL, 0, linux_version, sizeof(linux_version),
117 LINUX_KERN_VERSION, CTL_EOL);
118
119 #if 0
120 #ifndef LINUX_UNAME_ARCH
121 #define LINUX_UNAME_ARCH machine
122 #endif
123 strlcpy(linux_machine, LINUX_UNAME_ARCH, sizeof(linux_machine));
124 sysctl_createv(clog, 0, &node, NULL,
125 CTLFLAG_PERMANENT,
126 CTLTYPE_STRING, "machine", NULL,
127 NULL, 0, linux_machine, sizeof(linux_machine),
128 LINUX_KERN_VERSION, CTL_EOL);
129 #endif
130
131 linux_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
132 }
133
134 /*
135 * linux sysctl system call
136 */
137 int
138 linux_sys___sysctl(struct lwp *l, void *v, register_t *retval)
139 {
140 struct linux_sys___sysctl_args *uap = v;
141 struct linux___sysctl ls;
142 int error, nerror, name[CTL_MAXNAME];
143 size_t savelen = 0, oldlen = 0;
144
145 /*
146 * get linux args structure
147 */
148 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
149 return error;
150
151 /*
152 * get oldlen
153 */
154 oldlen = 0;
155 if (ls.oldlenp != NULL) {
156 error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
157 if (error)
158 return (error);
159 }
160 savelen = oldlen;
161
162 /*
163 * top-level sysctl names may or may not be non-terminal, but
164 * we don't care
165 */
166 if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
167 return (EINVAL);
168 error = copyin(ls.name, &name, ls.nlen * sizeof(int));
169 if (error)
170 return (error);
171
172 #ifdef KTRACE
173 if (KTRPOINT(l->l_proc, KTR_MIB))
174 ktrmib(l, name, ls.nlen);
175 #endif
176 /*
177 * wire old so that copyout() is less likely to fail?
178 */
179 error = sysctl_lock(l, ls.oldval, savelen);
180 if (error)
181 return (error);
182
183 /*
184 * dispatch request into linux sysctl tree
185 */
186 error = sysctl_dispatch(&name[0], ls.nlen,
187 ls.oldval, &oldlen,
188 ls.newval, ls.newlen,
189 &name[0], l, &linux_sysctl_root);
190
191 /*
192 * release the sysctl lock
193 */
194 sysctl_unlock(l);
195
196 /*
197 * reset caller's oldlen, even if we got an error
198 */
199 if (ls.oldlenp) {
200 nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
201 if (error == 0)
202 error = nerror;
203 }
204
205 /*
206 * if the only problem is that we weren't given enough space,
207 * that's an ENOMEM error
208 */
209 if (error == 0 && ls.oldval != NULL && savelen < oldlen)
210 error = ENOMEM;
211
212 return (error);
213 }
214
215 /*
216 * kernel related system variables under emul.linux in the main sysctl
217 * tree
218 */
219 SYSCTL_SETUP(sysctl_emul_linux_setup, "sysctl emul.linux subtree setup")
220 {
221
222 sysctl_createv(clog, 0, NULL, NULL,
223 CTLFLAG_PERMANENT,
224 CTLTYPE_NODE, "emul", NULL,
225 NULL, 0, NULL, 0,
226 CTL_EMUL, CTL_EOL);
227 sysctl_createv(clog, 0, NULL, NULL,
228 CTLFLAG_PERMANENT,
229 CTLTYPE_NODE, "linux",
230 SYSCTL_DESCR("Linux emulation settings"),
231 NULL, 0, NULL, 0,
232 CTL_EMUL, EMUL_LINUX, CTL_EOL);
233 sysctl_createv(clog, 0, NULL, NULL,
234 CTLFLAG_PERMANENT,
235 CTLTYPE_NODE, "kern",
236 SYSCTL_DESCR("Linux kernel emulation settings"),
237 NULL, 0, NULL, 0,
238 CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
239
240 sysctl_createv(clog, 0, NULL, NULL,
241 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
242 CTLTYPE_STRING, "ostype",
243 SYSCTL_DESCR("Linux operating system type"),
244 NULL, 0, linux_sysname, sizeof(linux_sysname),
245 CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
246 EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
247 sysctl_createv(clog, 0, NULL, NULL,
248 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
249 CTLTYPE_STRING, "osrelease",
250 SYSCTL_DESCR("Linux operating system release"),
251 NULL, 0, linux_release, sizeof(linux_release),
252 CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
253 EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
254 sysctl_createv(clog, 0, NULL, NULL,
255 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
256 CTLTYPE_STRING, "osversion",
257 SYSCTL_DESCR("Linux operating system revision"),
258 NULL, 0, linux_version, sizeof(linux_version),
259 CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
260 EMUL_LINUX_KERN_VERSION, CTL_EOL);
261 }
262