proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / emulation / linux / linux_mib.c
1 /*-
2  * Copyright (c) 1999 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer 
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/compat/linux/linux_mib.c,v 1.7.2.2 2001/11/05 19:08:22 marcel Exp $
29  * $DragonFly: src/sys/emulation/linux/linux_mib.c,v 1.4 2003/06/25 03:55:44 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/sysctl.h>
36 #include <sys/proc.h>
37 #include <sys/malloc.h>
38 #include <sys/jail.h>
39
40 #include <machine/../linux/linux.h>
41 #include <compat/linux/linux_mib.h>
42
43 struct linux_prison {
44         char    pr_osname[LINUX_MAX_UTSNAME];
45         char    pr_osrelease[LINUX_MAX_UTSNAME];
46         int     pr_oss_version;
47 };
48
49 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0,
50             "Linux mode");
51
52 static char     linux_osname[LINUX_MAX_UTSNAME] = "Linux";
53
54 static int
55 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
56 {
57         char osname[LINUX_MAX_UTSNAME];
58         int error;
59
60         strcpy(osname, linux_get_osname(req->td));
61         error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
62         if (error || req->newptr == NULL)
63                 return (error);
64         error = linux_set_osname(req->td, osname);
65         return (error);
66 }
67
68 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
69             CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
70             0, 0, linux_sysctl_osname, "A",
71             "Linux kernel OS name");
72
73 static char     linux_osrelease[LINUX_MAX_UTSNAME] = "2.4.2";
74
75 static int
76 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
77 {
78         char osrelease[LINUX_MAX_UTSNAME];
79         int error;
80
81         strcpy(osrelease, linux_get_osrelease(req->td));
82         error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
83         if (error || req->newptr == NULL)
84                 return (error);
85         error = linux_set_osrelease(req->td, osrelease);
86         return (error);
87 }
88
89 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
90             CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON,
91             0, 0, linux_sysctl_osrelease, "A",
92             "Linux kernel OS release");
93
94 static int      linux_oss_version = 0x030600;
95
96 static int
97 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
98 {
99         int oss_version;
100         int error;
101
102         oss_version = linux_get_oss_version(req->td);
103         error = sysctl_handle_int(oidp, &oss_version, 0, req);
104         if (error || req->newptr == NULL)
105                 return (error);
106         error = linux_set_oss_version(req->td, oss_version);
107         return (error);
108 }
109
110 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
111             CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
112             0, 0, linux_sysctl_oss_version, "I",
113             "Linux OSS version");
114
115 static struct linux_prison *
116 get_prison(struct thread *td)
117 {
118         struct prison *pr;
119         struct linux_prison *lpr;
120
121         KKASSERT(td->td_proc);
122         pr = td->td_proc->p_ucred->cr_prison;
123         if (pr == NULL)
124                 return (NULL);
125
126         if (pr->pr_linux == NULL) {
127                 MALLOC(lpr, struct linux_prison *, sizeof *lpr,
128                     M_PRISON, M_WAITOK|M_ZERO);
129                 pr->pr_linux = lpr;
130         }
131
132         return (pr->pr_linux);
133 }
134
135 char *
136 linux_get_osname(struct thread *td)
137 {
138         register struct prison *pr;
139         register struct linux_prison *lpr;
140
141         KKASSERT(td->td_proc);
142         pr = td->td_proc->p_ucred->cr_prison;
143         if (pr != NULL && pr->pr_linux != NULL) {
144                 lpr = pr->pr_linux;
145                 if (lpr->pr_osname[0])
146                         return (lpr->pr_osname);
147         }
148
149         return (linux_osname);
150 }
151
152 int
153 linux_set_osname(struct thread *td, char *osname)
154 {
155         register struct linux_prison *lpr;
156
157         KKASSERT(td->td_proc);
158         lpr = get_prison(td);
159         if (lpr != NULL)
160                 strcpy(lpr->pr_osname, osname);
161         else
162                 strcpy(linux_osname, osname);
163
164         return (0);
165 }
166
167 char *
168 linux_get_osrelease(struct thread *td)
169 {
170         register struct prison *pr;
171         register struct linux_prison *lpr;
172
173         KKASSERT(td->td_proc);
174         pr = td->td_proc->p_ucred->cr_prison;
175         if (pr != NULL && pr->pr_linux != NULL) {
176                 lpr = pr->pr_linux;
177                 if (lpr->pr_osrelease[0])
178                         return (lpr->pr_osrelease);
179         }
180
181         return (linux_osrelease);
182 }
183
184 int
185 linux_set_osrelease(struct thread *td, char *osrelease)
186 {
187         register struct linux_prison *lpr;
188
189         lpr = get_prison(td);
190         if (lpr != NULL)
191                 strcpy(lpr->pr_osrelease, osrelease);
192         else
193                 strcpy(linux_osrelease, osrelease);
194
195         return (0);
196 }
197
198 int
199 linux_get_oss_version(struct thread *td)
200 {
201         register struct prison *pr;
202         register struct linux_prison *lpr;
203
204         KKASSERT(td->td_proc);
205         pr = td->td_proc->p_ucred->cr_prison;
206         if (pr != NULL && pr->pr_linux != NULL) {
207                 lpr = pr->pr_linux;
208                 if (lpr->pr_oss_version)
209                         return (lpr->pr_oss_version);
210         }
211
212         return (linux_oss_version);
213 }
214
215 int
216 linux_set_oss_version(struct thread *td, int oss_version)
217 {
218         register struct linux_prison *lpr;
219
220         lpr = get_prison(td);
221         if (lpr != NULL)
222                 lpr->pr_oss_version = oss_version;
223         else
224                 linux_oss_version = oss_version;
225
226         return (0);
227 }
228
229 #ifdef DEBUG
230
231 u_char linux_debug_map[howmany(LINUX_SYS_MAXSYSCALL, sizeof(u_char))];
232
233 static int
234 linux_debug(int syscall, int toggle, int global)
235 {
236
237         if (global) {
238                 char c = toggle ? 0 : 0xff;
239
240                 memset(linux_debug_map, c, sizeof(linux_debug_map));
241                 return (0);
242         }
243         if (syscall < 0 || syscall >= LINUX_SYS_MAXSYSCALL)
244                 return (EINVAL);
245         if (toggle)
246                 clrbit(linux_debug_map, syscall);
247         else
248                 setbit(linux_debug_map, syscall);
249         return (0);
250 }
251
252 /*
253  * Usage: sysctl -w linux.debug=<syscall_nr>.<0/1>
254  *
255  *    E.g.: sysctl -w linux.debug=21.0
256  *
257  * As a special case, syscall "all" will apply to all syscalls globally.
258  */
259 #define LINUX_MAX_DEBUGSTR      16
260 static int
261 linux_sysctl_debug(SYSCTL_HANDLER_ARGS)
262 {
263         char value[LINUX_MAX_DEBUGSTR], *p;
264         int error, sysc, toggle;
265         int global = 0;
266
267         value[0] = '\0';
268         error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req);
269         if (error || req->newptr == NULL)
270                 return (error);
271         for (p = value; *p != '\0' && *p != '.'; p++);
272         if (*p == '\0')
273                 return (EINVAL);
274         *p++ = '\0';
275         sysc = strtol(value, NULL, 0);
276         toggle = strtol(p, NULL, 0);
277         if (strcmp(value, "all") == 0)
278                 global = 1;
279         error = linux_debug(sysc, toggle, global);
280         return (error);
281 }
282
283 SYSCTL_PROC(_compat_linux, OID_AUTO, debug,
284             CTLTYPE_STRING | CTLFLAG_RW,
285             0, 0, linux_sysctl_debug, "A",
286             "Linux debugging control");
287
288 #endif /* DEBUG */