freebsd_sysctl.c revision 1.15.36.1 1 /* $NetBSD: freebsd_sysctl.c,v 1.15.36.1 2014/10/19 19:40:55 snj Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * FreeBSD compatibility module. Try to deal with various FreeBSD sysctl calls.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: freebsd_sysctl.c,v 1.15.36.1 2014/10/19 19:40:55 snj Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/proc.h>
39 #include <sys/mount.h>
40 #include <sys/signal.h>
41 #include <sys/signalvar.h>
42 #include <sys/malloc.h>
43 #include <sys/mman.h>
44 #include <sys/sysctl.h>
45 #include <sys/ktrace.h>
46
47 #include <sys/syscallargs.h>
48
49 #include <compat/freebsd/freebsd_syscallargs.h>
50 #include <compat/common/compat_util.h>
51 #include <compat/freebsd/freebsd_rtprio.h>
52 #include <compat/freebsd/freebsd_timex.h>
53 #include <compat/freebsd/freebsd_signal.h>
54 #include <compat/freebsd/freebsd_mman.h>
55 #include <compat/freebsd/freebsd_sysctl.h>
56
57 static int freebsd_sysctl_name2oid(char *, int *, int *);
58
59 static struct sysctllog *freebsd_clog;
60
61 void
62 freebsd_sysctl_init(void)
63 {
64
65 sysctl_createv(&freebsd_clog, 0, NULL, NULL,
66 CTLFLAG_PERMANENT,
67 CTLTYPE_NODE, "kern", NULL,
68 NULL, 0, NULL, 0,
69 CTL_KERN, CTL_EOL);
70 sysctl_createv(&freebsd_clog, 0, NULL, NULL,
71 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
72 CTLTYPE_INT, "osreldate",
73 SYSCTL_DESCR("Operating system revision"),
74 NULL, __NetBSD_Version__, NULL, 0,
75 CTL_KERN, CTL_CREATE, CTL_EOL);
76 }
77
78 void
79 freebsd_sysctl_fini(void)
80 {
81
82 sysctl_teardown(&freebsd_clog);
83 }
84
85 int
86 freebsd_sys_sysctl(struct lwp *l, const struct freebsd_sys_sysctl_args *uap, register_t *retval)
87 {
88 /* {
89 syscallarg(int *) name;
90 syscallarg(u_int) namelen;
91 syscallarg(void *) old;
92 syscallarg(size_t *) oldlenp;
93 syscallarg(void *) new;
94 syscallarg(size_t) newlen;
95 } */
96 int error;
97 int name[CTL_MAXNAME];
98 size_t newlen, *oldlenp, oldlen;
99 u_int namelen;
100 void *new, *old;
101
102 namelen = SCARG(uap, namelen);
103
104 if ((namelen > CTL_MAXNAME) || (namelen < 1))
105 return EINVAL;
106
107 if ((error = copyin(SCARG(uap, name), name,
108 namelen * sizeof(*name))) != 0)
109 return error;
110
111 if (namelen > 0 && name[0] != 0)
112 return(sys___sysctl(l, (const void *)uap, retval));
113
114 ktrmib(name, namelen);
115
116 /*
117 * FreeBSD sysctl uses an undocumented set of special OIDs in it's
118 * sysctl MIB whose tree is rooted at oid 0. These OIDs are
119 * interpretted by their sysctl to implement functions that NetBSD
120 * performs in libc, such as sysctlgetmibinfo.
121 *
122 * From the FreeBSD kern_sysctl.c, these OIDs are:
123 * {0,0} printf the entire MIB-tree.
124 * {0,1,...} return the name of the "..." OID.
125 * {0,2,...} return the next OID.
126 * {0,3} return the OID of the name in "new"
127 * {0,4,...} return the kind & format info for the "..." OID.
128 * {0,5,...} return the description the "..." OID.
129 *
130 * Only implement {0,3} for now.
131 */
132
133 if (namelen < 2 || namelen > CTL_MAXNAME)
134 return(EINVAL);
135
136 if (name[1] == 3) {
137 char *locnew;
138 int oid[CTL_MAXNAME];
139 u_int oidlen = CTL_MAXNAME;
140
141 new = SCARG(uap, new);
142 newlen = SCARG(uap, newlen);
143 if (new == NULL || newlen < 1 ||
144 newlen > (SYSCTL_NAMELEN * CTL_MAXNAME))
145 return(EINVAL);
146
147 old = SCARG(uap, old);
148 oldlenp = SCARG(uap, oldlenp);
149 if (old == NULL || oldlenp == NULL)
150 return(EINVAL);
151
152 if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
153 return (error);
154 if (oldlen < sizeof(int))
155 return (EINVAL);
156
157 if ((locnew =
158 (char *) malloc(newlen + 1, M_TEMP, M_WAITOK)) == NULL)
159 return(ENOMEM);
160
161 if ((error = copyinstr(new, locnew, newlen + 1, NULL))) {
162 free(locnew, M_TEMP);
163 return(error);
164 }
165
166 ktrmibio(-1, UIO_WRITE, new, newlen + 1, error);
167 sysctl_lock(new != NULL);
168 error = freebsd_sysctl_name2oid(locnew, oid, &oidlen);
169 sysctl_unlock();
170 free(locnew, M_TEMP);
171 if (error)
172 return(error);
173
174 oidlen *= sizeof(int);
175 error = copyout(oid, SCARG(uap, old),
176 MIN(oidlen, oldlen));
177 if (error)
178 return(error);
179 ktrmibio(-1, UIO_READ, SCARG(uap, old),
180 MIN(oidlen, oldlen), 0);
181
182 error = copyout(&oidlen, SCARG(uap, oldlenp), sizeof(u_int));
183
184 return(error);
185 }
186
187 return(EOPNOTSUPP);
188 }
189
190 static int
191 freebsd_sysctl_name2oid(char *name, int *oid, int *oidlen)
192 {
193 char *dot;
194 int oi, ci;
195 struct sysctlnode *node, *pnode;
196
197 pnode = &sysctl_root;
198 node = sysctl_root.sysctl_child;
199 oi = 0;
200 if ((dot = strchr(name, (int)'.')) != NULL)
201 *dot++ = '\0';
202
203 next:
204 while (*name != '\0' && node != NULL) {
205 for (ci = 0; ci < pnode->sysctl_clen; ci++) {
206 if (strcmp(name, node[ci].sysctl_name) == 0) {
207 oid[oi++] = node[ci].sysctl_num;
208 if ((name = dot) == NULL) {
209 *oidlen = oi;
210 return(0);
211 }
212 if ((dot = strchr(name, (int)'.')) != NULL)
213 *dot++ = '\0';
214
215 if (SYSCTL_TYPE(node[ci].sysctl_flags) !=
216 CTLTYPE_NODE)
217 return(ENOTDIR);
218
219 pnode = &node[ci];
220 node = pnode->sysctl_child;
221 goto next;
222 }
223 }
224
225 /* no more nodes, it must not exist */
226 break;
227 }
228
229 return(ENOENT);
230 }
231