Sync usage with man page.
[netbsd-mini2440.git] / sys / compat / freebsd / freebsd_sysctl.c
blob7296c29e606b44aca33954946c5e3292b693a54b
1 /* $NetBSD: freebsd_sysctl.c,v 1.14 2008/04/28 20:23:41 martin Exp $ */
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
30 * FreeBSD compatibility module. Try to deal with various FreeBSD sysctl calls.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: freebsd_sysctl.c,v 1.14 2008/04/28 20:23:41 martin Exp $");
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>
47 #include <sys/syscallargs.h>
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>
57 static int freebsd_sysctl_name2oid(char *, int *, int *);
59 static struct sysctllog *freebsd_clog;
61 void
62 freebsd_sysctl_init(void)
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);
78 void
79 freebsd_sysctl_fini(void)
82 sysctl_teardown(&freebsd_clog);
85 int
86 freebsd_sys_sysctl(struct lwp *l, const struct freebsd_sys_sysctl_args *uap, register_t *retval)
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;
99 u_int namelen;
100 void *new, *old;
102 namelen = SCARG(uap, namelen);
104 if ((namelen > CTL_MAXNAME) || (namelen < 1))
105 return EINVAL;
107 if ((error = copyin(SCARG(uap, name), name,
108 namelen * sizeof(*name))) != 0)
109 return error;
111 if (namelen > 0 && name[0] != 0)
112 return(sys___sysctl(l, (const void *)uap, retval));
114 ktrmib(name, namelen);
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.
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.
130 * Only implement {0,3} for now.
133 if (namelen < 2 || namelen > CTL_MAXNAME)
134 return(EINVAL);
136 if (name[1] == 3) {
137 char *locnew;
138 int oid[CTL_MAXNAME];
139 u_int oidlen = CTL_MAXNAME;
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);
147 old = SCARG(uap, old);
148 oldlenp = SCARG(uap, oldlenp);
149 if (old == NULL || oldlenp == NULL || *oldlenp < sizeof(int))
150 return(EINVAL);
152 if ((locnew =
153 (char *) malloc(newlen + 1, M_TEMP, M_WAITOK)) == NULL)
154 return(ENOMEM);
156 if ((error = copyinstr(new, locnew, newlen + 1, NULL))) {
157 free(locnew, M_TEMP);
158 return(error);
161 ktrmibio(-1, UIO_WRITE, new, newlen + 1, error);
162 sysctl_lock(new != NULL);
163 error = freebsd_sysctl_name2oid(locnew, oid, &oidlen);
164 sysctl_unlock();
165 free(locnew, M_TEMP);
166 if (error)
167 return(error);
169 oidlen *= sizeof(int);
170 error = copyout(oid, SCARG(uap, old),
171 MIN(oidlen, *SCARG(uap, oldlenp)));
172 if (error)
173 return(error);
174 ktrmibio(-1, UIO_READ, SCARG(uap, old),
175 MIN(oidlen, *SCARG(uap, oldlenp)), 0);
177 error = copyout(&oidlen, SCARG(uap, oldlenp), sizeof(u_int));
179 return(error);
182 return(EOPNOTSUPP);
185 static int
186 freebsd_sysctl_name2oid(char *name, int *oid, int *oidlen)
188 char *dot;
189 int oi, ci;
190 struct sysctlnode *node, *pnode;
192 pnode = &sysctl_root;
193 node = sysctl_root.sysctl_child;
194 oi = 0;
195 if ((dot = strchr(name, (int)'.')) != NULL)
196 *dot++ = '\0';
198 next:
199 while (*name != '\0' && node != NULL) {
200 for (ci = 0; ci < pnode->sysctl_clen; ci++) {
201 if (strcmp(name, node[ci].sysctl_name) == 0) {
202 oid[oi++] = node[ci].sysctl_num;
203 if ((name = dot) == NULL) {
204 *oidlen = oi;
205 return(0);
207 if ((dot = strchr(name, (int)'.')) != NULL)
208 *dot++ = '\0';
210 if (SYSCTL_TYPE(node[ci].sysctl_flags) !=
211 CTLTYPE_NODE)
212 return(ENOTDIR);
214 pnode = &node[ci];
215 node = pnode->sysctl_child;
216 goto next;
220 /* no more nodes, it must not exist */
221 break;
224 return(ENOENT);