xref: /freebsd/sys/kern/kern_xxx.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
36 #include <sys/kernel.h>
37 #include <sys/priv.h>
38 #include <sys/proc.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 #include <sys/utsname.h>
44 
45 #include <vm/vm_param.h>
46 
47 #if defined(COMPAT_43)
48 
49 int
50 ogethostname(struct thread *td, struct ogethostname_args *uap)
51 {
52 	int name[2];
53 	size_t len = uap->len;
54 
55 	name[0] = CTL_KERN;
56 	name[1] = KERN_HOSTNAME;
57 	return (userland_sysctl(td, name, 2, uap->hostname, &len,
58 	    1, 0, 0, 0, 0));
59 }
60 
61 int
62 osethostname(struct thread *td, struct osethostname_args *uap)
63 {
64 	int name[2];
65 
66 	name[0] = CTL_KERN;
67 	name[1] = KERN_HOSTNAME;
68 	return (userland_sysctl(td, name, 2, 0, 0, 0, uap->hostname,
69 	    uap->len, 0, 0));
70 }
71 
72 #ifndef _SYS_SYSPROTO_H_
73 struct ogethostid_args {
74 	int	dummy;
75 };
76 #endif
77 /* ARGSUSED */
78 int
79 ogethostid(struct thread *td, struct ogethostid_args *uap)
80 {
81 	size_t len = sizeof(long);
82 	int name[2];
83 
84 	name[0] = CTL_KERN;
85 	name[1] = KERN_HOSTID;
86 	return (kernel_sysctl(td, name, 2, (long *)td->td_retval, &len,
87 	    NULL, 0, NULL, 0));
88 }
89 
90 int
91 osethostid(struct thread *td, struct osethostid_args *uap)
92 {
93 	int name[2];
94 
95 	name[0] = CTL_KERN;
96 	name[1] = KERN_HOSTID;
97 	return (kernel_sysctl(td, name, 2, NULL, NULL, &uap->hostid,
98 	    sizeof(uap->hostid), NULL, 0));
99 }
100 
101 int
102 oquota(struct thread *td, struct oquota_args *uap)
103 {
104 
105 	return (ENOSYS);
106 }
107 
108 #define	KINFO_PROC		(0<<8)
109 #define	KINFO_RT		(1<<8)
110 /* UNUSED, was KINFO_VNODE (2<<8) */
111 #define	KINFO_FILE		(3<<8)
112 #define	KINFO_METER		(4<<8)
113 #define	KINFO_LOADAVG		(5<<8)
114 #define	KINFO_CLOCKRATE		(6<<8)
115 
116 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
117 #define	KINFO_BSDI_SYSINFO	(101<<8)
118 
119 /*
120  * XXX this is bloat, but I hope it's better here than on the potentially
121  * limited kernel stack...  -Peter
122  */
123 
124 static struct {
125 	int	bsdi_machine;		/* "i386" on BSD/386 */
126 /*      ^^^ this is an offset to the string, relative to the struct start */
127 	char	*pad0;
128 	long	pad1;
129 	long	pad2;
130 	long	pad3;
131 	u_long	pad4;
132 	u_long	pad5;
133 	u_long	pad6;
134 
135 	int	bsdi_ostype;		/* "BSD/386" on BSD/386 */
136 	int	bsdi_osrelease;		/* "1.1" on BSD/386 */
137 	long	pad7;
138 	long	pad8;
139 	char	*pad9;
140 
141 	long	pad10;
142 	long	pad11;
143 	int	pad12;
144 	long	pad13;
145 	quad_t	pad14;
146 	long	pad15;
147 
148 	struct	timeval pad16;
149 	/* we dont set this, because BSDI's uname used gethostname() instead */
150 	int	bsdi_hostname;		/* hostname on BSD/386 */
151 
152 	/* the actual string data is appended here */
153 
154 } bsdi_si;
155 
156 /*
157  * this data is appended to the end of the bsdi_si structure during copyout.
158  * The "char *" offsets are relative to the base of the bsdi_si struct.
159  * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
160  * should not exceed the length of the buffer here... (or else!! :-)
161  */
162 static char bsdi_strings[80];	/* It had better be less than this! */
163 
164 int
165 ogetkerninfo(struct thread *td, struct ogetkerninfo_args *uap)
166 {
167 	int error, name[6];
168 	size_t size;
169 	u_int needed = 0;
170 
171 	switch (uap->op & 0xff00) {
172 	case KINFO_RT:
173 		name[0] = CTL_NET;
174 		name[1] = PF_ROUTE;
175 		name[2] = 0;
176 		name[3] = (uap->op & 0xff0000) >> 16;
177 		name[4] = uap->op & 0xff;
178 		name[5] = uap->arg;
179 		error = userland_sysctl(td, name, 6, uap->where, uap->size,
180 			0, 0, 0, &size, 0);
181 		break;
182 
183 	case KINFO_PROC:
184 		name[0] = CTL_KERN;
185 		name[1] = KERN_PROC;
186 		name[2] = uap->op & 0xff;
187 		name[3] = uap->arg;
188 		error = userland_sysctl(td, name, 4, uap->where, uap->size,
189 			0, 0, 0, &size, 0);
190 		break;
191 
192 	case KINFO_FILE:
193 		name[0] = CTL_KERN;
194 		name[1] = KERN_FILE;
195 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
196 			0, 0, 0, &size, 0);
197 		break;
198 
199 	case KINFO_METER:
200 		name[0] = CTL_VM;
201 		name[1] = VM_TOTAL;
202 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
203 			0, 0, 0, &size, 0);
204 		break;
205 
206 	case KINFO_LOADAVG:
207 		name[0] = CTL_VM;
208 		name[1] = VM_LOADAVG;
209 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
210 			0, 0, 0, &size, 0);
211 		break;
212 
213 	case KINFO_CLOCKRATE:
214 		name[0] = CTL_KERN;
215 		name[1] = KERN_CLOCKRATE;
216 		error = userland_sysctl(td, name, 2, uap->where, uap->size,
217 			0, 0, 0, &size, 0);
218 		break;
219 
220 	case KINFO_BSDI_SYSINFO: {
221 		/*
222 		 * this is pretty crude, but it's just enough for uname()
223 		 * from BSDI's 1.x libc to work.
224 		 *
225 		 * *size gives the size of the buffer before the call, and
226 		 * the amount of data copied after a successful call.
227 		 * If successful, the return value is the amount of data
228 		 * available, which can be larger than *size.
229 		 *
230 		 * BSDI's 2.x product apparently fails with ENOMEM if *size
231 		 * is too small.
232 		 */
233 
234 		u_int left;
235 		char *s;
236 
237 		bzero((char *)&bsdi_si, sizeof(bsdi_si));
238 		bzero(bsdi_strings, sizeof(bsdi_strings));
239 
240 		s = bsdi_strings;
241 
242 		bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
243 		strcpy(s, ostype);
244 		s += strlen(s) + 1;
245 
246 		bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
247 		strcpy(s, osrelease);
248 		s += strlen(s) + 1;
249 
250 		bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
251 		strcpy(s, machine);
252 		s += strlen(s) + 1;
253 
254 		needed = sizeof(bsdi_si) + (s - bsdi_strings);
255 
256 		if ((uap->where == NULL) || (uap->size == NULL)) {
257 			/* process is asking how much buffer to supply.. */
258 			size = needed;
259 			error = 0;
260 			break;
261 		}
262 
263 		if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
264 			break;
265 
266 		/* if too much buffer supplied, trim it down */
267 		if (size > needed)
268 			size = needed;
269 
270 		/* how much of the buffer is remaining */
271 		left = size;
272 
273 		if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
274 			break;
275 
276 		/* is there any point in continuing? */
277 		if (left > sizeof(bsdi_si)) {
278 			left -= sizeof(bsdi_si);
279 			error = copyout(&bsdi_strings,
280 					uap->where + sizeof(bsdi_si), left);
281 		}
282 		break;
283 	}
284 
285 	default:
286 		error = EOPNOTSUPP;
287 		break;
288 	}
289 	if (error == 0) {
290 		td->td_retval[0] = needed ? needed : size;
291 		if (uap->size) {
292 			error = copyout(&size, uap->size, sizeof(size));
293 		}
294 	}
295 	return (error);
296 }
297 #endif /* COMPAT_43 */
298 
299 #ifdef COMPAT_FREEBSD4
300 /*
301  * This is the FreeBSD-1.1 compatible uname(2) interface.  These days it is
302  * done in libc as a wrapper around a bunch of sysctl's.  This must maintain
303  * the old 1.1 binary ABI.
304  */
305 #if SYS_NMLN != 32
306 #error "FreeBSD-1.1 uname syscall has been broken"
307 #endif
308 #ifndef _SYS_SYSPROTO_H_
309 struct uname_args {
310 	struct utsname  *name;
311 };
312 #endif
313 /* ARGSUSED */
314 int
315 freebsd4_uname(struct thread *td, struct freebsd4_uname_args *uap)
316 {
317 	int name[2], error;
318 	size_t len;
319 	char *s, *us;
320 
321 	name[0] = CTL_KERN;
322 	name[1] = KERN_OSTYPE;
323 	len = sizeof (uap->name->sysname);
324 	error = userland_sysctl(td, name, 2, uap->name->sysname, &len,
325 		1, 0, 0, 0, 0);
326 	if (error)
327 		return (error);
328 	subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
329 
330 	name[1] = KERN_HOSTNAME;
331 	len = sizeof uap->name->nodename;
332 	error = userland_sysctl(td, name, 2, uap->name->nodename, &len,
333 		1, 0, 0, 0, 0);
334 	if (error)
335 		return (error);
336 	subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
337 
338 	name[1] = KERN_OSRELEASE;
339 	len = sizeof uap->name->release;
340 	error = userland_sysctl(td, name, 2, uap->name->release, &len,
341 		1, 0, 0, 0, 0);
342 	if (error)
343 		return (error);
344 	subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
345 
346 /*
347 	name = KERN_VERSION;
348 	len = sizeof uap->name->version;
349 	error = userland_sysctl(td, name, 2, uap->name->version, &len,
350 		1, 0, 0, 0, 0);
351 	if (error)
352 		return (error);
353 	subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
354 */
355 
356 /*
357  * this stupid hackery to make the version field look like FreeBSD 1.1
358  */
359 	for(s = version; *s && *s != '#'; s++);
360 
361 	for(us = uap->name->version; *s && *s != ':'; s++) {
362 		error = subyte( us++, *s);
363 		if (error)
364 			return (error);
365 	}
366 	error = subyte( us++, 0);
367 	if (error)
368 		return (error);
369 
370 	name[0] = CTL_HW;
371 	name[1] = HW_MACHINE;
372 	len = sizeof uap->name->machine;
373 	error = userland_sysctl(td, name, 2, uap->name->machine, &len,
374 		1, 0, 0, 0, 0);
375 	if (error)
376 		return (error);
377 	subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
378 	return (0);
379 }
380 
381 #ifndef _SYS_SYSPROTO_H_
382 struct getdomainname_args {
383 	char    *domainname;
384 	int     len;
385 };
386 #endif
387 /* ARGSUSED */
388 int
389 freebsd4_getdomainname(struct thread *td,
390     struct freebsd4_getdomainname_args *uap)
391 {
392 	int name[2];
393 	size_t len = uap->len;
394 
395 	name[0] = CTL_KERN;
396 	name[1] = KERN_NISDOMAINNAME;
397 	return (userland_sysctl(td, name, 2, uap->domainname, &len,
398 	    1, 0, 0, 0, 0));
399 }
400 
401 #ifndef _SYS_SYSPROTO_H_
402 struct setdomainname_args {
403 	char    *domainname;
404 	int     len;
405 };
406 #endif
407 /* ARGSUSED */
408 int
409 freebsd4_setdomainname(struct thread *td,
410     struct freebsd4_setdomainname_args *uap)
411 {
412 	int name[2];
413 
414 	name[0] = CTL_KERN;
415 	name[1] = KERN_NISDOMAINNAME;
416 	return (userland_sysctl(td, name, 2, 0, 0, 0, uap->domainname,
417 	    uap->len, 0, 0));
418 }
419 #endif /* COMPAT_FREEBSD4 */
420