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