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