xref: /netbsd/sys/compat/freebsd/freebsd_sysctl.c (revision 6550d01e)
1 /*	$NetBSD: freebsd_sysctl.c,v 1.15 2008/11/19 18:36:02 ad 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 2008/11/19 18:36:02 ad 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;
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 || *oldlenp < sizeof(int))
150 			return(EINVAL);
151 
152 		if ((locnew =
153 		     (char *) malloc(newlen + 1, M_TEMP, M_WAITOK)) == NULL)
154 			return(ENOMEM);
155 
156 		if ((error = copyinstr(new, locnew, newlen + 1, NULL))) {
157 			free(locnew, M_TEMP);
158 			return(error);
159 		}
160 
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);
168 
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);
176 
177 		error = copyout(&oidlen, SCARG(uap, oldlenp), sizeof(u_int));
178 
179 		return(error);
180 	}
181 
182 	return(EOPNOTSUPP);
183 }
184 
185 static int
186 freebsd_sysctl_name2oid(char *name, int *oid, int *oidlen)
187 {
188 	char *dot;
189 	int oi, ci;
190 	struct sysctlnode *node, *pnode;
191 
192 	pnode = &sysctl_root;
193 	node = sysctl_root.sysctl_child;
194 	oi = 0;
195 	if ((dot = strchr(name, (int)'.')) != NULL)
196 		*dot++ = '\0';
197 
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);
206 				}
207 				if ((dot = strchr(name, (int)'.')) != NULL)
208 					*dot++ = '\0';
209 
210 				if (SYSCTL_TYPE(node[ci].sysctl_flags) !=
211 				    CTLTYPE_NODE)
212 					return(ENOTDIR);
213 
214 				pnode = &node[ci];
215 				node = pnode->sysctl_child;
216 				goto next;
217 			}
218 		}
219 
220 		/* no more nodes, it must not exist */
221 		break;
222 	}
223 
224 	return(ENOENT);
225 }
226