Home | History | Annotate | Line # | Download | only in common
linux_sysctl.c revision 1.25.2.1
      1 /*	$NetBSD: linux_sysctl.c,v 1.25.2.1 2007/03/24 14:55:09 yamt 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.25.2.1 2007/03/24 14:55:09 yamt Exp $");
     45 
     46 #if defined (_KERNEL_OPT)
     47 #include "opt_ktrace.h"
     48 #endif
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/errno.h>
     53 #include <sys/proc.h>
     54 #include <sys/mount.h>
     55 #include <sys/sysctl.h>
     56 #include <sys/syscallargs.h>
     57 #ifdef KTRACE
     58 #include <sys/ktrace.h>
     59 #endif
     60 
     61 #include <compat/linux/common/linux_types.h>
     62 #include <compat/linux/common/linux_signal.h>
     63 
     64 #include <compat/linux/linux_syscallargs.h>
     65 #include <compat/linux/common/linux_sysctl.h>
     66 #include <compat/linux/common/linux_exec.h>
     67 
     68 char linux_sysname[128] = "Linux";
     69 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc__)
     70 char linux_release[128] = "2.4.18";
     71 char linux_version[128] = "#0 Wed Feb 20 20:00:02 CET 2002";
     72 #else
     73 char linux_release[128] = "2.0.38";
     74 char linux_version[128] = "#0 Sun Nov 11 11:11:11 MET 2000";
     75 #endif
     76 char linux_machine[128] = "";
     77 
     78 #ifndef _LKM
     79 static
     80 #endif
     81 struct sysctlnode linux_sysctl_root = {
     82 	.sysctl_flags = SYSCTL_VERSION|
     83 	    CTLFLAG_ROOT|CTLTYPE_NODE|CTLFLAG_READWRITE,
     84 	.sysctl_num = 0,
     85 	.sysctl_name = "(linux_root)",
     86 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
     87 };
     88 
     89 /*
     90  * setup for small sysctl tree used by emulation
     91  */
     92 SYSCTL_SETUP(linux_sysctl_setup, "linux emulated sysctl subtree setup")
     93 {
     94 	const struct sysctlnode *node = &linux_sysctl_root;
     95 
     96 	sysctl_createv(clog, 0, &node, &node,
     97 		       CTLFLAG_PERMANENT,
     98 		       CTLTYPE_NODE, "kern", NULL,
     99 		       NULL, 0, NULL, 0,
    100 		       LINUX_CTL_KERN, CTL_EOL);
    101 
    102 	sysctl_createv(clog, 0, &node, NULL,
    103 		       CTLFLAG_PERMANENT,
    104 		       CTLTYPE_STRING, "ostype", NULL,
    105 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
    106 		       LINUX_KERN_OSTYPE, CTL_EOL);
    107 	sysctl_createv(clog, 0, &node, NULL,
    108 		       CTLFLAG_PERMANENT,
    109 		       CTLTYPE_STRING, "osrelease", NULL,
    110 		       NULL, 0, linux_release, sizeof(linux_release),
    111 		       LINUX_KERN_OSRELEASE, CTL_EOL);
    112 	sysctl_createv(clog, 0, &node, NULL,
    113 		       CTLFLAG_PERMANENT,
    114 		       CTLTYPE_STRING, "version", NULL,
    115 		       NULL, 0, linux_version, sizeof(linux_version),
    116 		       LINUX_KERN_VERSION, CTL_EOL);
    117 
    118 #ifndef LINUX_UNAME_ARCH
    119 #define LINUX_UNAME_ARCH machine
    120 #endif
    121 	strlcpy(linux_machine, LINUX_UNAME_ARCH, sizeof(linux_machine));
    122 	sysctl_createv(clog, 0, &node, NULL,
    123 		       CTLFLAG_PERMANENT,
    124 		       CTLTYPE_STRING, "machine", NULL,
    125 		       NULL, 0, linux_machine, sizeof(linux_machine),
    126 		       LINUX_KERN_VERSION, CTL_EOL);
    127 
    128 	linux_sysctl_root.sysctl_flags &= ~CTLFLAG_READWRITE;
    129 }
    130 
    131 /*
    132  * linux sysctl system call
    133  */
    134 int
    135 linux_sys___sysctl(struct lwp *l, void *v, register_t *retval)
    136 {
    137 	struct linux_sys___sysctl_args *uap = v;
    138 	struct linux___sysctl ls;
    139 	int error, nerror, name[CTL_MAXNAME];
    140 	size_t savelen = 0, oldlen = 0;
    141 
    142 	/*
    143 	 * get linux args structure
    144 	 */
    145 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
    146 		return error;
    147 
    148 	/*
    149 	 * get oldlen
    150 	 */
    151 	oldlen = 0;
    152 	if (ls.oldlenp != NULL) {
    153 		error = copyin(ls.oldlenp, &oldlen, sizeof(oldlen));
    154 		if (error)
    155 			return (error);
    156 	}
    157 	savelen = oldlen;
    158 
    159 	/*
    160 	 * top-level sysctl names may or may not be non-terminal, but
    161 	 * we don't care
    162 	 */
    163 	if (ls.nlen > CTL_MAXNAME || ls.nlen < 1)
    164 		return (EINVAL);
    165 	error = copyin(ls.name, &name, ls.nlen * sizeof(int));
    166 	if (error)
    167 		return (error);
    168 
    169 #ifdef KTRACE
    170        if (KTRPOINT(l->l_proc, KTR_MIB))
    171                ktrmib(l, name, ls.nlen);
    172 #endif
    173 	/*
    174 	 * wire old so that copyout() is less likely to fail?
    175 	 */
    176 	error = sysctl_lock(l, ls.oldval, savelen);
    177 	if (error)
    178 		return (error);
    179 
    180 	/*
    181 	 * dispatch request into linux sysctl tree
    182 	 */
    183 	error = sysctl_dispatch(&name[0], ls.nlen,
    184 				ls.oldval, &oldlen,
    185 				ls.newval, ls.newlen,
    186 				&name[0], l, &linux_sysctl_root);
    187 
    188 	/*
    189 	 * release the sysctl lock
    190 	 */
    191 	sysctl_unlock(l);
    192 
    193 	/*
    194 	 * reset caller's oldlen, even if we got an error
    195 	 */
    196 	if (ls.oldlenp) {
    197 		nerror = copyout(&oldlen, ls.oldlenp, sizeof(oldlen));
    198 		if (error == 0)
    199 			error = nerror;
    200 	}
    201 
    202 	/*
    203 	 * if the only problem is that we weren't given enough space,
    204 	 * that's an ENOMEM error
    205 	 */
    206 	if (error == 0 && ls.oldval != NULL && savelen < oldlen)
    207 		error = ENOMEM;
    208 
    209 	return (error);
    210 }
    211 
    212 /*
    213  * kernel related system variables under emul.linux in the main sysctl
    214  * tree
    215  */
    216 SYSCTL_SETUP(sysctl_emul_linux_setup, "sysctl emul.linux subtree setup")
    217 {
    218 
    219 	sysctl_createv(clog, 0, NULL, NULL,
    220 		       CTLFLAG_PERMANENT,
    221 		       CTLTYPE_NODE, "emul", NULL,
    222 		       NULL, 0, NULL, 0,
    223 		       CTL_EMUL, CTL_EOL);
    224 	sysctl_createv(clog, 0, NULL, NULL,
    225 		       CTLFLAG_PERMANENT,
    226 		       CTLTYPE_NODE, "linux",
    227 		       SYSCTL_DESCR("Linux emulation settings"),
    228 		       NULL, 0, NULL, 0,
    229 		       CTL_EMUL, EMUL_LINUX, CTL_EOL);
    230 	sysctl_createv(clog, 0, NULL, NULL,
    231 		       CTLFLAG_PERMANENT,
    232 		       CTLTYPE_NODE, "kern",
    233 		       SYSCTL_DESCR("Linux kernel emulation settings"),
    234 		       NULL, 0, NULL, 0,
    235 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN, CTL_EOL);
    236 
    237 	sysctl_createv(clog, 0, NULL, NULL,
    238 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    239 		       CTLTYPE_STRING, "ostype",
    240 		       SYSCTL_DESCR("Linux operating system type"),
    241 		       NULL, 0, linux_sysname, sizeof(linux_sysname),
    242 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
    243 		       EMUL_LINUX_KERN_OSTYPE, CTL_EOL);
    244 	sysctl_createv(clog, 0, NULL, NULL,
    245 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    246 		       CTLTYPE_STRING, "osrelease",
    247 		       SYSCTL_DESCR("Linux operating system release"),
    248 		       NULL, 0, linux_release, sizeof(linux_release),
    249 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
    250 		       EMUL_LINUX_KERN_OSRELEASE, CTL_EOL);
    251 	sysctl_createv(clog, 0, NULL, NULL,
    252 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    253 		       CTLTYPE_STRING, "osversion",
    254 		       SYSCTL_DESCR("Linux operating system revision"),
    255 		       NULL, 0, linux_version, sizeof(linux_version),
    256 		       CTL_EMUL, EMUL_LINUX, EMUL_LINUX_KERN,
    257 		       EMUL_LINUX_KERN_VERSION, CTL_EOL);
    258 }
    259