xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
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 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_compat.h"
33 #include "opt_kdtrace.h"
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/sdt.h>
42 #include <sys/sysctl.h>
43 #include <sys/systm.h>
44 #include <sys/sbuf.h>
45 
46 #ifdef COMPAT_LINUX32
47 #include <machine/../linux32/linux.h>
48 #include <machine/../linux32/linux32_proto.h>
49 #else
50 #include <machine/../linux/linux.h>
51 #include <machine/../linux/linux_proto.h>
52 #endif
53 
54 #include <compat/linux/linux_dtrace.h>
55 #include <compat/linux/linux_misc.h>
56 #include <compat/linux/linux_util.h>
57 
58 #define	LINUX_CTL_KERN		1
59 #define	LINUX_CTL_VM		2
60 #define	LINUX_CTL_NET		3
61 #define	LINUX_CTL_PROC		4
62 #define	LINUX_CTL_FS		5
63 #define	LINUX_CTL_DEBUG		6
64 #define	LINUX_CTL_DEV		7
65 #define	LINUX_CTL_BUS		8
66 
67 /* CTL_KERN names */
68 #define	LINUX_KERN_OSTYPE	1
69 #define	LINUX_KERN_OSRELEASE	2
70 #define	LINUX_KERN_OSREV	3
71 #define	LINUX_KERN_VERSION	4
72 
73 /* DTrace init */
74 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
75 
76 /**
77  * DTrace probes in this module.
78  */
79 LIN_SDT_PROBE_DEFINE2(sysctl, handle_string, entry, "struct l___sysctl_args *",
80     "char *");
81 LIN_SDT_PROBE_DEFINE1(sysctl, handle_string, copyout_error, "int");
82 LIN_SDT_PROBE_DEFINE1(sysctl, handle_string, return, "int");
83 LIN_SDT_PROBE_DEFINE2(sysctl, linux_sysctl, entry, "struct l___sysctl_args *",
84     "struct thread *");
85 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, copyin_error, "int");
86 LIN_SDT_PROBE_DEFINE2(sysctl, linux_sysctl, wrong_length, "int", "int");
87 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, unsupported_sysctl, "char *");
88 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, return, "int");
89 
90 static int
91 handle_string(struct l___sysctl_args *la, char *value)
92 {
93 	int error;
94 
95 	LIN_SDT_PROBE2(sysctl, handle_string, entry, la, value);
96 
97 	if (la->oldval != 0) {
98 		l_int len = strlen(value);
99 		error = copyout(value, PTRIN(la->oldval), len + 1);
100 		if (!error && la->oldlenp != 0)
101 			error = copyout(&len, PTRIN(la->oldlenp), sizeof(len));
102 		if (error) {
103 			LIN_SDT_PROBE1(sysctl, handle_string, copyout_error,
104 			    error);
105 			LIN_SDT_PROBE1(sysctl, handle_string, return, error);
106 			return (error);
107 		}
108 	}
109 
110 	if (la->newval != 0) {
111 		LIN_SDT_PROBE1(sysctl, handle_string, return, ENOTDIR);
112 		return (ENOTDIR);
113 	}
114 
115 	LIN_SDT_PROBE1(sysctl, handle_string, return, 0);
116 	return (0);
117 }
118 
119 int
120 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
121 {
122 	struct l___sysctl_args la;
123 	struct sbuf *sb;
124 	l_int *mib;
125 	char *sysctl_string;
126 	int error, i;
127 
128 	LIN_SDT_PROBE2(sysctl, linux_sysctl, entry, td, args->args);
129 
130 	error = copyin(args->args, &la, sizeof(la));
131 	if (error) {
132 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
133 		LIN_SDT_PROBE1(sysctl, linux_sysctl, return, error);
134 		return (error);
135 	}
136 
137 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME) {
138 		LIN_SDT_PROBE2(sysctl, linux_sysctl, wrong_length, la.nlen,
139 		    LINUX_CTL_MAXNAME);
140 		LIN_SDT_PROBE1(sysctl, linux_sysctl, return, ENOTDIR);
141 		return (ENOTDIR);
142 	}
143 
144 	mib = malloc(la.nlen * sizeof(l_int), M_TEMP, M_WAITOK);
145 	error = copyin(PTRIN(la.name), mib, la.nlen * sizeof(l_int));
146 	if (error) {
147 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
148 		LIN_SDT_PROBE1(sysctl, linux_sysctl, return, error);
149 		free(mib, M_TEMP);
150 		return (error);
151 	}
152 
153 	switch (mib[0]) {
154 	case LINUX_CTL_KERN:
155 		if (la.nlen < 2)
156 			break;
157 
158 		switch (mib[1]) {
159 		case LINUX_KERN_VERSION:
160 			error = handle_string(&la, version);
161 			free(mib, M_TEMP);
162 			LIN_SDT_PROBE1(sysctl, linux_sysctl, return, error);
163 			return (error);
164 		default:
165 			break;
166 		}
167 		break;
168 	default:
169 		break;
170 	}
171 
172 	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
173 	if (sb == NULL) {
174 		linux_msg(td, "sysctl is not implemented");
175 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
176 		    "unknown sysctl, ENOMEM during lookup");
177 	} else {
178 		sbuf_printf(sb, "sysctl ");
179 		for (i = 0; i < la.nlen; i++)
180 			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
181 		sbuf_printf(sb, "} is not implemented");
182 		sbuf_finish(sb);
183 		sysctl_string = sbuf_data(sb);
184 		linux_msg(td, "%s", sbuf_data(sb));
185 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
186 		    sysctl_string);
187 		sbuf_delete(sb);
188 	}
189 
190 	free(mib, M_TEMP);
191 
192 	LIN_SDT_PROBE1(sysctl, linux_sysctl, return, ENOTDIR);
193 	return (ENOTDIR);
194 }
195