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