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