xref: /freebsd/sys/compat/linux/linux_sysctl.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001 Marcel Moolenaar
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/lock.h>
34 #include <sys/malloc.h>
35 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37 #include <sys/sbuf.h>
38 #include <sys/vnode.h>
39 
40 #ifdef COMPAT_LINUX32
41 #include <machine/../linux32/linux.h>
42 #include <machine/../linux32/linux32_proto.h>
43 #else
44 #include <machine/../linux/linux.h>
45 #include <machine/../linux/linux_proto.h>
46 #endif
47 
48 #include <compat/linux/linux_dtrace.h>
49 #include <compat/linux/linux_misc.h>
50 #include <compat/linux/linux_util.h>
51 
52 #define	LINUX_CTL_KERN		1
53 #define	LINUX_CTL_VM		2
54 #define	LINUX_CTL_NET		3
55 #define	LINUX_CTL_PROC		4
56 #define	LINUX_CTL_FS		5
57 #define	LINUX_CTL_DEBUG		6
58 #define	LINUX_CTL_DEV		7
59 #define	LINUX_CTL_BUS		8
60 
61 /* CTL_KERN names */
62 #define	LINUX_KERN_OSTYPE	1
63 #define	LINUX_KERN_OSRELEASE	2
64 #define	LINUX_KERN_OSREV	3
65 #define	LINUX_KERN_VERSION	4
66 
67 /* DTrace init */
68 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
69 
70 /**
71  * DTrace probes in this module.
72  */
73 LIN_SDT_PROBE_DEFINE1(sysctl, handle_string, copyout_error, "int");
74 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, copyin_error, "int");
75 LIN_SDT_PROBE_DEFINE2(sysctl, linux_sysctl, wrong_length, "int", "int");
76 LIN_SDT_PROBE_DEFINE1(sysctl, linux_sysctl, unsupported_sysctl, "char *");
77 
78 #ifdef LINUX_LEGACY_SYSCALLS
79 static int
80 handle_string(struct l___sysctl_args *la, char *value)
81 {
82 	int error;
83 
84 	if (la->oldval != 0) {
85 		l_int len = strlen(value);
86 		error = copyout(value, PTRIN(la->oldval), len + 1);
87 		if (!error && la->oldlenp != 0)
88 			error = copyout(&len, PTRIN(la->oldlenp), sizeof(len));
89 		if (error) {
90 			LIN_SDT_PROBE1(sysctl, handle_string, copyout_error,
91 			    error);
92 			return (error);
93 		}
94 	}
95 
96 	if (la->newval != 0)
97 		return (ENOTDIR);
98 
99 	return (0);
100 }
101 
102 int
103 linux_sysctl(struct thread *td, struct linux_sysctl_args *args)
104 {
105 	struct l___sysctl_args la;
106 	struct sbuf *sb;
107 	l_int *mib;
108 	char *sysctl_string;
109 	int error, i;
110 
111 	error = copyin(args->args, &la, sizeof(la));
112 	if (error) {
113 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
114 		return (error);
115 	}
116 
117 	if (la.nlen <= 0 || la.nlen > LINUX_CTL_MAXNAME) {
118 		LIN_SDT_PROBE2(sysctl, linux_sysctl, wrong_length, la.nlen,
119 		    LINUX_CTL_MAXNAME);
120 		return (ENOTDIR);
121 	}
122 
123 	mib = malloc(la.nlen * sizeof(l_int), M_LINUX, M_WAITOK);
124 	error = copyin(PTRIN(la.name), mib, la.nlen * sizeof(l_int));
125 	if (error) {
126 		LIN_SDT_PROBE1(sysctl, linux_sysctl, copyin_error, error);
127 		free(mib, M_LINUX);
128 		return (error);
129 	}
130 
131 	switch (mib[0]) {
132 	case LINUX_CTL_KERN:
133 		if (la.nlen < 2)
134 			break;
135 
136 		switch (mib[1]) {
137 		case LINUX_KERN_VERSION:
138 			error = handle_string(&la, version);
139 			free(mib, M_LINUX);
140 			return (error);
141 		default:
142 			break;
143 		}
144 		break;
145 	default:
146 		break;
147 	}
148 
149 	sb = sbuf_new(NULL, NULL, 20 + la.nlen * 5, SBUF_AUTOEXTEND);
150 	if (sb == NULL) {
151 		linux_msg(td, "sysctl is not implemented");
152 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
153 		    "unknown sysctl, ENOMEM during lookup");
154 	} else {
155 		sbuf_printf(sb, "sysctl ");
156 		for (i = 0; i < la.nlen; i++)
157 			sbuf_printf(sb, "%c%d", (i) ? ',' : '{', mib[i]);
158 		sbuf_printf(sb, "} is not implemented");
159 		sbuf_finish(sb);
160 		sysctl_string = sbuf_data(sb);
161 		linux_msg(td, "%s", sysctl_string);
162 		LIN_SDT_PROBE1(sysctl, linux_sysctl, unsupported_sysctl,
163 		    sysctl_string);
164 		sbuf_delete(sb);
165 	}
166 
167 	free(mib, M_LINUX);
168 
169 	return (ENOTDIR);
170 }
171 #endif
172