xref: /netbsd-src/sys/compat/freebsd/freebsd_sysctl.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: freebsd_sysctl.c,v 1.16 2014/02/25 18:30:09 pooka 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.16 2014/02/25 18:30:09 pooka 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|CTLFLAG_IMMEDIATE,
67 			CTLTYPE_INT, "osreldate",
68 			SYSCTL_DESCR("Operating system revision"),
69 			NULL, __NetBSD_Version__, NULL, 0,
70 			CTL_KERN, CTL_CREATE, CTL_EOL);
71 }
72 
73 void
74 freebsd_sysctl_fini(void)
75 {
76 
77 	sysctl_teardown(&freebsd_clog);
78 }
79 
80 int
81 freebsd_sys_sysctl(struct lwp *l, const struct freebsd_sys_sysctl_args *uap, register_t *retval)
82 {
83 	/* {
84 		syscallarg(int *) name;
85 		syscallarg(u_int) namelen;
86 		syscallarg(void *) old;
87 		syscallarg(size_t *) oldlenp;
88 		syscallarg(void *) new;
89 		syscallarg(size_t) newlen;
90 	} */
91 	int error;
92 	int name[CTL_MAXNAME];
93 	size_t newlen, *oldlenp;
94 	u_int namelen;
95 	void *new, *old;
96 
97 	namelen = SCARG(uap, namelen);
98 
99 	if ((namelen > CTL_MAXNAME) || (namelen < 1))
100 		return EINVAL;
101 
102 	if ((error = copyin(SCARG(uap, name), name,
103 	    namelen * sizeof(*name))) != 0)
104 		return error;
105 
106 	if (namelen > 0 && name[0] != 0)
107 		return(sys___sysctl(l, (const void *)uap, retval));
108 
109 	ktrmib(name, namelen);
110 
111 	/*
112 	 * FreeBSD sysctl uses an undocumented set of special OIDs in it's
113 	 * sysctl MIB whose tree is rooted at oid 0.  These OIDs are
114 	 * interpretted by their sysctl to implement functions that NetBSD
115 	 * performs in libc, such as sysctlgetmibinfo.
116 	 *
117 	 * From the FreeBSD kern_sysctl.c, these OIDs are:
118 	 * {0,0}        printf the entire MIB-tree.
119 	 * {0,1,...}    return the name of the "..." OID.
120 	 * {0,2,...}    return the next OID.
121 	 * {0,3}        return the OID of the name in "new"
122 	 * {0,4,...}    return the kind & format info for the "..." OID.
123 	 * {0,5,...}    return the description the "..." OID.
124 	 *
125 	 * Only implement {0,3} for now.
126 	 */
127 
128 	if (namelen < 2 || namelen > CTL_MAXNAME)
129 		return(EINVAL);
130 
131 	if (name[1] == 3) {
132 		char *locnew;
133 		int oid[CTL_MAXNAME];
134 		u_int oidlen = CTL_MAXNAME;
135 
136 		new = SCARG(uap, new);
137 		newlen = SCARG(uap, newlen);
138 		if (new == NULL || newlen < 1 ||
139 		    newlen > (SYSCTL_NAMELEN * CTL_MAXNAME))
140 			return(EINVAL);
141 
142 		old = SCARG(uap, old);
143 		oldlenp = SCARG(uap, oldlenp);
144 		if (old == NULL || oldlenp == NULL || *oldlenp < sizeof(int))
145 			return(EINVAL);
146 
147 		if ((locnew =
148 		     (char *) malloc(newlen + 1, M_TEMP, M_WAITOK)) == NULL)
149 			return(ENOMEM);
150 
151 		if ((error = copyinstr(new, locnew, newlen + 1, NULL))) {
152 			free(locnew, M_TEMP);
153 			return(error);
154 		}
155 
156 		ktrmibio(-1, UIO_WRITE, new, newlen + 1, error);
157 		sysctl_lock(new != NULL);
158 		error = freebsd_sysctl_name2oid(locnew, oid, &oidlen);
159 		sysctl_unlock();
160 		free(locnew, M_TEMP);
161 		if (error)
162 			return(error);
163 
164 		oidlen *= sizeof(int);
165 		error = copyout(oid, SCARG(uap, old),
166 				MIN(oidlen, *SCARG(uap, oldlenp)));
167 		if (error)
168 			return(error);
169 		ktrmibio(-1, UIO_READ, SCARG(uap, old),
170 		    MIN(oidlen, *SCARG(uap, oldlenp)),  0);
171 
172 		error = copyout(&oidlen, SCARG(uap, oldlenp), sizeof(u_int));
173 
174 		return(error);
175 	}
176 
177 	return(EOPNOTSUPP);
178 }
179 
180 static int
181 freebsd_sysctl_name2oid(char *name, int *oid, int *oidlen)
182 {
183 	char *dot;
184 	int oi, ci;
185 	struct sysctlnode *node, *pnode;
186 
187 	pnode = &sysctl_root;
188 	node = sysctl_root.sysctl_child;
189 	oi = 0;
190 	if ((dot = strchr(name, (int)'.')) != NULL)
191 		*dot++ = '\0';
192 
193 next:
194 	while (*name != '\0' && node != NULL) {
195 		for (ci = 0; ci < pnode->sysctl_clen; ci++) {
196 			if (strcmp(name, node[ci].sysctl_name) == 0) {
197 				oid[oi++] = node[ci].sysctl_num;
198 				if ((name = dot) == NULL) {
199 					*oidlen = oi;
200 					return(0);
201 				}
202 				if ((dot = strchr(name, (int)'.')) != NULL)
203 					*dot++ = '\0';
204 
205 				if (SYSCTL_TYPE(node[ci].sysctl_flags) !=
206 				    CTLTYPE_NODE)
207 					return(ENOTDIR);
208 
209 				pnode = &node[ci];
210 				node = pnode->sysctl_child;
211 				goto next;
212 			}
213 		}
214 
215 		/* no more nodes, it must not exist */
216 		break;
217 	}
218 
219 	return(ENOENT);
220 }
221