kern_sysctl.c revision 1.49 1 /* $NetBSD: kern_sysctl.c,v 1.49 1999/07/22 21:08:32 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 if (uvm_vslock(p, SCARG(uap, old), oldlen,
171 VM_PROT_NONE) != KERN_SUCCESS) {
172 memlock.sl_lock = 0;
173 if (memlock.sl_want) {
174 memlock.sl_want = 0;
175 wakeup((caddr_t)&memlock);
176 return (EFAULT);
177 }
178 }
179 }
180 savelen = oldlen;
181 }
182 error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
183 &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
184 if (SCARG(uap, old) != NULL) {
185 if (dolock)
186 uvm_vsunlock(p, SCARG(uap, old), savelen);
187 memlock.sl_lock = 0;
188 if (memlock.sl_want) {
189 memlock.sl_want = 0;
190 wakeup((caddr_t)&memlock);
191 }
192 }
193 if (error)
194 return (error);
195 if (SCARG(uap, oldlenp))
196 error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
197 return (error);
198 }
199
200 /*
201 * Attributes stored in the kernel.
202 */
203 char hostname[MAXHOSTNAMELEN];
204 int hostnamelen;
205 char domainname[MAXHOSTNAMELEN];
206 int domainnamelen;
207 long hostid;
208 #ifdef INSECURE
209 int securelevel = -1;
210 #else
211 int securelevel = 0;
212 #endif
213 #ifdef SHORTCORENAME
214 int shortcorename = 1;
215 #else
216 int shortcorename = 0;
217 #endif
218
219 /*
220 * kernel related system variables.
221 */
222 int
223 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
224 int *name;
225 u_int namelen;
226 void *oldp;
227 size_t *oldlenp;
228 void *newp;
229 size_t newlen;
230 struct proc *p;
231 {
232 int error, level, inthostid;
233 int old_autonicetime;
234 int old_vnodes;
235 int old_shortcorename;
236 extern char ostype[], osrelease[], version[];
237
238 /* All sysctl names at this level, except for a few, are terminal. */
239 switch (name[0]) {
240 case KERN_PROC:
241 case KERN_PROF:
242 case KERN_MBUF:
243 /* Not terminal. */
244 break;
245 default:
246 if (namelen != 1)
247 return (ENOTDIR); /* overloaded */
248 }
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 case KERN_MBUF:
398 return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
399 newp, newlen));
400 case KERN_MAPPED_FILES:
401 return (sysctl_rdint(oldp, oldlenp, newp, 1));
402 case KERN_MEMLOCK:
403 return (sysctl_rdint(oldp, oldlenp, newp, 1));
404 case KERN_MEMLOCK_RANGE:
405 return (sysctl_rdint(oldp, oldlenp, newp, 1));
406 case KERN_MEMORY_PROTECTION:
407 return (sysctl_rdint(oldp, oldlenp, newp, 1));
408 default:
409 return (EOPNOTSUPP);
410 }
411 /* NOTREACHED */
412 }
413
414 /*
415 * hardware related system variables.
416 */
417 int
418 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
419 int *name;
420 u_int namelen;
421 void *oldp;
422 size_t *oldlenp;
423 void *newp;
424 size_t newlen;
425 struct proc *p;
426 {
427 extern char machine[], machine_arch[], cpu_model[];
428
429 /* all sysctl names at this level are terminal */
430 if (namelen != 1)
431 return (ENOTDIR); /* overloaded */
432
433 switch (name[0]) {
434 case HW_MACHINE:
435 return (sysctl_rdstring(oldp, oldlenp, newp, machine));
436 case HW_MACHINE_ARCH:
437 return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
438 case HW_MODEL:
439 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
440 case HW_NCPU:
441 return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */
442 case HW_BYTEORDER:
443 return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
444 case HW_PHYSMEM:
445 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
446 case HW_USERMEM:
447 return (sysctl_rdint(oldp, oldlenp, newp,
448 ctob(physmem - uvmexp.wired)));
449 case HW_PAGESIZE:
450 return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
451 default:
452 return (EOPNOTSUPP);
453 }
454 /* NOTREACHED */
455 }
456
457 #ifdef DEBUG
458 /*
459 * Debugging related system variables.
460 */
461 struct ctldebug debug0, debug1, debug2, debug3, debug4;
462 struct ctldebug debug5, debug6, debug7, debug8, debug9;
463 struct ctldebug debug10, debug11, debug12, debug13, debug14;
464 struct ctldebug debug15, debug16, debug17, debug18, debug19;
465 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
466 &debug0, &debug1, &debug2, &debug3, &debug4,
467 &debug5, &debug6, &debug7, &debug8, &debug9,
468 &debug10, &debug11, &debug12, &debug13, &debug14,
469 &debug15, &debug16, &debug17, &debug18, &debug19,
470 };
471 int
472 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
473 int *name;
474 u_int namelen;
475 void *oldp;
476 size_t *oldlenp;
477 void *newp;
478 size_t newlen;
479 struct proc *p;
480 {
481 struct ctldebug *cdp;
482
483 /* all sysctl names at this level are name and field */
484 if (namelen != 2)
485 return (ENOTDIR); /* overloaded */
486 cdp = debugvars[name[0]];
487 if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
488 return (EOPNOTSUPP);
489 switch (name[1]) {
490 case CTL_DEBUG_NAME:
491 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
492 case CTL_DEBUG_VALUE:
493 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
494 default:
495 return (EOPNOTSUPP);
496 }
497 /* NOTREACHED */
498 }
499 #endif /* DEBUG */
500
501 /*
502 * Validate parameters and get old / set new parameters
503 * for an integer-valued sysctl function.
504 */
505 int
506 sysctl_int(oldp, oldlenp, newp, newlen, valp)
507 void *oldp;
508 size_t *oldlenp;
509 void *newp;
510 size_t newlen;
511 int *valp;
512 {
513 int error = 0;
514
515 if (oldp && *oldlenp < sizeof(int))
516 return (ENOMEM);
517 if (newp && newlen != sizeof(int))
518 return (EINVAL);
519 *oldlenp = sizeof(int);
520 if (oldp)
521 error = copyout(valp, oldp, sizeof(int));
522 if (error == 0 && newp)
523 error = copyin(newp, valp, sizeof(int));
524 return (error);
525 }
526
527 /*
528 * As above, but read-only.
529 */
530 int
531 sysctl_rdint(oldp, oldlenp, newp, val)
532 void *oldp;
533 size_t *oldlenp;
534 void *newp;
535 int val;
536 {
537 int error = 0;
538
539 if (oldp && *oldlenp < sizeof(int))
540 return (ENOMEM);
541 if (newp)
542 return (EPERM);
543 *oldlenp = sizeof(int);
544 if (oldp)
545 error = copyout((caddr_t)&val, oldp, sizeof(int));
546 return (error);
547 }
548
549 /*
550 * Validate parameters and get old / set new parameters
551 * for a string-valued sysctl function.
552 */
553 int
554 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
555 void *oldp;
556 size_t *oldlenp;
557 void *newp;
558 size_t newlen;
559 char *str;
560 int maxlen;
561 {
562 int len, error = 0;
563
564 len = strlen(str) + 1;
565 if (oldp && *oldlenp < len)
566 return (ENOMEM);
567 if (newp && newlen >= maxlen)
568 return (EINVAL);
569 if (oldp) {
570 *oldlenp = len;
571 error = copyout(str, oldp, len);
572 }
573 if (error == 0 && newp) {
574 error = copyin(newp, str, newlen);
575 str[newlen] = 0;
576 }
577 return (error);
578 }
579
580 /*
581 * As above, but read-only.
582 */
583 int
584 sysctl_rdstring(oldp, oldlenp, newp, str)
585 void *oldp;
586 size_t *oldlenp;
587 void *newp;
588 char *str;
589 {
590 int len, error = 0;
591
592 len = strlen(str) + 1;
593 if (oldp && *oldlenp < len)
594 return (ENOMEM);
595 if (newp)
596 return (EPERM);
597 *oldlenp = len;
598 if (oldp)
599 error = copyout(str, oldp, len);
600 return (error);
601 }
602
603 /*
604 * Validate parameters and get old / set new parameters
605 * for a structure oriented sysctl function.
606 */
607 int
608 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
609 void *oldp;
610 size_t *oldlenp;
611 void *newp;
612 size_t newlen;
613 void *sp;
614 int len;
615 {
616 int error = 0;
617
618 if (oldp && *oldlenp < len)
619 return (ENOMEM);
620 if (newp && newlen > len)
621 return (EINVAL);
622 if (oldp) {
623 *oldlenp = len;
624 error = copyout(sp, oldp, len);
625 }
626 if (error == 0 && newp)
627 error = copyin(newp, sp, len);
628 return (error);
629 }
630
631 /*
632 * Validate parameters and get old parameters
633 * for a structure oriented sysctl function.
634 */
635 int
636 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
637 void *oldp;
638 size_t *oldlenp;
639 void *newp, *sp;
640 int len;
641 {
642 int error = 0;
643
644 if (oldp && *oldlenp < len)
645 return (ENOMEM);
646 if (newp)
647 return (EPERM);
648 *oldlenp = len;
649 if (oldp)
650 error = copyout(sp, oldp, len);
651 return (error);
652 }
653
654 /*
655 * Get file structures.
656 */
657 int
658 sysctl_file(where, sizep)
659 char *where;
660 size_t *sizep;
661 {
662 int buflen, error;
663 struct file *fp;
664 char *start = where;
665
666 buflen = *sizep;
667 if (where == NULL) {
668 /*
669 * overestimate by 10 files
670 */
671 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
672 return (0);
673 }
674
675 /*
676 * first copyout filehead
677 */
678 if (buflen < sizeof(filehead)) {
679 *sizep = 0;
680 return (0);
681 }
682 error = copyout((caddr_t)&filehead, where, sizeof(filehead));
683 if (error)
684 return (error);
685 buflen -= sizeof(filehead);
686 where += sizeof(filehead);
687
688 /*
689 * followed by an array of file structures
690 */
691 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
692 if (buflen < sizeof(struct file)) {
693 *sizep = where - start;
694 return (ENOMEM);
695 }
696 error = copyout((caddr_t)fp, where, sizeof(struct file));
697 if (error)
698 return (error);
699 buflen -= sizeof(struct file);
700 where += sizeof(struct file);
701 }
702 *sizep = where - start;
703 return (0);
704 }
705
706 /*
707 * try over estimating by 5 procs
708 */
709 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
710
711 int
712 sysctl_doproc(name, namelen, where, sizep)
713 int *name;
714 u_int namelen;
715 char *where;
716 size_t *sizep;
717 {
718 register struct proc *p;
719 register struct kinfo_proc *dp = (struct kinfo_proc *)where;
720 register int needed = 0;
721 int buflen = where != NULL ? *sizep : 0;
722 const struct proclist_desc *pd;
723 struct eproc eproc;
724 int error = 0;
725
726 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
727 return (EINVAL);
728
729 proclist_lock_read(0);
730
731 pd = proclists;
732 again:
733 for (p = LIST_FIRST(pd->pd_list); p != NULL;
734 p = LIST_NEXT(p, p_list)) {
735 /*
736 * Skip embryonic processes.
737 */
738 if (p->p_stat == SIDL)
739 continue;
740 /*
741 * TODO - make more efficient (see notes below).
742 * do by session.
743 */
744 switch (name[0]) {
745
746 case KERN_PROC_PID:
747 /* could do this with just a lookup */
748 if (p->p_pid != (pid_t)name[1])
749 continue;
750 break;
751
752 case KERN_PROC_PGRP:
753 /* could do this by traversing pgrp */
754 if (p->p_pgrp->pg_id != (pid_t)name[1])
755 continue;
756 break;
757
758 case KERN_PROC_TTY:
759 if ((p->p_flag & P_CONTROLT) == 0 ||
760 p->p_session->s_ttyp == NULL ||
761 p->p_session->s_ttyp->t_dev != (dev_t)name[1])
762 continue;
763 break;
764
765 case KERN_PROC_UID:
766 if (p->p_ucred->cr_uid != (uid_t)name[1])
767 continue;
768 break;
769
770 case KERN_PROC_RUID:
771 if (p->p_cred->p_ruid != (uid_t)name[1])
772 continue;
773 break;
774 }
775 if (buflen >= sizeof(struct kinfo_proc)) {
776 fill_eproc(p, &eproc);
777 error = copyout((caddr_t)p, &dp->kp_proc,
778 sizeof(struct proc));
779 if (error)
780 return (error);
781 error = copyout((caddr_t)&eproc, &dp->kp_eproc,
782 sizeof(eproc));
783 if (error)
784 return (error);
785 dp++;
786 buflen -= sizeof(struct kinfo_proc);
787 }
788 needed += sizeof(struct kinfo_proc);
789 }
790 pd++;
791 if (pd->pd_list != NULL)
792 goto again;
793 proclist_unlock_read();
794
795 if (where != NULL) {
796 *sizep = (caddr_t)dp - where;
797 if (needed > *sizep)
798 return (ENOMEM);
799 } else {
800 needed += KERN_PROCSLOP;
801 *sizep = needed;
802 }
803 return (0);
804 }
805
806 /*
807 * Fill in an eproc structure for the specified process.
808 */
809 void
810 fill_eproc(p, ep)
811 register struct proc *p;
812 register struct eproc *ep;
813 {
814 register struct tty *tp;
815
816 ep->e_paddr = p;
817 ep->e_sess = p->p_pgrp->pg_session;
818 ep->e_pcred = *p->p_cred;
819 ep->e_ucred = *p->p_ucred;
820 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
821 ep->e_vm.vm_rssize = 0;
822 ep->e_vm.vm_tsize = 0;
823 ep->e_vm.vm_dsize = 0;
824 ep->e_vm.vm_ssize = 0;
825 /* ep->e_vm.vm_pmap = XXX; */
826 } else {
827 register struct vmspace *vm = p->p_vmspace;
828
829 ep->e_vm.vm_rssize = vm_resident_count(vm);
830 ep->e_vm.vm_tsize = vm->vm_tsize;
831 ep->e_vm.vm_dsize = vm->vm_dsize;
832 ep->e_vm.vm_ssize = vm->vm_ssize;
833 }
834 if (p->p_pptr)
835 ep->e_ppid = p->p_pptr->p_pid;
836 else
837 ep->e_ppid = 0;
838 ep->e_pgid = p->p_pgrp->pg_id;
839 ep->e_sid = ep->e_sess->s_sid;
840 ep->e_jobc = p->p_pgrp->pg_jobc;
841 if ((p->p_flag & P_CONTROLT) &&
842 (tp = ep->e_sess->s_ttyp)) {
843 ep->e_tdev = tp->t_dev;
844 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
845 ep->e_tsess = tp->t_session;
846 } else
847 ep->e_tdev = NODEV;
848 if (p->p_wmesg)
849 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
850 ep->e_xsize = ep->e_xrssize = 0;
851 ep->e_xccount = ep->e_xswrss = 0;
852 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
853 if (SESS_LEADER(p))
854 ep->e_flag |= EPROC_SLEADER;
855 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
856 }
857