kern_sysctl.c revision 1.42.2.1 1 /* $NetBSD: kern_sysctl.c,v 1.42.2.1 1998/11/09 06:06:32 chs Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Mike Karels at Berkeley Software Design, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
39 */
40
41 /*
42 * sysctl system call.
43 */
44
45 #include "opt_ddb.h"
46 #include "opt_insecure.h"
47 #include "opt_shortcorename.h"
48 #include "opt_uvm.h"
49 #include "opt_sysv.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/proc.h>
56 #include <sys/file.h>
57 #include <sys/vnode.h>
58 #include <sys/unistd.h>
59 #include <sys/buf.h>
60 #include <sys/ioctl.h>
61 #include <sys/tty.h>
62 #include <sys/disklabel.h>
63 #include <sys/device.h>
64 #include <vm/vm.h>
65 #include <sys/sysctl.h>
66 #include <sys/msgbuf.h>
67
68 #include <sys/mount.h>
69 #include <sys/syscallargs.h>
70
71 #if defined(UVM)
72 #include <uvm/uvm_extern.h>
73 #endif
74
75 #if defined(DDB)
76 #include <ddb/ddbvar.h>
77 #endif
78
79 /*
80 * Locking and stats
81 */
82 static struct sysctl_lock {
83 int sl_lock;
84 int sl_want;
85 int sl_locked;
86 } memlock;
87
88 int
89 sys___sysctl(p, v, retval)
90 struct proc *p;
91 void *v;
92 register_t *retval;
93 {
94 register struct sys___sysctl_args /* {
95 syscallarg(int *) name;
96 syscallarg(u_int) namelen;
97 syscallarg(void *) old;
98 syscallarg(size_t *) oldlenp;
99 syscallarg(void *) new;
100 syscallarg(size_t) newlen;
101 } */ *uap = v;
102 int error, dolock = 1;
103 size_t savelen = 0, oldlen = 0;
104 sysctlfn *fn;
105 int name[CTL_MAXNAME];
106
107 if (SCARG(uap, new) != NULL &&
108 (error = suser(p->p_ucred, &p->p_acflag)))
109 return (error);
110 /*
111 * all top-level sysctl names are non-terminal
112 */
113 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
114 return (EINVAL);
115 error = copyin(SCARG(uap, name), &name,
116 SCARG(uap, namelen) * sizeof(int));
117 if (error)
118 return (error);
119
120 switch (name[0]) {
121 case CTL_KERN:
122 fn = kern_sysctl;
123 if (name[2] != KERN_VNODE) /* XXX */
124 dolock = 0;
125 break;
126 case CTL_HW:
127 fn = hw_sysctl;
128 break;
129 case CTL_VM:
130 #if defined(UVM)
131 fn = uvm_sysctl;
132 #else
133 fn = vm_sysctl;
134 #endif
135 break;
136 case CTL_NET:
137 fn = net_sysctl;
138 break;
139 case CTL_VFS:
140 fn = vfs_sysctl;
141 break;
142 case CTL_MACHDEP:
143 fn = cpu_sysctl;
144 break;
145 #ifdef DEBUG
146 case CTL_DEBUG:
147 fn = debug_sysctl;
148 break;
149 #endif
150 #ifdef DDB
151 case CTL_DDB:
152 fn = ddb_sysctl;
153 break;
154 #endif
155 default:
156 return (EOPNOTSUPP);
157 }
158
159 if (SCARG(uap, oldlenp) &&
160 (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen))))
161 return (error);
162 if (SCARG(uap, old) != NULL) {
163 #if defined(UVM)
164 if (!uvm_useracc(SCARG(uap, old), oldlen, B_WRITE))
165 #else
166 if (!useracc(SCARG(uap, old), oldlen, B_WRITE))
167 #endif
168 return (EFAULT);
169 while (memlock.sl_lock) {
170 memlock.sl_want = 1;
171 sleep((caddr_t)&memlock, PRIBIO+1);
172 memlock.sl_locked++;
173 }
174 memlock.sl_lock = 1;
175 if (dolock)
176 #if defined(UVM)
177 uvm_vslock(p, SCARG(uap, old), oldlen);
178 #else
179 vslock(p, SCARG(uap, old), oldlen);
180 #endif
181 savelen = oldlen;
182 }
183 error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
184 &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
185 if (SCARG(uap, old) != NULL) {
186 if (dolock)
187 #if defined(UVM)
188 uvm_vsunlock(p, SCARG(uap, old), savelen);
189 #else
190 vsunlock(p, SCARG(uap, old), savelen);
191 #endif
192 memlock.sl_lock = 0;
193 if (memlock.sl_want) {
194 memlock.sl_want = 0;
195 wakeup((caddr_t)&memlock);
196 }
197 }
198 if (error)
199 return (error);
200 if (SCARG(uap, oldlenp))
201 error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
202 return (error);
203 }
204
205 /*
206 * Attributes stored in the kernel.
207 */
208 char hostname[MAXHOSTNAMELEN];
209 int hostnamelen;
210 char domainname[MAXHOSTNAMELEN];
211 int domainnamelen;
212 long hostid;
213 #ifdef INSECURE
214 int securelevel = -1;
215 #else
216 int securelevel = 0;
217 #endif
218 #ifdef SHORTCORENAME
219 int shortcorename = 1;
220 #else
221 int shortcorename = 0;
222 #endif
223
224 /*
225 * kernel related system variables.
226 */
227 int
228 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
229 int *name;
230 u_int namelen;
231 void *oldp;
232 size_t *oldlenp;
233 void *newp;
234 size_t newlen;
235 struct proc *p;
236 {
237 int error, level, inthostid;
238 int old_autonicetime;
239 int old_vnodes;
240 int old_shortcorename;
241 extern char ostype[], osrelease[], version[];
242
243 /* all sysctl names at this level are terminal */
244 if (namelen != 1 && !(name[0] == KERN_PROC || name[0] == KERN_PROF))
245 return (ENOTDIR); /* overloaded */
246
247 switch (name[0]) {
248 case KERN_OSTYPE:
249 return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
250 case KERN_OSRELEASE:
251 return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
252 case KERN_OSREV:
253 return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
254 case KERN_VERSION:
255 return (sysctl_rdstring(oldp, oldlenp, newp, version));
256 case KERN_MAXVNODES:
257 old_vnodes = desiredvnodes;
258 error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
259 if (old_vnodes > desiredvnodes) {
260 desiredvnodes = old_vnodes;
261 return (EINVAL);
262 }
263 return (error);
264 case KERN_MAXPROC:
265 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
266 case KERN_MAXFILES:
267 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
268 case KERN_ARGMAX:
269 return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
270 case KERN_SECURELVL:
271 level = securelevel;
272 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
273 newp == NULL)
274 return (error);
275 if (level < securelevel && p->p_pid != 1)
276 return (EPERM);
277 securelevel = level;
278 return (0);
279 case KERN_HOSTNAME:
280 error = sysctl_string(oldp, oldlenp, newp, newlen,
281 hostname, sizeof(hostname));
282 if (newp && !error)
283 hostnamelen = newlen;
284 return (error);
285 case KERN_DOMAINNAME:
286 error = sysctl_string(oldp, oldlenp, newp, newlen,
287 domainname, sizeof(domainname));
288 if (newp && !error)
289 domainnamelen = newlen;
290 return (error);
291 case KERN_HOSTID:
292 inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
293 error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
294 hostid = inthostid;
295 return (error);
296 case KERN_CLOCKRATE:
297 return (sysctl_clockrate(oldp, oldlenp));
298 case KERN_BOOTTIME:
299 return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
300 sizeof(struct timeval)));
301 case KERN_VNODE:
302 return (sysctl_vnode(oldp, oldlenp, p));
303 case KERN_PROC:
304 return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
305 case KERN_FILE:
306 return (sysctl_file(oldp, oldlenp));
307 #ifdef GPROF
308 case KERN_PROF:
309 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
310 newp, newlen));
311 #endif
312 case KERN_POSIX1:
313 return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
314 case KERN_NGROUPS:
315 return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
316 case KERN_JOB_CONTROL:
317 return (sysctl_rdint(oldp, oldlenp, newp, 1));
318 case KERN_SAVED_IDS:
319 #ifdef _POSIX_SAVED_IDS
320 return (sysctl_rdint(oldp, oldlenp, newp, 1));
321 #else
322 return (sysctl_rdint(oldp, oldlenp, newp, 0));
323 #endif
324 case KERN_MAXPARTITIONS:
325 return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
326 case KERN_RAWPARTITION:
327 return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
328 #ifdef NTP
329 case KERN_NTPTIME:
330 return (sysctl_ntptime(oldp, oldlenp));
331 #endif
332 case KERN_AUTONICETIME:
333 old_autonicetime = autonicetime;
334 error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
335 if (autonicetime < 0)
336 autonicetime = old_autonicetime;
337 return (error);
338 case KERN_AUTONICEVAL:
339 error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
340 if (autoniceval < PRIO_MIN)
341 autoniceval = PRIO_MIN;
342 if (autoniceval > PRIO_MAX)
343 autoniceval = PRIO_MAX;
344 return (error);
345 case KERN_RTC_OFFSET:
346 return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
347 case KERN_ROOT_DEVICE:
348 return (sysctl_rdstring(oldp, oldlenp, newp,
349 root_device->dv_xname));
350 case KERN_MSGBUFSIZE:
351 /*
352 * deal with cases where the message buffer has
353 * become corrupted.
354 */
355 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
356 msgbufenabled = 0;
357 return (ENXIO);
358 }
359 return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
360 case KERN_FSYNC:
361 return (sysctl_rdint(oldp, oldlenp, newp, 1));
362 case KERN_SYSVMSG:
363 #ifdef SYSVMSG
364 return (sysctl_rdint(oldp, oldlenp, newp, 1));
365 #else
366 return (sysctl_rdint(oldp, oldlenp, newp, 0));
367 #endif
368 case KERN_SYSVSEM:
369 #ifdef SYSVSEM
370 return (sysctl_rdint(oldp, oldlenp, newp, 1));
371 #else
372 return (sysctl_rdint(oldp, oldlenp, newp, 0));
373 #endif
374 case KERN_SYSVSHM:
375 #ifdef SYSVSHM
376 return (sysctl_rdint(oldp, oldlenp, newp, 1));
377 #else
378 return (sysctl_rdint(oldp, oldlenp, newp, 0));
379 #endif
380 case KERN_SHORTCORENAME:
381 /* Only allow values of zero or one. */
382 old_shortcorename = shortcorename;
383 error = sysctl_int(oldp, oldlenp, newp, newlen,
384 &shortcorename);
385 if (shortcorename != 0 && shortcorename != 1) {
386 shortcorename = old_shortcorename;
387 return (EINVAL);
388 }
389 return (error);
390 case KERN_SYNCHRONIZED_IO:
391 return (sysctl_rdint(oldp, oldlenp, newp, 1));
392 case KERN_IOV_MAX:
393 return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
394 default:
395 return (EOPNOTSUPP);
396 }
397 /* NOTREACHED */
398 }
399
400 /*
401 * hardware related system variables.
402 */
403 int
404 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
405 int *name;
406 u_int namelen;
407 void *oldp;
408 size_t *oldlenp;
409 void *newp;
410 size_t newlen;
411 struct proc *p;
412 {
413 extern char machine[], machine_arch[], cpu_model[];
414
415 /* all sysctl names at this level are terminal */
416 if (namelen != 1)
417 return (ENOTDIR); /* overloaded */
418
419 switch (name[0]) {
420 case HW_MACHINE:
421 return (sysctl_rdstring(oldp, oldlenp, newp, machine));
422 case HW_MACHINE_ARCH:
423 return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
424 case HW_MODEL:
425 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
426 case HW_NCPU:
427 return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */
428 case HW_BYTEORDER:
429 return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
430 case HW_PHYSMEM:
431 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
432 case HW_USERMEM:
433 #if defined(UVM)
434 return (sysctl_rdint(oldp, oldlenp, newp,
435 ctob(physmem - uvmexp.wired)));
436 #else
437 return (sysctl_rdint(oldp, oldlenp, newp,
438 ctob(physmem - cnt.v_wire_count)));
439 #endif
440 case HW_PAGESIZE:
441 return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
442 default:
443 return (EOPNOTSUPP);
444 }
445 /* NOTREACHED */
446 }
447
448 #ifdef DEBUG
449 /*
450 * Debugging related system variables.
451 */
452 struct ctldebug debug0, debug1, debug2, debug3, debug4;
453 struct ctldebug debug5, debug6, debug7, debug8, debug9;
454 struct ctldebug debug10, debug11, debug12, debug13, debug14;
455 struct ctldebug debug15, debug16, debug17, debug18, debug19;
456 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
457 &debug0, &debug1, &debug2, &debug3, &debug4,
458 &debug5, &debug6, &debug7, &debug8, &debug9,
459 &debug10, &debug11, &debug12, &debug13, &debug14,
460 &debug15, &debug16, &debug17, &debug18, &debug19,
461 };
462 int
463 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
464 int *name;
465 u_int namelen;
466 void *oldp;
467 size_t *oldlenp;
468 void *newp;
469 size_t newlen;
470 struct proc *p;
471 {
472 struct ctldebug *cdp;
473
474 /* all sysctl names at this level are name and field */
475 if (namelen != 2)
476 return (ENOTDIR); /* overloaded */
477 cdp = debugvars[name[0]];
478 if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
479 return (EOPNOTSUPP);
480 switch (name[1]) {
481 case CTL_DEBUG_NAME:
482 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
483 case CTL_DEBUG_VALUE:
484 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
485 default:
486 return (EOPNOTSUPP);
487 }
488 /* NOTREACHED */
489 }
490 #endif /* DEBUG */
491
492 /*
493 * Validate parameters and get old / set new parameters
494 * for an integer-valued sysctl function.
495 */
496 int
497 sysctl_int(oldp, oldlenp, newp, newlen, valp)
498 void *oldp;
499 size_t *oldlenp;
500 void *newp;
501 size_t newlen;
502 int *valp;
503 {
504 int error = 0;
505
506 if (oldp && *oldlenp < sizeof(int))
507 return (ENOMEM);
508 if (newp && newlen != sizeof(int))
509 return (EINVAL);
510 *oldlenp = sizeof(int);
511 if (oldp)
512 error = copyout(valp, oldp, sizeof(int));
513 if (error == 0 && newp)
514 error = copyin(newp, valp, sizeof(int));
515 return (error);
516 }
517
518 /*
519 * As above, but read-only.
520 */
521 int
522 sysctl_rdint(oldp, oldlenp, newp, val)
523 void *oldp;
524 size_t *oldlenp;
525 void *newp;
526 int val;
527 {
528 int error = 0;
529
530 if (oldp && *oldlenp < sizeof(int))
531 return (ENOMEM);
532 if (newp)
533 return (EPERM);
534 *oldlenp = sizeof(int);
535 if (oldp)
536 error = copyout((caddr_t)&val, oldp, sizeof(int));
537 return (error);
538 }
539
540 /*
541 * Validate parameters and get old / set new parameters
542 * for a string-valued sysctl function.
543 */
544 int
545 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
546 void *oldp;
547 size_t *oldlenp;
548 void *newp;
549 size_t newlen;
550 char *str;
551 int maxlen;
552 {
553 int len, error = 0;
554
555 len = strlen(str) + 1;
556 if (oldp && *oldlenp < len)
557 return (ENOMEM);
558 if (newp && newlen >= maxlen)
559 return (EINVAL);
560 if (oldp) {
561 *oldlenp = len;
562 error = copyout(str, oldp, len);
563 }
564 if (error == 0 && newp) {
565 error = copyin(newp, str, newlen);
566 str[newlen] = 0;
567 }
568 return (error);
569 }
570
571 /*
572 * As above, but read-only.
573 */
574 int
575 sysctl_rdstring(oldp, oldlenp, newp, str)
576 void *oldp;
577 size_t *oldlenp;
578 void *newp;
579 char *str;
580 {
581 int len, error = 0;
582
583 len = strlen(str) + 1;
584 if (oldp && *oldlenp < len)
585 return (ENOMEM);
586 if (newp)
587 return (EPERM);
588 *oldlenp = len;
589 if (oldp)
590 error = copyout(str, oldp, len);
591 return (error);
592 }
593
594 /*
595 * Validate parameters and get old / set new parameters
596 * for a structure oriented sysctl function.
597 */
598 int
599 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
600 void *oldp;
601 size_t *oldlenp;
602 void *newp;
603 size_t newlen;
604 void *sp;
605 int len;
606 {
607 int error = 0;
608
609 if (oldp && *oldlenp < len)
610 return (ENOMEM);
611 if (newp && newlen > len)
612 return (EINVAL);
613 if (oldp) {
614 *oldlenp = len;
615 error = copyout(sp, oldp, len);
616 }
617 if (error == 0 && newp)
618 error = copyin(newp, sp, len);
619 return (error);
620 }
621
622 /*
623 * Validate parameters and get old parameters
624 * for a structure oriented sysctl function.
625 */
626 int
627 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
628 void *oldp;
629 size_t *oldlenp;
630 void *newp, *sp;
631 int len;
632 {
633 int error = 0;
634
635 if (oldp && *oldlenp < len)
636 return (ENOMEM);
637 if (newp)
638 return (EPERM);
639 *oldlenp = len;
640 if (oldp)
641 error = copyout(sp, oldp, len);
642 return (error);
643 }
644
645 /*
646 * Get file structures.
647 */
648 int
649 sysctl_file(where, sizep)
650 char *where;
651 size_t *sizep;
652 {
653 int buflen, error;
654 struct file *fp;
655 char *start = where;
656
657 buflen = *sizep;
658 if (where == NULL) {
659 /*
660 * overestimate by 10 files
661 */
662 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
663 return (0);
664 }
665
666 /*
667 * first copyout filehead
668 */
669 if (buflen < sizeof(filehead)) {
670 *sizep = 0;
671 return (0);
672 }
673 error = copyout((caddr_t)&filehead, where, sizeof(filehead));
674 if (error)
675 return (error);
676 buflen -= sizeof(filehead);
677 where += sizeof(filehead);
678
679 /*
680 * followed by an array of file structures
681 */
682 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
683 if (buflen < sizeof(struct file)) {
684 *sizep = where - start;
685 return (ENOMEM);
686 }
687 error = copyout((caddr_t)fp, where, sizeof(struct file));
688 if (error)
689 return (error);
690 buflen -= sizeof(struct file);
691 where += sizeof(struct file);
692 }
693 *sizep = where - start;
694 return (0);
695 }
696
697 /*
698 * try over estimating by 5 procs
699 */
700 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
701
702 int
703 sysctl_doproc(name, namelen, where, sizep)
704 int *name;
705 u_int namelen;
706 char *where;
707 size_t *sizep;
708 {
709 register struct proc *p;
710 register struct kinfo_proc *dp = (struct kinfo_proc *)where;
711 register int needed = 0;
712 int buflen = where != NULL ? *sizep : 0;
713 const struct proclist_desc *pd;
714 struct eproc eproc;
715 int error = 0;
716
717 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
718 return (EINVAL);
719
720 pd = proclists;
721 again:
722 for (p = LIST_FIRST(pd->pd_list); p != NULL;
723 p = LIST_NEXT(p, p_list)) {
724 /*
725 * Skip embryonic processes.
726 */
727 if (p->p_stat == SIDL)
728 continue;
729 /*
730 * TODO - make more efficient (see notes below).
731 * do by session.
732 */
733 switch (name[0]) {
734
735 case KERN_PROC_PID:
736 /* could do this with just a lookup */
737 if (p->p_pid != (pid_t)name[1])
738 continue;
739 break;
740
741 case KERN_PROC_PGRP:
742 /* could do this by traversing pgrp */
743 if (p->p_pgrp->pg_id != (pid_t)name[1])
744 continue;
745 break;
746
747 case KERN_PROC_TTY:
748 if ((p->p_flag & P_CONTROLT) == 0 ||
749 p->p_session->s_ttyp == NULL ||
750 p->p_session->s_ttyp->t_dev != (dev_t)name[1])
751 continue;
752 break;
753
754 case KERN_PROC_UID:
755 if (p->p_ucred->cr_uid != (uid_t)name[1])
756 continue;
757 break;
758
759 case KERN_PROC_RUID:
760 if (p->p_cred->p_ruid != (uid_t)name[1])
761 continue;
762 break;
763 }
764 if (buflen >= sizeof(struct kinfo_proc)) {
765 fill_eproc(p, &eproc);
766 error = copyout((caddr_t)p, &dp->kp_proc,
767 sizeof(struct proc));
768 if (error)
769 return (error);
770 error = copyout((caddr_t)&eproc, &dp->kp_eproc,
771 sizeof(eproc));
772 if (error)
773 return (error);
774 dp++;
775 buflen -= sizeof(struct kinfo_proc);
776 }
777 needed += sizeof(struct kinfo_proc);
778 }
779 pd++;
780 if (pd->pd_list != NULL)
781 goto again;
782
783 if (where != NULL) {
784 *sizep = (caddr_t)dp - where;
785 if (needed > *sizep)
786 return (ENOMEM);
787 } else {
788 needed += KERN_PROCSLOP;
789 *sizep = needed;
790 }
791 return (0);
792 }
793
794 /*
795 * Fill in an eproc structure for the specified process.
796 */
797 void
798 fill_eproc(p, ep)
799 register struct proc *p;
800 register struct eproc *ep;
801 {
802 register struct tty *tp;
803
804 ep->e_paddr = p;
805 ep->e_sess = p->p_pgrp->pg_session;
806 ep->e_pcred = *p->p_cred;
807 ep->e_ucred = *p->p_ucred;
808 if (p->p_stat == SIDL || p->p_stat == SZOMB) {
809 ep->e_vm.vm_rssize = 0;
810 ep->e_vm.vm_tsize = 0;
811 ep->e_vm.vm_dsize = 0;
812 ep->e_vm.vm_ssize = 0;
813 /* ep->e_vm.vm_pmap = XXX; */
814 } else {
815 register struct vmspace *vm = p->p_vmspace;
816
817 ep->e_vm.vm_rssize = vm_resident_count(vm);
818 ep->e_vm.vm_tsize = vm->vm_tsize;
819 ep->e_vm.vm_dsize = vm->vm_dsize;
820 ep->e_vm.vm_ssize = vm->vm_ssize;
821 }
822 if (p->p_pptr)
823 ep->e_ppid = p->p_pptr->p_pid;
824 else
825 ep->e_ppid = 0;
826 ep->e_pgid = p->p_pgrp->pg_id;
827 ep->e_sid = ep->e_sess->s_sid;
828 ep->e_jobc = p->p_pgrp->pg_jobc;
829 if ((p->p_flag & P_CONTROLT) &&
830 (tp = ep->e_sess->s_ttyp)) {
831 ep->e_tdev = tp->t_dev;
832 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
833 ep->e_tsess = tp->t_session;
834 } else
835 ep->e_tdev = NODEV;
836 if (p->p_wmesg)
837 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
838 ep->e_xsize = ep->e_xrssize = 0;
839 ep->e_xccount = ep->e_xswrss = 0;
840 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
841 if (SESS_LEADER(p))
842 ep->e_flag |= EPROC_SLEADER;
843 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
844 }
845
846