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