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