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