xref: /netbsd-src/sys/compat/linux/common/linux_sysctl.c (revision ba65fde2d7fefa7d39838fa5fa855e62bd606b5e)
1 /*	$NetBSD: linux_sysctl.c,v 1.40 2010/07/07 01:30:35 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003, 2008 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.40 2010/07/07 01:30:35 chs 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 char linux_release[128] = "2.6.18";
61 char linux_version[128] = "#0 Wed Mar 3 03:03:03 PST 2010";
62 
63 struct sysctlnode linux_sysctl_root = {
64 	.sysctl_flags = SYSCTL_VERSION|
65 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
66 	.sysctl_num = 0,
67 	.sysctl_name = "(linux_root)",
68 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
69 };
70 
71 static struct sysctllog *linux_clog1;
72 static struct sysctllog *linux_clog2;
73 
74 void
75 linux_sysctl_fini(void)
76 {
77 
78 	sysctl_teardown(&linux_clog2);
79 	sysctl_teardown(&linux_clog1);
80 	sysctl_free(&linux_sysctl_root);
81 }
82 
83 void
84 linux_sysctl_init(void)
85 {
86 	const struct sysctlnode *node = &linux_sysctl_root;
87 
88 	sysctl_createv(&linux_clog1, 0, &node, &node,
89 		       CTLFLAG_PERMANENT,
90 		       CTLTYPE_NODE, "kern", NULL,
91 		       NULL, 0, NULL, 0,
92 		       LINUX_CTL_KERN, CTL_EOL);
93 	sysctl_createv(&linux_clog1, 0, &node, NULL,
94 		       CTLFLAG_PERMANENT,
95 		       CTLTYPE_STRING, "ostype", NULL,
96 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
97 		       LINUX_KERN_OSTYPE, CTL_EOL);
98 	sysctl_createv(&linux_clog1, 0, &node, NULL,
99 		       CTLFLAG_PERMANENT,
100 		       CTLTYPE_STRING, "osrelease", NULL,
101 		       NULL, 0, linux_release, sizeof(linux_release),
102 		       LINUX_KERN_OSRELEASE, CTL_EOL);
103 	sysctl_createv(&linux_clog1, 0, &node, NULL,
104 		       CTLFLAG_PERMANENT,
105 		       CTLTYPE_STRING, "version", NULL,
106 		       NULL, 0, linux_version, sizeof(linux_version),
107 		       LINUX_KERN_VERSION, CTL_EOL);
108 
109 	sysctl_createv(&linux_clog2, 0, NULL, NULL,
110 		       CTLFLAG_PERMANENT,
111 		       CTLTYPE_NODE, "emul", NULL,
112 		       NULL, 0, NULL, 0,
113 		       CTL_EMUL, CTL_EOL);
114 	sysctl_createv(&linux_clog2, 0, NULL, NULL,
115 		       CTLFLAG_PERMANENT,
116 		       CTLTYPE_NODE, "linux",
117 		       SYSCTL_DESCR("Linux emulation settings"),
118 		       NULL, 0, NULL, 0,
119 		       CTL_EMUL, EMUL_LINUX, CTL_EOL);
120 	sysctl_createv(&linux_clog2, 0, NULL, NULL,
121 		       CTLFLAG_PERMANENT,
122 		       CTLTYPE_NODE, "kern",
123 		       SYSCTL_DESCR("Linux kernel emulation settings"),
124 		       NULL, 0, NULL, 0,
125 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
126 	sysctl_createv(&linux_clog2, 0, NULL, NULL,
127 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
128 		       CTLTYPE_STRING, "ostype",
129 		       SYSCTL_DESCR("Linux operating system type"),
130 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
131 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
132 		       EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
133 	sysctl_createv(&linux_clog2, 0, NULL, NULL,
134 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
135 		       CTLTYPE_STRING, "osrelease",
136 		       SYSCTL_DESCR("Linux operating system release"),
137 		       NULL, 0, linux_release, sizeof(linux_release),
138 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
139 		       EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
140 	sysctl_createv(&linux_clog2, 0, NULL, NULL,
141 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
142 		       CTLTYPE_STRING, "osversion",
143 		       SYSCTL_DESCR("Linux operating system revision"),
144 		       NULL, 0, linux_version, sizeof(linux_version),
145 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
146 		       EMUL_LINUX_KERN_VERSION, CTL_EOL);
147 
148 	linux_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
149 }
150 
151 /*
152  * linux sysctl system call
153  */
154 int
155 linux_sys___sysctl(struct lwp *l, const struct linux_sys___sysctl_args *uap, register_t *retval)
156 {
157 	struct linux___sysctl ls;
158 	int error, nerror, name[CTL_MAXNAME];
159 	size_t savelen = 0, oldlen = 0;
160 
161 	/*
162 	 * get linux args structure
163 	 */
164 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
165 		return error;
166 
167 	/*
168 	 * get oldlen
169 	 */
170 	oldlen = 0;
171 	if (ls.oldlenp != NULL) {
172 		error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
173 		if (error)
174 			return (error);
175 	}
176 	savelen = oldlen;
177 
178 	/*
179 	 * top-level sysctl names may or may not be non-terminal, but
180 	 * we don't care
181 	 */
182 	if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
183 		return (ENOTDIR);
184 	error = copyin(ls.name, &name, ls.nlen * sizeof(int));
185 	if (error)
186 		return (error);
187 
188 	ktrmib(name, ls.nlen);
189 
190 	/*
191 	 * dispatch request into linux sysctl tree
192 	 */
193 	sysctl_lock(ls.newval != NULL);
194 	error = sysctl_dispatch(&name[0], ls.nlen,
195 				ls.oldval, &oldlen,
196 				ls.newval, ls.newlen,
197 				&name[0], l, &linux_sysctl_root);
198 	sysctl_unlock();
199 
200 	/*
201 	 * reset caller's oldlen, even if we got an error
202 	 */
203 	if (ls.oldlenp) {
204 		nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
205 		if (error == 0)
206 			error = nerror;
207 	}
208 
209 	/*
210 	 * if the only problem is that we weren't given enough space,
211 	 * that's an ENOMEM error
212 	 */
213 	if (error == 0 && ls.oldval != NULL && savelen < oldlen)
214 		error = ENOMEM;
215 
216 	return (error);
217 }
218