FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_xxx.c
1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)kern_xxx.c 8.2 (Berkeley) 11/14/93
30 */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD: releng/7.3/sys/kern/kern_xxx.c 189634 2009-03-10 17:28:23Z jhb $");
34
35 #include "opt_compat.h"
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(td, uap)
62 struct thread *td;
63 struct gethostname_args *uap;
64 {
65 int name[2];
66 size_t len = uap->len;
67
68 name[0] = CTL_KERN;
69 name[1] = KERN_HOSTNAME;
70 return (userland_sysctl(td, name, 2, uap->hostname, &len,
71 1, 0, 0, 0, 0));
72 }
73
74 #ifndef _SYS_SYSPROTO_H_
75 struct sethostname_args {
76 char *hostname;
77 u_int len;
78 };
79 #endif
80 /* ARGSUSED */
81 int
82 osethostname(td, uap)
83 struct thread *td;
84 register struct sethostname_args *uap;
85 {
86 int name[2];
87
88 name[0] = CTL_KERN;
89 name[1] = KERN_HOSTNAME;
90 return (userland_sysctl(td, name, 2, 0, 0, 0, uap->hostname,
91 uap->len, 0, 0));
92 }
93
94 #ifndef _SYS_SYSPROTO_H_
95 struct ogethostid_args {
96 int dummy;
97 };
98 #endif
99 /* ARGSUSED */
100 int
101 ogethostid(td, uap)
102 struct thread *td;
103 struct ogethostid_args *uap;
104 {
105
106 *(long *)(td->td_retval) = hostid;
107 return (0);
108 }
109 #endif /* COMPAT_43 */
110
111 #ifdef COMPAT_43
112 #ifndef _SYS_SYSPROTO_H_
113 struct osethostid_args {
114 long hostid;
115 };
116 #endif
117 /* ARGSUSED */
118 int
119 osethostid(td, uap)
120 struct thread *td;
121 struct osethostid_args *uap;
122 {
123 int error;
124
125 error = priv_check(td, PRIV_SETHOSTID);
126 if (error)
127 return (error);
128 mtx_lock(&Giant);
129 hostid = uap->hostid;
130 mtx_unlock(&Giant);
131 return (0);
132 }
133
134 int
135 oquota(td, uap)
136 struct thread *td;
137 struct oquota_args *uap;
138 {
139
140 return (ENOSYS);
141 }
142
143 #define KINFO_PROC (0<<8)
144 #define KINFO_RT (1<<8)
145 #define KINFO_VNODE (2<<8)
146 #define KINFO_FILE (3<<8)
147 #define KINFO_METER (4<<8)
148 #define KINFO_LOADAVG (5<<8)
149 #define KINFO_CLOCKRATE (6<<8)
150
151 /* Non-standard BSDI extension - only present on their 4.3 net-2 releases */
152 #define KINFO_BSDI_SYSINFO (101<<8)
153
154 /*
155 * XXX this is bloat, but I hope it's better here than on the potentially
156 * limited kernel stack... -Peter
157 */
158
159 static struct {
160 int bsdi_machine; /* "i386" on BSD/386 */
161 /* ^^^ this is an offset to the string, relative to the struct start */
162 char *pad0;
163 long pad1;
164 long pad2;
165 long pad3;
166 u_long pad4;
167 u_long pad5;
168 u_long pad6;
169
170 int bsdi_ostype; /* "BSD/386" on BSD/386 */
171 int bsdi_osrelease; /* "1.1" on BSD/386 */
172 long pad7;
173 long pad8;
174 char *pad9;
175
176 long pad10;
177 long pad11;
178 int pad12;
179 long pad13;
180 quad_t pad14;
181 long pad15;
182
183 struct timeval pad16;
184 /* we dont set this, because BSDI's uname used gethostname() instead */
185 int bsdi_hostname; /* hostname on BSD/386 */
186
187 /* the actual string data is appended here */
188
189 } bsdi_si;
190
191 /*
192 * this data is appended to the end of the bsdi_si structure during copyout.
193 * The "char *" offsets are relative to the base of the bsdi_si struct.
194 * This contains "FreeBSD\02.0-BUILT-nnnnnn\0i386\0", and these strings
195 * should not exceed the length of the buffer here... (or else!! :-)
196 */
197 static char bsdi_strings[80]; /* It had better be less than this! */
198
199 #ifndef _SYS_SYSPROTO_H_
200 struct getkerninfo_args {
201 int op;
202 char *where;
203 size_t *size;
204 int arg;
205 };
206 #endif
207 int
208 ogetkerninfo(struct thread *td, struct getkerninfo_args *uap)
209 {
210 int error, name[6];
211 size_t size;
212 u_int needed = 0;
213
214 switch (uap->op & 0xff00) {
215
216 case KINFO_RT:
217 name[0] = CTL_NET;
218 name[1] = PF_ROUTE;
219 name[2] = 0;
220 name[3] = (uap->op & 0xff0000) >> 16;
221 name[4] = uap->op & 0xff;
222 name[5] = uap->arg;
223 error = userland_sysctl(td, name, 6, uap->where, uap->size,
224 0, 0, 0, &size, 0);
225 break;
226
227 case KINFO_VNODE:
228 name[0] = CTL_KERN;
229 name[1] = KERN_VNODE;
230 error = userland_sysctl(td, name, 2, uap->where, uap->size,
231 0, 0, 0, &size, 0);
232 break;
233
234 case KINFO_PROC:
235 name[0] = CTL_KERN;
236 name[1] = KERN_PROC;
237 name[2] = uap->op & 0xff;
238 name[3] = uap->arg;
239 error = userland_sysctl(td, name, 4, uap->where, uap->size,
240 0, 0, 0, &size, 0);
241 break;
242
243 case KINFO_FILE:
244 name[0] = CTL_KERN;
245 name[1] = KERN_FILE;
246 error = userland_sysctl(td, name, 2, uap->where, uap->size,
247 0, 0, 0, &size, 0);
248 break;
249
250 case KINFO_METER:
251 name[0] = CTL_VM;
252 name[1] = VM_TOTAL;
253 error = userland_sysctl(td, name, 2, uap->where, uap->size,
254 0, 0, 0, &size, 0);
255 break;
256
257 case KINFO_LOADAVG:
258 name[0] = CTL_VM;
259 name[1] = VM_LOADAVG;
260 error = userland_sysctl(td, name, 2, uap->where, uap->size,
261 0, 0, 0, &size, 0);
262 break;
263
264 case KINFO_CLOCKRATE:
265 name[0] = CTL_KERN;
266 name[1] = KERN_CLOCKRATE;
267 error = userland_sysctl(td, name, 2, uap->where, uap->size,
268 0, 0, 0, &size, 0);
269 break;
270
271 case KINFO_BSDI_SYSINFO: {
272 /*
273 * this is pretty crude, but it's just enough for uname()
274 * from BSDI's 1.x libc to work.
275 *
276 * *size gives the size of the buffer before the call, and
277 * the amount of data copied after a successful call.
278 * If successful, the return value is the amount of data
279 * available, which can be larger than *size.
280 *
281 * BSDI's 2.x product apparently fails with ENOMEM if *size
282 * is too small.
283 */
284
285 u_int left;
286 char *s;
287
288 bzero((char *)&bsdi_si, sizeof(bsdi_si));
289 bzero(bsdi_strings, sizeof(bsdi_strings));
290
291 s = bsdi_strings;
292
293 bsdi_si.bsdi_ostype = (s - bsdi_strings) + sizeof(bsdi_si);
294 strcpy(s, ostype);
295 s += strlen(s) + 1;
296
297 bsdi_si.bsdi_osrelease = (s - bsdi_strings) + sizeof(bsdi_si);
298 strcpy(s, osrelease);
299 s += strlen(s) + 1;
300
301 bsdi_si.bsdi_machine = (s - bsdi_strings) + sizeof(bsdi_si);
302 strcpy(s, machine);
303 s += strlen(s) + 1;
304
305 needed = sizeof(bsdi_si) + (s - bsdi_strings);
306
307 if ((uap->where == NULL) || (uap->size == NULL)) {
308 /* process is asking how much buffer to supply.. */
309 size = needed;
310 error = 0;
311 break;
312 }
313
314 if ((error = copyin(uap->size, &size, sizeof(size))) != 0)
315 break;
316
317 /* if too much buffer supplied, trim it down */
318 if (size > needed)
319 size = needed;
320
321 /* how much of the buffer is remaining */
322 left = size;
323
324 if ((error = copyout((char *)&bsdi_si, uap->where, left)) != 0)
325 break;
326
327 /* is there any point in continuing? */
328 if (left > sizeof(bsdi_si)) {
329 left -= sizeof(bsdi_si);
330 error = copyout(&bsdi_strings,
331 uap->where + sizeof(bsdi_si), left);
332 }
333 break;
334 }
335
336 default:
337 error = EOPNOTSUPP;
338 break;
339 }
340 if (error == 0) {
341 td->td_retval[0] = needed ? needed : size;
342 if (uap->size) {
343 error = copyout(&size, uap->size, sizeof(size));
344 }
345 }
346 return (error);
347 }
348 #endif /* COMPAT_43 */
349
350 /*
351 * This is the FreeBSD-1.1 compatable uname(2) interface. These days it is
352 * done in libc as a wrapper around a bunch of sysctl's. This must maintain
353 * the old 1.1 binary ABI.
354 */
355 #if SYS_NMLN != 32
356 #error "FreeBSD-1.1 uname syscall has been broken"
357 #endif
358 #ifndef _SYS_SYSPROTO_H_
359 struct uname_args {
360 struct utsname *name;
361 };
362 #endif
363 /* ARGSUSED */
364 int
365 uname(td, uap)
366 struct thread *td;
367 struct uname_args *uap;
368 {
369 int name[2], error;
370 size_t len;
371 char *s, *us;
372
373 name[0] = CTL_KERN;
374 name[1] = KERN_OSTYPE;
375 len = sizeof (uap->name->sysname);
376 error = userland_sysctl(td, name, 2, uap->name->sysname, &len,
377 1, 0, 0, 0, 0);
378 if (error)
379 return (error);
380 subyte( uap->name->sysname + sizeof(uap->name->sysname) - 1, 0);
381
382 name[1] = KERN_HOSTNAME;
383 len = sizeof uap->name->nodename;
384 error = userland_sysctl(td, name, 2, uap->name->nodename, &len,
385 1, 0, 0, 0, 0);
386 if (error)
387 return (error);
388 subyte( uap->name->nodename + sizeof(uap->name->nodename) - 1, 0);
389
390 name[1] = KERN_OSRELEASE;
391 len = sizeof uap->name->release;
392 error = userland_sysctl(td, name, 2, uap->name->release, &len,
393 1, 0, 0, 0, 0);
394 if (error)
395 return (error);
396 subyte( uap->name->release + sizeof(uap->name->release) - 1, 0);
397
398 /*
399 name = KERN_VERSION;
400 len = sizeof uap->name->version;
401 error = userland_sysctl(td, name, 2, uap->name->version, &len,
402 1, 0, 0, 0, 0);
403 if (error)
404 return (error);
405 subyte( uap->name->version + sizeof(uap->name->version) - 1, 0);
406 */
407
408 /*
409 * this stupid hackery to make the version field look like FreeBSD 1.1
410 */
411 for(s = version; *s && *s != '#'; s++);
412
413 for(us = uap->name->version; *s && *s != ':'; s++) {
414 error = subyte( us++, *s);
415 if (error)
416 return (error);
417 }
418 error = subyte( us++, 0);
419 if (error)
420 return (error);
421
422 name[0] = CTL_HW;
423 name[1] = HW_MACHINE;
424 len = sizeof uap->name->machine;
425 error = userland_sysctl(td, name, 2, uap->name->machine, &len,
426 1, 0, 0, 0, 0);
427 if (error)
428 return (error);
429 subyte( uap->name->machine + sizeof(uap->name->machine) - 1, 0);
430 return (0);
431 }
432
433 #ifndef _SYS_SYSPROTO_H_
434 struct getdomainname_args {
435 char *domainname;
436 int len;
437 };
438 #endif
439 /* ARGSUSED */
440 int
441 getdomainname(td, uap)
442 struct thread *td;
443 struct getdomainname_args *uap;
444 {
445 int domainnamelen;
446 int error;
447
448 mtx_lock(&Giant);
449 domainnamelen = strlen(domainname) + 1;
450 if ((u_int)uap->len > domainnamelen)
451 uap->len = domainnamelen;
452 error = copyout(domainname, uap->domainname, uap->len);
453 mtx_unlock(&Giant);
454 return (error);
455 }
456
457 #ifndef _SYS_SYSPROTO_H_
458 struct setdomainname_args {
459 char *domainname;
460 int len;
461 };
462 #endif
463 /* ARGSUSED */
464 int
465 setdomainname(td, uap)
466 struct thread *td;
467 struct setdomainname_args *uap;
468 {
469 int error, domainnamelen;
470
471 error = priv_check(td, PRIV_SETDOMAINNAME);
472 if (error)
473 return (error);
474 mtx_lock(&Giant);
475 if ((u_int)uap->len > sizeof (domainname) - 1) {
476 error = EINVAL;
477 goto done2;
478 }
479 domainnamelen = uap->len;
480 error = copyin(uap->domainname, domainname, uap->len);
481 domainname[domainnamelen] = 0;
482 done2:
483 mtx_unlock(&Giant);
484 return (error);
485 }
Cache object: 3d5e3ebcb2c6d4ce94af70a75e00a44b
|