xref: /openbsd-src/sys/kern/kern_sysctl.c (revision 505ee9ea3b177e2387d907a91ca7da069f3f14d8)
1 /*	$OpenBSD: kern_sysctl.c,v 1.373 2020/06/22 02:45:18 dlg Exp $	*/
2 /*	$NetBSD: kern_sysctl.c,v 1.17 1996/05/20 17:49:05 mrg Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Mike Karels at Berkeley Software Design, Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)kern_sysctl.c	8.4 (Berkeley) 4/14/94
36  */
37 
38 /*
39  * sysctl system call.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/pool.h>
47 #include <sys/proc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/signalvar.h>
50 #include <sys/fcntl.h>
51 #include <sys/file.h>
52 #include <sys/filedesc.h>
53 #include <sys/vnode.h>
54 #include <sys/unistd.h>
55 #include <sys/buf.h>
56 #include <sys/ioctl.h>
57 #include <sys/tty.h>
58 #include <sys/disklabel.h>
59 #include <sys/disk.h>
60 #include <sys/sysctl.h>
61 #include <sys/msgbuf.h>
62 #include <sys/vmmeter.h>
63 #include <sys/namei.h>
64 #include <sys/exec.h>
65 #include <sys/mbuf.h>
66 #include <sys/percpu.h>
67 #include <sys/sensors.h>
68 #include <sys/pipe.h>
69 #include <sys/eventvar.h>
70 #include <sys/socketvar.h>
71 #include <sys/socket.h>
72 #include <sys/domain.h>
73 #include <sys/protosw.h>
74 #include <sys/pledge.h>
75 #include <sys/timetc.h>
76 #include <sys/evcount.h>
77 #include <sys/un.h>
78 #include <sys/unpcb.h>
79 #include <sys/sched.h>
80 #include <sys/mount.h>
81 #include <sys/syscallargs.h>
82 #include <sys/wait.h>
83 #include <sys/witness.h>
84 
85 #include <uvm/uvm_extern.h>
86 
87 #include <dev/cons.h>
88 
89 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_var.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip6.h>
95 #include <netinet/tcp.h>
96 #include <netinet/tcp_timer.h>
97 #include <netinet/tcp_var.h>
98 #include <netinet/udp.h>
99 #include <netinet/udp_var.h>
100 #include <netinet6/ip6_var.h>
101 
102 #ifdef DDB
103 #include <ddb/db_var.h>
104 #endif
105 
106 #ifdef SYSVMSG
107 #include <sys/msg.h>
108 #endif
109 #ifdef SYSVSEM
110 #include <sys/sem.h>
111 #endif
112 #ifdef SYSVSHM
113 #include <sys/shm.h>
114 #endif
115 
116 #include "audio.h"
117 #include "pf.h"
118 
119 extern struct forkstat forkstat;
120 extern struct nchstats nchstats;
121 extern int nselcoll, fscale;
122 extern struct disklist_head disklist;
123 extern fixpt_t ccpu;
124 extern  long numvnodes;
125 #if NAUDIO > 0
126 extern int audio_record_enable;
127 #endif
128 
129 int allowkmem;
130 int allowdt;
131 
132 int sysctl_diskinit(int, struct proc *);
133 int sysctl_proc_args(int *, u_int, void *, size_t *, struct proc *);
134 int sysctl_proc_cwd(int *, u_int, void *, size_t *, struct proc *);
135 int sysctl_proc_nobroadcastkill(int *, u_int, void *, size_t, void *, size_t *,
136 	struct proc *);
137 int sysctl_proc_vmmap(int *, u_int, void *, size_t *, struct proc *);
138 int sysctl_intrcnt(int *, u_int, void *, size_t *);
139 int sysctl_sensors(int *, u_int, void *, size_t *, void *, size_t);
140 int sysctl_cptime2(int *, u_int, void *, size_t *, void *, size_t);
141 #if NAUDIO > 0
142 int sysctl_audio(int *, u_int, void *, size_t *, void *, size_t);
143 #endif
144 int sysctl_cpustats(int *, u_int, void *, size_t *, void *, size_t);
145 int sysctl_utc_offset(void *, size_t *, void *, size_t);
146 
147 void fill_file(struct kinfo_file *, struct file *, struct filedesc *, int,
148     struct vnode *, struct process *, struct proc *, struct socket *, int);
149 void fill_kproc(struct process *, struct kinfo_proc *, struct proc *, int);
150 
151 int (*cpu_cpuspeed)(int *);
152 
153 /*
154  * Lock to avoid too many processes vslocking a large amount of memory
155  * at the same time.
156  */
157 struct rwlock sysctl_lock = RWLOCK_INITIALIZER("sysctllk");
158 struct rwlock sysctl_disklock = RWLOCK_INITIALIZER("sysctldlk");
159 
160 int
161 sys_sysctl(struct proc *p, void *v, register_t *retval)
162 {
163 	struct sys_sysctl_args /* {
164 		syscallarg(const int *) name;
165 		syscallarg(u_int) namelen;
166 		syscallarg(void *) old;
167 		syscallarg(size_t *) oldlenp;
168 		syscallarg(void *) new;
169 		syscallarg(size_t) newlen;
170 	} */ *uap = v;
171 	int error, dolock = 1;
172 	size_t savelen = 0, oldlen = 0;
173 	sysctlfn *fn;
174 	int name[CTL_MAXNAME];
175 
176 	if (SCARG(uap, new) != NULL &&
177 	    (error = suser(p)))
178 		return (error);
179 	/*
180 	 * all top-level sysctl names are non-terminal
181 	 */
182 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
183 		return (EINVAL);
184 	error = copyin(SCARG(uap, name), name,
185 		       SCARG(uap, namelen) * sizeof(int));
186 	if (error)
187 		return (error);
188 
189 	error = pledge_sysctl(p, SCARG(uap, namelen),
190 	    name, SCARG(uap, new));
191 	if (error)
192 		return (error);
193 
194 	switch (name[0]) {
195 	case CTL_KERN:
196 		fn = kern_sysctl;
197 		break;
198 	case CTL_HW:
199 		fn = hw_sysctl;
200 		break;
201 	case CTL_VM:
202 		fn = uvm_sysctl;
203 		break;
204 	case CTL_NET:
205 		fn = net_sysctl;
206 		break;
207 	case CTL_FS:
208 		fn = fs_sysctl;
209 		break;
210 	case CTL_VFS:
211 		fn = vfs_sysctl;
212 		break;
213 	case CTL_MACHDEP:
214 		fn = cpu_sysctl;
215 		break;
216 #ifdef DEBUG
217 	case CTL_DEBUG:
218 		fn = debug_sysctl;
219 		break;
220 #endif
221 #ifdef DDB
222 	case CTL_DDB:
223 		fn = ddb_sysctl;
224 		break;
225 #endif
226 	default:
227 		return (EOPNOTSUPP);
228 	}
229 
230 	if (SCARG(uap, oldlenp) &&
231 	    (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen))))
232 		return (error);
233 	if (SCARG(uap, old) != NULL) {
234 		if ((error = rw_enter(&sysctl_lock, RW_WRITE|RW_INTR)) != 0)
235 			return (error);
236 		if (dolock) {
237 			if (atop(oldlen) > uvmexp.wiredmax - uvmexp.wired) {
238 				rw_exit_write(&sysctl_lock);
239 				return (ENOMEM);
240 			}
241 			error = uvm_vslock(p, SCARG(uap, old), oldlen,
242 			    PROT_READ | PROT_WRITE);
243 			if (error) {
244 				rw_exit_write(&sysctl_lock);
245 				return (error);
246 			}
247 		}
248 		savelen = oldlen;
249 	}
250 	error = (*fn)(&name[1], SCARG(uap, namelen) - 1, SCARG(uap, old),
251 	    &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
252 	if (SCARG(uap, old) != NULL) {
253 		if (dolock)
254 			uvm_vsunlock(p, SCARG(uap, old), savelen);
255 		rw_exit_write(&sysctl_lock);
256 	}
257 	if (error)
258 		return (error);
259 	if (SCARG(uap, oldlenp))
260 		error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
261 	return (error);
262 }
263 
264 /*
265  * Attributes stored in the kernel.
266  */
267 char hostname[MAXHOSTNAMELEN];
268 int hostnamelen;
269 char domainname[MAXHOSTNAMELEN];
270 int domainnamelen;
271 long hostid;
272 char *disknames = NULL;
273 size_t disknameslen;
274 struct diskstats *diskstats = NULL;
275 size_t diskstatslen;
276 int securelevel;
277 
278 /*
279  * kernel related system variables.
280  */
281 int
282 kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
283     size_t newlen, struct proc *p)
284 {
285 	int error, level, inthostid, stackgap;
286 	dev_t dev;
287 	extern int somaxconn, sominconn;
288 	extern int nosuidcoredump;
289 	extern int maxlocksperuid;
290 	extern int pool_debug;
291 	extern int uvm_wxabort;
292 
293 	/* all sysctl names at this level are terminal except a ton of them */
294 	if (namelen != 1) {
295 		switch (name[0]) {
296 		case KERN_PROC:
297 		case KERN_PROF:
298 		case KERN_MALLOCSTATS:
299 		case KERN_TTY:
300 		case KERN_POOL:
301 		case KERN_PROC_ARGS:
302 		case KERN_PROC_CWD:
303 		case KERN_PROC_NOBROADCASTKILL:
304 		case KERN_PROC_VMMAP:
305 		case KERN_SYSVIPC_INFO:
306 		case KERN_SEMINFO:
307 		case KERN_SHMINFO:
308 		case KERN_INTRCNT:
309 		case KERN_WATCHDOG:
310 		case KERN_EVCOUNT:
311 		case KERN_TIMECOUNTER:
312 		case KERN_CPTIME2:
313 		case KERN_FILE:
314 		case KERN_WITNESS:
315 		case KERN_AUDIO:
316 		case KERN_CPUSTATS:
317 			break;
318 		default:
319 			return (ENOTDIR);	/* overloaded */
320 		}
321 	}
322 
323 	switch (name[0]) {
324 	case KERN_OSTYPE:
325 		return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
326 	case KERN_OSRELEASE:
327 		return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
328 	case KERN_OSREV:
329 		return (sysctl_rdint(oldp, oldlenp, newp, OpenBSD));
330 	case KERN_OSVERSION:
331 		return (sysctl_rdstring(oldp, oldlenp, newp, osversion));
332 	case KERN_VERSION:
333 		return (sysctl_rdstring(oldp, oldlenp, newp, version));
334 	case KERN_MAXVNODES:
335 		return(sysctl_int(oldp, oldlenp, newp, newlen, &maxvnodes));
336 	case KERN_MAXPROC:
337 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxprocess));
338 	case KERN_MAXFILES:
339 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
340 	case KERN_NFILES:
341 		return (sysctl_rdint(oldp, oldlenp, newp, numfiles));
342 	case KERN_TTYCOUNT:
343 		return (sysctl_rdint(oldp, oldlenp, newp, tty_count));
344 	case KERN_NUMVNODES:
345 		return (sysctl_rdint(oldp, oldlenp, newp, numvnodes));
346 	case KERN_ARGMAX:
347 		return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
348 	case KERN_NSELCOLL:
349 		return (sysctl_rdint(oldp, oldlenp, newp, nselcoll));
350 	case KERN_SECURELVL:
351 		level = securelevel;
352 		if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
353 		    newp == NULL)
354 			return (error);
355 		if ((securelevel > 0 || level < -1) &&
356 		    level < securelevel && p->p_p->ps_pid != 1)
357 			return (EPERM);
358 		securelevel = level;
359 		return (0);
360 	case KERN_ALLOWDT:
361 		if (securelevel > 0)
362 			return (sysctl_rdint(oldp, oldlenp, newp, allowdt));
363 		return (sysctl_int(oldp, oldlenp, newp, newlen,  &allowdt));
364 	case KERN_ALLOWKMEM:
365 		if (securelevel > 0)
366 			return (sysctl_rdint(oldp, oldlenp, newp, allowkmem));
367 		return (sysctl_int(oldp, oldlenp, newp, newlen, &allowkmem));
368 	case KERN_HOSTNAME:
369 		error = sysctl_tstring(oldp, oldlenp, newp, newlen,
370 		    hostname, sizeof(hostname));
371 		if (newp && !error)
372 			hostnamelen = newlen;
373 		return (error);
374 	case KERN_DOMAINNAME:
375 		error = sysctl_tstring(oldp, oldlenp, newp, newlen,
376 		    domainname, sizeof(domainname));
377 		if (newp && !error)
378 			domainnamelen = newlen;
379 		return (error);
380 	case KERN_HOSTID:
381 		inthostid = hostid;  /* XXX assumes sizeof long <= sizeof int */
382 		error =  sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
383 		hostid = inthostid;
384 		return (error);
385 	case KERN_CLOCKRATE:
386 		return (sysctl_clockrate(oldp, oldlenp, newp));
387 	case KERN_BOOTTIME: {
388 		struct timeval bt;
389 		memset(&bt, 0, sizeof bt);
390 		microboottime(&bt);
391 		return (sysctl_rdstruct(oldp, oldlenp, newp, &bt, sizeof bt));
392 	  }
393 #ifndef SMALL_KERNEL
394 	case KERN_PROC:
395 		return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
396 	case KERN_PROC_ARGS:
397 		return (sysctl_proc_args(name + 1, namelen - 1, oldp, oldlenp,
398 		     p));
399 	case KERN_PROC_CWD:
400 		return (sysctl_proc_cwd(name + 1, namelen - 1, oldp, oldlenp,
401 		     p));
402 	case KERN_PROC_NOBROADCASTKILL:
403 		return (sysctl_proc_nobroadcastkill(name + 1, namelen - 1,
404 		     newp, newlen, oldp, oldlenp, p));
405 	case KERN_PROC_VMMAP:
406 		return (sysctl_proc_vmmap(name + 1, namelen - 1, oldp, oldlenp,
407 		     p));
408 	case KERN_FILE:
409 		return (sysctl_file(name + 1, namelen - 1, oldp, oldlenp, p));
410 #endif
411 	case KERN_MBSTAT: {
412 		extern struct cpumem *mbstat;
413 		uint64_t counters[MBSTAT_COUNT];
414 		struct mbstat mbs;
415 		unsigned int i;
416 
417 		memset(&mbs, 0, sizeof(mbs));
418 		counters_read(mbstat, counters, MBSTAT_COUNT);
419 		for (i = 0; i < MBSTAT_TYPES; i++)
420 			mbs.m_mtypes[i] = counters[i];
421 
422 		mbs.m_drops = counters[MBSTAT_DROPS];
423 		mbs.m_wait = counters[MBSTAT_WAIT];
424 		mbs.m_drain = counters[MBSTAT_DRAIN];
425 
426 		return (sysctl_rdstruct(oldp, oldlenp, newp,
427 		    &mbs, sizeof(mbs)));
428 	}
429 #if defined(GPROF) || defined(DDBPROF)
430 	case KERN_PROF:
431 		return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
432 		    newp, newlen));
433 #endif
434 	case KERN_POSIX1:
435 		return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
436 	case KERN_NGROUPS:
437 		return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
438 	case KERN_JOB_CONTROL:
439 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
440 	case KERN_SAVED_IDS:
441 		return (sysctl_rdint(oldp, oldlenp, newp, 1));
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 	case KERN_MAXTHREAD:
447 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxthread));
448 	case KERN_NTHREADS:
449 		return (sysctl_rdint(oldp, oldlenp, newp, nthreads));
450 	case KERN_SOMAXCONN: {
451 		int val = somaxconn;
452 		error = sysctl_int(oldp, oldlenp, newp, newlen, &val);
453 		if (error)
454 			return error;
455 		if (val < 0 || val > SHRT_MAX)
456 			return EINVAL;
457 		somaxconn = val;
458 		return 0;
459 	}
460 	case KERN_SOMINCONN: {
461 		int val = sominconn;
462 		error = sysctl_int(oldp, oldlenp, newp, newlen, &val);
463 		if (error)
464 			return error;
465 		if (val < 0 || val > SHRT_MAX)
466 			return EINVAL;
467 		sominconn = val;
468 		return 0;
469 	}
470 	case KERN_NOSUIDCOREDUMP:
471 		return (sysctl_int(oldp, oldlenp, newp, newlen, &nosuidcoredump));
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_MSGBUFSIZE:
493 	case KERN_CONSBUFSIZE: {
494 		struct msgbuf *mp;
495 		mp = (name[0] == KERN_MSGBUFSIZE) ? msgbufp : consbufp;
496 		/*
497 		 * deal with cases where the message buffer has
498 		 * become corrupted.
499 		 */
500 		if (!mp || mp->msg_magic != MSG_MAGIC)
501 			return (ENXIO);
502 		return (sysctl_rdint(oldp, oldlenp, newp, mp->msg_bufs));
503 	}
504 	case KERN_CONSBUF:
505 		if ((error = suser(p)))
506 			return (error);
507 		/* FALLTHROUGH */
508 	case KERN_MSGBUF: {
509 		struct msgbuf *mp;
510 		mp = (name[0] == KERN_MSGBUF) ? msgbufp : consbufp;
511 		/* see note above */
512 		if (!mp || mp->msg_magic != MSG_MAGIC)
513 			return (ENXIO);
514 		return (sysctl_rdstruct(oldp, oldlenp, newp, mp,
515 		    mp->msg_bufs + offsetof(struct msgbuf, msg_bufc)));
516 	}
517 	case KERN_MALLOCSTATS:
518 		return (sysctl_malloc(name + 1, namelen - 1, oldp, oldlenp,
519 		    newp, newlen, p));
520 	case KERN_CPTIME:
521 	{
522 		CPU_INFO_ITERATOR cii;
523 		struct cpu_info *ci;
524 		long cp_time[CPUSTATES];
525 		int i, n = 0;
526 
527 		memset(cp_time, 0, sizeof(cp_time));
528 
529 		CPU_INFO_FOREACH(cii, ci) {
530 			if (!cpu_is_online(ci))
531 				continue;
532 			n++;
533 			for (i = 0; i < CPUSTATES; i++)
534 				cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
535 		}
536 
537 		for (i = 0; i < CPUSTATES; i++)
538 			cp_time[i] /= n;
539 
540 		return (sysctl_rdstruct(oldp, oldlenp, newp, &cp_time,
541 		    sizeof(cp_time)));
542 	}
543 	case KERN_NCHSTATS:
544 		return (sysctl_rdstruct(oldp, oldlenp, newp, &nchstats,
545 		    sizeof(struct nchstats)));
546 	case KERN_FORKSTAT:
547 		return (sysctl_rdstruct(oldp, oldlenp, newp, &forkstat,
548 		    sizeof(struct forkstat)));
549 	case KERN_TTY:
550 		return (sysctl_tty(name + 1, namelen - 1, oldp, oldlenp,
551 		    newp, newlen));
552 	case KERN_FSCALE:
553 		return (sysctl_rdint(oldp, oldlenp, newp, fscale));
554 	case KERN_CCPU:
555 		return (sysctl_rdint(oldp, oldlenp, newp, ccpu));
556 	case KERN_NPROCS:
557 		return (sysctl_rdint(oldp, oldlenp, newp, nprocesses));
558 	case KERN_POOL:
559 		return (sysctl_dopool(name + 1, namelen - 1, oldp, oldlenp));
560 	case KERN_STACKGAPRANDOM:
561 		stackgap = stackgap_random;
562 		error = sysctl_int(oldp, oldlenp, newp, newlen, &stackgap);
563 		if (error)
564 			return (error);
565 		/*
566 		 * Safety harness.
567 		 */
568 		if ((stackgap < ALIGNBYTES && stackgap != 0) ||
569 		    !powerof2(stackgap) || stackgap >= MAXSSIZ)
570 			return (EINVAL);
571 		stackgap_random = stackgap;
572 		return (0);
573 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
574 	case KERN_SYSVIPC_INFO:
575 		return (sysctl_sysvipc(name + 1, namelen - 1, oldp, oldlenp));
576 #endif
577 	case KERN_SPLASSERT:
578 		return (sysctl_int(oldp, oldlenp, newp, newlen,
579 		    &splassert_ctl));
580 #ifdef SYSVSEM
581 	case KERN_SEMINFO:
582 		return (sysctl_sysvsem(name + 1, namelen - 1, oldp, oldlenp,
583 		    newp, newlen));
584 #endif
585 #ifdef SYSVSHM
586 	case KERN_SHMINFO:
587 		return (sysctl_sysvshm(name + 1, namelen - 1, oldp, oldlenp,
588 		    newp, newlen));
589 #endif
590 #ifndef SMALL_KERNEL
591 	case KERN_INTRCNT:
592 		return (sysctl_intrcnt(name + 1, namelen - 1, oldp, oldlenp));
593 	case KERN_WATCHDOG:
594 		return (sysctl_wdog(name + 1, namelen - 1, oldp, oldlenp,
595 		    newp, newlen));
596 #endif
597 	case KERN_MAXCLUSTERS: {
598 		int val = nmbclust;
599 		error = sysctl_int(oldp, oldlenp, newp, newlen, &val);
600 		if (error == 0 && val != nmbclust)
601 			error = nmbclust_update(val);
602 		return (error);
603 	}
604 #ifndef SMALL_KERNEL
605 	case KERN_EVCOUNT:
606 		return (evcount_sysctl(name + 1, namelen - 1, oldp, oldlenp,
607 		    newp, newlen));
608 #endif
609 	case KERN_TIMECOUNTER:
610 		return (sysctl_tc(name + 1, namelen - 1, oldp, oldlenp,
611 		    newp, newlen));
612 	case KERN_MAXLOCKSPERUID:
613 		return (sysctl_int(oldp, oldlenp, newp, newlen, &maxlocksperuid));
614 	case KERN_CPTIME2:
615 		return (sysctl_cptime2(name + 1, namelen -1, oldp, oldlenp,
616 		    newp, newlen));
617 	case KERN_CACHEPCT: {
618 		u_int64_t dmapages;
619 		int opct, pgs;
620 		opct = bufcachepercent;
621 		error = sysctl_int(oldp, oldlenp, newp, newlen,
622 		    &bufcachepercent);
623 		if (error)
624 			return(error);
625 		if (bufcachepercent > 90 || bufcachepercent < 5) {
626 			bufcachepercent = opct;
627 			return (EINVAL);
628 		}
629 		dmapages = uvm_pagecount(&dma_constraint);
630 		if (bufcachepercent != opct) {
631 			pgs = bufcachepercent * dmapages / 100;
632 			bufadjust(pgs); /* adjust bufpages */
633 			bufhighpages = bufpages; /* set high water mark */
634 		}
635 		return(0);
636 	}
637 	case KERN_WXABORT:
638 		return (sysctl_int(oldp, oldlenp, newp, newlen, &uvm_wxabort));
639 	case KERN_CONSDEV:
640 		if (cn_tab != NULL)
641 			dev = cn_tab->cn_dev;
642 		else
643 			dev = NODEV;
644 		return sysctl_rdstruct(oldp, oldlenp, newp, &dev, sizeof(dev));
645 	case KERN_NETLIVELOCKS:
646 		return (sysctl_rdint(oldp, oldlenp, newp, 0));
647 	case KERN_POOL_DEBUG: {
648 		int old_pool_debug = pool_debug;
649 
650 		error = sysctl_int(oldp, oldlenp, newp, newlen,
651 		    &pool_debug);
652 		if (error == 0 && pool_debug != old_pool_debug)
653 			pool_reclaim_all();
654 		return (error);
655 	}
656 #ifdef PTRACE
657 	case KERN_GLOBAL_PTRACE: {
658 		extern int global_ptrace;
659 
660 		return sysctl_int(oldp, oldlenp, newp, newlen, &global_ptrace);
661 	}
662 #endif
663 #ifdef WITNESS
664 	case KERN_WITNESSWATCH:
665 		return witness_sysctl_watch(oldp, oldlenp, newp, newlen);
666 	case KERN_WITNESS:
667 		return witness_sysctl(name + 1, namelen - 1, oldp, oldlenp,
668 		    newp, newlen);
669 #endif
670 #if NAUDIO > 0
671 	case KERN_AUDIO:
672 		return (sysctl_audio(name + 1, namelen - 1, oldp, oldlenp,
673 		    newp, newlen));
674 #endif
675 	case KERN_CPUSTATS:
676 		return (sysctl_cpustats(name + 1, namelen - 1, oldp, oldlenp,
677 		    newp, newlen));
678 #if NPF > 0
679 	case KERN_PFSTATUS:
680 		return (pf_sysctl(oldp, oldlenp, newp, newlen));
681 #endif
682 	case KERN_TIMEOUT_STATS:
683 		return (timeout_sysctl(oldp, oldlenp, newp, newlen));
684 	case KERN_UTC_OFFSET:
685 		return (sysctl_utc_offset(oldp, oldlenp, newp, newlen));
686 	default:
687 		return (EOPNOTSUPP);
688 	}
689 	/* NOTREACHED */
690 }
691 
692 /*
693  * hardware related system variables.
694  */
695 char *hw_vendor, *hw_prod, *hw_uuid, *hw_serial, *hw_ver;
696 int allowpowerdown = 1;
697 
698 int
699 hw_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
700     size_t newlen, struct proc *p)
701 {
702 	extern char machine[], cpu_model[];
703 	int err, cpuspeed;
704 
705 	/* all sysctl names at this level except sensors are terminal */
706 	if (name[0] != HW_SENSORS && namelen != 1)
707 		return (ENOTDIR);		/* overloaded */
708 
709 	switch (name[0]) {
710 	case HW_MACHINE:
711 		return (sysctl_rdstring(oldp, oldlenp, newp, machine));
712 	case HW_MODEL:
713 		return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
714 	case HW_NCPU:
715 		return (sysctl_rdint(oldp, oldlenp, newp, ncpus));
716 	case HW_NCPUFOUND:
717 		return (sysctl_rdint(oldp, oldlenp, newp, ncpusfound));
718 	case HW_NCPUONLINE:
719 		return (sysctl_rdint(oldp, oldlenp, newp,
720 		    sysctl_hwncpuonline()));
721 	case HW_BYTEORDER:
722 		return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
723 	case HW_PHYSMEM:
724 		return (sysctl_rdint(oldp, oldlenp, newp, ptoa(physmem)));
725 	case HW_USERMEM:
726 		return (sysctl_rdint(oldp, oldlenp, newp,
727 		    ptoa(physmem - uvmexp.wired)));
728 	case HW_PAGESIZE:
729 		return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
730 	case HW_DISKNAMES:
731 		err = sysctl_diskinit(0, p);
732 		if (err)
733 			return err;
734 		if (disknames)
735 			return (sysctl_rdstring(oldp, oldlenp, newp,
736 			    disknames));
737 		else
738 			return (sysctl_rdstring(oldp, oldlenp, newp, ""));
739 	case HW_DISKSTATS:
740 		err = sysctl_diskinit(1, p);
741 		if (err)
742 			return err;
743 		return (sysctl_rdstruct(oldp, oldlenp, newp, diskstats,
744 		    disk_count * sizeof(struct diskstats)));
745 	case HW_DISKCOUNT:
746 		return (sysctl_rdint(oldp, oldlenp, newp, disk_count));
747 	case HW_CPUSPEED:
748 		if (!cpu_cpuspeed)
749 			return (EOPNOTSUPP);
750 		err = cpu_cpuspeed(&cpuspeed);
751 		if (err)
752 			return err;
753 		return (sysctl_rdint(oldp, oldlenp, newp, cpuspeed));
754 #ifndef	SMALL_KERNEL
755 	case HW_SENSORS:
756 		return (sysctl_sensors(name + 1, namelen - 1, oldp, oldlenp,
757 		    newp, newlen));
758 	case HW_SETPERF:
759 		return (sysctl_hwsetperf(oldp, oldlenp, newp, newlen));
760 	case HW_PERFPOLICY:
761 		return (sysctl_hwperfpolicy(oldp, oldlenp, newp, newlen));
762 #endif /* !SMALL_KERNEL */
763 	case HW_VENDOR:
764 		if (hw_vendor)
765 			return (sysctl_rdstring(oldp, oldlenp, newp,
766 			    hw_vendor));
767 		else
768 			return (EOPNOTSUPP);
769 	case HW_PRODUCT:
770 		if (hw_prod)
771 			return (sysctl_rdstring(oldp, oldlenp, newp, hw_prod));
772 		else
773 			return (EOPNOTSUPP);
774 	case HW_VERSION:
775 		if (hw_ver)
776 			return (sysctl_rdstring(oldp, oldlenp, newp, hw_ver));
777 		else
778 			return (EOPNOTSUPP);
779 	case HW_SERIALNO:
780 		if (hw_serial)
781 			return (sysctl_rdstring(oldp, oldlenp, newp,
782 			    hw_serial));
783 		else
784 			return (EOPNOTSUPP);
785 	case HW_UUID:
786 		if (hw_uuid)
787 			return (sysctl_rdstring(oldp, oldlenp, newp, hw_uuid));
788 		else
789 			return (EOPNOTSUPP);
790 	case HW_PHYSMEM64:
791 		return (sysctl_rdquad(oldp, oldlenp, newp,
792 		    ptoa((psize_t)physmem)));
793 	case HW_USERMEM64:
794 		return (sysctl_rdquad(oldp, oldlenp, newp,
795 		    ptoa((psize_t)physmem - uvmexp.wired)));
796 	case HW_ALLOWPOWERDOWN:
797 		if (securelevel > 0)
798 			return (sysctl_rdint(oldp, oldlenp, newp,
799 			    allowpowerdown));
800 		return (sysctl_int(oldp, oldlenp, newp, newlen,
801 		    &allowpowerdown));
802 #ifdef __HAVE_CPU_TOPOLOGY
803 	case HW_SMT:
804 		return (sysctl_hwsmt(oldp, oldlenp, newp, newlen));
805 #endif
806 	default:
807 		return (EOPNOTSUPP);
808 	}
809 	/* NOTREACHED */
810 }
811 
812 #ifdef DEBUG
813 /*
814  * Debugging related system variables.
815  */
816 extern struct ctldebug debug0, debug1;
817 struct ctldebug debug2, debug3, debug4;
818 struct ctldebug debug5, debug6, debug7, debug8, debug9;
819 struct ctldebug debug10, debug11, debug12, debug13, debug14;
820 struct ctldebug debug15, debug16, debug17, debug18, debug19;
821 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
822 	&debug0, &debug1, &debug2, &debug3, &debug4,
823 	&debug5, &debug6, &debug7, &debug8, &debug9,
824 	&debug10, &debug11, &debug12, &debug13, &debug14,
825 	&debug15, &debug16, &debug17, &debug18, &debug19,
826 };
827 int
828 debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
829     size_t newlen, struct proc *p)
830 {
831 	struct ctldebug *cdp;
832 
833 	/* all sysctl names at this level are name and field */
834 	if (namelen != 2)
835 		return (ENOTDIR);		/* overloaded */
836 	if (name[0] < 0 || name[0] >= nitems(debugvars))
837 		return (EOPNOTSUPP);
838 	cdp = debugvars[name[0]];
839 	if (cdp->debugname == 0)
840 		return (EOPNOTSUPP);
841 	switch (name[1]) {
842 	case CTL_DEBUG_NAME:
843 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
844 	case CTL_DEBUG_VALUE:
845 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
846 	default:
847 		return (EOPNOTSUPP);
848 	}
849 	/* NOTREACHED */
850 }
851 #endif /* DEBUG */
852 
853 /*
854  * Reads, or writes that lower the value
855  */
856 int
857 sysctl_int_lower(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
858 {
859 	unsigned int oval = *valp, val = *valp;
860 	int error;
861 
862 	if (newp == NULL)
863 		return (sysctl_rdint(oldp, oldlenp, newp, *valp));
864 
865 	if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &val)))
866 		return (error);
867 	if (val > oval)
868 		return (EPERM);		/* do not allow raising */
869 	*(unsigned int *)valp = val;
870 	return (0);
871 }
872 
873 /*
874  * Validate parameters and get old / set new parameters
875  * for an integer-valued sysctl function.
876  */
877 int
878 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
879 {
880 	int error = 0;
881 	int val;
882 
883 	if (oldp && *oldlenp < sizeof(int))
884 		return (ENOMEM);
885 	if (newp && newlen != sizeof(int))
886 		return (EINVAL);
887 	*oldlenp = sizeof(int);
888 	val = *valp;
889 	if (oldp)
890 		error = copyout(&val, oldp, sizeof(int));
891 	if (error == 0 && newp)
892 		error = copyin(newp, &val, sizeof(int));
893 	if (error == 0)
894 		*valp = val;
895 	return (error);
896 }
897 
898 /*
899  * As above, but read-only.
900  */
901 int
902 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
903 {
904 	int error = 0;
905 
906 	if (oldp && *oldlenp < sizeof(int))
907 		return (ENOMEM);
908 	if (newp)
909 		return (EPERM);
910 	*oldlenp = sizeof(int);
911 	if (oldp)
912 		error = copyout((caddr_t)&val, oldp, sizeof(int));
913 	return (error);
914 }
915 
916 /*
917  * Array of integer values.
918  */
919 int
920 sysctl_int_arr(int **valpp, int *name, u_int namelen, void *oldp,
921     size_t *oldlenp, void *newp, size_t newlen)
922 {
923 	if (namelen > 1)
924 		return (ENOTDIR);
925 	if (name[0] < 0 || valpp[name[0]] == NULL)
926 		return (EOPNOTSUPP);
927 	return (sysctl_int(oldp, oldlenp, newp, newlen, valpp[name[0]]));
928 }
929 
930 /*
931  * Validate parameters and get old / set new parameters
932  * for an integer-valued sysctl function.
933  */
934 int
935 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
936     int64_t *valp)
937 {
938 	int error = 0;
939 
940 	if (oldp && *oldlenp < sizeof(int64_t))
941 		return (ENOMEM);
942 	if (newp && newlen != sizeof(int64_t))
943 		return (EINVAL);
944 	*oldlenp = sizeof(int64_t);
945 	if (oldp)
946 		error = copyout(valp, oldp, sizeof(int64_t));
947 	if (error == 0 && newp)
948 		error = copyin(newp, valp, sizeof(int64_t));
949 	return (error);
950 }
951 
952 /*
953  * As above, but read-only.
954  */
955 int
956 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, int64_t val)
957 {
958 	int error = 0;
959 
960 	if (oldp && *oldlenp < sizeof(int64_t))
961 		return (ENOMEM);
962 	if (newp)
963 		return (EPERM);
964 	*oldlenp = sizeof(int64_t);
965 	if (oldp)
966 		error = copyout((caddr_t)&val, oldp, sizeof(int64_t));
967 	return (error);
968 }
969 
970 /*
971  * Validate parameters and get old / set new parameters
972  * for a string-valued sysctl function.
973  */
974 int
975 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
976     size_t maxlen)
977 {
978 	return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 0);
979 }
980 
981 int
982 sysctl_tstring(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
983     char *str, size_t maxlen)
984 {
985 	return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 1);
986 }
987 
988 int
989 sysctl__string(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
990     char *str, size_t maxlen, int trunc)
991 {
992 	size_t len;
993 	int error = 0;
994 
995 	len = strlen(str) + 1;
996 	if (oldp && *oldlenp < len) {
997 		if (trunc == 0 || *oldlenp == 0)
998 			return (ENOMEM);
999 	}
1000 	if (newp && newlen >= maxlen)
1001 		return (EINVAL);
1002 	if (oldp) {
1003 		if (trunc && *oldlenp < len) {
1004 			len = *oldlenp;
1005 			error = copyout(str, oldp, len - 1);
1006 			if (error == 0)
1007 				error = copyout("", (char *)oldp + len - 1, 1);
1008 		} else {
1009 			error = copyout(str, oldp, len);
1010 		}
1011 	}
1012 	*oldlenp = len;
1013 	if (error == 0 && newp) {
1014 		error = copyin(newp, str, newlen);
1015 		str[newlen] = 0;
1016 	}
1017 	return (error);
1018 }
1019 
1020 /*
1021  * As above, but read-only.
1022  */
1023 int
1024 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
1025 {
1026 	size_t len;
1027 	int error = 0;
1028 
1029 	len = strlen(str) + 1;
1030 	if (oldp && *oldlenp < len)
1031 		return (ENOMEM);
1032 	if (newp)
1033 		return (EPERM);
1034 	*oldlenp = len;
1035 	if (oldp)
1036 		error = copyout(str, oldp, len);
1037 	return (error);
1038 }
1039 
1040 /*
1041  * Validate parameters and get old / set new parameters
1042  * for a structure oriented sysctl function.
1043  */
1044 int
1045 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
1046     size_t len)
1047 {
1048 	int error = 0;
1049 
1050 	if (oldp && *oldlenp < len)
1051 		return (ENOMEM);
1052 	if (newp && newlen > len)
1053 		return (EINVAL);
1054 	if (oldp) {
1055 		*oldlenp = len;
1056 		error = copyout(sp, oldp, len);
1057 	}
1058 	if (error == 0 && newp)
1059 		error = copyin(newp, sp, len);
1060 	return (error);
1061 }
1062 
1063 /*
1064  * Validate parameters and get old parameters
1065  * for a structure oriented sysctl function.
1066  */
1067 int
1068 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1069     size_t len)
1070 {
1071 	int error = 0;
1072 
1073 	if (oldp && *oldlenp < len)
1074 		return (ENOMEM);
1075 	if (newp)
1076 		return (EPERM);
1077 	*oldlenp = len;
1078 	if (oldp)
1079 		error = copyout(sp, oldp, len);
1080 	return (error);
1081 }
1082 
1083 #ifndef SMALL_KERNEL
1084 void
1085 fill_file(struct kinfo_file *kf, struct file *fp, struct filedesc *fdp,
1086 	  int fd, struct vnode *vp, struct process *pr, struct proc *p,
1087 	  struct socket *so, int show_pointers)
1088 {
1089 	struct vattr va;
1090 
1091 	memset(kf, 0, sizeof(*kf));
1092 
1093 	kf->fd_fd = fd;		/* might not really be an fd */
1094 
1095 	if (fp != NULL) {
1096 		if (show_pointers)
1097 			kf->f_fileaddr = PTRTOINT64(fp);
1098 		kf->f_flag = fp->f_flag;
1099 		kf->f_iflags = fp->f_iflags;
1100 		kf->f_type = fp->f_type;
1101 		kf->f_count = fp->f_count;
1102 		if (show_pointers)
1103 			kf->f_ucred = PTRTOINT64(fp->f_cred);
1104 		kf->f_uid = fp->f_cred->cr_uid;
1105 		kf->f_gid = fp->f_cred->cr_gid;
1106 		if (show_pointers)
1107 			kf->f_ops = PTRTOINT64(fp->f_ops);
1108 		if (show_pointers)
1109 			kf->f_data = PTRTOINT64(fp->f_data);
1110 		kf->f_usecount = 0;
1111 
1112 		if (suser(p) == 0 || p->p_ucred->cr_uid == fp->f_cred->cr_uid) {
1113 			mtx_enter(&fp->f_mtx);
1114 			kf->f_offset = fp->f_offset;
1115 			kf->f_rxfer = fp->f_rxfer;
1116 			kf->f_rwfer = fp->f_wxfer;
1117 			kf->f_seek = fp->f_seek;
1118 			kf->f_rbytes = fp->f_rbytes;
1119 			kf->f_wbytes = fp->f_wbytes;
1120 			mtx_leave(&fp->f_mtx);
1121 		} else
1122 			kf->f_offset = -1;
1123 	} else if (vp != NULL) {
1124 		/* fake it */
1125 		kf->f_type = DTYPE_VNODE;
1126 		kf->f_flag = FREAD;
1127 		if (fd == KERN_FILE_TRACE)
1128 			kf->f_flag |= FWRITE;
1129 	} else if (so != NULL) {
1130 		/* fake it */
1131 		kf->f_type = DTYPE_SOCKET;
1132 	}
1133 
1134 	/* information about the object associated with this file */
1135 	switch (kf->f_type) {
1136 	case DTYPE_VNODE:
1137 		if (fp != NULL)
1138 			vp = (struct vnode *)fp->f_data;
1139 
1140 		if (show_pointers)
1141 			kf->v_un = PTRTOINT64(vp->v_un.vu_socket);
1142 		kf->v_type = vp->v_type;
1143 		kf->v_tag = vp->v_tag;
1144 		kf->v_flag = vp->v_flag;
1145 		if (show_pointers)
1146 			kf->v_data = PTRTOINT64(vp->v_data);
1147 		if (show_pointers)
1148 			kf->v_mount = PTRTOINT64(vp->v_mount);
1149 		if (vp->v_mount)
1150 			strlcpy(kf->f_mntonname,
1151 			    vp->v_mount->mnt_stat.f_mntonname,
1152 			    sizeof(kf->f_mntonname));
1153 
1154 		if (VOP_GETATTR(vp, &va, p->p_ucred, p) == 0) {
1155 			kf->va_fileid = va.va_fileid;
1156 			kf->va_mode = MAKEIMODE(va.va_type, va.va_mode);
1157 			kf->va_size = va.va_size;
1158 			kf->va_rdev = va.va_rdev;
1159 			kf->va_fsid = va.va_fsid & 0xffffffff;
1160 			kf->va_nlink = va.va_nlink;
1161 		}
1162 		break;
1163 
1164 	case DTYPE_SOCKET: {
1165 		int locked = 0;
1166 
1167 		if (so == NULL) {
1168 			so = (struct socket *)fp->f_data;
1169 			/* if so is passed as parameter it is already locked */
1170 			switch (so->so_proto->pr_domain->dom_family) {
1171 			case AF_INET:
1172 			case AF_INET6:
1173 				NET_LOCK();
1174 				locked = 1;
1175 				break;
1176 			}
1177 		}
1178 
1179 		kf->so_type = so->so_type;
1180 		kf->so_state = so->so_state;
1181 		if (show_pointers)
1182 			kf->so_pcb = PTRTOINT64(so->so_pcb);
1183 		else
1184 			kf->so_pcb = -1;
1185 		kf->so_protocol = so->so_proto->pr_protocol;
1186 		kf->so_family = so->so_proto->pr_domain->dom_family;
1187 		kf->so_rcv_cc = so->so_rcv.sb_cc;
1188 		kf->so_snd_cc = so->so_snd.sb_cc;
1189 		if (isspliced(so)) {
1190 			if (show_pointers)
1191 				kf->so_splice =
1192 				    PTRTOINT64(so->so_sp->ssp_socket);
1193 			kf->so_splicelen = so->so_sp->ssp_len;
1194 		} else if (issplicedback(so))
1195 			kf->so_splicelen = -1;
1196 		if (so->so_pcb == NULL) {
1197 			if (locked)
1198 				NET_UNLOCK();
1199 			break;
1200 		}
1201 		switch (kf->so_family) {
1202 		case AF_INET: {
1203 			struct inpcb *inpcb = so->so_pcb;
1204 
1205 			NET_ASSERT_LOCKED();
1206 			if (show_pointers)
1207 				kf->inp_ppcb = PTRTOINT64(inpcb->inp_ppcb);
1208 			kf->inp_lport = inpcb->inp_lport;
1209 			kf->inp_laddru[0] = inpcb->inp_laddr.s_addr;
1210 			kf->inp_fport = inpcb->inp_fport;
1211 			kf->inp_faddru[0] = inpcb->inp_faddr.s_addr;
1212 			kf->inp_rtableid = inpcb->inp_rtableid;
1213 			if (so->so_type == SOCK_RAW)
1214 				kf->inp_proto = inpcb->inp_ip.ip_p;
1215 			if (so->so_proto->pr_protocol == IPPROTO_TCP) {
1216 				struct tcpcb *tcpcb = (void *)inpcb->inp_ppcb;
1217 				kf->t_rcv_wnd = tcpcb->rcv_wnd;
1218 				kf->t_snd_wnd = tcpcb->snd_wnd;
1219 				kf->t_snd_cwnd = tcpcb->snd_cwnd;
1220 				kf->t_state = tcpcb->t_state;
1221 			}
1222 			break;
1223 		    }
1224 		case AF_INET6: {
1225 			struct inpcb *inpcb = so->so_pcb;
1226 
1227 			NET_ASSERT_LOCKED();
1228 			if (show_pointers)
1229 				kf->inp_ppcb = PTRTOINT64(inpcb->inp_ppcb);
1230 			kf->inp_lport = inpcb->inp_lport;
1231 			kf->inp_laddru[0] = inpcb->inp_laddr6.s6_addr32[0];
1232 			kf->inp_laddru[1] = inpcb->inp_laddr6.s6_addr32[1];
1233 			kf->inp_laddru[2] = inpcb->inp_laddr6.s6_addr32[2];
1234 			kf->inp_laddru[3] = inpcb->inp_laddr6.s6_addr32[3];
1235 			kf->inp_fport = inpcb->inp_fport;
1236 			kf->inp_faddru[0] = inpcb->inp_faddr6.s6_addr32[0];
1237 			kf->inp_faddru[1] = inpcb->inp_faddr6.s6_addr32[1];
1238 			kf->inp_faddru[2] = inpcb->inp_faddr6.s6_addr32[2];
1239 			kf->inp_faddru[3] = inpcb->inp_faddr6.s6_addr32[3];
1240 			kf->inp_rtableid = inpcb->inp_rtableid;
1241 			if (so->so_type == SOCK_RAW)
1242 				kf->inp_proto = inpcb->inp_ipv6.ip6_nxt;
1243 			if (so->so_proto->pr_protocol == IPPROTO_TCP) {
1244 				struct tcpcb *tcpcb = (void *)inpcb->inp_ppcb;
1245 				kf->t_rcv_wnd = tcpcb->rcv_wnd;
1246 				kf->t_snd_wnd = tcpcb->snd_wnd;
1247 				kf->t_state = tcpcb->t_state;
1248 			}
1249 			break;
1250 		    }
1251 		case AF_UNIX: {
1252 			struct unpcb *unpcb = so->so_pcb;
1253 
1254 			kf->f_msgcount = unpcb->unp_msgcount;
1255 			if (show_pointers) {
1256 				kf->unp_conn	= PTRTOINT64(unpcb->unp_conn);
1257 				kf->unp_refs	= PTRTOINT64(
1258 				    SLIST_FIRST(&unpcb->unp_refs));
1259 				kf->unp_nextref	= PTRTOINT64(
1260 				    SLIST_NEXT(unpcb, unp_nextref));
1261 				kf->v_un	= PTRTOINT64(unpcb->unp_vnode);
1262 				kf->unp_addr	= PTRTOINT64(unpcb->unp_addr);
1263 			}
1264 			if (unpcb->unp_addr != NULL) {
1265 				struct sockaddr_un *un = mtod(unpcb->unp_addr,
1266 				    struct sockaddr_un *);
1267 				memcpy(kf->unp_path, un->sun_path, un->sun_len
1268 				    - offsetof(struct sockaddr_un,sun_path));
1269 			}
1270 			break;
1271 		    }
1272 		}
1273 		if (locked)
1274 			NET_UNLOCK();
1275 		break;
1276 	    }
1277 
1278 	case DTYPE_PIPE: {
1279 		struct pipe *pipe = (struct pipe *)fp->f_data;
1280 
1281 		if (show_pointers)
1282 			kf->pipe_peer = PTRTOINT64(pipe->pipe_peer);
1283 		kf->pipe_state = pipe->pipe_state;
1284 		break;
1285 	    }
1286 
1287 	case DTYPE_KQUEUE: {
1288 		struct kqueue *kqi = (struct kqueue *)fp->f_data;
1289 
1290 		kf->kq_count = kqi->kq_count;
1291 		kf->kq_state = kqi->kq_state;
1292 		break;
1293 	    }
1294 	}
1295 
1296 	/* per-process information for KERN_FILE_BY[PU]ID */
1297 	if (pr != NULL) {
1298 		kf->p_pid = pr->ps_pid;
1299 		kf->p_uid = pr->ps_ucred->cr_uid;
1300 		kf->p_gid = pr->ps_ucred->cr_gid;
1301 		kf->p_tid = -1;
1302 		strlcpy(kf->p_comm, pr->ps_comm, sizeof(kf->p_comm));
1303 	}
1304 	if (fdp != NULL) {
1305 		fdplock(fdp);
1306 		kf->fd_ofileflags = fdp->fd_ofileflags[fd];
1307 		fdpunlock(fdp);
1308 	}
1309 }
1310 
1311 /*
1312  * Get file structures.
1313  */
1314 int
1315 sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
1316     struct proc *p)
1317 {
1318 	struct kinfo_file *kf;
1319 	struct filedesc *fdp;
1320 	struct file *fp;
1321 	struct process *pr;
1322 	size_t buflen, elem_size, elem_count, outsize;
1323 	char *dp = where;
1324 	int arg, i, error = 0, needed = 0, matched;
1325 	u_int op;
1326 	int show_pointers;
1327 
1328 	if (namelen > 4)
1329 		return (ENOTDIR);
1330 	if (namelen < 4 || name[2] > sizeof(*kf))
1331 		return (EINVAL);
1332 
1333 	buflen = where != NULL ? *sizep : 0;
1334 	op = name[0];
1335 	arg = name[1];
1336 	elem_size = name[2];
1337 	elem_count = name[3];
1338 	outsize = MIN(sizeof(*kf), elem_size);
1339 
1340 	if (elem_size < 1)
1341 		return (EINVAL);
1342 
1343 	show_pointers = suser(curproc) == 0;
1344 
1345 	kf = malloc(sizeof(*kf), M_TEMP, M_WAITOK);
1346 
1347 #define FILLIT2(fp, fdp, i, vp, pr, so) do {				\
1348 	if (buflen >= elem_size && elem_count > 0) {			\
1349 		fill_file(kf, fp, fdp, i, vp, pr, p, so, show_pointers);\
1350 		error = copyout(kf, dp, outsize);			\
1351 		if (error)						\
1352 			break;						\
1353 		dp += elem_size;					\
1354 		buflen -= elem_size;					\
1355 		elem_count--;						\
1356 	}								\
1357 	needed += elem_size;						\
1358 } while (0)
1359 #define FILLIT(fp, fdp, i, vp, pr) \
1360 	FILLIT2(fp, fdp, i, vp, pr, NULL)
1361 #define FILLSO(so) \
1362 	FILLIT2(NULL, NULL, 0, NULL, NULL, so)
1363 
1364 	switch (op) {
1365 	case KERN_FILE_BYFILE:
1366 		/* use the inp-tables to pick up closed connections, too */
1367 		if (arg == DTYPE_SOCKET) {
1368 			struct inpcb *inp;
1369 
1370 			NET_LOCK();
1371 			TAILQ_FOREACH(inp, &tcbtable.inpt_queue, inp_queue)
1372 				FILLSO(inp->inp_socket);
1373 			TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue)
1374 				FILLSO(inp->inp_socket);
1375 			TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue)
1376 				FILLSO(inp->inp_socket);
1377 #ifdef INET6
1378 			TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue,
1379 			    inp_queue)
1380 				FILLSO(inp->inp_socket);
1381 #endif
1382 			NET_UNLOCK();
1383 		}
1384 		fp = NULL;
1385 		while ((fp = fd_iterfile(fp, p)) != NULL) {
1386 			if ((arg == 0 || fp->f_type == arg)) {
1387 				int af, skip = 0;
1388 				if (arg == DTYPE_SOCKET && fp->f_type == arg) {
1389 					af = ((struct socket *)fp->f_data)->
1390 					    so_proto->pr_domain->dom_family;
1391 					if (af == AF_INET || af == AF_INET6)
1392 						skip = 1;
1393 				}
1394 				if (!skip)
1395 					FILLIT(fp, NULL, 0, NULL, NULL);
1396 			}
1397 		}
1398 		break;
1399 	case KERN_FILE_BYPID:
1400 		/* A arg of -1 indicates all processes */
1401 		if (arg < -1) {
1402 			error = EINVAL;
1403 			break;
1404 		}
1405 		matched = 0;
1406 		LIST_FOREACH(pr, &allprocess, ps_list) {
1407 			/*
1408 			 * skip system, exiting, embryonic and undead
1409 			 * processes
1410 			 */
1411 			if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
1412 				continue;
1413 			if (arg > 0 && pr->ps_pid != (pid_t)arg) {
1414 				/* not the pid we are looking for */
1415 				continue;
1416 			}
1417 			matched = 1;
1418 			fdp = pr->ps_fd;
1419 			if (pr->ps_textvp)
1420 				FILLIT(NULL, NULL, KERN_FILE_TEXT, pr->ps_textvp, pr);
1421 			if (fdp->fd_cdir)
1422 				FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr);
1423 			if (fdp->fd_rdir)
1424 				FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr);
1425 			if (pr->ps_tracevp)
1426 				FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr);
1427 			for (i = 0; i < fdp->fd_nfiles; i++) {
1428 				if ((fp = fd_getfile(fdp, i)) == NULL)
1429 					continue;
1430 				FILLIT(fp, fdp, i, NULL, pr);
1431 				FRELE(fp, p);
1432 			}
1433 		}
1434 		if (!matched)
1435 			error = ESRCH;
1436 		break;
1437 	case KERN_FILE_BYUID:
1438 		LIST_FOREACH(pr, &allprocess, ps_list) {
1439 			/*
1440 			 * skip system, exiting, embryonic and undead
1441 			 * processes
1442 			 */
1443 			if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
1444 				continue;
1445 			if (arg >= 0 && pr->ps_ucred->cr_uid != (uid_t)arg) {
1446 				/* not the uid we are looking for */
1447 				continue;
1448 			}
1449 			fdp = pr->ps_fd;
1450 			if (fdp->fd_cdir)
1451 				FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr);
1452 			if (fdp->fd_rdir)
1453 				FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr);
1454 			if (pr->ps_tracevp)
1455 				FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr);
1456 			for (i = 0; i < fdp->fd_nfiles; i++) {
1457 				if ((fp = fd_getfile(fdp, i)) == NULL)
1458 					continue;
1459 				FILLIT(fp, fdp, i, NULL, pr);
1460 				FRELE(fp, p);
1461 			}
1462 		}
1463 		break;
1464 	default:
1465 		error = EINVAL;
1466 		break;
1467 	}
1468 	free(kf, M_TEMP, sizeof(*kf));
1469 
1470 	if (!error) {
1471 		if (where == NULL)
1472 			needed += KERN_FILESLOP * elem_size;
1473 		else if (*sizep < needed)
1474 			error = ENOMEM;
1475 		*sizep = needed;
1476 	}
1477 
1478 	return (error);
1479 }
1480 
1481 /*
1482  * try over estimating by 5 procs
1483  */
1484 #define KERN_PROCSLOP	5
1485 
1486 int
1487 sysctl_doproc(int *name, u_int namelen, char *where, size_t *sizep)
1488 {
1489 	struct kinfo_proc *kproc = NULL;
1490 	struct proc *p;
1491 	struct process *pr;
1492 	char *dp;
1493 	int arg, buflen, doingzomb, elem_size, elem_count;
1494 	int error, needed, op;
1495 	int dothreads = 0;
1496 	int show_pointers;
1497 
1498 	dp = where;
1499 	buflen = where != NULL ? *sizep : 0;
1500 	needed = error = 0;
1501 
1502 	if (namelen != 4 || name[2] <= 0 || name[3] < 0 ||
1503 	    name[2] > sizeof(*kproc))
1504 		return (EINVAL);
1505 	op = name[0];
1506 	arg = name[1];
1507 	elem_size = name[2];
1508 	elem_count = name[3];
1509 
1510 	dothreads = op & KERN_PROC_SHOW_THREADS;
1511 	op &= ~KERN_PROC_SHOW_THREADS;
1512 
1513 	show_pointers = suser(curproc) == 0;
1514 
1515 	if (where != NULL)
1516 		kproc = malloc(sizeof(*kproc), M_TEMP, M_WAITOK);
1517 
1518 	pr = LIST_FIRST(&allprocess);
1519 	doingzomb = 0;
1520 again:
1521 	for (; pr != NULL; pr = LIST_NEXT(pr, ps_list)) {
1522 		/* XXX skip processes in the middle of being zapped */
1523 		if (pr->ps_pgrp == NULL)
1524 			continue;
1525 
1526 		/*
1527 		 * Skip embryonic processes.
1528 		 */
1529 		if (pr->ps_flags & PS_EMBRYO)
1530 			continue;
1531 
1532 		/*
1533 		 * TODO - make more efficient (see notes below).
1534 		 */
1535 		switch (op) {
1536 
1537 		case KERN_PROC_PID:
1538 			/* could do this with just a lookup */
1539 			if (pr->ps_pid != (pid_t)arg)
1540 				continue;
1541 			break;
1542 
1543 		case KERN_PROC_PGRP:
1544 			/* could do this by traversing pgrp */
1545 			if (pr->ps_pgrp->pg_id != (pid_t)arg)
1546 				continue;
1547 			break;
1548 
1549 		case KERN_PROC_SESSION:
1550 			if (pr->ps_session->s_leader == NULL ||
1551 			    pr->ps_session->s_leader->ps_pid != (pid_t)arg)
1552 				continue;
1553 			break;
1554 
1555 		case KERN_PROC_TTY:
1556 			if ((pr->ps_flags & PS_CONTROLT) == 0 ||
1557 			    pr->ps_session->s_ttyp == NULL ||
1558 			    pr->ps_session->s_ttyp->t_dev != (dev_t)arg)
1559 				continue;
1560 			break;
1561 
1562 		case KERN_PROC_UID:
1563 			if (pr->ps_ucred->cr_uid != (uid_t)arg)
1564 				continue;
1565 			break;
1566 
1567 		case KERN_PROC_RUID:
1568 			if (pr->ps_ucred->cr_ruid != (uid_t)arg)
1569 				continue;
1570 			break;
1571 
1572 		case KERN_PROC_ALL:
1573 			if (pr->ps_flags & PS_SYSTEM)
1574 				continue;
1575 			break;
1576 
1577 		case KERN_PROC_KTHREAD:
1578 			/* no filtering */
1579 			break;
1580 
1581 		default:
1582 			error = EINVAL;
1583 			goto err;
1584 		}
1585 
1586 		if (buflen >= elem_size && elem_count > 0) {
1587 			fill_kproc(pr, kproc, NULL, show_pointers);
1588 			error = copyout(kproc, dp, elem_size);
1589 			if (error)
1590 				goto err;
1591 			dp += elem_size;
1592 			buflen -= elem_size;
1593 			elem_count--;
1594 		}
1595 		needed += elem_size;
1596 
1597 		/* Skip per-thread entries if not required by op */
1598 		if (!dothreads)
1599 			continue;
1600 
1601 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
1602 			if (buflen >= elem_size && elem_count > 0) {
1603 				fill_kproc(pr, kproc, p, show_pointers);
1604 				error = copyout(kproc, dp, elem_size);
1605 				if (error)
1606 					goto err;
1607 				dp += elem_size;
1608 				buflen -= elem_size;
1609 				elem_count--;
1610 			}
1611 			needed += elem_size;
1612 		}
1613 	}
1614 	if (doingzomb == 0) {
1615 		pr = LIST_FIRST(&zombprocess);
1616 		doingzomb++;
1617 		goto again;
1618 	}
1619 	if (where != NULL) {
1620 		*sizep = dp - where;
1621 		if (needed > *sizep) {
1622 			error = ENOMEM;
1623 			goto err;
1624 		}
1625 	} else {
1626 		needed += KERN_PROCSLOP * elem_size;
1627 		*sizep = needed;
1628 	}
1629 err:
1630 	if (kproc)
1631 		free(kproc, M_TEMP, sizeof(*kproc));
1632 	return (error);
1633 }
1634 
1635 /*
1636  * Fill in a kproc structure for the specified process.
1637  */
1638 void
1639 fill_kproc(struct process *pr, struct kinfo_proc *ki, struct proc *p,
1640     int show_pointers)
1641 {
1642 	struct session *s = pr->ps_session;
1643 	struct tty *tp;
1644 	struct vmspace *vm = pr->ps_vmspace;
1645 	struct timespec booted, st, ut, utc;
1646 	int isthread;
1647 
1648 	isthread = p != NULL;
1649 	if (!isthread)
1650 		p = pr->ps_mainproc;		/* XXX */
1651 
1652 	FILL_KPROC(ki, strlcpy, p, pr, pr->ps_ucred, pr->ps_pgrp,
1653 	    p, pr, s, vm, pr->ps_limit, pr->ps_sigacts, isthread,
1654 	    show_pointers);
1655 
1656 	/* stuff that's too painful to generalize into the macros */
1657 	if (pr->ps_pptr)
1658 		ki->p_ppid = pr->ps_pptr->ps_pid;
1659 	if (s->s_leader)
1660 		ki->p_sid = s->s_leader->ps_pid;
1661 
1662 	if ((pr->ps_flags & PS_CONTROLT) && (tp = s->s_ttyp)) {
1663 		ki->p_tdev = tp->t_dev;
1664 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : -1;
1665 		if (show_pointers)
1666 			ki->p_tsess = PTRTOINT64(tp->t_session);
1667 	} else {
1668 		ki->p_tdev = NODEV;
1669 		ki->p_tpgid = -1;
1670 	}
1671 
1672 	/* fixups that can only be done in the kernel */
1673 	if ((pr->ps_flags & PS_ZOMBIE) == 0) {
1674 		if ((pr->ps_flags & PS_EMBRYO) == 0 && vm != NULL)
1675 			ki->p_vm_rssize = vm_resident_count(vm);
1676 		calctsru(isthread ? &p->p_tu : &pr->ps_tu, &ut, &st, NULL);
1677 		ki->p_uutime_sec = ut.tv_sec;
1678 		ki->p_uutime_usec = ut.tv_nsec/1000;
1679 		ki->p_ustime_sec = st.tv_sec;
1680 		ki->p_ustime_usec = st.tv_nsec/1000;
1681 
1682 		/* Convert starting uptime to a starting UTC time. */
1683 		nanoboottime(&booted);
1684 		timespecadd(&booted, &pr->ps_start, &utc);
1685 		ki->p_ustart_sec = utc.tv_sec;
1686 		ki->p_ustart_usec = utc.tv_nsec / 1000;
1687 
1688 #ifdef MULTIPROCESSOR
1689 		if (p->p_cpu != NULL)
1690 			ki->p_cpuid = CPU_INFO_UNIT(p->p_cpu);
1691 #endif
1692 	}
1693 
1694 	/* get %cpu and schedule state: just one thread or sum of all? */
1695 	if (isthread) {
1696 		ki->p_pctcpu = p->p_pctcpu;
1697 		ki->p_stat   = p->p_stat;
1698 	} else {
1699 		ki->p_pctcpu = 0;
1700 		ki->p_stat = (pr->ps_flags & PS_ZOMBIE) ? SDEAD : SIDL;
1701 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
1702 			ki->p_pctcpu += p->p_pctcpu;
1703 			/* find best state: ONPROC > RUN > STOP > SLEEP > .. */
1704 			if (p->p_stat == SONPROC || ki->p_stat == SONPROC)
1705 				ki->p_stat = SONPROC;
1706 			else if (p->p_stat == SRUN || ki->p_stat == SRUN)
1707 				ki->p_stat = SRUN;
1708 			else if (p->p_stat == SSTOP || ki->p_stat == SSTOP)
1709 				ki->p_stat = SSTOP;
1710 			else if (p->p_stat == SSLEEP)
1711 				ki->p_stat = SSLEEP;
1712 		}
1713 	}
1714 }
1715 
1716 int
1717 sysctl_proc_args(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1718     struct proc *cp)
1719 {
1720 	struct process *vpr;
1721 	pid_t pid;
1722 	struct ps_strings pss;
1723 	struct iovec iov;
1724 	struct uio uio;
1725 	int error, cnt, op;
1726 	size_t limit;
1727 	char **rargv, **vargv;		/* reader vs. victim */
1728 	char *rarg, *varg, *buf;
1729 	struct vmspace *vm;
1730 	vaddr_t ps_strings;
1731 
1732 	if (namelen > 2)
1733 		return (ENOTDIR);
1734 	if (namelen < 2)
1735 		return (EINVAL);
1736 
1737 	pid = name[0];
1738 	op = name[1];
1739 
1740 	switch (op) {
1741 	case KERN_PROC_ARGV:
1742 	case KERN_PROC_NARGV:
1743 	case KERN_PROC_ENV:
1744 	case KERN_PROC_NENV:
1745 		break;
1746 	default:
1747 		return (EOPNOTSUPP);
1748 	}
1749 
1750 	if ((vpr = prfind(pid)) == NULL)
1751 		return (ESRCH);
1752 
1753 	if (oldp == NULL) {
1754 		if (op == KERN_PROC_NARGV || op == KERN_PROC_NENV)
1755 			*oldlenp = sizeof(int);
1756 		else
1757 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
1758 		return (0);
1759 	}
1760 
1761 	/* Either system process or exiting/zombie */
1762 	if (vpr->ps_flags & (PS_SYSTEM | PS_EXITING))
1763 		return (EINVAL);
1764 
1765 	/* Execing - danger. */
1766 	if ((vpr->ps_flags & PS_INEXEC))
1767 		return (EBUSY);
1768 
1769 	/* Only owner or root can get env */
1770 	if ((op == KERN_PROC_NENV || op == KERN_PROC_ENV) &&
1771 	    (vpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid &&
1772 	    (error = suser(cp)) != 0))
1773 		return (error);
1774 
1775 	ps_strings = vpr->ps_strings;
1776 	vm = vpr->ps_vmspace;
1777 	vm->vm_refcnt++;
1778 	vpr = NULL;
1779 
1780 	buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
1781 
1782 	iov.iov_base = &pss;
1783 	iov.iov_len = sizeof(pss);
1784 	uio.uio_iov = &iov;
1785 	uio.uio_iovcnt = 1;
1786 	uio.uio_offset = (off_t)ps_strings;
1787 	uio.uio_resid = sizeof(pss);
1788 	uio.uio_segflg = UIO_SYSSPACE;
1789 	uio.uio_rw = UIO_READ;
1790 	uio.uio_procp = cp;
1791 
1792 	if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0)
1793 		goto out;
1794 
1795 	if (op == KERN_PROC_NARGV) {
1796 		error = sysctl_rdint(oldp, oldlenp, NULL, pss.ps_nargvstr);
1797 		goto out;
1798 	}
1799 	if (op == KERN_PROC_NENV) {
1800 		error = sysctl_rdint(oldp, oldlenp, NULL, pss.ps_nenvstr);
1801 		goto out;
1802 	}
1803 
1804 	if (op == KERN_PROC_ARGV) {
1805 		cnt = pss.ps_nargvstr;
1806 		vargv = pss.ps_argvstr;
1807 	} else {
1808 		cnt = pss.ps_nenvstr;
1809 		vargv = pss.ps_envstr;
1810 	}
1811 
1812 	/* -1 to have space for a terminating NUL */
1813 	limit = *oldlenp - 1;
1814 	*oldlenp = 0;
1815 
1816 	rargv = oldp;
1817 
1818 	/*
1819 	 * *oldlenp - number of bytes copied out into readers buffer.
1820 	 * limit - maximal number of bytes allowed into readers buffer.
1821 	 * rarg - pointer into readers buffer where next arg will be stored.
1822 	 * rargv - pointer into readers buffer where the next rarg pointer
1823 	 *  will be stored.
1824 	 * vargv - pointer into victim address space where the next argument
1825 	 *  will be read.
1826 	 */
1827 
1828 	/* space for cnt pointers and a NULL */
1829 	rarg = (char *)(rargv + cnt + 1);
1830 	*oldlenp += (cnt + 1) * sizeof(char **);
1831 
1832 	while (cnt > 0 && *oldlenp < limit) {
1833 		size_t len, vstrlen;
1834 
1835 		/* Write to readers argv */
1836 		if ((error = copyout(&rarg, rargv, sizeof(rarg))) != 0)
1837 			goto out;
1838 
1839 		/* read the victim argv */
1840 		iov.iov_base = &varg;
1841 		iov.iov_len = sizeof(varg);
1842 		uio.uio_iov = &iov;
1843 		uio.uio_iovcnt = 1;
1844 		uio.uio_offset = (off_t)(vaddr_t)vargv;
1845 		uio.uio_resid = sizeof(varg);
1846 		uio.uio_segflg = UIO_SYSSPACE;
1847 		uio.uio_rw = UIO_READ;
1848 		uio.uio_procp = cp;
1849 		if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0)
1850 			goto out;
1851 
1852 		if (varg == NULL)
1853 			break;
1854 
1855 		/*
1856 		 * read the victim arg. We must jump through hoops to avoid
1857 		 * crossing a page boundary too much and returning an error.
1858 		 */
1859 more:
1860 		len = PAGE_SIZE - (((vaddr_t)varg) & PAGE_MASK);
1861 		/* leave space for the terminating NUL */
1862 		iov.iov_base = buf;
1863 		iov.iov_len = len;
1864 		uio.uio_iov = &iov;
1865 		uio.uio_iovcnt = 1;
1866 		uio.uio_offset = (off_t)(vaddr_t)varg;
1867 		uio.uio_resid = len;
1868 		uio.uio_segflg = UIO_SYSSPACE;
1869 		uio.uio_rw = UIO_READ;
1870 		uio.uio_procp = cp;
1871 		if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0)
1872 			goto out;
1873 
1874 		for (vstrlen = 0; vstrlen < len; vstrlen++) {
1875 			if (buf[vstrlen] == '\0')
1876 				break;
1877 		}
1878 
1879 		/* Don't overflow readers buffer. */
1880 		if (*oldlenp + vstrlen + 1 >= limit) {
1881 			error = ENOMEM;
1882 			goto out;
1883 		}
1884 
1885 		if ((error = copyout(buf, rarg, vstrlen)) != 0)
1886 			goto out;
1887 
1888 		*oldlenp += vstrlen;
1889 		rarg += vstrlen;
1890 
1891 		/* The string didn't end in this page? */
1892 		if (vstrlen == len) {
1893 			varg += vstrlen;
1894 			goto more;
1895 		}
1896 
1897 		/* End of string. Terminate it with a NUL */
1898 		buf[0] = '\0';
1899 		if ((error = copyout(buf, rarg, 1)) != 0)
1900 			goto out;
1901 		*oldlenp += 1;
1902 		rarg += 1;
1903 
1904 		vargv++;
1905 		rargv++;
1906 		cnt--;
1907 	}
1908 
1909 	if (*oldlenp >= limit) {
1910 		error = ENOMEM;
1911 		goto out;
1912 	}
1913 
1914 	/* Write the terminating null */
1915 	rarg = NULL;
1916 	error = copyout(&rarg, rargv, sizeof(rarg));
1917 
1918 out:
1919 	uvmspace_free(vm);
1920 	free(buf, M_TEMP, PAGE_SIZE);
1921 	return (error);
1922 }
1923 
1924 int
1925 sysctl_proc_cwd(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1926     struct proc *cp)
1927 {
1928 	struct process *findpr;
1929 	struct vnode *vp;
1930 	pid_t pid;
1931 	int error;
1932 	size_t lenused, len;
1933 	char *path, *bp, *bend;
1934 
1935 	if (namelen > 1)
1936 		return (ENOTDIR);
1937 	if (namelen < 1)
1938 		return (EINVAL);
1939 
1940 	pid = name[0];
1941 	if ((findpr = prfind(pid)) == NULL)
1942 		return (ESRCH);
1943 
1944 	if (oldp == NULL) {
1945 		*oldlenp = MAXPATHLEN * 4;
1946 		return (0);
1947 	}
1948 
1949 	/* Either system process or exiting/zombie */
1950 	if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING))
1951 		return (EINVAL);
1952 
1953 	/* Only owner or root can get cwd */
1954 	if (findpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid &&
1955 	    (error = suser(cp)) != 0)
1956 		return (error);
1957 
1958 	len = *oldlenp;
1959 	if (len > MAXPATHLEN * 4)
1960 		len = MAXPATHLEN * 4;
1961 	else if (len < 2)
1962 		return (ERANGE);
1963 	*oldlenp = 0;
1964 
1965 	/* snag a reference to the vnode before we can sleep */
1966 	vp = findpr->ps_fd->fd_cdir;
1967 	vref(vp);
1968 
1969 	path = malloc(len, M_TEMP, M_WAITOK);
1970 
1971 	bp = &path[len];
1972 	bend = bp;
1973 	*(--bp) = '\0';
1974 
1975 	/* Same as sys__getcwd */
1976 	error = vfs_getcwd_common(vp, NULL,
1977 	    &bp, path, len / 2, GETCWD_CHECK_ACCESS, cp);
1978 	if (error == 0) {
1979 		*oldlenp = lenused = bend - bp;
1980 		error = copyout(bp, oldp, lenused);
1981 	}
1982 
1983 	vrele(vp);
1984 	free(path, M_TEMP, len);
1985 
1986 	return (error);
1987 }
1988 
1989 int
1990 sysctl_proc_nobroadcastkill(int *name, u_int namelen, void *newp, size_t newlen,
1991     void *oldp, size_t *oldlenp, struct proc *cp)
1992 {
1993 	struct process *findpr;
1994 	pid_t pid;
1995 	int error, flag;
1996 
1997 	if (namelen > 1)
1998 		return (ENOTDIR);
1999 	if (namelen < 1)
2000 		return (EINVAL);
2001 
2002 	pid = name[0];
2003 	if ((findpr = prfind(pid)) == NULL)
2004 		return (ESRCH);
2005 
2006 	/* Either system process or exiting/zombie */
2007 	if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING))
2008 		return (EINVAL);
2009 
2010 	/* Only root can change PS_NOBROADCASTKILL */
2011 	if (newp != 0 && (error = suser(cp)) != 0)
2012 		return (error);
2013 
2014 	/* get the PS_NOBROADCASTKILL flag */
2015 	flag = findpr->ps_flags & PS_NOBROADCASTKILL ? 1 : 0;
2016 
2017 	error = sysctl_int(oldp, oldlenp, newp, newlen, &flag);
2018 	if (error == 0 && newp) {
2019 		if (flag)
2020 			atomic_setbits_int(&findpr->ps_flags,
2021 			    PS_NOBROADCASTKILL);
2022 		else
2023 			atomic_clearbits_int(&findpr->ps_flags,
2024 			    PS_NOBROADCASTKILL);
2025 	}
2026 
2027 	return (error);
2028 }
2029 
2030 /* Arbitrary but reasonable limit for one iteration. */
2031 #define	VMMAP_MAXLEN	MAXPHYS
2032 
2033 int
2034 sysctl_proc_vmmap(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2035     struct proc *cp)
2036 {
2037 	struct process *findpr;
2038 	pid_t pid;
2039 	int error;
2040 	size_t oldlen, len;
2041 	struct kinfo_vmentry *kve, *ukve;
2042 	u_long *ustart, start;
2043 
2044 	if (namelen > 1)
2045 		return (ENOTDIR);
2046 	if (namelen < 1)
2047 		return (EINVAL);
2048 
2049 	/* Provide max buffer length as hint. */
2050 	if (oldp == NULL) {
2051 		if (oldlenp == NULL)
2052 			return (EINVAL);
2053 		else {
2054 			*oldlenp = VMMAP_MAXLEN;
2055 			return (0);
2056 		}
2057 	}
2058 
2059 	pid = name[0];
2060 	if (pid == cp->p_p->ps_pid) {
2061 		/* Self process mapping. */
2062 		findpr = cp->p_p;
2063 	} else if (pid > 0) {
2064 		if ((findpr = prfind(pid)) == NULL)
2065 			return (ESRCH);
2066 
2067 		/* Either system process or exiting/zombie */
2068 		if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING))
2069 			return (EINVAL);
2070 
2071 #if 1
2072 		/* XXX Allow only root for now */
2073 		if ((error = suser(cp)) != 0)
2074 			return (error);
2075 #else
2076 		/* Only owner or root can get vmmap */
2077 		if (findpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid &&
2078 		    (error = suser(cp)) != 0)
2079 			return (error);
2080 #endif
2081 	} else {
2082 		/* Only root can get kernel_map */
2083 		if ((error = suser(cp)) != 0)
2084 			return (error);
2085 		findpr = NULL;
2086 	}
2087 
2088 	/* Check the given size. */
2089 	oldlen = *oldlenp;
2090 	if (oldlen == 0 || oldlen % sizeof(*kve) != 0)
2091 		return (EINVAL);
2092 
2093 	/* Deny huge allocation. */
2094 	if (oldlen > VMMAP_MAXLEN)
2095 		return (EINVAL);
2096 
2097 	/*
2098 	 * Iterate from the given address passed as the first element's
2099 	 * kve_start via oldp.
2100 	 */
2101 	ukve = (struct kinfo_vmentry *)oldp;
2102 	ustart = &ukve->kve_start;
2103 	error = copyin(ustart, &start, sizeof(start));
2104 	if (error != 0)
2105 		return (error);
2106 
2107 	/* Allocate wired memory to not block. */
2108 	kve = malloc(oldlen, M_TEMP, M_WAITOK);
2109 
2110 	/* Set the base address and read entries. */
2111 	kve[0].kve_start = start;
2112 	len = oldlen;
2113 	error = fill_vmmap(findpr, kve, &len);
2114 	if (error != 0 && error != ENOMEM)
2115 		goto done;
2116 	if (len == 0)
2117 		goto done;
2118 
2119 	KASSERT(len <= oldlen);
2120 	KASSERT((len % sizeof(struct kinfo_vmentry)) == 0);
2121 
2122 	error = copyout(kve, oldp, len);
2123 
2124 done:
2125 	*oldlenp = len;
2126 
2127 	free(kve, M_TEMP, oldlen);
2128 
2129 	return (error);
2130 }
2131 #endif
2132 
2133 /*
2134  * Initialize disknames/diskstats for export by sysctl. If update is set,
2135  * then we simply update the disk statistics information.
2136  */
2137 int
2138 sysctl_diskinit(int update, struct proc *p)
2139 {
2140 	struct diskstats *sdk;
2141 	struct disk *dk;
2142 	const char *duid;
2143 	int i, tlen, l;
2144 
2145 	if ((i = rw_enter(&sysctl_disklock, RW_WRITE|RW_INTR)) != 0)
2146 		return i;
2147 
2148 	if (disk_change) {
2149 		for (dk = TAILQ_FIRST(&disklist), tlen = 0; dk;
2150 		    dk = TAILQ_NEXT(dk, dk_link)) {
2151 			if (dk->dk_name)
2152 				tlen += strlen(dk->dk_name);
2153 			tlen += 18;	/* label uid + separators */
2154 		}
2155 		tlen++;
2156 
2157 		if (disknames)
2158 			free(disknames, M_SYSCTL, disknameslen);
2159 		if (diskstats)
2160 			free(diskstats, M_SYSCTL, diskstatslen);
2161 		diskstats = NULL;
2162 		disknames = NULL;
2163 		diskstats = mallocarray(disk_count, sizeof(struct diskstats),
2164 		    M_SYSCTL, M_WAITOK|M_ZERO);
2165 		diskstatslen = disk_count * sizeof(struct diskstats);
2166 		disknames = malloc(tlen, M_SYSCTL, M_WAITOK|M_ZERO);
2167 		disknameslen = tlen;
2168 		disknames[0] = '\0';
2169 
2170 		for (dk = TAILQ_FIRST(&disklist), i = 0, l = 0; dk;
2171 		    dk = TAILQ_NEXT(dk, dk_link), i++) {
2172 			duid = NULL;
2173 			if (dk->dk_label && !duid_iszero(dk->dk_label->d_uid))
2174 				duid = duid_format(dk->dk_label->d_uid);
2175 			snprintf(disknames + l, tlen - l, "%s:%s,",
2176 			    dk->dk_name ? dk->dk_name : "",
2177 			    duid ? duid : "");
2178 			l += strlen(disknames + l);
2179 			sdk = diskstats + i;
2180 			strlcpy(sdk->ds_name, dk->dk_name,
2181 			    sizeof(sdk->ds_name));
2182 			mtx_enter(&dk->dk_mtx);
2183 			sdk->ds_busy = dk->dk_busy;
2184 			sdk->ds_rxfer = dk->dk_rxfer;
2185 			sdk->ds_wxfer = dk->dk_wxfer;
2186 			sdk->ds_seek = dk->dk_seek;
2187 			sdk->ds_rbytes = dk->dk_rbytes;
2188 			sdk->ds_wbytes = dk->dk_wbytes;
2189 			sdk->ds_attachtime = dk->dk_attachtime;
2190 			sdk->ds_timestamp = dk->dk_timestamp;
2191 			sdk->ds_time = dk->dk_time;
2192 			mtx_leave(&dk->dk_mtx);
2193 		}
2194 
2195 		/* Eliminate trailing comma */
2196 		if (l != 0)
2197 			disknames[l - 1] = '\0';
2198 		disk_change = 0;
2199 	} else if (update) {
2200 		/* Just update, number of drives hasn't changed */
2201 		for (dk = TAILQ_FIRST(&disklist), i = 0; dk;
2202 		    dk = TAILQ_NEXT(dk, dk_link), i++) {
2203 			sdk = diskstats + i;
2204 			strlcpy(sdk->ds_name, dk->dk_name,
2205 			    sizeof(sdk->ds_name));
2206 			mtx_enter(&dk->dk_mtx);
2207 			sdk->ds_busy = dk->dk_busy;
2208 			sdk->ds_rxfer = dk->dk_rxfer;
2209 			sdk->ds_wxfer = dk->dk_wxfer;
2210 			sdk->ds_seek = dk->dk_seek;
2211 			sdk->ds_rbytes = dk->dk_rbytes;
2212 			sdk->ds_wbytes = dk->dk_wbytes;
2213 			sdk->ds_attachtime = dk->dk_attachtime;
2214 			sdk->ds_timestamp = dk->dk_timestamp;
2215 			sdk->ds_time = dk->dk_time;
2216 			mtx_leave(&dk->dk_mtx);
2217 		}
2218 	}
2219 	rw_exit_write(&sysctl_disklock);
2220 	return 0;
2221 }
2222 
2223 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
2224 int
2225 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
2226 {
2227 #ifdef SYSVSEM
2228 	struct sem_sysctl_info *semsi;
2229 #endif
2230 #ifdef SYSVSHM
2231 	struct shm_sysctl_info *shmsi;
2232 #endif
2233 	size_t infosize, dssize, tsize, buflen, bufsiz;
2234 	int i, nds, error, ret;
2235 	void *buf;
2236 
2237 	if (namelen != 1)
2238 		return (EINVAL);
2239 
2240 	buflen = *sizep;
2241 
2242 	switch (*name) {
2243 	case KERN_SYSVIPC_MSG_INFO:
2244 #ifdef SYSVMSG
2245 		return (sysctl_sysvmsg(name, namelen, where, sizep));
2246 #else
2247 		return (EOPNOTSUPP);
2248 #endif
2249 	case KERN_SYSVIPC_SEM_INFO:
2250 #ifdef SYSVSEM
2251 		infosize = sizeof(semsi->seminfo);
2252 		nds = seminfo.semmni;
2253 		dssize = sizeof(semsi->semids[0]);
2254 		break;
2255 #else
2256 		return (EOPNOTSUPP);
2257 #endif
2258 	case KERN_SYSVIPC_SHM_INFO:
2259 #ifdef SYSVSHM
2260 		infosize = sizeof(shmsi->shminfo);
2261 		nds = shminfo.shmmni;
2262 		dssize = sizeof(shmsi->shmids[0]);
2263 		break;
2264 #else
2265 		return (EOPNOTSUPP);
2266 #endif
2267 	default:
2268 		return (EINVAL);
2269 	}
2270 	tsize = infosize + (nds * dssize);
2271 
2272 	/* Return just the total size required. */
2273 	if (where == NULL) {
2274 		*sizep = tsize;
2275 		return (0);
2276 	}
2277 
2278 	/* Not enough room for even the info struct. */
2279 	if (buflen < infosize) {
2280 		*sizep = 0;
2281 		return (ENOMEM);
2282 	}
2283 	bufsiz = min(tsize, buflen);
2284 	buf = malloc(bufsiz, M_TEMP, M_WAITOK|M_ZERO);
2285 
2286 	switch (*name) {
2287 #ifdef SYSVSEM
2288 	case KERN_SYSVIPC_SEM_INFO:
2289 		semsi = (struct sem_sysctl_info *)buf;
2290 		semsi->seminfo = seminfo;
2291 		break;
2292 #endif
2293 #ifdef SYSVSHM
2294 	case KERN_SYSVIPC_SHM_INFO:
2295 		shmsi = (struct shm_sysctl_info *)buf;
2296 		shmsi->shminfo = shminfo;
2297 		break;
2298 #endif
2299 	}
2300 	buflen -= infosize;
2301 
2302 	ret = 0;
2303 	if (buflen > 0) {
2304 		/* Fill in the IPC data structures.  */
2305 		for (i = 0; i < nds; i++) {
2306 			if (buflen < dssize) {
2307 				ret = ENOMEM;
2308 				break;
2309 			}
2310 			switch (*name) {
2311 #ifdef SYSVSEM
2312 			case KERN_SYSVIPC_SEM_INFO:
2313 				if (sema[i] != NULL)
2314 					memcpy(&semsi->semids[i], sema[i],
2315 					    dssize);
2316 				else
2317 					memset(&semsi->semids[i], 0, dssize);
2318 				break;
2319 #endif
2320 #ifdef SYSVSHM
2321 			case KERN_SYSVIPC_SHM_INFO:
2322 				if (shmsegs[i] != NULL)
2323 					memcpy(&shmsi->shmids[i], shmsegs[i],
2324 					    dssize);
2325 				else
2326 					memset(&shmsi->shmids[i], 0, dssize);
2327 				break;
2328 #endif
2329 			}
2330 			buflen -= dssize;
2331 		}
2332 	}
2333 	*sizep -= buflen;
2334 	error = copyout(buf, where, *sizep);
2335 	free(buf, M_TEMP, bufsiz);
2336 	/* If copyout succeeded, use return code set earlier. */
2337 	return (error ? error : ret);
2338 }
2339 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
2340 
2341 #ifndef	SMALL_KERNEL
2342 
2343 int
2344 sysctl_intrcnt(int *name, u_int namelen, void *oldp, size_t *oldlenp)
2345 {
2346 	return (evcount_sysctl(name, namelen, oldp, oldlenp, NULL, 0));
2347 }
2348 
2349 
2350 int
2351 sysctl_sensors(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2352     void *newp, size_t newlen)
2353 {
2354 	struct ksensor *ks;
2355 	struct sensor *us;
2356 	struct ksensordev *ksd;
2357 	struct sensordev *usd;
2358 	int dev, numt, ret;
2359 	enum sensor_type type;
2360 
2361 	if (namelen != 1 && namelen != 3)
2362 		return (ENOTDIR);
2363 
2364 	dev = name[0];
2365 	if (namelen == 1) {
2366 		ret = sensordev_get(dev, &ksd);
2367 		if (ret)
2368 			return (ret);
2369 
2370 		/* Grab a copy, to clear the kernel pointers */
2371 		usd = malloc(sizeof(*usd), M_TEMP, M_WAITOK|M_ZERO);
2372 		usd->num = ksd->num;
2373 		strlcpy(usd->xname, ksd->xname, sizeof(usd->xname));
2374 		memcpy(usd->maxnumt, ksd->maxnumt, sizeof(usd->maxnumt));
2375 		usd->sensors_count = ksd->sensors_count;
2376 
2377 		ret = sysctl_rdstruct(oldp, oldlenp, newp, usd,
2378 		    sizeof(struct sensordev));
2379 
2380 		free(usd, M_TEMP, sizeof(*usd));
2381 		return (ret);
2382 	}
2383 
2384 	type = name[1];
2385 	numt = name[2];
2386 
2387 	ret = sensor_find(dev, type, numt, &ks);
2388 	if (ret)
2389 		return (ret);
2390 
2391 	/* Grab a copy, to clear the kernel pointers */
2392 	us = malloc(sizeof(*us), M_TEMP, M_WAITOK|M_ZERO);
2393 	memcpy(us->desc, ks->desc, sizeof(us->desc));
2394 	us->tv = ks->tv;
2395 	us->value = ks->value;
2396 	us->type = ks->type;
2397 	us->status = ks->status;
2398 	us->numt = ks->numt;
2399 	us->flags = ks->flags;
2400 
2401 	ret = sysctl_rdstruct(oldp, oldlenp, newp, us,
2402 	    sizeof(struct sensor));
2403 	free(us, M_TEMP, sizeof(*us));
2404 	return (ret);
2405 }
2406 #endif	/* SMALL_KERNEL */
2407 
2408 int
2409 sysctl_cptime2(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2410     void *newp, size_t newlen)
2411 {
2412 	CPU_INFO_ITERATOR cii;
2413 	struct cpu_info *ci;
2414 	int found = 0;
2415 
2416 	if (namelen != 1)
2417 		return (ENOTDIR);
2418 
2419 	CPU_INFO_FOREACH(cii, ci) {
2420 		if (name[0] == CPU_INFO_UNIT(ci)) {
2421 			found = 1;
2422 			break;
2423 		}
2424 	}
2425 	if (!found)
2426 		return (ENOENT);
2427 
2428 	return (sysctl_rdstruct(oldp, oldlenp, newp,
2429 	    &ci->ci_schedstate.spc_cp_time,
2430 	    sizeof(ci->ci_schedstate.spc_cp_time)));
2431 }
2432 
2433 #if NAUDIO > 0
2434 int
2435 sysctl_audio(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2436     void *newp, size_t newlen)
2437 {
2438 	if (namelen != 1)
2439 		return (ENOTDIR);
2440 
2441 	if (name[0] != KERN_AUDIO_RECORD)
2442 		return (ENOENT);
2443 
2444 	return (sysctl_int(oldp, oldlenp, newp, newlen, &audio_record_enable));
2445 }
2446 #endif
2447 
2448 int
2449 sysctl_cpustats(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2450     void *newp, size_t newlen)
2451 {
2452 	CPU_INFO_ITERATOR cii;
2453 	struct cpustats cs;
2454 	struct cpu_info *ci;
2455 	int found = 0;
2456 
2457 	if (namelen != 1)
2458 		return (ENOTDIR);
2459 
2460 	CPU_INFO_FOREACH(cii, ci) {
2461 		if (name[0] == CPU_INFO_UNIT(ci)) {
2462 			found = 1;
2463 			break;
2464 		}
2465 	}
2466 	if (!found)
2467 		return (ENOENT);
2468 
2469 	memcpy(&cs.cs_time, &ci->ci_schedstate.spc_cp_time, sizeof(cs.cs_time));
2470 	cs.cs_flags = 0;
2471 	if (cpu_is_online(ci))
2472 		cs.cs_flags |= CPUSTATS_ONLINE;
2473 
2474 	return (sysctl_rdstruct(oldp, oldlenp, newp, &cs, sizeof(cs)));
2475 }
2476 
2477 int
2478 sysctl_utc_offset(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
2479 {
2480 	struct timespec adjusted, now;
2481 	int adjustment_seconds, error, new_offset_minutes, old_offset_minutes;
2482 
2483 	old_offset_minutes = utc_offset / 60;	/* seconds -> minutes */
2484 	if (securelevel > 0)
2485 		return sysctl_rdint(oldp, oldlenp, newp, old_offset_minutes);
2486 
2487 	new_offset_minutes = old_offset_minutes;
2488 	error = sysctl_int(oldp, oldlenp, newp, newlen, &new_offset_minutes);
2489 	if (error)
2490 		return error;
2491 	if (new_offset_minutes < -24 * 60 || new_offset_minutes > 24 * 60)
2492 		return EINVAL;
2493 	if (new_offset_minutes == old_offset_minutes)
2494 		return 0;
2495 
2496 	utc_offset = new_offset_minutes * 60;	/* minutes -> seconds */
2497 	adjustment_seconds = (new_offset_minutes - old_offset_minutes) * 60;
2498 
2499 	nanotime(&now);
2500 	adjusted = now;
2501 	adjusted.tv_sec -= adjustment_seconds;
2502 	tc_setrealtimeclock(&adjusted);
2503 	resettodr();
2504 
2505 	return 0;
2506 }
2507