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