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