xref: /openbsd-src/sys/kern/kern_sysctl.c (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /*	$OpenBSD: kern_sysctl.c,v 1.379 2020/09/01 01:53:50 gnezdo 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_SYSCTL
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_SYSCTL
813 /*
814  * Debugging related system variables.
815  */
816 extern struct ctldebug debug_vfs_busyprt;
817 struct ctldebug debug1, 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 	&debug_vfs_busyprt,
823 	&debug1, &debug2, &debug3, &debug4,
824 	&debug5, &debug6, &debug7, &debug8, &debug9,
825 	&debug10, &debug11, &debug12, &debug13, &debug14,
826 	&debug15, &debug16, &debug17, &debug18, &debug19,
827 };
828 int
829 debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
830     size_t newlen, struct proc *p)
831 {
832 	struct ctldebug *cdp;
833 
834 	/* all sysctl names at this level are name and field */
835 	if (namelen != 2)
836 		return (ENOTDIR);		/* overloaded */
837 	if (name[0] < 0 || name[0] >= nitems(debugvars))
838 		return (EOPNOTSUPP);
839 	cdp = debugvars[name[0]];
840 	if (cdp->debugname == 0)
841 		return (EOPNOTSUPP);
842 	switch (name[1]) {
843 	case CTL_DEBUG_NAME:
844 		return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
845 	case CTL_DEBUG_VALUE:
846 		return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
847 	default:
848 		return (EOPNOTSUPP);
849 	}
850 	/* NOTREACHED */
851 }
852 #endif /* DEBUG_SYSCTL */
853 
854 /*
855  * Reads, or writes that lower the value
856  */
857 int
858 sysctl_int_lower(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
859 {
860 	unsigned int oval = *valp, val = *valp;
861 	int error;
862 
863 	if (newp == NULL)
864 		return (sysctl_rdint(oldp, oldlenp, newp, *valp));
865 
866 	if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &val)))
867 		return (error);
868 	if (val > oval)
869 		return (EPERM);		/* do not allow raising */
870 	*(unsigned int *)valp = val;
871 	return (0);
872 }
873 
874 /*
875  * Validate parameters and get old / set new parameters
876  * for an integer-valued sysctl function.
877  */
878 int
879 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
880 {
881 	return (sysctl_int_bounded(oldp, oldlenp, newp, newlen, valp, 0, 0));
882 }
883 
884 int
885 sysctl_int_bounded(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
886     int *valp, int minimum, int maximum)
887 {
888 	int error = 0;
889 	int val;
890 
891 	if (oldp && *oldlenp < sizeof(int))
892 		return (ENOMEM);
893 	if (newp && newlen != sizeof(int))
894 		return (EINVAL);
895 	*oldlenp = sizeof(int);
896 	val = *valp;
897 	if (oldp)
898 		error = copyout(&val, oldp, sizeof(int));
899 	if (error == 0 && newp)
900 		error = copyin(newp, &val, sizeof(int));
901 	if (error)
902 		return (error);
903 	if (minimum == maximum || (minimum <= val && val <= maximum))
904 		*valp = val;
905 	else
906 		error = EINVAL;
907 	return (error);
908 }
909 
910 /*
911  * As above, but read-only.
912  */
913 int
914 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
915 {
916 	int error = 0;
917 
918 	if (oldp && *oldlenp < sizeof(int))
919 		return (ENOMEM);
920 	if (newp)
921 		return (EPERM);
922 	*oldlenp = sizeof(int);
923 	if (oldp)
924 		error = copyout((caddr_t)&val, oldp, sizeof(int));
925 	return (error);
926 }
927 
928 /*
929  * Array of bounded integer values.
930  */
931 int
932 sysctl_bounded_arr(const struct sysctl_bounded_args *valpp, u_int valplen,
933     int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
934     size_t newlen)
935 {
936 	u_int i;
937 	if (namelen != 1)
938 		return (ENOTDIR);
939 	for (i = 0; i < valplen; ++i) {
940 		if (valpp[i].mib == name[0]) {
941 			return (sysctl_int_bounded(oldp, oldlenp, newp, newlen,
942 			    valpp[i].var, valpp[i].minimum, valpp[i].maximum));
943 		}
944 	}
945 	return (EOPNOTSUPP);
946 }
947 
948 /*
949  * Validate parameters and get old / set new parameters
950  * for an integer-valued sysctl function.
951  */
952 int
953 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
954     int64_t *valp)
955 {
956 	int error = 0;
957 
958 	if (oldp && *oldlenp < sizeof(int64_t))
959 		return (ENOMEM);
960 	if (newp && newlen != sizeof(int64_t))
961 		return (EINVAL);
962 	*oldlenp = sizeof(int64_t);
963 	if (oldp)
964 		error = copyout(valp, oldp, sizeof(int64_t));
965 	if (error == 0 && newp)
966 		error = copyin(newp, valp, sizeof(int64_t));
967 	return (error);
968 }
969 
970 /*
971  * As above, but read-only.
972  */
973 int
974 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, int64_t val)
975 {
976 	int error = 0;
977 
978 	if (oldp && *oldlenp < sizeof(int64_t))
979 		return (ENOMEM);
980 	if (newp)
981 		return (EPERM);
982 	*oldlenp = sizeof(int64_t);
983 	if (oldp)
984 		error = copyout((caddr_t)&val, oldp, sizeof(int64_t));
985 	return (error);
986 }
987 
988 /*
989  * Validate parameters and get old / set new parameters
990  * for a string-valued sysctl function.
991  */
992 int
993 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
994     size_t maxlen)
995 {
996 	return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 0);
997 }
998 
999 int
1000 sysctl_tstring(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
1001     char *str, size_t maxlen)
1002 {
1003 	return sysctl__string(oldp, oldlenp, newp, newlen, str, maxlen, 1);
1004 }
1005 
1006 int
1007 sysctl__string(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
1008     char *str, size_t maxlen, int trunc)
1009 {
1010 	size_t len;
1011 	int error = 0;
1012 
1013 	len = strlen(str) + 1;
1014 	if (oldp && *oldlenp < len) {
1015 		if (trunc == 0 || *oldlenp == 0)
1016 			return (ENOMEM);
1017 	}
1018 	if (newp && newlen >= maxlen)
1019 		return (EINVAL);
1020 	if (oldp) {
1021 		if (trunc && *oldlenp < len) {
1022 			len = *oldlenp;
1023 			error = copyout(str, oldp, len - 1);
1024 			if (error == 0)
1025 				error = copyout("", (char *)oldp + len - 1, 1);
1026 		} else {
1027 			error = copyout(str, oldp, len);
1028 		}
1029 	}
1030 	*oldlenp = len;
1031 	if (error == 0 && newp) {
1032 		error = copyin(newp, str, newlen);
1033 		str[newlen] = 0;
1034 	}
1035 	return (error);
1036 }
1037 
1038 /*
1039  * As above, but read-only.
1040  */
1041 int
1042 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
1043 {
1044 	size_t len;
1045 	int error = 0;
1046 
1047 	len = strlen(str) + 1;
1048 	if (oldp && *oldlenp < len)
1049 		return (ENOMEM);
1050 	if (newp)
1051 		return (EPERM);
1052 	*oldlenp = len;
1053 	if (oldp)
1054 		error = copyout(str, oldp, len);
1055 	return (error);
1056 }
1057 
1058 /*
1059  * Validate parameters and get old / set new parameters
1060  * for a structure oriented sysctl function.
1061  */
1062 int
1063 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
1064     size_t len)
1065 {
1066 	int error = 0;
1067 
1068 	if (oldp && *oldlenp < len)
1069 		return (ENOMEM);
1070 	if (newp && newlen > len)
1071 		return (EINVAL);
1072 	if (oldp) {
1073 		*oldlenp = len;
1074 		error = copyout(sp, oldp, len);
1075 	}
1076 	if (error == 0 && newp)
1077 		error = copyin(newp, sp, len);
1078 	return (error);
1079 }
1080 
1081 /*
1082  * Validate parameters and get old parameters
1083  * for a structure oriented sysctl function.
1084  */
1085 int
1086 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1087     size_t len)
1088 {
1089 	int error = 0;
1090 
1091 	if (oldp && *oldlenp < len)
1092 		return (ENOMEM);
1093 	if (newp)
1094 		return (EPERM);
1095 	*oldlenp = len;
1096 	if (oldp)
1097 		error = copyout(sp, oldp, len);
1098 	return (error);
1099 }
1100 
1101 #ifndef SMALL_KERNEL
1102 void
1103 fill_file(struct kinfo_file *kf, struct file *fp, struct filedesc *fdp,
1104 	  int fd, struct vnode *vp, struct process *pr, struct proc *p,
1105 	  struct socket *so, int show_pointers)
1106 {
1107 	struct vattr va;
1108 
1109 	memset(kf, 0, sizeof(*kf));
1110 
1111 	kf->fd_fd = fd;		/* might not really be an fd */
1112 
1113 	if (fp != NULL) {
1114 		if (show_pointers)
1115 			kf->f_fileaddr = PTRTOINT64(fp);
1116 		kf->f_flag = fp->f_flag;
1117 		kf->f_iflags = fp->f_iflags;
1118 		kf->f_type = fp->f_type;
1119 		kf->f_count = fp->f_count;
1120 		if (show_pointers)
1121 			kf->f_ucred = PTRTOINT64(fp->f_cred);
1122 		kf->f_uid = fp->f_cred->cr_uid;
1123 		kf->f_gid = fp->f_cred->cr_gid;
1124 		if (show_pointers)
1125 			kf->f_ops = PTRTOINT64(fp->f_ops);
1126 		if (show_pointers)
1127 			kf->f_data = PTRTOINT64(fp->f_data);
1128 		kf->f_usecount = 0;
1129 
1130 		if (suser(p) == 0 || p->p_ucred->cr_uid == fp->f_cred->cr_uid) {
1131 			mtx_enter(&fp->f_mtx);
1132 			kf->f_offset = fp->f_offset;
1133 			kf->f_rxfer = fp->f_rxfer;
1134 			kf->f_rwfer = fp->f_wxfer;
1135 			kf->f_seek = fp->f_seek;
1136 			kf->f_rbytes = fp->f_rbytes;
1137 			kf->f_wbytes = fp->f_wbytes;
1138 			mtx_leave(&fp->f_mtx);
1139 		} else
1140 			kf->f_offset = -1;
1141 	} else if (vp != NULL) {
1142 		/* fake it */
1143 		kf->f_type = DTYPE_VNODE;
1144 		kf->f_flag = FREAD;
1145 		if (fd == KERN_FILE_TRACE)
1146 			kf->f_flag |= FWRITE;
1147 	} else if (so != NULL) {
1148 		/* fake it */
1149 		kf->f_type = DTYPE_SOCKET;
1150 	}
1151 
1152 	/* information about the object associated with this file */
1153 	switch (kf->f_type) {
1154 	case DTYPE_VNODE:
1155 		if (fp != NULL)
1156 			vp = (struct vnode *)fp->f_data;
1157 
1158 		if (show_pointers)
1159 			kf->v_un = PTRTOINT64(vp->v_un.vu_socket);
1160 		kf->v_type = vp->v_type;
1161 		kf->v_tag = vp->v_tag;
1162 		kf->v_flag = vp->v_flag;
1163 		if (show_pointers)
1164 			kf->v_data = PTRTOINT64(vp->v_data);
1165 		if (show_pointers)
1166 			kf->v_mount = PTRTOINT64(vp->v_mount);
1167 		if (vp->v_mount)
1168 			strlcpy(kf->f_mntonname,
1169 			    vp->v_mount->mnt_stat.f_mntonname,
1170 			    sizeof(kf->f_mntonname));
1171 
1172 		if (VOP_GETATTR(vp, &va, p->p_ucred, p) == 0) {
1173 			kf->va_fileid = va.va_fileid;
1174 			kf->va_mode = MAKEIMODE(va.va_type, va.va_mode);
1175 			kf->va_size = va.va_size;
1176 			kf->va_rdev = va.va_rdev;
1177 			kf->va_fsid = va.va_fsid & 0xffffffff;
1178 			kf->va_nlink = va.va_nlink;
1179 		}
1180 		break;
1181 
1182 	case DTYPE_SOCKET: {
1183 		int locked = 0;
1184 
1185 		if (so == NULL) {
1186 			so = (struct socket *)fp->f_data;
1187 			/* if so is passed as parameter it is already locked */
1188 			switch (so->so_proto->pr_domain->dom_family) {
1189 			case AF_INET:
1190 			case AF_INET6:
1191 				NET_LOCK();
1192 				locked = 1;
1193 				break;
1194 			}
1195 		}
1196 
1197 		kf->so_type = so->so_type;
1198 		kf->so_state = so->so_state;
1199 		if (show_pointers)
1200 			kf->so_pcb = PTRTOINT64(so->so_pcb);
1201 		else
1202 			kf->so_pcb = -1;
1203 		kf->so_protocol = so->so_proto->pr_protocol;
1204 		kf->so_family = so->so_proto->pr_domain->dom_family;
1205 		kf->so_rcv_cc = so->so_rcv.sb_cc;
1206 		kf->so_snd_cc = so->so_snd.sb_cc;
1207 		if (isspliced(so)) {
1208 			if (show_pointers)
1209 				kf->so_splice =
1210 				    PTRTOINT64(so->so_sp->ssp_socket);
1211 			kf->so_splicelen = so->so_sp->ssp_len;
1212 		} else if (issplicedback(so))
1213 			kf->so_splicelen = -1;
1214 		if (so->so_pcb == NULL) {
1215 			if (locked)
1216 				NET_UNLOCK();
1217 			break;
1218 		}
1219 		switch (kf->so_family) {
1220 		case AF_INET: {
1221 			struct inpcb *inpcb = so->so_pcb;
1222 
1223 			NET_ASSERT_LOCKED();
1224 			if (show_pointers)
1225 				kf->inp_ppcb = PTRTOINT64(inpcb->inp_ppcb);
1226 			kf->inp_lport = inpcb->inp_lport;
1227 			kf->inp_laddru[0] = inpcb->inp_laddr.s_addr;
1228 			kf->inp_fport = inpcb->inp_fport;
1229 			kf->inp_faddru[0] = inpcb->inp_faddr.s_addr;
1230 			kf->inp_rtableid = inpcb->inp_rtableid;
1231 			if (so->so_type == SOCK_RAW)
1232 				kf->inp_proto = inpcb->inp_ip.ip_p;
1233 			if (so->so_proto->pr_protocol == IPPROTO_TCP) {
1234 				struct tcpcb *tcpcb = (void *)inpcb->inp_ppcb;
1235 				kf->t_rcv_wnd = tcpcb->rcv_wnd;
1236 				kf->t_snd_wnd = tcpcb->snd_wnd;
1237 				kf->t_snd_cwnd = tcpcb->snd_cwnd;
1238 				kf->t_state = tcpcb->t_state;
1239 			}
1240 			break;
1241 		    }
1242 		case AF_INET6: {
1243 			struct inpcb *inpcb = so->so_pcb;
1244 
1245 			NET_ASSERT_LOCKED();
1246 			if (show_pointers)
1247 				kf->inp_ppcb = PTRTOINT64(inpcb->inp_ppcb);
1248 			kf->inp_lport = inpcb->inp_lport;
1249 			kf->inp_laddru[0] = inpcb->inp_laddr6.s6_addr32[0];
1250 			kf->inp_laddru[1] = inpcb->inp_laddr6.s6_addr32[1];
1251 			kf->inp_laddru[2] = inpcb->inp_laddr6.s6_addr32[2];
1252 			kf->inp_laddru[3] = inpcb->inp_laddr6.s6_addr32[3];
1253 			kf->inp_fport = inpcb->inp_fport;
1254 			kf->inp_faddru[0] = inpcb->inp_faddr6.s6_addr32[0];
1255 			kf->inp_faddru[1] = inpcb->inp_faddr6.s6_addr32[1];
1256 			kf->inp_faddru[2] = inpcb->inp_faddr6.s6_addr32[2];
1257 			kf->inp_faddru[3] = inpcb->inp_faddr6.s6_addr32[3];
1258 			kf->inp_rtableid = inpcb->inp_rtableid;
1259 			if (so->so_type == SOCK_RAW)
1260 				kf->inp_proto = inpcb->inp_ipv6.ip6_nxt;
1261 			if (so->so_proto->pr_protocol == IPPROTO_TCP) {
1262 				struct tcpcb *tcpcb = (void *)inpcb->inp_ppcb;
1263 				kf->t_rcv_wnd = tcpcb->rcv_wnd;
1264 				kf->t_snd_wnd = tcpcb->snd_wnd;
1265 				kf->t_state = tcpcb->t_state;
1266 			}
1267 			break;
1268 		    }
1269 		case AF_UNIX: {
1270 			struct unpcb *unpcb = so->so_pcb;
1271 
1272 			kf->f_msgcount = unpcb->unp_msgcount;
1273 			if (show_pointers) {
1274 				kf->unp_conn	= PTRTOINT64(unpcb->unp_conn);
1275 				kf->unp_refs	= PTRTOINT64(
1276 				    SLIST_FIRST(&unpcb->unp_refs));
1277 				kf->unp_nextref	= PTRTOINT64(
1278 				    SLIST_NEXT(unpcb, unp_nextref));
1279 				kf->v_un	= PTRTOINT64(unpcb->unp_vnode);
1280 				kf->unp_addr	= PTRTOINT64(unpcb->unp_addr);
1281 			}
1282 			if (unpcb->unp_addr != NULL) {
1283 				struct sockaddr_un *un = mtod(unpcb->unp_addr,
1284 				    struct sockaddr_un *);
1285 				memcpy(kf->unp_path, un->sun_path, un->sun_len
1286 				    - offsetof(struct sockaddr_un,sun_path));
1287 			}
1288 			break;
1289 		    }
1290 		}
1291 		if (locked)
1292 			NET_UNLOCK();
1293 		break;
1294 	    }
1295 
1296 	case DTYPE_PIPE: {
1297 		struct pipe *pipe = (struct pipe *)fp->f_data;
1298 
1299 		if (show_pointers)
1300 			kf->pipe_peer = PTRTOINT64(pipe->pipe_peer);
1301 		kf->pipe_state = pipe->pipe_state;
1302 		break;
1303 	    }
1304 
1305 	case DTYPE_KQUEUE: {
1306 		struct kqueue *kqi = (struct kqueue *)fp->f_data;
1307 
1308 		kf->kq_count = kqi->kq_count;
1309 		kf->kq_state = kqi->kq_state;
1310 		break;
1311 	    }
1312 	}
1313 
1314 	/* per-process information for KERN_FILE_BY[PU]ID */
1315 	if (pr != NULL) {
1316 		kf->p_pid = pr->ps_pid;
1317 		kf->p_uid = pr->ps_ucred->cr_uid;
1318 		kf->p_gid = pr->ps_ucred->cr_gid;
1319 		kf->p_tid = -1;
1320 		strlcpy(kf->p_comm, pr->ps_comm, sizeof(kf->p_comm));
1321 	}
1322 	if (fdp != NULL) {
1323 		fdplock(fdp);
1324 		kf->fd_ofileflags = fdp->fd_ofileflags[fd];
1325 		fdpunlock(fdp);
1326 	}
1327 }
1328 
1329 /*
1330  * Get file structures.
1331  */
1332 int
1333 sysctl_file(int *name, u_int namelen, char *where, size_t *sizep,
1334     struct proc *p)
1335 {
1336 	struct kinfo_file *kf;
1337 	struct filedesc *fdp;
1338 	struct file *fp;
1339 	struct process *pr;
1340 	size_t buflen, elem_size, elem_count, outsize;
1341 	char *dp = where;
1342 	int arg, i, error = 0, needed = 0, matched;
1343 	u_int op;
1344 	int show_pointers;
1345 
1346 	if (namelen > 4)
1347 		return (ENOTDIR);
1348 	if (namelen < 4 || name[2] > sizeof(*kf))
1349 		return (EINVAL);
1350 
1351 	buflen = where != NULL ? *sizep : 0;
1352 	op = name[0];
1353 	arg = name[1];
1354 	elem_size = name[2];
1355 	elem_count = name[3];
1356 	outsize = MIN(sizeof(*kf), elem_size);
1357 
1358 	if (elem_size < 1)
1359 		return (EINVAL);
1360 
1361 	show_pointers = suser(curproc) == 0;
1362 
1363 	kf = malloc(sizeof(*kf), M_TEMP, M_WAITOK);
1364 
1365 #define FILLIT2(fp, fdp, i, vp, pr, so) do {				\
1366 	if (buflen >= elem_size && elem_count > 0) {			\
1367 		fill_file(kf, fp, fdp, i, vp, pr, p, so, show_pointers);\
1368 		error = copyout(kf, dp, outsize);			\
1369 		if (error)						\
1370 			break;						\
1371 		dp += elem_size;					\
1372 		buflen -= elem_size;					\
1373 		elem_count--;						\
1374 	}								\
1375 	needed += elem_size;						\
1376 } while (0)
1377 #define FILLIT(fp, fdp, i, vp, pr) \
1378 	FILLIT2(fp, fdp, i, vp, pr, NULL)
1379 #define FILLSO(so) \
1380 	FILLIT2(NULL, NULL, 0, NULL, NULL, so)
1381 
1382 	switch (op) {
1383 	case KERN_FILE_BYFILE:
1384 		/* use the inp-tables to pick up closed connections, too */
1385 		if (arg == DTYPE_SOCKET) {
1386 			struct inpcb *inp;
1387 
1388 			NET_LOCK();
1389 			TAILQ_FOREACH(inp, &tcbtable.inpt_queue, inp_queue)
1390 				FILLSO(inp->inp_socket);
1391 			TAILQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue)
1392 				FILLSO(inp->inp_socket);
1393 			TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue)
1394 				FILLSO(inp->inp_socket);
1395 #ifdef INET6
1396 			TAILQ_FOREACH(inp, &rawin6pcbtable.inpt_queue,
1397 			    inp_queue)
1398 				FILLSO(inp->inp_socket);
1399 #endif
1400 			NET_UNLOCK();
1401 		}
1402 		fp = NULL;
1403 		while ((fp = fd_iterfile(fp, p)) != NULL) {
1404 			if ((arg == 0 || fp->f_type == arg)) {
1405 				int af, skip = 0;
1406 				if (arg == DTYPE_SOCKET && fp->f_type == arg) {
1407 					af = ((struct socket *)fp->f_data)->
1408 					    so_proto->pr_domain->dom_family;
1409 					if (af == AF_INET || af == AF_INET6)
1410 						skip = 1;
1411 				}
1412 				if (!skip)
1413 					FILLIT(fp, NULL, 0, NULL, NULL);
1414 			}
1415 		}
1416 		break;
1417 	case KERN_FILE_BYPID:
1418 		/* A arg of -1 indicates all processes */
1419 		if (arg < -1) {
1420 			error = EINVAL;
1421 			break;
1422 		}
1423 		matched = 0;
1424 		LIST_FOREACH(pr, &allprocess, ps_list) {
1425 			/*
1426 			 * skip system, exiting, embryonic and undead
1427 			 * processes
1428 			 */
1429 			if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
1430 				continue;
1431 			if (arg > 0 && pr->ps_pid != (pid_t)arg) {
1432 				/* not the pid we are looking for */
1433 				continue;
1434 			}
1435 			matched = 1;
1436 			fdp = pr->ps_fd;
1437 			if (pr->ps_textvp)
1438 				FILLIT(NULL, NULL, KERN_FILE_TEXT, pr->ps_textvp, pr);
1439 			if (fdp->fd_cdir)
1440 				FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr);
1441 			if (fdp->fd_rdir)
1442 				FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr);
1443 			if (pr->ps_tracevp)
1444 				FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr);
1445 			for (i = 0; i < fdp->fd_nfiles; i++) {
1446 				if ((fp = fd_getfile(fdp, i)) == NULL)
1447 					continue;
1448 				FILLIT(fp, fdp, i, NULL, pr);
1449 				FRELE(fp, p);
1450 			}
1451 		}
1452 		if (!matched)
1453 			error = ESRCH;
1454 		break;
1455 	case KERN_FILE_BYUID:
1456 		LIST_FOREACH(pr, &allprocess, ps_list) {
1457 			/*
1458 			 * skip system, exiting, embryonic and undead
1459 			 * processes
1460 			 */
1461 			if (pr->ps_flags & (PS_SYSTEM | PS_EMBRYO | PS_EXITING))
1462 				continue;
1463 			if (arg >= 0 && pr->ps_ucred->cr_uid != (uid_t)arg) {
1464 				/* not the uid we are looking for */
1465 				continue;
1466 			}
1467 			fdp = pr->ps_fd;
1468 			if (fdp->fd_cdir)
1469 				FILLIT(NULL, NULL, KERN_FILE_CDIR, fdp->fd_cdir, pr);
1470 			if (fdp->fd_rdir)
1471 				FILLIT(NULL, NULL, KERN_FILE_RDIR, fdp->fd_rdir, pr);
1472 			if (pr->ps_tracevp)
1473 				FILLIT(NULL, NULL, KERN_FILE_TRACE, pr->ps_tracevp, pr);
1474 			for (i = 0; i < fdp->fd_nfiles; i++) {
1475 				if ((fp = fd_getfile(fdp, i)) == NULL)
1476 					continue;
1477 				FILLIT(fp, fdp, i, NULL, pr);
1478 				FRELE(fp, p);
1479 			}
1480 		}
1481 		break;
1482 	default:
1483 		error = EINVAL;
1484 		break;
1485 	}
1486 	free(kf, M_TEMP, sizeof(*kf));
1487 
1488 	if (!error) {
1489 		if (where == NULL)
1490 			needed += KERN_FILESLOP * elem_size;
1491 		else if (*sizep < needed)
1492 			error = ENOMEM;
1493 		*sizep = needed;
1494 	}
1495 
1496 	return (error);
1497 }
1498 
1499 /*
1500  * try over estimating by 5 procs
1501  */
1502 #define KERN_PROCSLOP	5
1503 
1504 int
1505 sysctl_doproc(int *name, u_int namelen, char *where, size_t *sizep)
1506 {
1507 	struct kinfo_proc *kproc = NULL;
1508 	struct proc *p;
1509 	struct process *pr;
1510 	char *dp;
1511 	int arg, buflen, doingzomb, elem_size, elem_count;
1512 	int error, needed, op;
1513 	int dothreads = 0;
1514 	int show_pointers;
1515 
1516 	dp = where;
1517 	buflen = where != NULL ? *sizep : 0;
1518 	needed = error = 0;
1519 
1520 	if (namelen != 4 || name[2] <= 0 || name[3] < 0 ||
1521 	    name[2] > sizeof(*kproc))
1522 		return (EINVAL);
1523 	op = name[0];
1524 	arg = name[1];
1525 	elem_size = name[2];
1526 	elem_count = name[3];
1527 
1528 	dothreads = op & KERN_PROC_SHOW_THREADS;
1529 	op &= ~KERN_PROC_SHOW_THREADS;
1530 
1531 	show_pointers = suser(curproc) == 0;
1532 
1533 	if (where != NULL)
1534 		kproc = malloc(sizeof(*kproc), M_TEMP, M_WAITOK);
1535 
1536 	pr = LIST_FIRST(&allprocess);
1537 	doingzomb = 0;
1538 again:
1539 	for (; pr != NULL; pr = LIST_NEXT(pr, ps_list)) {
1540 		/* XXX skip processes in the middle of being zapped */
1541 		if (pr->ps_pgrp == NULL)
1542 			continue;
1543 
1544 		/*
1545 		 * Skip embryonic processes.
1546 		 */
1547 		if (pr->ps_flags & PS_EMBRYO)
1548 			continue;
1549 
1550 		/*
1551 		 * TODO - make more efficient (see notes below).
1552 		 */
1553 		switch (op) {
1554 
1555 		case KERN_PROC_PID:
1556 			/* could do this with just a lookup */
1557 			if (pr->ps_pid != (pid_t)arg)
1558 				continue;
1559 			break;
1560 
1561 		case KERN_PROC_PGRP:
1562 			/* could do this by traversing pgrp */
1563 			if (pr->ps_pgrp->pg_id != (pid_t)arg)
1564 				continue;
1565 			break;
1566 
1567 		case KERN_PROC_SESSION:
1568 			if (pr->ps_session->s_leader == NULL ||
1569 			    pr->ps_session->s_leader->ps_pid != (pid_t)arg)
1570 				continue;
1571 			break;
1572 
1573 		case KERN_PROC_TTY:
1574 			if ((pr->ps_flags & PS_CONTROLT) == 0 ||
1575 			    pr->ps_session->s_ttyp == NULL ||
1576 			    pr->ps_session->s_ttyp->t_dev != (dev_t)arg)
1577 				continue;
1578 			break;
1579 
1580 		case KERN_PROC_UID:
1581 			if (pr->ps_ucred->cr_uid != (uid_t)arg)
1582 				continue;
1583 			break;
1584 
1585 		case KERN_PROC_RUID:
1586 			if (pr->ps_ucred->cr_ruid != (uid_t)arg)
1587 				continue;
1588 			break;
1589 
1590 		case KERN_PROC_ALL:
1591 			if (pr->ps_flags & PS_SYSTEM)
1592 				continue;
1593 			break;
1594 
1595 		case KERN_PROC_KTHREAD:
1596 			/* no filtering */
1597 			break;
1598 
1599 		default:
1600 			error = EINVAL;
1601 			goto err;
1602 		}
1603 
1604 		if (buflen >= elem_size && elem_count > 0) {
1605 			fill_kproc(pr, kproc, NULL, show_pointers);
1606 			error = copyout(kproc, dp, elem_size);
1607 			if (error)
1608 				goto err;
1609 			dp += elem_size;
1610 			buflen -= elem_size;
1611 			elem_count--;
1612 		}
1613 		needed += elem_size;
1614 
1615 		/* Skip per-thread entries if not required by op */
1616 		if (!dothreads)
1617 			continue;
1618 
1619 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
1620 			if (buflen >= elem_size && elem_count > 0) {
1621 				fill_kproc(pr, kproc, p, show_pointers);
1622 				error = copyout(kproc, dp, elem_size);
1623 				if (error)
1624 					goto err;
1625 				dp += elem_size;
1626 				buflen -= elem_size;
1627 				elem_count--;
1628 			}
1629 			needed += elem_size;
1630 		}
1631 	}
1632 	if (doingzomb == 0) {
1633 		pr = LIST_FIRST(&zombprocess);
1634 		doingzomb++;
1635 		goto again;
1636 	}
1637 	if (where != NULL) {
1638 		*sizep = dp - where;
1639 		if (needed > *sizep) {
1640 			error = ENOMEM;
1641 			goto err;
1642 		}
1643 	} else {
1644 		needed += KERN_PROCSLOP * elem_size;
1645 		*sizep = needed;
1646 	}
1647 err:
1648 	if (kproc)
1649 		free(kproc, M_TEMP, sizeof(*kproc));
1650 	return (error);
1651 }
1652 
1653 /*
1654  * Fill in a kproc structure for the specified process.
1655  */
1656 void
1657 fill_kproc(struct process *pr, struct kinfo_proc *ki, struct proc *p,
1658     int show_pointers)
1659 {
1660 	struct session *s = pr->ps_session;
1661 	struct tty *tp;
1662 	struct vmspace *vm = pr->ps_vmspace;
1663 	struct timespec booted, st, ut, utc;
1664 	int isthread;
1665 
1666 	isthread = p != NULL;
1667 	if (!isthread)
1668 		p = pr->ps_mainproc;		/* XXX */
1669 
1670 	FILL_KPROC(ki, strlcpy, p, pr, pr->ps_ucred, pr->ps_pgrp,
1671 	    p, pr, s, vm, pr->ps_limit, pr->ps_sigacts, isthread,
1672 	    show_pointers);
1673 
1674 	/* stuff that's too painful to generalize into the macros */
1675 	if (pr->ps_pptr)
1676 		ki->p_ppid = pr->ps_pptr->ps_pid;
1677 	if (s->s_leader)
1678 		ki->p_sid = s->s_leader->ps_pid;
1679 
1680 	if ((pr->ps_flags & PS_CONTROLT) && (tp = s->s_ttyp)) {
1681 		ki->p_tdev = tp->t_dev;
1682 		ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : -1;
1683 		if (show_pointers)
1684 			ki->p_tsess = PTRTOINT64(tp->t_session);
1685 	} else {
1686 		ki->p_tdev = NODEV;
1687 		ki->p_tpgid = -1;
1688 	}
1689 
1690 	/* fixups that can only be done in the kernel */
1691 	if ((pr->ps_flags & PS_ZOMBIE) == 0) {
1692 		if ((pr->ps_flags & PS_EMBRYO) == 0 && vm != NULL)
1693 			ki->p_vm_rssize = vm_resident_count(vm);
1694 		calctsru(isthread ? &p->p_tu : &pr->ps_tu, &ut, &st, NULL);
1695 		ki->p_uutime_sec = ut.tv_sec;
1696 		ki->p_uutime_usec = ut.tv_nsec/1000;
1697 		ki->p_ustime_sec = st.tv_sec;
1698 		ki->p_ustime_usec = st.tv_nsec/1000;
1699 
1700 		/* Convert starting uptime to a starting UTC time. */
1701 		nanoboottime(&booted);
1702 		timespecadd(&booted, &pr->ps_start, &utc);
1703 		ki->p_ustart_sec = utc.tv_sec;
1704 		ki->p_ustart_usec = utc.tv_nsec / 1000;
1705 
1706 #ifdef MULTIPROCESSOR
1707 		if (p->p_cpu != NULL)
1708 			ki->p_cpuid = CPU_INFO_UNIT(p->p_cpu);
1709 #endif
1710 	}
1711 
1712 	/* get %cpu and schedule state: just one thread or sum of all? */
1713 	if (isthread) {
1714 		ki->p_pctcpu = p->p_pctcpu;
1715 		ki->p_stat   = p->p_stat;
1716 	} else {
1717 		ki->p_pctcpu = 0;
1718 		ki->p_stat = (pr->ps_flags & PS_ZOMBIE) ? SDEAD : SIDL;
1719 		TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) {
1720 			ki->p_pctcpu += p->p_pctcpu;
1721 			/* find best state: ONPROC > RUN > STOP > SLEEP > .. */
1722 			if (p->p_stat == SONPROC || ki->p_stat == SONPROC)
1723 				ki->p_stat = SONPROC;
1724 			else if (p->p_stat == SRUN || ki->p_stat == SRUN)
1725 				ki->p_stat = SRUN;
1726 			else if (p->p_stat == SSTOP || ki->p_stat == SSTOP)
1727 				ki->p_stat = SSTOP;
1728 			else if (p->p_stat == SSLEEP)
1729 				ki->p_stat = SSLEEP;
1730 		}
1731 	}
1732 }
1733 
1734 int
1735 sysctl_proc_args(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1736     struct proc *cp)
1737 {
1738 	struct process *vpr;
1739 	pid_t pid;
1740 	struct ps_strings pss;
1741 	struct iovec iov;
1742 	struct uio uio;
1743 	int error, cnt, op;
1744 	size_t limit;
1745 	char **rargv, **vargv;		/* reader vs. victim */
1746 	char *rarg, *varg, *buf;
1747 	struct vmspace *vm;
1748 	vaddr_t ps_strings;
1749 
1750 	if (namelen > 2)
1751 		return (ENOTDIR);
1752 	if (namelen < 2)
1753 		return (EINVAL);
1754 
1755 	pid = name[0];
1756 	op = name[1];
1757 
1758 	switch (op) {
1759 	case KERN_PROC_ARGV:
1760 	case KERN_PROC_NARGV:
1761 	case KERN_PROC_ENV:
1762 	case KERN_PROC_NENV:
1763 		break;
1764 	default:
1765 		return (EOPNOTSUPP);
1766 	}
1767 
1768 	if ((vpr = prfind(pid)) == NULL)
1769 		return (ESRCH);
1770 
1771 	if (oldp == NULL) {
1772 		if (op == KERN_PROC_NARGV || op == KERN_PROC_NENV)
1773 			*oldlenp = sizeof(int);
1774 		else
1775 			*oldlenp = ARG_MAX;	/* XXX XXX XXX */
1776 		return (0);
1777 	}
1778 
1779 	/* Either system process or exiting/zombie */
1780 	if (vpr->ps_flags & (PS_SYSTEM | PS_EXITING))
1781 		return (EINVAL);
1782 
1783 	/* Execing - danger. */
1784 	if ((vpr->ps_flags & PS_INEXEC))
1785 		return (EBUSY);
1786 
1787 	/* Only owner or root can get env */
1788 	if ((op == KERN_PROC_NENV || op == KERN_PROC_ENV) &&
1789 	    (vpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid &&
1790 	    (error = suser(cp)) != 0))
1791 		return (error);
1792 
1793 	ps_strings = vpr->ps_strings;
1794 	vm = vpr->ps_vmspace;
1795 	vm->vm_refcnt++;
1796 	vpr = NULL;
1797 
1798 	buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
1799 
1800 	iov.iov_base = &pss;
1801 	iov.iov_len = sizeof(pss);
1802 	uio.uio_iov = &iov;
1803 	uio.uio_iovcnt = 1;
1804 	uio.uio_offset = (off_t)ps_strings;
1805 	uio.uio_resid = sizeof(pss);
1806 	uio.uio_segflg = UIO_SYSSPACE;
1807 	uio.uio_rw = UIO_READ;
1808 	uio.uio_procp = cp;
1809 
1810 	if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0)
1811 		goto out;
1812 
1813 	if (op == KERN_PROC_NARGV) {
1814 		error = sysctl_rdint(oldp, oldlenp, NULL, pss.ps_nargvstr);
1815 		goto out;
1816 	}
1817 	if (op == KERN_PROC_NENV) {
1818 		error = sysctl_rdint(oldp, oldlenp, NULL, pss.ps_nenvstr);
1819 		goto out;
1820 	}
1821 
1822 	if (op == KERN_PROC_ARGV) {
1823 		cnt = pss.ps_nargvstr;
1824 		vargv = pss.ps_argvstr;
1825 	} else {
1826 		cnt = pss.ps_nenvstr;
1827 		vargv = pss.ps_envstr;
1828 	}
1829 
1830 	/* -1 to have space for a terminating NUL */
1831 	limit = *oldlenp - 1;
1832 	*oldlenp = 0;
1833 
1834 	rargv = oldp;
1835 
1836 	/*
1837 	 * *oldlenp - number of bytes copied out into readers buffer.
1838 	 * limit - maximal number of bytes allowed into readers buffer.
1839 	 * rarg - pointer into readers buffer where next arg will be stored.
1840 	 * rargv - pointer into readers buffer where the next rarg pointer
1841 	 *  will be stored.
1842 	 * vargv - pointer into victim address space where the next argument
1843 	 *  will be read.
1844 	 */
1845 
1846 	/* space for cnt pointers and a NULL */
1847 	rarg = (char *)(rargv + cnt + 1);
1848 	*oldlenp += (cnt + 1) * sizeof(char **);
1849 
1850 	while (cnt > 0 && *oldlenp < limit) {
1851 		size_t len, vstrlen;
1852 
1853 		/* Write to readers argv */
1854 		if ((error = copyout(&rarg, rargv, sizeof(rarg))) != 0)
1855 			goto out;
1856 
1857 		/* read the victim argv */
1858 		iov.iov_base = &varg;
1859 		iov.iov_len = sizeof(varg);
1860 		uio.uio_iov = &iov;
1861 		uio.uio_iovcnt = 1;
1862 		uio.uio_offset = (off_t)(vaddr_t)vargv;
1863 		uio.uio_resid = sizeof(varg);
1864 		uio.uio_segflg = UIO_SYSSPACE;
1865 		uio.uio_rw = UIO_READ;
1866 		uio.uio_procp = cp;
1867 		if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0)
1868 			goto out;
1869 
1870 		if (varg == NULL)
1871 			break;
1872 
1873 		/*
1874 		 * read the victim arg. We must jump through hoops to avoid
1875 		 * crossing a page boundary too much and returning an error.
1876 		 */
1877 more:
1878 		len = PAGE_SIZE - (((vaddr_t)varg) & PAGE_MASK);
1879 		/* leave space for the terminating NUL */
1880 		iov.iov_base = buf;
1881 		iov.iov_len = len;
1882 		uio.uio_iov = &iov;
1883 		uio.uio_iovcnt = 1;
1884 		uio.uio_offset = (off_t)(vaddr_t)varg;
1885 		uio.uio_resid = len;
1886 		uio.uio_segflg = UIO_SYSSPACE;
1887 		uio.uio_rw = UIO_READ;
1888 		uio.uio_procp = cp;
1889 		if ((error = uvm_io(&vm->vm_map, &uio, 0)) != 0)
1890 			goto out;
1891 
1892 		for (vstrlen = 0; vstrlen < len; vstrlen++) {
1893 			if (buf[vstrlen] == '\0')
1894 				break;
1895 		}
1896 
1897 		/* Don't overflow readers buffer. */
1898 		if (*oldlenp + vstrlen + 1 >= limit) {
1899 			error = ENOMEM;
1900 			goto out;
1901 		}
1902 
1903 		if ((error = copyout(buf, rarg, vstrlen)) != 0)
1904 			goto out;
1905 
1906 		*oldlenp += vstrlen;
1907 		rarg += vstrlen;
1908 
1909 		/* The string didn't end in this page? */
1910 		if (vstrlen == len) {
1911 			varg += vstrlen;
1912 			goto more;
1913 		}
1914 
1915 		/* End of string. Terminate it with a NUL */
1916 		buf[0] = '\0';
1917 		if ((error = copyout(buf, rarg, 1)) != 0)
1918 			goto out;
1919 		*oldlenp += 1;
1920 		rarg += 1;
1921 
1922 		vargv++;
1923 		rargv++;
1924 		cnt--;
1925 	}
1926 
1927 	if (*oldlenp >= limit) {
1928 		error = ENOMEM;
1929 		goto out;
1930 	}
1931 
1932 	/* Write the terminating null */
1933 	rarg = NULL;
1934 	error = copyout(&rarg, rargv, sizeof(rarg));
1935 
1936 out:
1937 	uvmspace_free(vm);
1938 	free(buf, M_TEMP, PAGE_SIZE);
1939 	return (error);
1940 }
1941 
1942 int
1943 sysctl_proc_cwd(int *name, u_int namelen, void *oldp, size_t *oldlenp,
1944     struct proc *cp)
1945 {
1946 	struct process *findpr;
1947 	struct vnode *vp;
1948 	pid_t pid;
1949 	int error;
1950 	size_t lenused, len;
1951 	char *path, *bp, *bend;
1952 
1953 	if (namelen > 1)
1954 		return (ENOTDIR);
1955 	if (namelen < 1)
1956 		return (EINVAL);
1957 
1958 	pid = name[0];
1959 	if ((findpr = prfind(pid)) == NULL)
1960 		return (ESRCH);
1961 
1962 	if (oldp == NULL) {
1963 		*oldlenp = MAXPATHLEN * 4;
1964 		return (0);
1965 	}
1966 
1967 	/* Either system process or exiting/zombie */
1968 	if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING))
1969 		return (EINVAL);
1970 
1971 	/* Only owner or root can get cwd */
1972 	if (findpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid &&
1973 	    (error = suser(cp)) != 0)
1974 		return (error);
1975 
1976 	len = *oldlenp;
1977 	if (len > MAXPATHLEN * 4)
1978 		len = MAXPATHLEN * 4;
1979 	else if (len < 2)
1980 		return (ERANGE);
1981 	*oldlenp = 0;
1982 
1983 	/* snag a reference to the vnode before we can sleep */
1984 	vp = findpr->ps_fd->fd_cdir;
1985 	vref(vp);
1986 
1987 	path = malloc(len, M_TEMP, M_WAITOK);
1988 
1989 	bp = &path[len];
1990 	bend = bp;
1991 	*(--bp) = '\0';
1992 
1993 	/* Same as sys__getcwd */
1994 	error = vfs_getcwd_common(vp, NULL,
1995 	    &bp, path, len / 2, GETCWD_CHECK_ACCESS, cp);
1996 	if (error == 0) {
1997 		*oldlenp = lenused = bend - bp;
1998 		error = copyout(bp, oldp, lenused);
1999 	}
2000 
2001 	vrele(vp);
2002 	free(path, M_TEMP, len);
2003 
2004 	return (error);
2005 }
2006 
2007 int
2008 sysctl_proc_nobroadcastkill(int *name, u_int namelen, void *newp, size_t newlen,
2009     void *oldp, size_t *oldlenp, struct proc *cp)
2010 {
2011 	struct process *findpr;
2012 	pid_t pid;
2013 	int error, flag;
2014 
2015 	if (namelen > 1)
2016 		return (ENOTDIR);
2017 	if (namelen < 1)
2018 		return (EINVAL);
2019 
2020 	pid = name[0];
2021 	if ((findpr = prfind(pid)) == NULL)
2022 		return (ESRCH);
2023 
2024 	/* Either system process or exiting/zombie */
2025 	if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING))
2026 		return (EINVAL);
2027 
2028 	/* Only root can change PS_NOBROADCASTKILL */
2029 	if (newp != 0 && (error = suser(cp)) != 0)
2030 		return (error);
2031 
2032 	/* get the PS_NOBROADCASTKILL flag */
2033 	flag = findpr->ps_flags & PS_NOBROADCASTKILL ? 1 : 0;
2034 
2035 	error = sysctl_int(oldp, oldlenp, newp, newlen, &flag);
2036 	if (error == 0 && newp) {
2037 		if (flag)
2038 			atomic_setbits_int(&findpr->ps_flags,
2039 			    PS_NOBROADCASTKILL);
2040 		else
2041 			atomic_clearbits_int(&findpr->ps_flags,
2042 			    PS_NOBROADCASTKILL);
2043 	}
2044 
2045 	return (error);
2046 }
2047 
2048 /* Arbitrary but reasonable limit for one iteration. */
2049 #define	VMMAP_MAXLEN	MAXPHYS
2050 
2051 int
2052 sysctl_proc_vmmap(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2053     struct proc *cp)
2054 {
2055 	struct process *findpr;
2056 	pid_t pid;
2057 	int error;
2058 	size_t oldlen, len;
2059 	struct kinfo_vmentry *kve, *ukve;
2060 	u_long *ustart, start;
2061 
2062 	if (namelen > 1)
2063 		return (ENOTDIR);
2064 	if (namelen < 1)
2065 		return (EINVAL);
2066 
2067 	/* Provide max buffer length as hint. */
2068 	if (oldp == NULL) {
2069 		if (oldlenp == NULL)
2070 			return (EINVAL);
2071 		else {
2072 			*oldlenp = VMMAP_MAXLEN;
2073 			return (0);
2074 		}
2075 	}
2076 
2077 	pid = name[0];
2078 	if (pid == cp->p_p->ps_pid) {
2079 		/* Self process mapping. */
2080 		findpr = cp->p_p;
2081 	} else if (pid > 0) {
2082 		if ((findpr = prfind(pid)) == NULL)
2083 			return (ESRCH);
2084 
2085 		/* Either system process or exiting/zombie */
2086 		if (findpr->ps_flags & (PS_SYSTEM | PS_EXITING))
2087 			return (EINVAL);
2088 
2089 #if 1
2090 		/* XXX Allow only root for now */
2091 		if ((error = suser(cp)) != 0)
2092 			return (error);
2093 #else
2094 		/* Only owner or root can get vmmap */
2095 		if (findpr->ps_ucred->cr_uid != cp->p_ucred->cr_uid &&
2096 		    (error = suser(cp)) != 0)
2097 			return (error);
2098 #endif
2099 	} else {
2100 		/* Only root can get kernel_map */
2101 		if ((error = suser(cp)) != 0)
2102 			return (error);
2103 		findpr = NULL;
2104 	}
2105 
2106 	/* Check the given size. */
2107 	oldlen = *oldlenp;
2108 	if (oldlen == 0 || oldlen % sizeof(*kve) != 0)
2109 		return (EINVAL);
2110 
2111 	/* Deny huge allocation. */
2112 	if (oldlen > VMMAP_MAXLEN)
2113 		return (EINVAL);
2114 
2115 	/*
2116 	 * Iterate from the given address passed as the first element's
2117 	 * kve_start via oldp.
2118 	 */
2119 	ukve = (struct kinfo_vmentry *)oldp;
2120 	ustart = &ukve->kve_start;
2121 	error = copyin(ustart, &start, sizeof(start));
2122 	if (error != 0)
2123 		return (error);
2124 
2125 	/* Allocate wired memory to not block. */
2126 	kve = malloc(oldlen, M_TEMP, M_WAITOK);
2127 
2128 	/* Set the base address and read entries. */
2129 	kve[0].kve_start = start;
2130 	len = oldlen;
2131 	error = fill_vmmap(findpr, kve, &len);
2132 	if (error != 0 && error != ENOMEM)
2133 		goto done;
2134 	if (len == 0)
2135 		goto done;
2136 
2137 	KASSERT(len <= oldlen);
2138 	KASSERT((len % sizeof(struct kinfo_vmentry)) == 0);
2139 
2140 	error = copyout(kve, oldp, len);
2141 
2142 done:
2143 	*oldlenp = len;
2144 
2145 	free(kve, M_TEMP, oldlen);
2146 
2147 	return (error);
2148 }
2149 #endif
2150 
2151 /*
2152  * Initialize disknames/diskstats for export by sysctl. If update is set,
2153  * then we simply update the disk statistics information.
2154  */
2155 int
2156 sysctl_diskinit(int update, struct proc *p)
2157 {
2158 	struct diskstats *sdk;
2159 	struct disk *dk;
2160 	const char *duid;
2161 	int i, tlen, l;
2162 
2163 	if ((i = rw_enter(&sysctl_disklock, RW_WRITE|RW_INTR)) != 0)
2164 		return i;
2165 
2166 	if (disk_change) {
2167 		for (dk = TAILQ_FIRST(&disklist), tlen = 0; dk;
2168 		    dk = TAILQ_NEXT(dk, dk_link)) {
2169 			if (dk->dk_name)
2170 				tlen += strlen(dk->dk_name);
2171 			tlen += 18;	/* label uid + separators */
2172 		}
2173 		tlen++;
2174 
2175 		if (disknames)
2176 			free(disknames, M_SYSCTL, disknameslen);
2177 		if (diskstats)
2178 			free(diskstats, M_SYSCTL, diskstatslen);
2179 		diskstats = NULL;
2180 		disknames = NULL;
2181 		diskstats = mallocarray(disk_count, sizeof(struct diskstats),
2182 		    M_SYSCTL, M_WAITOK|M_ZERO);
2183 		diskstatslen = disk_count * sizeof(struct diskstats);
2184 		disknames = malloc(tlen, M_SYSCTL, M_WAITOK|M_ZERO);
2185 		disknameslen = tlen;
2186 		disknames[0] = '\0';
2187 
2188 		for (dk = TAILQ_FIRST(&disklist), i = 0, l = 0; dk;
2189 		    dk = TAILQ_NEXT(dk, dk_link), i++) {
2190 			duid = NULL;
2191 			if (dk->dk_label && !duid_iszero(dk->dk_label->d_uid))
2192 				duid = duid_format(dk->dk_label->d_uid);
2193 			snprintf(disknames + l, tlen - l, "%s:%s,",
2194 			    dk->dk_name ? dk->dk_name : "",
2195 			    duid ? duid : "");
2196 			l += strlen(disknames + l);
2197 			sdk = diskstats + i;
2198 			strlcpy(sdk->ds_name, dk->dk_name,
2199 			    sizeof(sdk->ds_name));
2200 			mtx_enter(&dk->dk_mtx);
2201 			sdk->ds_busy = dk->dk_busy;
2202 			sdk->ds_rxfer = dk->dk_rxfer;
2203 			sdk->ds_wxfer = dk->dk_wxfer;
2204 			sdk->ds_seek = dk->dk_seek;
2205 			sdk->ds_rbytes = dk->dk_rbytes;
2206 			sdk->ds_wbytes = dk->dk_wbytes;
2207 			sdk->ds_attachtime = dk->dk_attachtime;
2208 			sdk->ds_timestamp = dk->dk_timestamp;
2209 			sdk->ds_time = dk->dk_time;
2210 			mtx_leave(&dk->dk_mtx);
2211 		}
2212 
2213 		/* Eliminate trailing comma */
2214 		if (l != 0)
2215 			disknames[l - 1] = '\0';
2216 		disk_change = 0;
2217 	} else if (update) {
2218 		/* Just update, number of drives hasn't changed */
2219 		for (dk = TAILQ_FIRST(&disklist), i = 0; dk;
2220 		    dk = TAILQ_NEXT(dk, dk_link), i++) {
2221 			sdk = diskstats + i;
2222 			strlcpy(sdk->ds_name, dk->dk_name,
2223 			    sizeof(sdk->ds_name));
2224 			mtx_enter(&dk->dk_mtx);
2225 			sdk->ds_busy = dk->dk_busy;
2226 			sdk->ds_rxfer = dk->dk_rxfer;
2227 			sdk->ds_wxfer = dk->dk_wxfer;
2228 			sdk->ds_seek = dk->dk_seek;
2229 			sdk->ds_rbytes = dk->dk_rbytes;
2230 			sdk->ds_wbytes = dk->dk_wbytes;
2231 			sdk->ds_attachtime = dk->dk_attachtime;
2232 			sdk->ds_timestamp = dk->dk_timestamp;
2233 			sdk->ds_time = dk->dk_time;
2234 			mtx_leave(&dk->dk_mtx);
2235 		}
2236 	}
2237 	rw_exit_write(&sysctl_disklock);
2238 	return 0;
2239 }
2240 
2241 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
2242 int
2243 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
2244 {
2245 #ifdef SYSVSEM
2246 	struct sem_sysctl_info *semsi;
2247 #endif
2248 #ifdef SYSVSHM
2249 	struct shm_sysctl_info *shmsi;
2250 #endif
2251 	size_t infosize, dssize, tsize, buflen, bufsiz;
2252 	int i, nds, error, ret;
2253 	void *buf;
2254 
2255 	if (namelen != 1)
2256 		return (EINVAL);
2257 
2258 	buflen = *sizep;
2259 
2260 	switch (*name) {
2261 	case KERN_SYSVIPC_MSG_INFO:
2262 #ifdef SYSVMSG
2263 		return (sysctl_sysvmsg(name, namelen, where, sizep));
2264 #else
2265 		return (EOPNOTSUPP);
2266 #endif
2267 	case KERN_SYSVIPC_SEM_INFO:
2268 #ifdef SYSVSEM
2269 		infosize = sizeof(semsi->seminfo);
2270 		nds = seminfo.semmni;
2271 		dssize = sizeof(semsi->semids[0]);
2272 		break;
2273 #else
2274 		return (EOPNOTSUPP);
2275 #endif
2276 	case KERN_SYSVIPC_SHM_INFO:
2277 #ifdef SYSVSHM
2278 		infosize = sizeof(shmsi->shminfo);
2279 		nds = shminfo.shmmni;
2280 		dssize = sizeof(shmsi->shmids[0]);
2281 		break;
2282 #else
2283 		return (EOPNOTSUPP);
2284 #endif
2285 	default:
2286 		return (EINVAL);
2287 	}
2288 	tsize = infosize + (nds * dssize);
2289 
2290 	/* Return just the total size required. */
2291 	if (where == NULL) {
2292 		*sizep = tsize;
2293 		return (0);
2294 	}
2295 
2296 	/* Not enough room for even the info struct. */
2297 	if (buflen < infosize) {
2298 		*sizep = 0;
2299 		return (ENOMEM);
2300 	}
2301 	bufsiz = min(tsize, buflen);
2302 	buf = malloc(bufsiz, M_TEMP, M_WAITOK|M_ZERO);
2303 
2304 	switch (*name) {
2305 #ifdef SYSVSEM
2306 	case KERN_SYSVIPC_SEM_INFO:
2307 		semsi = (struct sem_sysctl_info *)buf;
2308 		semsi->seminfo = seminfo;
2309 		break;
2310 #endif
2311 #ifdef SYSVSHM
2312 	case KERN_SYSVIPC_SHM_INFO:
2313 		shmsi = (struct shm_sysctl_info *)buf;
2314 		shmsi->shminfo = shminfo;
2315 		break;
2316 #endif
2317 	}
2318 	buflen -= infosize;
2319 
2320 	ret = 0;
2321 	if (buflen > 0) {
2322 		/* Fill in the IPC data structures.  */
2323 		for (i = 0; i < nds; i++) {
2324 			if (buflen < dssize) {
2325 				ret = ENOMEM;
2326 				break;
2327 			}
2328 			switch (*name) {
2329 #ifdef SYSVSEM
2330 			case KERN_SYSVIPC_SEM_INFO:
2331 				if (sema[i] != NULL)
2332 					memcpy(&semsi->semids[i], sema[i],
2333 					    dssize);
2334 				else
2335 					memset(&semsi->semids[i], 0, dssize);
2336 				break;
2337 #endif
2338 #ifdef SYSVSHM
2339 			case KERN_SYSVIPC_SHM_INFO:
2340 				if (shmsegs[i] != NULL)
2341 					memcpy(&shmsi->shmids[i], shmsegs[i],
2342 					    dssize);
2343 				else
2344 					memset(&shmsi->shmids[i], 0, dssize);
2345 				break;
2346 #endif
2347 			}
2348 			buflen -= dssize;
2349 		}
2350 	}
2351 	*sizep -= buflen;
2352 	error = copyout(buf, where, *sizep);
2353 	free(buf, M_TEMP, bufsiz);
2354 	/* If copyout succeeded, use return code set earlier. */
2355 	return (error ? error : ret);
2356 }
2357 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
2358 
2359 #ifndef	SMALL_KERNEL
2360 
2361 int
2362 sysctl_intrcnt(int *name, u_int namelen, void *oldp, size_t *oldlenp)
2363 {
2364 	return (evcount_sysctl(name, namelen, oldp, oldlenp, NULL, 0));
2365 }
2366 
2367 
2368 int
2369 sysctl_sensors(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2370     void *newp, size_t newlen)
2371 {
2372 	struct ksensor *ks;
2373 	struct sensor *us;
2374 	struct ksensordev *ksd;
2375 	struct sensordev *usd;
2376 	int dev, numt, ret;
2377 	enum sensor_type type;
2378 
2379 	if (namelen != 1 && namelen != 3)
2380 		return (ENOTDIR);
2381 
2382 	dev = name[0];
2383 	if (namelen == 1) {
2384 		ret = sensordev_get(dev, &ksd);
2385 		if (ret)
2386 			return (ret);
2387 
2388 		/* Grab a copy, to clear the kernel pointers */
2389 		usd = malloc(sizeof(*usd), M_TEMP, M_WAITOK|M_ZERO);
2390 		usd->num = ksd->num;
2391 		strlcpy(usd->xname, ksd->xname, sizeof(usd->xname));
2392 		memcpy(usd->maxnumt, ksd->maxnumt, sizeof(usd->maxnumt));
2393 		usd->sensors_count = ksd->sensors_count;
2394 
2395 		ret = sysctl_rdstruct(oldp, oldlenp, newp, usd,
2396 		    sizeof(struct sensordev));
2397 
2398 		free(usd, M_TEMP, sizeof(*usd));
2399 		return (ret);
2400 	}
2401 
2402 	type = name[1];
2403 	numt = name[2];
2404 
2405 	ret = sensor_find(dev, type, numt, &ks);
2406 	if (ret)
2407 		return (ret);
2408 
2409 	/* Grab a copy, to clear the kernel pointers */
2410 	us = malloc(sizeof(*us), M_TEMP, M_WAITOK|M_ZERO);
2411 	memcpy(us->desc, ks->desc, sizeof(us->desc));
2412 	us->tv = ks->tv;
2413 	us->value = ks->value;
2414 	us->type = ks->type;
2415 	us->status = ks->status;
2416 	us->numt = ks->numt;
2417 	us->flags = ks->flags;
2418 
2419 	ret = sysctl_rdstruct(oldp, oldlenp, newp, us,
2420 	    sizeof(struct sensor));
2421 	free(us, M_TEMP, sizeof(*us));
2422 	return (ret);
2423 }
2424 #endif	/* SMALL_KERNEL */
2425 
2426 int
2427 sysctl_cptime2(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2428     void *newp, size_t newlen)
2429 {
2430 	CPU_INFO_ITERATOR cii;
2431 	struct cpu_info *ci;
2432 	int found = 0;
2433 
2434 	if (namelen != 1)
2435 		return (ENOTDIR);
2436 
2437 	CPU_INFO_FOREACH(cii, ci) {
2438 		if (name[0] == CPU_INFO_UNIT(ci)) {
2439 			found = 1;
2440 			break;
2441 		}
2442 	}
2443 	if (!found)
2444 		return (ENOENT);
2445 
2446 	return (sysctl_rdstruct(oldp, oldlenp, newp,
2447 	    &ci->ci_schedstate.spc_cp_time,
2448 	    sizeof(ci->ci_schedstate.spc_cp_time)));
2449 }
2450 
2451 #if NAUDIO > 0
2452 int
2453 sysctl_audio(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2454     void *newp, size_t newlen)
2455 {
2456 	if (namelen != 1)
2457 		return (ENOTDIR);
2458 
2459 	if (name[0] != KERN_AUDIO_RECORD)
2460 		return (ENOENT);
2461 
2462 	return (sysctl_int(oldp, oldlenp, newp, newlen, &audio_record_enable));
2463 }
2464 #endif
2465 
2466 int
2467 sysctl_cpustats(int *name, u_int namelen, void *oldp, size_t *oldlenp,
2468     void *newp, size_t newlen)
2469 {
2470 	CPU_INFO_ITERATOR cii;
2471 	struct cpustats cs;
2472 	struct cpu_info *ci;
2473 	int found = 0;
2474 
2475 	if (namelen != 1)
2476 		return (ENOTDIR);
2477 
2478 	CPU_INFO_FOREACH(cii, ci) {
2479 		if (name[0] == CPU_INFO_UNIT(ci)) {
2480 			found = 1;
2481 			break;
2482 		}
2483 	}
2484 	if (!found)
2485 		return (ENOENT);
2486 
2487 	memcpy(&cs.cs_time, &ci->ci_schedstate.spc_cp_time, sizeof(cs.cs_time));
2488 	cs.cs_flags = 0;
2489 	if (cpu_is_online(ci))
2490 		cs.cs_flags |= CPUSTATS_ONLINE;
2491 
2492 	return (sysctl_rdstruct(oldp, oldlenp, newp, &cs, sizeof(cs)));
2493 }
2494 
2495 int
2496 sysctl_utc_offset(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
2497 {
2498 	struct timespec adjusted, now;
2499 	int adjustment_seconds, error, new_offset_minutes, old_offset_minutes;
2500 
2501 	old_offset_minutes = utc_offset / 60;	/* seconds -> minutes */
2502 	if (securelevel > 0)
2503 		return sysctl_rdint(oldp, oldlenp, newp, old_offset_minutes);
2504 
2505 	new_offset_minutes = old_offset_minutes;
2506 	error = sysctl_int(oldp, oldlenp, newp, newlen, &new_offset_minutes);
2507 	if (error)
2508 		return error;
2509 	if (new_offset_minutes < -24 * 60 || new_offset_minutes > 24 * 60)
2510 		return EINVAL;
2511 	if (new_offset_minutes == old_offset_minutes)
2512 		return 0;
2513 
2514 	utc_offset = new_offset_minutes * 60;	/* minutes -> seconds */
2515 	adjustment_seconds = (new_offset_minutes - old_offset_minutes) * 60;
2516 
2517 	nanotime(&now);
2518 	adjusted = now;
2519 	adjusted.tv_sec -= adjustment_seconds;
2520 	tc_setrealtimeclock(&adjusted);
2521 	resettodr();
2522 
2523 	return 0;
2524 }
2525