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