xref: /netbsd/sys/compat/freebsd/freebsd_sysctl.c (revision 0a799f91)
1 /*	$NetBSD: freebsd_sysctl.c,v 1.19 2015/02/14 07:37:19 dholland 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.19 2015/02/14 07:37:19 dholland 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
freebsd_sysctl_init(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
freebsd_sysctl_fini(void)74 freebsd_sysctl_fini(void)
75 {
76 
77 	sysctl_teardown(&freebsd_clog);
78 }
79 
80 int
freebsd_sys_sysctl(struct lwp * l,const struct freebsd_sys_sysctl_args * uap,register_t * retval)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, oldlen;
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 its
113 	 * sysctl MIB whose tree is rooted at oid 0.  These OIDs are
114 	 * interpreted 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)
145 			return(EINVAL);
146 
147 		if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
148 			return (error);
149 		if (oldlen < 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, oldlen));
172 		if (error)
173 			return(error);
174 		ktrmibio(-1, UIO_READ, SCARG(uap, old),
175 		    MIN(oidlen, oldlen),  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
freebsd_sysctl_name2oid(char * name,int * oid,int * oidlen)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