xref: /netbsd-src/sys/compat/linux/common/linux_sysctl.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: linux_sysctl.c,v 1.11 2003/12/04 19:38:23 atatat 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.11 2003/12/04 19:38:23 atatat Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/sysctl.h>
52 #include <sys/sa.h>
53 #include <sys/syscallargs.h>
54 
55 #include <compat/linux/common/linux_types.h>
56 #include <compat/linux/common/linux_signal.h>
57 
58 #include <compat/linux/linux_syscallargs.h>
59 #include <compat/linux/common/linux_sysctl.h>
60 #include <compat/linux/common/linux_exec.h>
61 
62 char linux_sysname[128] = "Linux";
63 #if defined(__i386__) || defined(__powerpc__)
64 char linux_release[128] = "2.4.18";
65 char linux_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
66 #else
67 char linux_release[128] = "2.0.38";
68 char linux_version[128] = "#0 Sun Nov 11 11:11:11 MET 2000";
69 #endif
70 
71 static struct sysctlnode linux_sysctl_root = {
72 	.sysctl_flags = SYSCTL_ROOT|CTLTYPE_NODE,
73 	.sysctl_num = 0,
74 	.sysctl_size = sizeof(struct sysctlnode),
75 	.sysctl_name = "(linux_root)",
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 	struct sysctlnode *rnode, *node;
84 
85 	rnode = &linux_sysctl_root;
86 	sysctl_createv(SYSCTL_PERMANENT,
87 		       CTLTYPE_NODE, "kern", &rnode,
88 		       NULL, 0, NULL, 0,
89 		       LINUX_CTL_KERN, CTL_EOL);
90 
91 	node = rnode;
92 	sysctl_createv(SYSCTL_PERMANENT,
93 		       CTLTYPE_STRING, "ostype", &node,
94 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
95 		       LINUX_KERN_OSTYPE, CTL_EOL);
96 	node = rnode;
97 	sysctl_createv(SYSCTL_PERMANENT,
98 		       CTLTYPE_STRING, "osrelease", &node,
99 		       NULL, 0, linux_release, sizeof(linux_release),
100 		       LINUX_KERN_OSRELEASE, CTL_EOL);
101 	node = rnode;
102 	sysctl_createv(SYSCTL_PERMANENT,
103 		       CTLTYPE_STRING, "version", &node,
104 		       NULL, 0, linux_version, sizeof(linux_version),
105 		       LINUX_KERN_VERSION, CTL_EOL);
106 }
107 
108 /*
109  * linux sysctl system call
110  */
111 int
112 linux_sys___sysctl(struct lwp *l, void *v, register_t *retval)
113 {
114 	struct linux_sys___sysctl_args *uap = v;
115 	struct linux___sysctl ls;
116 	int error, nerror, name[CTL_MAXNAME];
117 	size_t savelen = 0, oldlen = 0;
118 
119 	/*
120 	 * get linux args structure
121 	 */
122 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
123 		return error;
124 
125 	/*
126 	 * get oldlen
127 	 */
128 	oldlen = 0;
129 	if (ls.oldlenp != NULL) {
130 		error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
131 		if (error)
132 			return (error);
133 	}
134 	savelen = oldlen;
135 
136 	/*
137 	 * top-level sysctl names may or may not be non-terminal, but
138 	 * we don't care
139 	 */
140 	if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
141 		return (EINVAL);
142 	error = copyin(ls.name, &name, ls.nlen * sizeof(int));
143 	if (error)
144 		return (error);
145 
146 	/*
147 	 * wire old so that copyout() is less likely to fail?
148 	 */
149 	error = sysctl_lock(l, ls.oldval, savelen);
150 	if (error)
151 		return (error);
152 
153 	/*
154 	 * dispatch request into linux sysctl tree
155 	 */
156 	error = sysctl_dispatch(&name[0], ls.nlen,
157 				ls.oldval, &oldlen,
158 				ls.newval, ls.newlen,
159 				&name[0], l, &linux_sysctl_root);
160 
161 	/*
162 	 * release the sysctl lock
163 	 */
164 	sysctl_unlock(l);
165 
166 	/*
167 	 * lock state should now be only "needlock" and "canwait"
168 	 */
169 	/* XXX KASSERT */
170 
171 	/*
172 	 * reset caller's oldlen, even if we got an error
173 	 */
174 	if (ls.oldlenp) {
175 		nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
176 		if (error == 0)
177 			error = nerror;
178 	}
179 
180 	/*
181 	 * if the only problem is that we weren't given enough space,
182 	 * that's an ENOMEM error
183 	 */
184 	if (error == 0 && ls.oldval != NULL && savelen < oldlen)
185 		error = ENOMEM;
186 
187 	return (error);
188 }
189 
190 /*
191  * kernel related system variables under emul.linux in the main sysctl
192  * tree
193  */
194 SYSCTL_SETUP(sysctl_emul_linux_setup, "sysctl emul.linux subtree setup")
195 {
196 
197 	sysctl_createv(SYSCTL_PERMANENT,
198 		       CTLTYPE_NODE, "emul", NULL,
199 		       NULL, 0, NULL, 0,
200 		       CTL_EMUL, CTL_EOL);
201 	sysctl_createv(SYSCTL_PERMANENT,
202 		       CTLTYPE_NODE, "linux", NULL,
203 		       NULL, 0, NULL, 0,
204 		       CTL_EMUL, EMUL_LINUX, CTL_EOL);
205 	sysctl_createv(SYSCTL_PERMANENT,
206 		       CTLTYPE_NODE, "kern", NULL,
207 		       NULL, 0, NULL, 0,
208 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
209 
210 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
211 		       CTLTYPE_STRING, "ostype", NULL,
212 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
213 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
214 		       EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
215 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
216 		       CTLTYPE_STRING, "osrelease", NULL,
217 		       NULL, 0, linux_release, sizeof(linux_release),
218 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
219 		       EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
220 	sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE,
221 		       CTLTYPE_STRING, "osversion", NULL,
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