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