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