2 * Copyright (c) 1999 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
44 #include <compat/linux/linux_mib.h>
45 #include <compat/linux/linux_misc.h>
48 char pr_osname[LINUX_MAX_UTSNAME];
49 char pr_osrelease[LINUX_MAX_UTSNAME];
54 static struct linux_prison lprison0 = {
56 .pr_osrelease = LINUX_VERSION_STR,
57 .pr_oss_version = 0x030600,
58 .pr_osrel = LINUX_VERSION_CODE
61 static unsigned linux_osd_jail_slot;
63 SYSCTL_NODE(_compat, OID_AUTO, linux, CTLFLAG_RW, 0, "Linux mode");
65 static int linux_set_osname(struct thread *td, char *osname);
66 static int linux_set_osrelease(struct thread *td, char *osrelease);
67 static int linux_set_oss_version(struct thread *td, int oss_version);
70 linux_sysctl_osname(SYSCTL_HANDLER_ARGS)
72 char osname[LINUX_MAX_UTSNAME];
75 linux_get_osname(req->td, osname);
76 error = sysctl_handle_string(oidp, osname, LINUX_MAX_UTSNAME, req);
77 if (error != 0 || req->newptr == NULL)
79 error = linux_set_osname(req->td, osname);
84 SYSCTL_PROC(_compat_linux, OID_AUTO, osname,
85 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
86 0, 0, linux_sysctl_osname, "A",
87 "Linux kernel OS name");
90 linux_sysctl_osrelease(SYSCTL_HANDLER_ARGS)
92 char osrelease[LINUX_MAX_UTSNAME];
95 linux_get_osrelease(req->td, osrelease);
96 error = sysctl_handle_string(oidp, osrelease, LINUX_MAX_UTSNAME, req);
97 if (error != 0 || req->newptr == NULL)
99 error = linux_set_osrelease(req->td, osrelease);
104 SYSCTL_PROC(_compat_linux, OID_AUTO, osrelease,
105 CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
106 0, 0, linux_sysctl_osrelease, "A",
107 "Linux kernel OS release");
110 linux_sysctl_oss_version(SYSCTL_HANDLER_ARGS)
115 oss_version = linux_get_oss_version(req->td);
116 error = sysctl_handle_int(oidp, &oss_version, 0, req);
117 if (error != 0 || req->newptr == NULL)
119 error = linux_set_oss_version(req->td, oss_version);
124 SYSCTL_PROC(_compat_linux, OID_AUTO, oss_version,
125 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON | CTLFLAG_MPSAFE,
126 0, 0, linux_sysctl_oss_version, "I",
127 "Linux OSS version");
130 * Map the osrelease into integer
133 linux_map_osrel(char *osrelease, int *osrel)
135 char *sep, *eosrelease;
136 int len, v0, v1, v2, v;
138 len = strlen(osrelease);
139 eosrelease = osrelease + len;
140 v0 = strtol(osrelease, &sep, 10);
141 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
144 v1 = strtol(osrelease, &sep, 10);
145 if (osrelease == sep || sep + 1 >= eosrelease || *sep != '.')
148 v2 = strtol(osrelease, &sep, 10);
149 if (osrelease == sep || sep != eosrelease)
152 v = v0 * 1000000 + v1 * 1000 + v2;
163 * Find a prison with Linux info.
164 * Return the Linux info and the (locked) prison.
166 static struct linux_prison *
167 linux_find_prison(struct prison *spr, struct prison **prp)
170 struct linux_prison *lpr;
172 for (pr = spr;; pr = pr->pr_parent) {
173 mtx_lock(&pr->pr_mtx);
174 lpr = (pr == &prison0)
176 : osd_jail_get(pr, linux_osd_jail_slot);
179 mtx_unlock(&pr->pr_mtx);
187 * Ensure a prison has its own Linux info. If lprp is non-null, point it to
188 * the Linux info and lock the prison.
191 linux_alloc_prison(struct prison *pr, struct linux_prison **lprp)
194 struct linux_prison *lpr, *nlpr;
197 /* If this prison already has Linux info, return that. */
198 lpr = linux_find_prison(pr, &ppr);
202 * Allocate a new info record. Then check again, in case something
203 * changed during the allocation.
205 mtx_unlock(&ppr->pr_mtx);
206 nlpr = malloc(sizeof(struct linux_prison), M_PRISON, M_WAITOK);
207 rsv = osd_reserve(linux_osd_jail_slot);
208 lpr = linux_find_prison(pr, &ppr);
210 free(nlpr, M_PRISON);
211 osd_free_reserved(rsv);
214 /* Inherit the initial values from the ancestor. */
215 mtx_lock(&pr->pr_mtx);
216 (void)osd_jail_set_reserved(pr, linux_osd_jail_slot, rsv, nlpr);
217 bcopy(lpr, nlpr, sizeof(*lpr));
219 mtx_unlock(&ppr->pr_mtx);
224 mtx_unlock(&pr->pr_mtx);
228 * Jail OSD methods for Linux prison data.
231 linux_prison_create(void *obj, void *data)
233 struct prison *pr = obj;
234 struct vfsoptlist *opts = data;
237 if (vfs_copyopt(opts, "linux", &jsys, sizeof(jsys)) == 0 &&
238 jsys == JAIL_SYS_INHERIT)
241 * Inherit a prison's initial values from its parent
242 * (different from JAIL_SYS_INHERIT which also inherits changes).
244 linux_alloc_prison(pr, NULL);
249 linux_prison_check(void *obj __unused, void *data)
251 struct vfsoptlist *opts = data;
252 char *osname, *osrelease;
253 int error, jsys, len, oss_version;
255 /* Check that the parameters are correct. */
256 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
257 if (error != ENOENT) {
260 if (jsys != JAIL_SYS_NEW && jsys != JAIL_SYS_INHERIT)
263 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
264 if (error != ENOENT) {
267 if (len == 0 || osname[len - 1] != '\0')
269 if (len > LINUX_MAX_UTSNAME) {
270 vfs_opterror(opts, "linux.osname too long");
271 return (ENAMETOOLONG);
274 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
275 if (error != ENOENT) {
278 if (len == 0 || osrelease[len - 1] != '\0')
280 if (len > LINUX_MAX_UTSNAME) {
281 vfs_opterror(opts, "linux.osrelease too long");
282 return (ENAMETOOLONG);
284 error = linux_map_osrel(osrelease, NULL);
286 vfs_opterror(opts, "linux.osrelease format error");
290 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
291 sizeof(oss_version));
299 linux_prison_set(void *obj, void *data)
301 struct linux_prison *lpr;
302 struct prison *pr = obj;
303 struct vfsoptlist *opts = data;
304 char *osname, *osrelease;
305 int error, gotversion, jsys, len, oss_version;
307 /* Set the parameters, which should be correct. */
308 error = vfs_copyopt(opts, "linux", &jsys, sizeof(jsys));
311 error = vfs_getopt(opts, "linux.osname", (void **)&osname, &len);
316 error = vfs_getopt(opts, "linux.osrelease", (void **)&osrelease, &len);
321 error = vfs_copyopt(opts, "linux.oss_version", &oss_version,
322 sizeof(oss_version));
330 case JAIL_SYS_INHERIT:
331 /* "linux=inherit": inherit the parent's Linux info. */
332 mtx_lock(&pr->pr_mtx);
333 osd_jail_del(pr, linux_osd_jail_slot);
334 mtx_unlock(&pr->pr_mtx);
338 * "linux=new" or "linux.*":
339 * the prison gets its own Linux info.
341 linux_alloc_prison(pr, &lpr);
343 (void)linux_map_osrel(osrelease, &lpr->pr_osrel);
344 strlcpy(lpr->pr_osrelease, osrelease,
348 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
350 lpr->pr_oss_version = oss_version;
351 mtx_unlock(&pr->pr_mtx);
357 SYSCTL_JAIL_PARAM_SYS_NODE(linux, CTLFLAG_RW, "Jail Linux parameters");
358 SYSCTL_JAIL_PARAM_STRING(_linux, osname, CTLFLAG_RW, LINUX_MAX_UTSNAME,
359 "Jail Linux kernel OS name");
360 SYSCTL_JAIL_PARAM_STRING(_linux, osrelease, CTLFLAG_RW, LINUX_MAX_UTSNAME,
361 "Jail Linux kernel OS release");
362 SYSCTL_JAIL_PARAM(_linux, oss_version, CTLTYPE_INT | CTLFLAG_RW,
363 "I", "Jail Linux OSS version");
366 linux_prison_get(void *obj, void *data)
368 struct linux_prison *lpr;
370 struct prison *pr = obj;
371 struct vfsoptlist *opts = data;
376 /* See if this prison is the one with the Linux info. */
377 lpr = linux_find_prison(pr, &ppr);
378 i = (ppr == pr) ? JAIL_SYS_NEW : JAIL_SYS_INHERIT;
379 error = vfs_setopt(opts, "linux", &i, sizeof(i));
380 if (error != 0 && error != ENOENT)
383 error = vfs_setopts(opts, "linux.osname", lpr->pr_osname);
384 if (error != 0 && error != ENOENT)
386 error = vfs_setopts(opts, "linux.osrelease", lpr->pr_osrelease);
387 if (error != 0 && error != ENOENT)
389 error = vfs_setopt(opts, "linux.oss_version",
390 &lpr->pr_oss_version, sizeof(lpr->pr_oss_version));
391 if (error != 0 && error != ENOENT)
395 * If this prison is inheriting its Linux info, report
396 * empty/zero parameters.
398 error = vfs_setopts(opts, "linux.osname", "");
399 if (error != 0 && error != ENOENT)
401 error = vfs_setopts(opts, "linux.osrelease", "");
402 if (error != 0 && error != ENOENT)
404 error = vfs_setopt(opts, "linux.oss_version", &version0,
405 sizeof(lpr->pr_oss_version));
406 if (error != 0 && error != ENOENT)
412 mtx_unlock(&ppr->pr_mtx);
418 linux_prison_destructor(void *data)
421 free(data, M_PRISON);
425 linux_osd_jail_register(void)
428 osd_method_t methods[PR_MAXMETHOD] = {
429 [PR_METHOD_CREATE] = linux_prison_create,
430 [PR_METHOD_GET] = linux_prison_get,
431 [PR_METHOD_SET] = linux_prison_set,
432 [PR_METHOD_CHECK] = linux_prison_check
435 linux_osd_jail_slot =
436 osd_jail_register(linux_prison_destructor, methods);
437 /* Copy the system linux info to any current prisons. */
438 sx_slock(&allprison_lock);
439 TAILQ_FOREACH(pr, &allprison, pr_list)
440 linux_alloc_prison(pr, NULL);
441 sx_sunlock(&allprison_lock);
445 linux_osd_jail_deregister(void)
448 osd_jail_deregister(linux_osd_jail_slot);
452 linux_get_osname(struct thread *td, char *dst)
455 struct linux_prison *lpr;
457 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
458 bcopy(lpr->pr_osname, dst, LINUX_MAX_UTSNAME);
459 mtx_unlock(&pr->pr_mtx);
463 linux_set_osname(struct thread *td, char *osname)
466 struct linux_prison *lpr;
468 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
469 strlcpy(lpr->pr_osname, osname, LINUX_MAX_UTSNAME);
470 mtx_unlock(&pr->pr_mtx);
476 linux_get_osrelease(struct thread *td, char *dst)
479 struct linux_prison *lpr;
481 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
482 bcopy(lpr->pr_osrelease, dst, LINUX_MAX_UTSNAME);
483 mtx_unlock(&pr->pr_mtx);
487 linux_kernver(struct thread *td)
490 struct linux_prison *lpr;
493 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
494 osrel = lpr->pr_osrel;
495 mtx_unlock(&pr->pr_mtx);
501 linux_set_osrelease(struct thread *td, char *osrelease)
504 struct linux_prison *lpr;
507 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
508 error = linux_map_osrel(osrelease, &lpr->pr_osrel);
510 strlcpy(lpr->pr_osrelease, osrelease, LINUX_MAX_UTSNAME);
511 mtx_unlock(&pr->pr_mtx);
517 linux_get_oss_version(struct thread *td)
520 struct linux_prison *lpr;
523 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
524 version = lpr->pr_oss_version;
525 mtx_unlock(&pr->pr_mtx);
531 linux_set_oss_version(struct thread *td, int oss_version)
534 struct linux_prison *lpr;
536 lpr = linux_find_prison(td->td_ucred->cr_prison, &pr);
537 lpr->pr_oss_version = oss_version;
538 mtx_unlock(&pr->pr_mtx);