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