xref: /netbsd-src/sys/compat/linux/common/linux_sysctl.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: linux_sysctl.c,v 1.36 2008/06/18 12:24:18 tsutsui 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.36 2008/06/18 12:24:18 tsutsui 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/sched.h>
46 #include <sys/syscallargs.h>
47 #include <sys/ktrace.h>
48 
49 #include <compat/linux/common/linux_types.h>
50 #include <compat/linux/common/linux_signal.h>
51 #include <compat/linux/common/linux_ipc.h>
52 #include <compat/linux/common/linux_sem.h>
53 
54 #include <compat/linux/linux_syscallargs.h>
55 #include <compat/linux/common/linux_sysctl.h>
56 #include <compat/linux/common/linux_exec.h>
57 #include <compat/linux/common/linux_machdep.h>
58 
59 char linux_sysname[128] = "Linux";
60 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc__)
61 char linux_release[128] = "2.4.18";
62 char linux_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
63 #else
64 char linux_release[128] = "2.0.38";
65 char linux_version[128] = "#0 Sun Nov 11 11:11:11 MET 2000";
66 #endif
67 
68 #ifndef _LKM
69 static
70 #endif
71 struct sysctlnode linux_sysctl_root = {
72 	.sysctl_flags = SYSCTL_VERSION|
73 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
74 	.sysctl_num = 0,
75 	.sysctl_name = "(linux_root)",
76 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
77 };
78 
79 /*
80  * setup for small sysctl tree used by emulation
81  */
82 SYSCTL_SETUP(linux_sysctl_setup, "linux emulated sysctl subtree setup")
83 {
84 	const struct sysctlnode *node = &linux_sysctl_root;
85 
86 	sysctl_createv(clog, 0, &node, &node,
87 		       CTLFLAG_PERMANENT,
88 		       CTLTYPE_NODE, "kern", NULL,
89 		       NULL, 0, NULL, 0,
90 		       LINUX_CTL_KERN, CTL_EOL);
91 
92 	sysctl_createv(clog, 0, &node, NULL,
93 		       CTLFLAG_PERMANENT,
94 		       CTLTYPE_STRING, "ostype", NULL,
95 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
96 		       LINUX_KERN_OSTYPE, CTL_EOL);
97 	sysctl_createv(clog, 0, &node, NULL,
98 		       CTLFLAG_PERMANENT,
99 		       CTLTYPE_STRING, "osrelease", NULL,
100 		       NULL, 0, linux_release, sizeof(linux_release),
101 		       LINUX_KERN_OSRELEASE, CTL_EOL);
102 	sysctl_createv(clog, 0, &node, NULL,
103 		       CTLFLAG_PERMANENT,
104 		       CTLTYPE_STRING, "version", NULL,
105 		       NULL, 0, linux_version, sizeof(linux_version),
106 		       LINUX_KERN_VERSION, CTL_EOL);
107 
108 	linux_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
109 }
110 
111 /*
112  * linux sysctl system call
113  */
114 int
115 linux_sys___sysctl(struct lwp *l, const struct linux_sys___sysctl_args *uap, register_t *retval)
116 {
117 	struct linux___sysctl ls;
118 	int error, nerror, name[CTL_MAXNAME];
119 	size_t savelen = 0, oldlen = 0;
120 
121 	/*
122 	 * get linux args structure
123 	 */
124 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
125 		return error;
126 
127 	/*
128 	 * get oldlen
129 	 */
130 	oldlen = 0;
131 	if (ls.oldlenp != NULL) {
132 		error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
133 		if (error)
134 			return (error);
135 	}
136 	savelen = oldlen;
137 
138 	/*
139 	 * top-level sysctl names may or may not be non-terminal, but
140 	 * we don't care
141 	 */
142 	if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
143 		return (EINVAL);
144 	error = copyin(ls.name, &name, ls.nlen * sizeof(int));
145 	if (error)
146 		return (error);
147 
148 	ktrmib(name, ls.nlen);
149 
150 	/*
151 	 * dispatch request into linux sysctl tree
152 	 */
153 	sysctl_lock(ls.newval != NULL);
154 	error = sysctl_dispatch(&name[0], ls.nlen,
155 				ls.oldval, &oldlen,
156 				ls.newval, ls.newlen,
157 				&name[0], l, &linux_sysctl_root);
158 	sysctl_unlock();
159 
160 	/*
161 	 * reset caller's oldlen, even if we got an error
162 	 */
163 	if (ls.oldlenp) {
164 		nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
165 		if (error == 0)
166 			error = nerror;
167 	}
168 
169 	/*
170 	 * if the only problem is that we weren't given enough space,
171 	 * that's an ENOMEM error
172 	 */
173 	if (error == 0 && ls.oldval != NULL && savelen < oldlen)
174 		error = ENOMEM;
175 
176 	return (error);
177 }
178 
179 /*
180  * kernel related system variables under emul.linux in the main sysctl
181  * tree
182  */
183 SYSCTL_SETUP(sysctl_emul_linux_setup, "sysctl emul.linux subtree setup")
184 {
185 
186 	sysctl_createv(clog, 0, NULL, NULL,
187 		       CTLFLAG_PERMANENT,
188 		       CTLTYPE_NODE, "emul", NULL,
189 		       NULL, 0, NULL, 0,
190 		       CTL_EMUL, CTL_EOL);
191 	sysctl_createv(clog, 0, NULL, NULL,
192 		       CTLFLAG_PERMANENT,
193 		       CTLTYPE_NODE, "linux",
194 		       SYSCTL_DESCR("Linux emulation settings"),
195 		       NULL, 0, NULL, 0,
196 		       CTL_EMUL, EMUL_LINUX, CTL_EOL);
197 	sysctl_createv(clog, 0, NULL, NULL,
198 		       CTLFLAG_PERMANENT,
199 		       CTLTYPE_NODE, "kern",
200 		       SYSCTL_DESCR("Linux kernel emulation settings"),
201 		       NULL, 0, NULL, 0,
202 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
203 
204 	sysctl_createv(clog, 0, NULL, NULL,
205 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
206 		       CTLTYPE_STRING, "ostype",
207 		       SYSCTL_DESCR("Linux operating system type"),
208 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
209 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
210 		       EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
211 	sysctl_createv(clog, 0, NULL, NULL,
212 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
213 		       CTLTYPE_STRING, "osrelease",
214 		       SYSCTL_DESCR("Linux operating system release"),
215 		       NULL, 0, linux_release, sizeof(linux_release),
216 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
217 		       EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
218 	sysctl_createv(clog, 0, NULL, NULL,
219 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
220 		       CTLTYPE_STRING, "osversion",
221 		       SYSCTL_DESCR("Linux operating system revision"),
222 		       NULL, 0, linux_version, sizeof(linux_version),
223 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
224 		       EMUL_LINUX_KERN_VERSION, CTL_EOL);
225 }
226