xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
1 /*-
2  * Copyright (c) 2001 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$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/lock.h>
33 #include <sys/malloc.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 #include <sys/sbuf.h>
39 
40 #include <machine/../linux/linux.h>
41 #include <machine/../linux/linux_proto.h>
42 
43 #include <compat/linux/linux_util.h>
44 
45 #define	LINUX_CTL_KERN		1
46 #define	LINUX_CTL_VM		2
47 #define	LINUX_CTL_NET		3
48 #define	LINUX_CTL_PROC		4
49 #define	LINUX_CTL_FS		5
50 #define	LINUX_CTL_DEBUG		6
51 #define	LINUX_CTL_DEV		7
52 #define	LINUX_CTL_BUS		8
53 
54 /* CTL_KERN names */
55 #define	LINUX_KERN_OSTYPE	1
56 #define	LINUX_KERN_OSRELEASE	2
57 #define	LINUX_KERN_OSREV	3
58 #define	LINUX_KERN_VERSION	4
59 
60 static int
61 handle_string(struct l___sysctl_args *la, char *value)
62 {
63 	int error;
64 
65 	if (la->oldval != NULL) {
66 		l_int len = strlen(value);
67 		error = copyout(value, la->oldval, len + 1);
68 		if (!error && la->oldlenp != NULL)
69 			error = copyout(&len, la->oldlenp, sizeof(len));
70 		if (error)
71 			return (error);
72 	}
73 
74 	if (la->newval != NULL)
75 		return (ENOTDIR);
76 
77 	return (0);
78 }
79 
80 int
81 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
82 {
83 	struct l___sysctl_args la;
84 	struct sbuf *sb;
85 	l_int *mib;
86 	int error, i;
87 
88 	error = copyin((caddr_t)args->args, &la, sizeof(la));
89 	if (error)
90 		return (error);
91 
92 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME)
93 		return (ENOTDIR);
94 
95 	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, 0);
96 	error = copyin(la.name, mib, la.nlen * sizeof(l_int));
97 	if (error) {
98 		free(mib, M_TEMP);
99 		return (error);
100 	}
101 
102 	switch (mib[0]) {
103 	case LINUX_CTL_KERN:
104 		if (la.nlen < 2)
105 			break;
106 
107 		switch (mib[1]) {
108 		case LINUX_KERN_VERSION:
109 			error = handle_string(&la, version);
110 			free(mib, M_TEMP);
111 			return (error);
112 		default:
113 			break;
114 		}
115 		break;
116 	default:
117 		break;
118 	}
119 
120 	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
121 	if (sb == NULL) {
122 		linux_msg(td, "sysctl is not implemented");
123 	} else {
124 		sbuf_printf(sb, "sysctl ");
125 		for (i = 0; i < la.nlen; i++)
126 			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
127 		sbuf_printf(sb, "} is not implemented");
128 		sbuf_finish(sb);
129 		linux_msg(td, "%s", sbuf_data(sb));
130 		sbuf_delete(sb);
131 	}
132 
133 	free(mib, M_TEMP);
134 	return (ENOTDIR);
135 }
136