kern_sysctl.c revision 1.109 1 /* $NetBSD: kern_sysctl.c,v 1.109 2002/07/02 17:06:17 itojun 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 <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.109 2002/07/02 17:06:17 itojun Exp $");
47
48 #include "opt_ddb.h"
49 #include "opt_insecure.h"
50 #include "opt_defcorename.h"
51 #include "opt_pipe.h"
52 #include "opt_sysv.h"
53 #include "pty.h"
54 #include "rnd.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/buf.h>
60 #include <sys/device.h>
61 #include <sys/disklabel.h>
62 #include <sys/dkstat.h>
63 #include <sys/exec.h>
64 #include <sys/file.h>
65 #include <sys/ioctl.h>
66 #include <sys/malloc.h>
67 #include <sys/mount.h>
68 #include <sys/msgbuf.h>
69 #include <sys/pool.h>
70 #include <sys/proc.h>
71 #include <sys/resource.h>
72 #include <sys/resourcevar.h>
73 #include <sys/syscallargs.h>
74 #include <sys/tty.h>
75 #include <sys/unistd.h>
76 #include <sys/vnode.h>
77 #include <sys/socketvar.h>
78 #define __SYSCTL_PRIVATE
79 #include <sys/sysctl.h>
80 #include <sys/lock.h>
81 #include <sys/namei.h>
82
83 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
84 #include <sys/ipc.h>
85 #endif
86 #ifdef SYSVMSG
87 #include <sys/msg.h>
88 #endif
89 #ifdef SYSVSEM
90 #include <sys/sem.h>
91 #endif
92 #ifdef SYSVSHM
93 #include <sys/shm.h>
94 #endif
95
96 #include <dev/cons.h>
97
98 #if defined(DDB)
99 #include <ddb/ddbvar.h>
100 #endif
101
102 #ifndef PIPE_SOCKETPAIR
103 #include <sys/pipe.h>
104 #endif
105
106 #if NRND > 0
107 #include <sys/rnd.h>
108 #endif
109
110 #define PTRTOINT64(foo) ((u_int64_t)(uintptr_t)(foo))
111
112 static int sysctl_file(void *, size_t *);
113 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
114 static int sysctl_sysvipc(int *, u_int, void *, size_t *);
115 #endif
116 static int sysctl_msgbuf(void *, size_t *);
117 static int sysctl_doeproc(int *, u_int, void *, size_t *);
118 static int sysctl_dotkstat(int *, u_int, void *, size_t *, void *);
119 #ifdef MULTIPROCESSOR
120 static int sysctl_docptime(void *, size_t *, void *);
121 static int sysctl_ncpus(void);
122 #endif
123 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
124 static int sysctl_procargs(int *, u_int, void *, size_t *, struct proc *);
125 #if NPTY > 0
126 static int sysctl_pty(void *, size_t *, void *, size_t);
127 #endif
128
129 /*
130 * The `sysctl_memlock' is intended to keep too many processes from
131 * locking down memory by doing sysctls at once. Whether or not this
132 * is really a good idea to worry about it probably a subject of some
133 * debate.
134 */
135 struct lock sysctl_memlock;
136
137 void
138 sysctl_init(void)
139 {
140
141 lockinit(&sysctl_memlock, PRIBIO|PCATCH, "sysctl", 0, 0);
142 }
143
144 int
145 sys___sysctl(struct proc *p, void *v, register_t *retval)
146 {
147 struct sys___sysctl_args /* {
148 syscallarg(int *) name;
149 syscallarg(u_int) namelen;
150 syscallarg(void *) old;
151 syscallarg(size_t *) oldlenp;
152 syscallarg(void *) new;
153 syscallarg(size_t) newlen;
154 } */ *uap = v;
155 int error;
156 size_t savelen = 0, oldlen = 0;
157 sysctlfn *fn;
158 int name[CTL_MAXNAME];
159 size_t *oldlenp;
160
161 /*
162 * all top-level sysctl names are non-terminal
163 */
164 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
165 return (EINVAL);
166 error = copyin(SCARG(uap, name), &name,
167 SCARG(uap, namelen) * sizeof(int));
168 if (error)
169 return (error);
170
171 /*
172 * For all but CTL_PROC, must be root to change a value.
173 * For CTL_PROC, must be root, or owner of the proc (and not suid),
174 * this is checked in proc_sysctl() (once we know the targer proc).
175 */
176 if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
177 (error = suser(p->p_ucred, &p->p_acflag)))
178 return error;
179
180 switch (name[0]) {
181 case CTL_KERN:
182 fn = kern_sysctl;
183 break;
184 case CTL_HW:
185 fn = hw_sysctl;
186 break;
187 case CTL_VM:
188 fn = uvm_sysctl;
189 break;
190 case CTL_NET:
191 fn = net_sysctl;
192 break;
193 case CTL_VFS:
194 fn = vfs_sysctl;
195 break;
196 case CTL_MACHDEP:
197 fn = cpu_sysctl;
198 break;
199 #ifdef DEBUG
200 case CTL_DEBUG:
201 fn = debug_sysctl;
202 break;
203 #endif
204 #ifdef DDB
205 case CTL_DDB:
206 fn = ddb_sysctl;
207 break;
208 #endif
209 case CTL_PROC:
210 fn = proc_sysctl;
211 break;
212
213 case CTL_EMUL:
214 fn = emul_sysctl;
215 break;
216 default:
217 return (EOPNOTSUPP);
218 }
219
220 /*
221 * XXX Hey, we wire `old', but what about `new'?
222 */
223
224 oldlenp = SCARG(uap, oldlenp);
225 if (oldlenp) {
226 if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
227 return (error);
228 oldlenp = &oldlen;
229 }
230 if (SCARG(uap, old) != NULL) {
231 error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
232 if (error)
233 return (error);
234 error = uvm_vslock(p, SCARG(uap, old), oldlen, VM_PROT_WRITE);
235 if (error) {
236 (void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
237 return error;
238 }
239 savelen = oldlen;
240 }
241 error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
242 oldlenp, SCARG(uap, new), SCARG(uap, newlen), p);
243 if (SCARG(uap, old) != NULL) {
244 uvm_vsunlock(p, SCARG(uap, old), savelen);
245 (void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
246 }
247 if (error)
248 return (error);
249 if (SCARG(uap, oldlenp))
250 error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
251 return (error);
252 }
253
254 /*
255 * Attributes stored in the kernel.
256 */
257 char hostname[MAXHOSTNAMELEN];
258 int hostnamelen;
259
260 char domainname[MAXHOSTNAMELEN];
261 int domainnamelen;
262
263 long hostid;
264
265 #ifdef INSECURE
266 int securelevel = -1;
267 #else
268 int securelevel = 0;
269 #endif
270
271 #ifndef DEFCORENAME
272 #define DEFCORENAME "%n.core"
273 #endif
274 char defcorename[MAXPATHLEN] = DEFCORENAME;
275 int defcorenamelen = sizeof(DEFCORENAME);
276
277 extern int kern_logsigexit;
278 extern fixpt_t ccpu;
279
280 #ifndef MULTIPROCESSOR
281 #define sysctl_ncpus() 1
282 #endif
283
284 #ifdef MULTIPROCESSOR
285
286 #ifndef CPU_INFO_FOREACH
287 #define CPU_INFO_ITERATOR int
288 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
289 #endif
290
291 static int
292 sysctl_docptime(void *oldp, size_t *oldlenp, void *newp)
293 {
294 u_int64_t cp_time[CPUSTATES];
295 int i;
296 struct cpu_info *ci;
297 CPU_INFO_ITERATOR cii;
298
299 for (i=0; i<CPUSTATES; i++)
300 cp_time[i] = 0;
301
302 for (CPU_INFO_FOREACH(cii, ci)) {
303 for (i=0; i<CPUSTATES; i++)
304 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
305 }
306 return (sysctl_rdstruct(oldp, oldlenp, newp,
307 cp_time, sizeof(cp_time)));
308 }
309
310 static int
311 sysctl_ncpus(void)
312 {
313 struct cpu_info *ci;
314 CPU_INFO_ITERATOR cii;
315
316 int ncpus = 0;
317 for (CPU_INFO_FOREACH(cii, ci))
318 ncpus++;
319 return ncpus;
320 }
321
322 #endif
323
324 /*
325 * kernel related system variables.
326 */
327 int
328 kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
329 void *newp, size_t newlen, struct proc *p)
330 {
331 int error, level, inthostid;
332 int old_autonicetime;
333 int old_vnodes;
334 dev_t consdev;
335 #if NRND > 0
336 int v;
337 #endif
338
339 /* All sysctl names at this level, except for a few, are terminal. */
340 switch (name[0]) {
341 case KERN_PROC:
342 case KERN_PROC2:
343 case KERN_PROF:
344 case KERN_MBUF:
345 case KERN_PROC_ARGS:
346 case KERN_SYSVIPC_INFO:
347 case KERN_PIPE:
348 case KERN_TKSTAT:
349 /* Not terminal. */
350 break;
351 default:
352 if (namelen != 1)
353 return (ENOTDIR); /* overloaded */
354 }
355
356 switch (name[0]) {
357 case KERN_OSTYPE:
358 return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
359 case KERN_OSRELEASE:
360 return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
361 case KERN_OSREV:
362 return (sysctl_rdint(oldp, oldlenp, newp, __NetBSD_Version__));
363 case KERN_VERSION:
364 return (sysctl_rdstring(oldp, oldlenp, newp, version));
365 case KERN_MAXVNODES:
366 old_vnodes = desiredvnodes;
367 error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
368 if (newp && !error) {
369 if (old_vnodes > desiredvnodes) {
370 desiredvnodes = old_vnodes;
371 return (EINVAL);
372 }
373 vfs_reinit();
374 nchreinit();
375 }
376 return (error);
377 case KERN_MAXPROC:
378 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
379 case KERN_MAXFILES:
380 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
381 case KERN_ARGMAX:
382 return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
383 case KERN_SECURELVL:
384 level = securelevel;
385 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
386 newp == NULL)
387 return (error);
388 if (level < securelevel && p->p_pid != 1)
389 return (EPERM);
390 securelevel = level;
391 return (0);
392 case KERN_HOSTNAME:
393 error = sysctl_string(oldp, oldlenp, newp, newlen,
394 hostname, sizeof(hostname));
395 if (newp && !error)
396 hostnamelen = newlen;
397 return (error);
398 case KERN_DOMAINNAME:
399 error = sysctl_string(oldp, oldlenp, newp, newlen,
400 domainname, sizeof(domainname));
401 if (newp && !error)
402 domainnamelen = newlen;
403 return (error);
404 case KERN_HOSTID:
405 inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
406 error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
407 if (newp && !error)
408 hostid = inthostid;
409 return (error);
410 case KERN_CLOCKRATE:
411 return (sysctl_clockrate(oldp, oldlenp));
412 case KERN_BOOTTIME:
413 return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
414 sizeof(struct timeval)));
415 case KERN_VNODE:
416 return (sysctl_vnode(oldp, oldlenp, p));
417 case KERN_PROC:
418 case KERN_PROC2:
419 return (sysctl_doeproc(name, namelen, oldp, oldlenp));
420 case KERN_PROC_ARGS:
421 return (sysctl_procargs(name + 1, namelen - 1,
422 oldp, oldlenp, p));
423 case KERN_FILE:
424 return (sysctl_file(oldp, oldlenp));
425 #ifdef GPROF
426 case KERN_PROF:
427 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
428 newp, newlen));
429 #endif
430 case KERN_POSIX1:
431 return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
432 case KERN_NGROUPS:
433 return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
434 case KERN_JOB_CONTROL:
435 return (sysctl_rdint(oldp, oldlenp, newp, 1));
436 case KERN_SAVED_IDS:
437 #ifdef _POSIX_SAVED_IDS
438 return (sysctl_rdint(oldp, oldlenp, newp, 1));
439 #else
440 return (sysctl_rdint(oldp, oldlenp, newp, 0));
441 #endif
442 case KERN_MAXPARTITIONS:
443 return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
444 case KERN_RAWPARTITION:
445 return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
446 #ifdef NTP
447 case KERN_NTPTIME:
448 return (sysctl_ntptime(oldp, oldlenp));
449 #endif
450 case KERN_AUTONICETIME:
451 old_autonicetime = autonicetime;
452 error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
453 if (autonicetime < 0)
454 autonicetime = old_autonicetime;
455 return (error);
456 case KERN_AUTONICEVAL:
457 error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
458 if (autoniceval < PRIO_MIN)
459 autoniceval = PRIO_MIN;
460 if (autoniceval > PRIO_MAX)
461 autoniceval = PRIO_MAX;
462 return (error);
463 case KERN_RTC_OFFSET:
464 return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
465 case KERN_ROOT_DEVICE:
466 return (sysctl_rdstring(oldp, oldlenp, newp,
467 root_device->dv_xname));
468 case KERN_MSGBUFSIZE:
469 /*
470 * deal with cases where the message buffer has
471 * become corrupted.
472 */
473 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
474 msgbufenabled = 0;
475 return (ENXIO);
476 }
477 return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
478 case KERN_FSYNC:
479 return (sysctl_rdint(oldp, oldlenp, newp, 1));
480 case KERN_SYSVMSG:
481 #ifdef SYSVMSG
482 return (sysctl_rdint(oldp, oldlenp, newp, 1));
483 #else
484 return (sysctl_rdint(oldp, oldlenp, newp, 0));
485 #endif
486 case KERN_SYSVSEM:
487 #ifdef SYSVSEM
488 return (sysctl_rdint(oldp, oldlenp, newp, 1));
489 #else
490 return (sysctl_rdint(oldp, oldlenp, newp, 0));
491 #endif
492 case KERN_SYSVSHM:
493 #ifdef SYSVSHM
494 return (sysctl_rdint(oldp, oldlenp, newp, 1));
495 #else
496 return (sysctl_rdint(oldp, oldlenp, newp, 0));
497 #endif
498 case KERN_DEFCORENAME:
499 if (newp && newlen < 1)
500 return (EINVAL);
501 error = sysctl_string(oldp, oldlenp, newp, newlen,
502 defcorename, sizeof(defcorename));
503 if (newp && !error)
504 defcorenamelen = newlen;
505 return (error);
506 case KERN_SYNCHRONIZED_IO:
507 return (sysctl_rdint(oldp, oldlenp, newp, 1));
508 case KERN_IOV_MAX:
509 return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
510 case KERN_MBUF:
511 return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
512 newp, newlen));
513 case KERN_MAPPED_FILES:
514 return (sysctl_rdint(oldp, oldlenp, newp, 1));
515 case KERN_MEMLOCK:
516 return (sysctl_rdint(oldp, oldlenp, newp, 1));
517 case KERN_MEMLOCK_RANGE:
518 return (sysctl_rdint(oldp, oldlenp, newp, 1));
519 case KERN_MEMORY_PROTECTION:
520 return (sysctl_rdint(oldp, oldlenp, newp, 1));
521 case KERN_LOGIN_NAME_MAX:
522 return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
523 case KERN_LOGSIGEXIT:
524 return (sysctl_int(oldp, oldlenp, newp, newlen,
525 &kern_logsigexit));
526 case KERN_FSCALE:
527 return (sysctl_rdint(oldp, oldlenp, newp, FSCALE));
528 case KERN_CCPU:
529 return (sysctl_rdint(oldp, oldlenp, newp, ccpu));
530 case KERN_CP_TIME:
531 #ifndef MULTIPROCESSOR
532 return (sysctl_rdstruct(oldp, oldlenp, newp,
533 curcpu()->ci_schedstate.spc_cp_time,
534 sizeof(curcpu()->ci_schedstate.spc_cp_time)));
535 #else
536 return (sysctl_docptime(oldp, oldlenp, newp));
537 #endif
538 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
539 case KERN_SYSVIPC_INFO:
540 return (sysctl_sysvipc(name + 1, namelen - 1, oldp, oldlenp));
541 #endif
542 case KERN_MSGBUF:
543 return (sysctl_msgbuf(oldp, oldlenp));
544 case KERN_CONSDEV:
545 if (cn_tab != NULL)
546 consdev = cn_tab->cn_dev;
547 else
548 consdev = NODEV;
549 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
550 sizeof consdev));
551 #if NPTY > 0
552 case KERN_MAXPTYS:
553 return sysctl_pty(oldp, oldlenp, newp, newlen);
554 #endif
555 #ifndef PIPE_SOCKETPAIR
556 case KERN_PIPE:
557 return (sysctl_dopipe(name + 1, namelen - 1, oldp, oldlenp,
558 newp, newlen));
559 #endif
560 case KERN_MAXPHYS:
561 return (sysctl_rdint(oldp, oldlenp, newp, MAXPHYS));
562 case KERN_SBMAX:
563 {
564 int new_sbmax = sb_max;
565
566 error = sysctl_int(oldp, oldlenp, newp, newlen, &new_sbmax);
567 if (newp && !error) {
568 if (new_sbmax < (16 * 1024)) /* sanity */
569 return (EINVAL);
570 sb_max = new_sbmax;
571 }
572 return (error);
573 }
574 case KERN_TKSTAT:
575 return (sysctl_dotkstat(name + 1, namelen - 1, oldp, oldlenp,
576 newp));
577 case KERN_MONOTONIC_CLOCK: /* XXX _POSIX_VERSION */
578 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
579 case KERN_URND:
580 #if NRND > 0
581 if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) ==
582 sizeof(v))
583 return (sysctl_rdint(oldp, oldlenp, newp, v));
584 else
585 return (EIO); /*XXX*/
586 #else
587 return (EOPNOTSUPP);
588 #endif
589 default:
590 return (EOPNOTSUPP);
591 }
592 /* NOTREACHED */
593 }
594
595 /*
596 * hardware related system variables.
597 */
598 int
599 hw_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
600 void *newp, size_t newlen, struct proc *p)
601 {
602
603 /* All sysctl names at this level, except for a few, are terminal. */
604 switch (name[0]) {
605 case HW_DISKSTATS:
606 /* Not terminal. */
607 break;
608 default:
609 if (namelen != 1)
610 return (ENOTDIR); /* overloaded */
611 }
612
613 switch (name[0]) {
614 case HW_MACHINE:
615 return (sysctl_rdstring(oldp, oldlenp, newp, machine));
616 case HW_MACHINE_ARCH:
617 return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
618 case HW_MODEL:
619 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
620 case HW_NCPU:
621 return (sysctl_rdint(oldp, oldlenp, newp, sysctl_ncpus()));
622 case HW_BYTEORDER:
623 return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
624 case HW_PHYSMEM:
625 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
626 case HW_USERMEM:
627 return (sysctl_rdint(oldp, oldlenp, newp,
628 ctob(physmem - uvmexp.wired)));
629 case HW_PAGESIZE:
630 return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
631 case HW_ALIGNBYTES:
632 return (sysctl_rdint(oldp, oldlenp, newp, ALIGNBYTES));
633 case HW_DISKNAMES:
634 return (sysctl_disknames(oldp, oldlenp));
635 case HW_DISKSTATS:
636 return (sysctl_diskstats(name + 1, namelen - 1, oldp, oldlenp));
637 case HW_CNMAGIC: {
638 char magic[CNS_LEN];
639 int error;
640
641 if (oldp)
642 cn_get_magic(magic, CNS_LEN);
643 error = sysctl_string(oldp, oldlenp, newp, newlen,
644 magic, sizeof(magic));
645 if (newp && !error) {
646 error = cn_set_magic(magic);
647 }
648 return (error);
649 }
650 default:
651 return (EOPNOTSUPP);
652 }
653 /* NOTREACHED */
654 }
655
656 #ifdef DEBUG
657 /*
658 * Debugging related system variables.
659 */
660 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
661 struct ctldebug debug5, debug6, debug7, debug8, debug9;
662 struct ctldebug debug10, debug11, debug12, debug13, debug14;
663 struct ctldebug debug15, debug16, debug17, debug18, debug19;
664 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
665 &debug0, &debug1, &debug2, &debug3, &debug4,
666 &debug5, &debug6, &debug7, &debug8, &debug9,
667 &debug10, &debug11, &debug12, &debug13, &debug14,
668 &debug15, &debug16, &debug17, &debug18, &debug19,
669 };
670
671 int
672 debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
673 void *newp, size_t newlen, struct proc *p)
674 {
675 struct ctldebug *cdp;
676
677 /* all sysctl names at this level are name and field */
678 if (namelen != 2)
679 return (ENOTDIR); /* overloaded */
680 if (name[0] >= CTL_DEBUG_MAXID)
681 return (EOPNOTSUPP);
682 cdp = debugvars[name[0]];
683 if (cdp->debugname == 0)
684 return (EOPNOTSUPP);
685 switch (name[1]) {
686 case CTL_DEBUG_NAME:
687 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
688 case CTL_DEBUG_VALUE:
689 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
690 default:
691 return (EOPNOTSUPP);
692 }
693 /* NOTREACHED */
694 }
695 #endif /* DEBUG */
696
697 int
698 proc_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
699 void *newp, size_t newlen, struct proc *p)
700 {
701 struct proc *ptmp = NULL;
702 const struct proclist_desc *pd;
703 int error = 0;
704 struct rlimit alim;
705 struct plimit *newplim;
706 char *tmps = NULL;
707 int i, curlen, len;
708
709 if (namelen < 2)
710 return EINVAL;
711
712 if (name[0] == PROC_CURPROC) {
713 ptmp = p;
714 } else {
715 proclist_lock_read();
716 for (pd = proclists; pd->pd_list != NULL; pd++) {
717 for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
718 ptmp = LIST_NEXT(ptmp, p_list)) {
719 /* Skip embryonic processes. */
720 if (ptmp->p_stat == SIDL)
721 continue;
722 if (ptmp->p_pid == (pid_t)name[0])
723 break;
724 }
725 if (ptmp != NULL)
726 break;
727 }
728 proclist_unlock_read();
729 if (ptmp == NULL)
730 return(ESRCH);
731 if (p->p_ucred->cr_uid != 0) {
732 if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
733 p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
734 return EPERM;
735 if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
736 return EPERM; /* sgid proc */
737 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
738 if (p->p_ucred->cr_groups[i] ==
739 ptmp->p_cred->p_rgid)
740 break;
741 }
742 if (i == p->p_ucred->cr_ngroups)
743 return EPERM;
744 }
745 }
746 if (name[1] == PROC_PID_CORENAME) {
747 if (namelen != 2)
748 return EINVAL;
749 /*
750 * Can't use sysctl_string() here because we may malloc a new
751 * area during the process, so we have to do it by hand.
752 */
753 curlen = strlen(ptmp->p_limit->pl_corename) + 1;
754 if (oldlenp && *oldlenp < curlen) {
755 if (!oldp)
756 *oldlenp = curlen;
757 return (ENOMEM);
758 }
759 if (newp) {
760 if (securelevel > 2)
761 return EPERM;
762 if (newlen > MAXPATHLEN)
763 return ENAMETOOLONG;
764 tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
765 if (tmps == NULL)
766 return ENOMEM;
767 error = copyin(newp, tmps, newlen + 1);
768 tmps[newlen] = '\0';
769 if (error)
770 goto cleanup;
771 /* Enforce to be either 'core' for end with '.core' */
772 if (newlen < 4) { /* c.o.r.e */
773 error = EINVAL;
774 goto cleanup;
775 }
776 len = newlen - 4;
777 if (len > 0) {
778 if (tmps[len - 1] != '.' &&
779 tmps[len - 1] != '/') {
780 error = EINVAL;
781 goto cleanup;
782 }
783 }
784 if (strcmp(&tmps[len], "core") != 0) {
785 error = EINVAL;
786 goto cleanup;
787 }
788 }
789 if (oldp && oldlenp) {
790 *oldlenp = curlen;
791 error = copyout(ptmp->p_limit->pl_corename, oldp,
792 curlen);
793 }
794 if (newp && error == 0) {
795 /* if the 2 strings are identical, don't limcopy() */
796 if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
797 error = 0;
798 goto cleanup;
799 }
800 if (ptmp->p_limit->p_refcnt > 1 &&
801 (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
802 newplim = limcopy(ptmp->p_limit);
803 limfree(ptmp->p_limit);
804 ptmp->p_limit = newplim;
805 }
806 if (ptmp->p_limit->pl_corename != defcorename) {
807 free(ptmp->p_limit->pl_corename, M_TEMP);
808 }
809 ptmp->p_limit->pl_corename = tmps;
810 return (0);
811 }
812 cleanup:
813 if (tmps)
814 free(tmps, M_TEMP);
815 return (error);
816 }
817 if (name[1] == PROC_PID_LIMIT) {
818 if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
819 return EINVAL;
820 memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
821 if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
822 error = sysctl_quad(oldp, oldlenp, newp, newlen,
823 &alim.rlim_max);
824 else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
825 error = sysctl_quad(oldp, oldlenp, newp, newlen,
826 &alim.rlim_cur);
827 else
828 error = EINVAL;
829
830 if (error)
831 return error;
832
833 if (newp)
834 error = dosetrlimit(ptmp, p->p_cred,
835 name[2] - 1, &alim);
836 return error;
837 }
838 return (EINVAL);
839 }
840
841 int
842 emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
843 void *newp, size_t newlen, struct proc *p)
844 {
845 static struct {
846 const char *name;
847 int type;
848 } emulations[] = CTL_EMUL_NAMES;
849 const struct emul *e;
850 const char *ename;
851 #ifdef LKM
852 extern struct lock exec_lock; /* XXX */
853 int error;
854 #else
855 extern int nexecs_builtin;
856 extern const struct execsw execsw_builtin[];
857 int i;
858 #endif
859
860 /* all sysctl names at this level are name and field */
861 if (namelen < 2)
862 return (ENOTDIR); /* overloaded */
863
864 if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
865 return (EOPNOTSUPP);
866
867 ename = emulations[name[0]].name;
868
869 #ifdef LKM
870 lockmgr(&exec_lock, LK_SHARED, NULL);
871 if ((e = emul_search(ename))) {
872 error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
873 newp, newlen, p);
874 } else
875 error = EOPNOTSUPP;
876 lockmgr(&exec_lock, LK_RELEASE, NULL);
877
878 return (error);
879 #else
880 for (i = 0; i < nexecs_builtin; i++) {
881 e = execsw_builtin[i].es_emul;
882 if (e == NULL || strcmp(ename, e->e_name) != 0 ||
883 e->e_sysctl != NULL)
884 continue;
885
886 return (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
887 newp, newlen, p);
888 }
889
890 return (EOPNOTSUPP);
891 #endif
892 }
893 /*
894 * Convenience macros.
895 */
896
897 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) \
898 if (oldlenp) { \
899 if (!oldp) \
900 *oldlenp = len; \
901 else { \
902 if (*oldlenp < len) \
903 return(ENOMEM); \
904 *oldlenp = len; \
905 error = copyout((caddr_t)valp, oldp, len); \
906 } \
907 }
908
909 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
910 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
911
912 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len) \
913 if (newp && newlen != len) \
914 return (EINVAL);
915
916 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ) \
917 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
918
919 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len) \
920 if (error == 0 && newp) \
921 error = copyin(newp, valp, len);
922
923 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ) \
924 SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
925
926 #define SYSCTL_STRING_CORE(oldp, oldlenp, str) \
927 if (oldlenp) { \
928 len = strlen(str) + 1; \
929 if (!oldp) \
930 *oldlenp = len; \
931 else { \
932 if (*oldlenp < len) { \
933 err2 = ENOMEM; \
934 len = *oldlenp; \
935 } else \
936 *oldlenp = len; \
937 error = copyout(str, oldp, len);\
938 if (error == 0) \
939 error = err2; \
940 } \
941 }
942
943 /*
944 * Validate parameters and get old / set new parameters
945 * for an integer-valued sysctl function.
946 */
947 int
948 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
949 {
950 int error = 0;
951
952 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
953 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
954 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
955
956 return (error);
957 }
958
959
960 /*
961 * As above, but read-only.
962 */
963 int
964 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
965 {
966 int error = 0;
967
968 if (newp)
969 return (EPERM);
970
971 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
972
973 return (error);
974 }
975
976 /*
977 * Validate parameters and get old / set new parameters
978 * for an quad-valued sysctl function.
979 */
980 int
981 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
982 quad_t *valp)
983 {
984 int error = 0;
985
986 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
987 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
988 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
989
990 return (error);
991 }
992
993 /*
994 * As above, but read-only.
995 */
996 int
997 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
998 {
999 int error = 0;
1000
1001 if (newp)
1002 return (EPERM);
1003
1004 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
1005
1006 return (error);
1007 }
1008
1009 /*
1010 * Validate parameters and get old / set new parameters
1011 * for a string-valued sysctl function.
1012 */
1013 int
1014 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
1015 int maxlen)
1016 {
1017 int len, error = 0, err2 = 0;
1018
1019 if (newp && newlen >= maxlen)
1020 return (EINVAL);
1021
1022 SYSCTL_STRING_CORE(oldp, oldlenp, str);
1023
1024 if (error == 0 && newp) {
1025 error = copyin(newp, str, newlen);
1026 str[newlen] = 0;
1027 }
1028 return (error);
1029 }
1030
1031 /*
1032 * As above, but read-only.
1033 */
1034 int
1035 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
1036 {
1037 int len, error = 0, err2 = 0;
1038
1039 if (newp)
1040 return (EPERM);
1041
1042 SYSCTL_STRING_CORE(oldp, oldlenp, str);
1043
1044 return (error);
1045 }
1046
1047 /*
1048 * Validate parameters and get old / set new parameters
1049 * for a structure oriented sysctl function.
1050 */
1051 int
1052 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
1053 int len)
1054 {
1055 int error = 0;
1056
1057 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
1058 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1059 SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
1060
1061 return (error);
1062 }
1063
1064 /*
1065 * Validate parameters and get old parameters
1066 * for a structure oriented sysctl function.
1067 */
1068 int
1069 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1070 int len)
1071 {
1072 int error = 0;
1073
1074 if (newp)
1075 return (EPERM);
1076
1077 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1078
1079 return (error);
1080 }
1081
1082 /*
1083 * As above, but can return a truncated result.
1084 */
1085 int
1086 sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1087 int len)
1088 {
1089 int error = 0;
1090
1091 if (newp)
1092 return (EPERM);
1093
1094 len = min(*oldlenp, len);
1095 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1096
1097 return (error);
1098 }
1099
1100 /*
1101 * Get file structures.
1102 */
1103 static int
1104 sysctl_file(void *vwhere, size_t *sizep)
1105 {
1106 int buflen, error;
1107 struct file *fp;
1108 char *start, *where;
1109
1110 start = where = vwhere;
1111 buflen = *sizep;
1112 if (where == NULL) {
1113 /*
1114 * overestimate by 10 files
1115 */
1116 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
1117 return (0);
1118 }
1119
1120 /*
1121 * first copyout filehead
1122 */
1123 if (buflen < sizeof(filehead)) {
1124 *sizep = 0;
1125 return (0);
1126 }
1127 error = copyout((caddr_t)&filehead, where, sizeof(filehead));
1128 if (error)
1129 return (error);
1130 buflen -= sizeof(filehead);
1131 where += sizeof(filehead);
1132
1133 /*
1134 * followed by an array of file structures
1135 */
1136 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
1137 if (buflen < sizeof(struct file)) {
1138 *sizep = where - start;
1139 return (ENOMEM);
1140 }
1141 error = copyout((caddr_t)fp, where, sizeof(struct file));
1142 if (error)
1143 return (error);
1144 buflen -= sizeof(struct file);
1145 where += sizeof(struct file);
1146 }
1147 *sizep = where - start;
1148 return (0);
1149 }
1150
1151 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
1152 #define FILL_PERM(src, dst) do { \
1153 (dst)._key = (src)._key; \
1154 (dst).uid = (src).uid; \
1155 (dst).gid = (src).gid; \
1156 (dst).cuid = (src).cuid; \
1157 (dst).cgid = (src).cgid; \
1158 (dst).mode = (src).mode; \
1159 (dst)._seq = (src)._seq; \
1160 } while (0);
1161 #define FILL_MSG(src, dst) do { \
1162 FILL_PERM((src).msg_perm, (dst).msg_perm); \
1163 (dst).msg_qnum = (src).msg_qnum; \
1164 (dst).msg_qbytes = (src).msg_qbytes; \
1165 (dst)._msg_cbytes = (src)._msg_cbytes; \
1166 (dst).msg_lspid = (src).msg_lspid; \
1167 (dst).msg_lrpid = (src).msg_lrpid; \
1168 (dst).msg_stime = (src).msg_stime; \
1169 (dst).msg_rtime = (src).msg_rtime; \
1170 (dst).msg_ctime = (src).msg_ctime; \
1171 } while (0)
1172 #define FILL_SEM(src, dst) do { \
1173 FILL_PERM((src).sem_perm, (dst).sem_perm); \
1174 (dst).sem_nsems = (src).sem_nsems; \
1175 (dst).sem_otime = (src).sem_otime; \
1176 (dst).sem_ctime = (src).sem_ctime; \
1177 } while (0)
1178 #define FILL_SHM(src, dst) do { \
1179 FILL_PERM((src).shm_perm, (dst).shm_perm); \
1180 (dst).shm_segsz = (src).shm_segsz; \
1181 (dst).shm_lpid = (src).shm_lpid; \
1182 (dst).shm_cpid = (src).shm_cpid; \
1183 (dst).shm_atime = (src).shm_atime; \
1184 (dst).shm_dtime = (src).shm_dtime; \
1185 (dst).shm_ctime = (src).shm_ctime; \
1186 (dst).shm_nattch = (src).shm_nattch; \
1187 } while (0)
1188
1189 static int
1190 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
1191 {
1192 #ifdef SYSVMSG
1193 struct msg_sysctl_info *msgsi;
1194 #endif
1195 #ifdef SYSVSEM
1196 struct sem_sysctl_info *semsi;
1197 #endif
1198 #ifdef SYSVSHM
1199 struct shm_sysctl_info *shmsi;
1200 #endif
1201 size_t infosize, dssize, tsize, buflen;
1202 void *buf = NULL;
1203 char *start;
1204 int32_t nds;
1205 int i, error, ret;
1206
1207 if (namelen != 1)
1208 return (EINVAL);
1209
1210 start = where;
1211 buflen = *sizep;
1212
1213 switch (*name) {
1214 case KERN_SYSVIPC_MSG_INFO:
1215 #ifdef SYSVMSG
1216 infosize = sizeof(msgsi->msginfo);
1217 nds = msginfo.msgmni;
1218 dssize = sizeof(msgsi->msgids[0]);
1219 break;
1220 #else
1221 return (EINVAL);
1222 #endif
1223 case KERN_SYSVIPC_SEM_INFO:
1224 #ifdef SYSVSEM
1225 infosize = sizeof(semsi->seminfo);
1226 nds = seminfo.semmni;
1227 dssize = sizeof(semsi->semids[0]);
1228 break;
1229 #else
1230 return (EINVAL);
1231 #endif
1232 case KERN_SYSVIPC_SHM_INFO:
1233 #ifdef SYSVSHM
1234 infosize = sizeof(shmsi->shminfo);
1235 nds = shminfo.shmmni;
1236 dssize = sizeof(shmsi->shmids[0]);
1237 break;
1238 #else
1239 return (EINVAL);
1240 #endif
1241 default:
1242 return (EINVAL);
1243 }
1244 /*
1245 * Round infosize to 64 bit boundary if requesting more than just
1246 * the info structure or getting the total data size.
1247 */
1248 if (where == NULL || *sizep > infosize)
1249 infosize = ((infosize + 7) / 8) * 8;
1250 tsize = infosize + nds * dssize;
1251
1252 /* Return just the total size required. */
1253 if (where == NULL) {
1254 *sizep = tsize;
1255 return (0);
1256 }
1257
1258 /* Not enough room for even the info struct. */
1259 if (buflen < infosize) {
1260 *sizep = 0;
1261 return (ENOMEM);
1262 }
1263 buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
1264 memset(buf, 0, min(tsize, buflen));
1265
1266 switch (*name) {
1267 #ifdef SYSVMSG
1268 case KERN_SYSVIPC_MSG_INFO:
1269 msgsi = (struct msg_sysctl_info *)buf;
1270 msgsi->msginfo = msginfo;
1271 break;
1272 #endif
1273 #ifdef SYSVSEM
1274 case KERN_SYSVIPC_SEM_INFO:
1275 semsi = (struct sem_sysctl_info *)buf;
1276 semsi->seminfo = seminfo;
1277 break;
1278 #endif
1279 #ifdef SYSVSHM
1280 case KERN_SYSVIPC_SHM_INFO:
1281 shmsi = (struct shm_sysctl_info *)buf;
1282 shmsi->shminfo = shminfo;
1283 break;
1284 #endif
1285 }
1286 buflen -= infosize;
1287
1288 ret = 0;
1289 if (buflen > 0) {
1290 /* Fill in the IPC data structures. */
1291 for (i = 0; i < nds; i++) {
1292 if (buflen < dssize) {
1293 ret = ENOMEM;
1294 break;
1295 }
1296 switch (*name) {
1297 #ifdef SYSVMSG
1298 case KERN_SYSVIPC_MSG_INFO:
1299 FILL_MSG(msqids[i], msgsi->msgids[i]);
1300 break;
1301 #endif
1302 #ifdef SYSVSEM
1303 case KERN_SYSVIPC_SEM_INFO:
1304 FILL_SEM(sema[i], semsi->semids[i]);
1305 break;
1306 #endif
1307 #ifdef SYSVSHM
1308 case KERN_SYSVIPC_SHM_INFO:
1309 FILL_SHM(shmsegs[i], shmsi->shmids[i]);
1310 break;
1311 #endif
1312 }
1313 buflen -= dssize;
1314 }
1315 }
1316 *sizep -= buflen;
1317 error = copyout(buf, start, *sizep);
1318 /* If copyout succeeded, use return code set earlier. */
1319 if (error == 0)
1320 error = ret;
1321 if (buf)
1322 free(buf, M_TEMP);
1323 return (error);
1324 }
1325 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
1326
1327 static int
1328 sysctl_msgbuf(void *vwhere, size_t *sizep)
1329 {
1330 char *where = vwhere;
1331 size_t len, maxlen = *sizep;
1332 long beg, end;
1333 int error;
1334
1335 /*
1336 * deal with cases where the message buffer has
1337 * become corrupted.
1338 */
1339 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1340 msgbufenabled = 0;
1341 return (ENXIO);
1342 }
1343
1344 if (where == NULL) {
1345 /* always return full buffer size */
1346 *sizep = msgbufp->msg_bufs;
1347 return (0);
1348 }
1349
1350 error = 0;
1351 maxlen = min(msgbufp->msg_bufs, maxlen);
1352
1353 /*
1354 * First, copy from the write pointer to the end of
1355 * message buffer.
1356 */
1357 beg = msgbufp->msg_bufx;
1358 end = msgbufp->msg_bufs;
1359 while (maxlen > 0) {
1360 len = min(end - beg, maxlen);
1361 if (len == 0)
1362 break;
1363 error = copyout(&msgbufp->msg_bufc[beg], where, len);
1364 if (error)
1365 break;
1366 where += len;
1367 maxlen -= len;
1368
1369 /*
1370 * ... then, copy from the beginning of message buffer to
1371 * the write pointer.
1372 */
1373 beg = 0;
1374 end = msgbufp->msg_bufx;
1375 }
1376 return (error);
1377 }
1378
1379 /*
1380 * try over estimating by 5 procs
1381 */
1382 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
1383
1384 static int
1385 sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
1386 {
1387 struct eproc eproc;
1388 struct kinfo_proc2 kproc2;
1389 struct kinfo_proc *dp;
1390 struct proc *p;
1391 const struct proclist_desc *pd;
1392 char *where, *dp2;
1393 int type, op, arg, elem_size, elem_count;
1394 int buflen, needed, error;
1395
1396 dp = vwhere;
1397 dp2 = where = vwhere;
1398 buflen = where != NULL ? *sizep : 0;
1399 error = needed = 0;
1400 type = name[0];
1401
1402 if (type == KERN_PROC) {
1403 if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
1404 return (EINVAL);
1405 op = name[1];
1406 if (op != KERN_PROC_ALL)
1407 arg = name[2];
1408 } else {
1409 if (namelen != 5)
1410 return (EINVAL);
1411 op = name[1];
1412 arg = name[2];
1413 elem_size = name[3];
1414 elem_count = name[4];
1415 }
1416
1417 proclist_lock_read();
1418
1419 pd = proclists;
1420 again:
1421 for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
1422 /*
1423 * Skip embryonic processes.
1424 */
1425 if (p->p_stat == SIDL)
1426 continue;
1427 /*
1428 * TODO - make more efficient (see notes below).
1429 * do by session.
1430 */
1431 switch (op) {
1432
1433 case KERN_PROC_PID:
1434 /* could do this with just a lookup */
1435 if (p->p_pid != (pid_t)arg)
1436 continue;
1437 break;
1438
1439 case KERN_PROC_PGRP:
1440 /* could do this by traversing pgrp */
1441 if (p->p_pgrp->pg_id != (pid_t)arg)
1442 continue;
1443 break;
1444
1445 case KERN_PROC_SESSION:
1446 if (p->p_session->s_sid != (pid_t)arg)
1447 continue;
1448 break;
1449
1450 case KERN_PROC_TTY:
1451 if (arg == KERN_PROC_TTY_REVOKE) {
1452 if ((p->p_flag & P_CONTROLT) == 0 ||
1453 p->p_session->s_ttyp == NULL ||
1454 p->p_session->s_ttyvp != NULL)
1455 continue;
1456 } else if ((p->p_flag & P_CONTROLT) == 0 ||
1457 p->p_session->s_ttyp == NULL) {
1458 if ((dev_t)arg != KERN_PROC_TTY_NODEV)
1459 continue;
1460 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
1461 continue;
1462 break;
1463
1464 case KERN_PROC_UID:
1465 if (p->p_ucred->cr_uid != (uid_t)arg)
1466 continue;
1467 break;
1468
1469 case KERN_PROC_RUID:
1470 if (p->p_cred->p_ruid != (uid_t)arg)
1471 continue;
1472 break;
1473
1474 case KERN_PROC_GID:
1475 if (p->p_ucred->cr_gid != (uid_t)arg)
1476 continue;
1477 break;
1478
1479 case KERN_PROC_RGID:
1480 if (p->p_cred->p_rgid != (uid_t)arg)
1481 continue;
1482 break;
1483
1484 case KERN_PROC_ALL:
1485 /* allow everything */
1486 break;
1487
1488 default:
1489 error = EINVAL;
1490 goto cleanup;
1491 }
1492 if (type == KERN_PROC) {
1493 if (buflen >= sizeof(struct kinfo_proc)) {
1494 fill_eproc(p, &eproc);
1495 error = copyout((caddr_t)p, &dp->kp_proc,
1496 sizeof(struct proc));
1497 if (error)
1498 goto cleanup;
1499 error = copyout((caddr_t)&eproc, &dp->kp_eproc,
1500 sizeof(eproc));
1501 if (error)
1502 goto cleanup;
1503 dp++;
1504 buflen -= sizeof(struct kinfo_proc);
1505 }
1506 needed += sizeof(struct kinfo_proc);
1507 } else { /* KERN_PROC2 */
1508 if (buflen >= elem_size && elem_count > 0) {
1509 fill_kproc2(p, &kproc2);
1510 /*
1511 * Copy out elem_size, but not larger than
1512 * the size of a struct kinfo_proc2.
1513 */
1514 error = copyout(&kproc2, dp2,
1515 min(sizeof(kproc2), elem_size));
1516 if (error)
1517 goto cleanup;
1518 dp2 += elem_size;
1519 buflen -= elem_size;
1520 elem_count--;
1521 }
1522 needed += elem_size;
1523 }
1524 }
1525 pd++;
1526 if (pd->pd_list != NULL)
1527 goto again;
1528 proclist_unlock_read();
1529
1530 if (where != NULL) {
1531 if (type == KERN_PROC)
1532 *sizep = (caddr_t)dp - where;
1533 else
1534 *sizep = dp2 - where;
1535 if (needed > *sizep)
1536 return (ENOMEM);
1537 } else {
1538 needed += KERN_PROCSLOP;
1539 *sizep = needed;
1540 }
1541 return (0);
1542 cleanup:
1543 proclist_unlock_read();
1544 return (error);
1545 }
1546
1547 /*
1548 * Fill in an eproc structure for the specified process.
1549 */
1550 void
1551 fill_eproc(struct proc *p, struct eproc *ep)
1552 {
1553 struct tty *tp;
1554
1555 ep->e_paddr = p;
1556 ep->e_sess = p->p_session;
1557 ep->e_pcred = *p->p_cred;
1558 ep->e_ucred = *p->p_ucred;
1559 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1560 ep->e_vm.vm_rssize = 0;
1561 ep->e_vm.vm_tsize = 0;
1562 ep->e_vm.vm_dsize = 0;
1563 ep->e_vm.vm_ssize = 0;
1564 /* ep->e_vm.vm_pmap = XXX; */
1565 } else {
1566 struct vmspace *vm = p->p_vmspace;
1567
1568 ep->e_vm.vm_rssize = vm_resident_count(vm);
1569 ep->e_vm.vm_tsize = vm->vm_tsize;
1570 ep->e_vm.vm_dsize = vm->vm_dsize;
1571 ep->e_vm.vm_ssize = vm->vm_ssize;
1572 }
1573 if (p->p_pptr)
1574 ep->e_ppid = p->p_pptr->p_pid;
1575 else
1576 ep->e_ppid = 0;
1577 ep->e_pgid = p->p_pgrp->pg_id;
1578 ep->e_sid = ep->e_sess->s_sid;
1579 ep->e_jobc = p->p_pgrp->pg_jobc;
1580 if ((p->p_flag & P_CONTROLT) &&
1581 (tp = ep->e_sess->s_ttyp)) {
1582 ep->e_tdev = tp->t_dev;
1583 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1584 ep->e_tsess = tp->t_session;
1585 } else
1586 ep->e_tdev = NODEV;
1587 if (p->p_wmesg)
1588 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
1589 ep->e_xsize = ep->e_xrssize = 0;
1590 ep->e_xccount = ep->e_xswrss = 0;
1591 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
1592 if (SESS_LEADER(p))
1593 ep->e_flag |= EPROC_SLEADER;
1594 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
1595 }
1596
1597 /*
1598 * Fill in an eproc structure for the specified process.
1599 */
1600 static void
1601 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
1602 {
1603 struct tty *tp;
1604
1605 memset(ki, 0, sizeof(*ki));
1606
1607 ki->p_forw = PTRTOINT64(p->p_forw);
1608 ki->p_back = PTRTOINT64(p->p_back);
1609 ki->p_paddr = PTRTOINT64(p);
1610
1611 ki->p_addr = PTRTOINT64(p->p_addr);
1612 ki->p_fd = PTRTOINT64(p->p_fd);
1613 ki->p_cwdi = PTRTOINT64(p->p_cwdi);
1614 ki->p_stats = PTRTOINT64(p->p_stats);
1615 ki->p_limit = PTRTOINT64(p->p_limit);
1616 ki->p_vmspace = PTRTOINT64(p->p_vmspace);
1617 ki->p_sigacts = PTRTOINT64(p->p_sigacts);
1618 ki->p_sess = PTRTOINT64(p->p_session);
1619 ki->p_tsess = 0; /* may be changed if controlling tty below */
1620 ki->p_ru = PTRTOINT64(p->p_ru);
1621
1622 ki->p_eflag = 0;
1623 ki->p_exitsig = p->p_exitsig;
1624 ki->p_flag = p->p_flag;
1625
1626 ki->p_pid = p->p_pid;
1627 if (p->p_pptr)
1628 ki->p_ppid = p->p_pptr->p_pid;
1629 else
1630 ki->p_ppid = 0;
1631 ki->p_sid = p->p_session->s_sid;
1632 ki->p__pgid = p->p_pgrp->pg_id;
1633
1634 ki->p_tpgid = NO_PID; /* may be changed if controlling tty below */
1635
1636 ki->p_uid = p->p_ucred->cr_uid;
1637 ki->p_ruid = p->p_cred->p_ruid;
1638 ki->p_gid = p->p_ucred->cr_gid;
1639 ki->p_rgid = p->p_cred->p_rgid;
1640
1641 memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
1642 min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
1643 ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
1644
1645 ki->p_jobc = p->p_pgrp->pg_jobc;
1646 if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
1647 ki->p_tdev = tp->t_dev;
1648 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1649 ki->p_tsess = PTRTOINT64(tp->t_session);
1650 } else {
1651 ki->p_tdev = NODEV;
1652 }
1653
1654 ki->p_estcpu = p->p_estcpu;
1655 ki->p_rtime_sec = p->p_rtime.tv_sec;
1656 ki->p_rtime_usec = p->p_rtime.tv_usec;
1657 ki->p_cpticks = p->p_cpticks;
1658 ki->p_pctcpu = p->p_pctcpu;
1659 ki->p_swtime = p->p_swtime;
1660 ki->p_slptime = p->p_slptime;
1661 if (p->p_stat == SONPROC) {
1662 KDASSERT(p->p_cpu != NULL);
1663 ki->p_schedflags = p->p_cpu->ci_schedstate.spc_flags;
1664 } else
1665 ki->p_schedflags = 0;
1666
1667 ki->p_uticks = p->p_uticks;
1668 ki->p_sticks = p->p_sticks;
1669 ki->p_iticks = p->p_iticks;
1670
1671 ki->p_tracep = PTRTOINT64(p->p_tracep);
1672 ki->p_traceflag = p->p_traceflag;
1673
1674 ki->p_holdcnt = p->p_holdcnt;
1675
1676 memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
1677 memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
1678 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
1679 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
1680
1681 ki->p_stat = p->p_stat;
1682 ki->p_priority = p->p_priority;
1683 ki->p_usrpri = p->p_usrpri;
1684 ki->p_nice = p->p_nice;
1685
1686 ki->p_xstat = p->p_xstat;
1687 ki->p_acflag = p->p_acflag;
1688
1689 strncpy(ki->p_comm, p->p_comm,
1690 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
1691
1692 if (p->p_wmesg)
1693 strncpy(ki->p_wmesg, p->p_wmesg, sizeof(ki->p_wmesg));
1694 ki->p_wchan = PTRTOINT64(p->p_wchan);
1695
1696 strncpy(ki->p_login, p->p_session->s_login, sizeof(ki->p_login));
1697
1698 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1699 ki->p_vm_rssize = 0;
1700 ki->p_vm_tsize = 0;
1701 ki->p_vm_dsize = 0;
1702 ki->p_vm_ssize = 0;
1703 } else {
1704 struct vmspace *vm = p->p_vmspace;
1705
1706 ki->p_vm_rssize = vm_resident_count(vm);
1707 ki->p_vm_tsize = vm->vm_tsize;
1708 ki->p_vm_dsize = vm->vm_dsize;
1709 ki->p_vm_ssize = vm->vm_ssize;
1710 }
1711
1712 if (p->p_session->s_ttyvp)
1713 ki->p_eflag |= EPROC_CTTY;
1714 if (SESS_LEADER(p))
1715 ki->p_eflag |= EPROC_SLEADER;
1716
1717 /* XXX Is this double check necessary? */
1718 if ((p->p_flag & P_INMEM) == 0 || P_ZOMBIE(p)) {
1719 ki->p_uvalid = 0;
1720 } else {
1721 ki->p_uvalid = 1;
1722
1723 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
1724 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
1725
1726 ki->p_uutime_sec = p->p_stats->p_ru.ru_utime.tv_sec;
1727 ki->p_uutime_usec = p->p_stats->p_ru.ru_utime.tv_usec;
1728 ki->p_ustime_sec = p->p_stats->p_ru.ru_stime.tv_sec;
1729 ki->p_ustime_usec = p->p_stats->p_ru.ru_stime.tv_usec;
1730
1731 ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
1732 ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
1733 ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
1734 ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
1735 ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
1736 ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
1737 ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
1738 ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
1739 ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
1740 ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
1741 ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
1742 ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
1743 ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
1744 ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
1745
1746 ki->p_uctime_sec = p->p_stats->p_cru.ru_utime.tv_sec +
1747 p->p_stats->p_cru.ru_stime.tv_sec;
1748 ki->p_uctime_usec = p->p_stats->p_cru.ru_utime.tv_usec +
1749 p->p_stats->p_cru.ru_stime.tv_usec;
1750 }
1751 #ifdef MULTIPROCESSOR
1752 if (p->p_cpu != NULL)
1753 ki->p_cpuid = p->p_cpu->ci_cpuid;
1754 else
1755 #endif
1756 ki->p_cpuid = KI_NOCPU;
1757 }
1758
1759 int
1760 sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
1761 struct proc *up)
1762 {
1763 struct ps_strings pss;
1764 struct proc *p;
1765 size_t len, upper_bound, xlen;
1766 struct uio auio;
1767 struct iovec aiov;
1768 vaddr_t argv;
1769 pid_t pid;
1770 int nargv, type, error, i;
1771 char *arg;
1772 char *tmp;
1773
1774 if (namelen != 2)
1775 return (EINVAL);
1776 pid = name[0];
1777 type = name[1];
1778
1779 switch (type) {
1780 case KERN_PROC_ARGV:
1781 case KERN_PROC_NARGV:
1782 case KERN_PROC_ENV:
1783 case KERN_PROC_NENV:
1784 /* ok */
1785 break;
1786 default:
1787 return (EINVAL);
1788 }
1789
1790 /* check pid */
1791 if ((p = pfind(pid)) == NULL)
1792 return (EINVAL);
1793
1794 /* only root or same user change look at the environment */
1795 if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
1796 if (up->p_ucred->cr_uid != 0) {
1797 if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
1798 up->p_cred->p_ruid != p->p_cred->p_svuid)
1799 return (EPERM);
1800 }
1801 }
1802
1803 if (sizep != NULL && where == NULL) {
1804 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
1805 *sizep = sizeof (int);
1806 else
1807 *sizep = ARG_MAX; /* XXX XXX XXX */
1808 return (0);
1809 }
1810 if (where == NULL || sizep == NULL)
1811 return (EINVAL);
1812
1813 /*
1814 * Zombies don't have a stack, so we can't read their psstrings.
1815 * System processes also don't have a user stack.
1816 */
1817 if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
1818 return (EINVAL);
1819
1820 /*
1821 * Lock the process down in memory.
1822 */
1823 /* XXXCDC: how should locking work here? */
1824 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
1825 return (EFAULT);
1826 p->p_vmspace->vm_refcnt++; /* XXX */
1827
1828 /*
1829 * Allocate a temporary buffer to hold the arguments.
1830 */
1831 arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
1832
1833 /*
1834 * Read in the ps_strings structure.
1835 */
1836 aiov.iov_base = &pss;
1837 aiov.iov_len = sizeof(pss);
1838 auio.uio_iov = &aiov;
1839 auio.uio_iovcnt = 1;
1840 auio.uio_offset = (vaddr_t)p->p_psstr;
1841 auio.uio_resid = sizeof(pss);
1842 auio.uio_segflg = UIO_SYSSPACE;
1843 auio.uio_rw = UIO_READ;
1844 auio.uio_procp = NULL;
1845 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1846 if (error)
1847 goto done;
1848
1849 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
1850 memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
1851 else
1852 memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
1853 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
1854 error = copyout(&nargv, where, sizeof(nargv));
1855 *sizep = sizeof(nargv);
1856 goto done;
1857 }
1858 /*
1859 * Now read the address of the argument vector.
1860 */
1861 switch (type) {
1862 case KERN_PROC_ARGV:
1863 /* XXX compat32 stuff here */
1864 memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
1865 break;
1866 case KERN_PROC_ENV:
1867 memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
1868 break;
1869 default:
1870 return (EINVAL);
1871 }
1872 auio.uio_offset = (off_t)(long)tmp;
1873 aiov.iov_base = &argv;
1874 aiov.iov_len = sizeof(argv);
1875 auio.uio_iov = &aiov;
1876 auio.uio_iovcnt = 1;
1877 auio.uio_resid = sizeof(argv);
1878 auio.uio_segflg = UIO_SYSSPACE;
1879 auio.uio_rw = UIO_READ;
1880 auio.uio_procp = NULL;
1881 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1882 if (error)
1883 goto done;
1884
1885 /*
1886 * Now copy in the actual argument vector, one page at a time,
1887 * since we don't know how long the vector is (though, we do
1888 * know how many NUL-terminated strings are in the vector).
1889 */
1890 len = 0;
1891 upper_bound = *sizep;
1892 for (; nargv != 0 && len < upper_bound; len += xlen) {
1893 aiov.iov_base = arg;
1894 aiov.iov_len = PAGE_SIZE;
1895 auio.uio_iov = &aiov;
1896 auio.uio_iovcnt = 1;
1897 auio.uio_offset = argv + len;
1898 xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
1899 auio.uio_resid = xlen;
1900 auio.uio_segflg = UIO_SYSSPACE;
1901 auio.uio_rw = UIO_READ;
1902 auio.uio_procp = NULL;
1903 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1904 if (error)
1905 goto done;
1906
1907 for (i = 0; i < xlen && nargv != 0; i++) {
1908 if (arg[i] == '\0')
1909 nargv--; /* one full string */
1910 }
1911
1912 /* make sure we don't copyout past the end of the user's buffer */
1913 if (len + i > upper_bound)
1914 i = upper_bound - len;
1915
1916 error = copyout(arg, (char *)where + len, i);
1917 if (error)
1918 break;
1919
1920 if (nargv == 0) {
1921 len += i;
1922 break;
1923 }
1924 }
1925 *sizep = len;
1926
1927 done:
1928 uvmspace_free(p->p_vmspace);
1929
1930 free(arg, M_TEMP);
1931 return (error);
1932 }
1933
1934 #if NPTY > 0
1935 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
1936
1937 /*
1938 * Validate parameters and get old / set new parameters
1939 * for pty sysctl function.
1940 */
1941 static int
1942 sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
1943 {
1944 int error = 0;
1945 int oldmax = 0, newmax = 0;
1946
1947 /* get current value of maxptys */
1948 oldmax = pty_maxptys(0, 0);
1949
1950 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
1951
1952 if (!error && newp) {
1953 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
1954 SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
1955
1956 if (newmax != pty_maxptys(newmax, (newp != NULL)))
1957 return (EINVAL);
1958
1959 }
1960
1961 return (error);
1962 }
1963 #endif /* NPTY > 0 */
1964
1965 static int
1966 sysctl_dotkstat(name, namelen, where, sizep, newp)
1967 int *name;
1968 u_int namelen;
1969 void *where;
1970 size_t *sizep;
1971 void *newp;
1972 {
1973 /* all sysctl names at this level are terminal */
1974 if (namelen != 1)
1975 return (ENOTDIR); /* overloaded */
1976
1977 switch (name[0]) {
1978 case KERN_TKSTAT_NIN:
1979 return (sysctl_rdquad(where, sizep, newp, tk_nin));
1980 case KERN_TKSTAT_NOUT:
1981 return (sysctl_rdquad(where, sizep, newp, tk_nout));
1982 case KERN_TKSTAT_CANCC:
1983 return (sysctl_rdquad(where, sizep, newp, tk_cancc));
1984 case KERN_TKSTAT_RAWCC:
1985 return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
1986 default:
1987 return (EOPNOTSUPP);
1988 }
1989 }
1990