init_sysctl.c revision 1.125.2.4 1 /* $NetBSD: init_sysctl.c,v 1.125.2.4 2008/11/20 20:45:38 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2003, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Brown, and by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: init_sysctl.c,v 1.125.2.4 2008/11/20 20:45:38 christos Exp $");
34
35 #include "opt_sysv.h"
36 #include "opt_compat_netbsd32.h"
37 #include "opt_compat_netbsd.h"
38 #include "opt_sa.h"
39 #include "opt_posix.h"
40 #include "pty.h"
41 #include "rnd.h"
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/sysctl.h>
46 #include <sys/cpu.h>
47 #include <sys/errno.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/unistd.h>
51 #include <sys/disklabel.h>
52 #include <sys/rnd.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/msgbuf.h>
57 #include <dev/cons.h>
58 #include <sys/socketvar.h>
59 #include <sys/file.h>
60 #include <sys/filedesc.h>
61 #include <sys/tty.h>
62 #include <sys/malloc.h>
63 #include <sys/resource.h>
64 #include <sys/resourcevar.h>
65 #include <sys/exec.h>
66 #include <sys/conf.h>
67 #include <sys/device.h>
68 #include <sys/stat.h>
69 #include <sys/kauth.h>
70 #include <sys/ktrace.h>
71 #include <sys/ksem.h>
72
73 #include <miscfs/specfs/specdev.h>
74
75 #ifdef COMPAT_NETBSD32
76 #include <compat/netbsd32/netbsd32.h>
77 #endif
78 #ifdef COMPAT_50
79 #include <compat/sys/time.h>
80 #endif
81
82 #ifdef KERN_SA
83 #include <sys/sa.h>
84 #endif
85
86 #include <sys/cpu.h>
87
88 #if defined(MODULAR) || defined(P1003_1B_SEMAPHORE)
89 int posix_semaphores = 200112;
90 #else
91 int posix_semaphores;
92 #endif
93
94 int security_setidcore_dump;
95 char security_setidcore_path[MAXPATHLEN] = "/var/crash/%n.core";
96 uid_t security_setidcore_owner = 0;
97 gid_t security_setidcore_group = 0;
98 mode_t security_setidcore_mode = (S_IRUSR|S_IWUSR);
99
100 static const u_int sysctl_flagmap[] = {
101 PK_ADVLOCK, P_ADVLOCK,
102 PK_EXEC, P_EXEC,
103 PK_NOCLDWAIT, P_NOCLDWAIT,
104 PK_32, P_32,
105 PK_CLDSIGIGN, P_CLDSIGIGN,
106 PK_SUGID, P_SUGID,
107 0
108 };
109
110 static const u_int sysctl_sflagmap[] = {
111 PS_NOCLDSTOP, P_NOCLDSTOP,
112 PS_WEXIT, P_WEXIT,
113 PS_STOPFORK, P_STOPFORK,
114 PS_STOPEXEC, P_STOPEXEC,
115 PS_STOPEXIT, P_STOPEXIT,
116 0
117 };
118
119 static const u_int sysctl_slflagmap[] = {
120 PSL_TRACED, P_TRACED,
121 PSL_FSTRACE, P_FSTRACE,
122 PSL_CHTRACED, P_CHTRACED,
123 PSL_SYSCALL, P_SYSCALL,
124 0
125 };
126
127 static const u_int sysctl_lflagmap[] = {
128 PL_CONTROLT, P_CONTROLT,
129 PL_PPWAIT, P_PPWAIT,
130 0
131 };
132
133 static const u_int sysctl_stflagmap[] = {
134 PST_PROFIL, P_PROFIL,
135 0
136
137 };
138
139 static const u_int sysctl_lwpflagmap[] = {
140 LW_INMEM, P_INMEM,
141 LW_SINTR, P_SINTR,
142 LW_SYSTEM, P_SYSTEM,
143 LW_SA, P_SA, /* WRS ??? */
144 0
145 };
146
147 static const u_int sysctl_lwpprflagmap[] = {
148 LPR_DETACHED, L_DETACHED,
149 0
150 };
151
152 /*
153 * try over estimating by 5 procs/lwps
154 */
155 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
156 #define KERN_LWPSLOP (5 * sizeof(struct kinfo_lwp))
157
158 static int dcopyout(struct lwp *, const void *, void *, size_t);
159
160 static int
161 dcopyout(struct lwp *l, const void *kaddr, void *uaddr, size_t len)
162 {
163 int error;
164
165 error = copyout(kaddr, uaddr, len);
166 ktrmibio(-1, UIO_READ, uaddr, len, error);
167
168 return error;
169 }
170
171 #ifdef DIAGNOSTIC
172 static int sysctl_kern_trigger_panic(SYSCTLFN_PROTO);
173 #endif
174 static int sysctl_kern_maxvnodes(SYSCTLFN_PROTO);
175 static int sysctl_kern_rtc_offset(SYSCTLFN_PROTO);
176 static int sysctl_kern_maxproc(SYSCTLFN_PROTO);
177 static int sysctl_kern_hostid(SYSCTLFN_PROTO);
178 static int sysctl_setlen(SYSCTLFN_PROTO);
179 static int sysctl_kern_clockrate(SYSCTLFN_PROTO);
180 static int sysctl_kern_file(SYSCTLFN_PROTO);
181 static int sysctl_msgbuf(SYSCTLFN_PROTO);
182 static int sysctl_kern_defcorename(SYSCTLFN_PROTO);
183 static int sysctl_kern_cptime(SYSCTLFN_PROTO);
184 #if NPTY > 0
185 static int sysctl_kern_maxptys(SYSCTLFN_PROTO);
186 #endif /* NPTY > 0 */
187 static int sysctl_kern_sbmax(SYSCTLFN_PROTO);
188 static int sysctl_kern_urnd(SYSCTLFN_PROTO);
189 static int sysctl_kern_arnd(SYSCTLFN_PROTO);
190 static int sysctl_kern_lwp(SYSCTLFN_PROTO);
191 static int sysctl_kern_forkfsleep(SYSCTLFN_PROTO);
192 static int sysctl_kern_root_partition(SYSCTLFN_PROTO);
193 static int sysctl_kern_drivers(SYSCTLFN_PROTO);
194 static int sysctl_kern_file2(SYSCTLFN_PROTO);
195 static int sysctl_security_setidcore(SYSCTLFN_PROTO);
196 static int sysctl_security_setidcorename(SYSCTLFN_PROTO);
197 static int sysctl_kern_cpid(SYSCTLFN_PROTO);
198 static int sysctl_doeproc(SYSCTLFN_PROTO);
199 static int sysctl_kern_proc_args(SYSCTLFN_PROTO);
200 static int sysctl_hw_usermem(SYSCTLFN_PROTO);
201 static int sysctl_hw_cnmagic(SYSCTLFN_PROTO);
202
203 static u_int sysctl_map_flags(const u_int *, u_int);
204 static void fill_kproc2(struct proc *, struct kinfo_proc2 *, bool);
205 static void fill_lwp(struct lwp *l, struct kinfo_lwp *kl);
206 static void fill_file(struct kinfo_file *, const file_t *, const fdfile_t *,
207 int, pid_t);
208
209 /*
210 * ********************************************************************
211 * section 1: setup routines
212 * ********************************************************************
213 * These functions are stuffed into a link set for sysctl setup
214 * functions. They're never called or referenced from anywhere else.
215 * ********************************************************************
216 */
217
218 /*
219 * sets up the base nodes...
220 */
221 SYSCTL_SETUP(sysctl_root_setup, "sysctl base setup")
222 {
223
224 sysctl_createv(clog, 0, NULL, NULL,
225 CTLFLAG_PERMANENT,
226 CTLTYPE_NODE, "kern",
227 SYSCTL_DESCR("High kernel"),
228 NULL, 0, NULL, 0,
229 CTL_KERN, CTL_EOL);
230 sysctl_createv(clog, 0, NULL, NULL,
231 CTLFLAG_PERMANENT,
232 CTLTYPE_NODE, "vm",
233 SYSCTL_DESCR("Virtual memory"),
234 NULL, 0, NULL, 0,
235 CTL_VM, CTL_EOL);
236 sysctl_createv(clog, 0, NULL, NULL,
237 CTLFLAG_PERMANENT,
238 CTLTYPE_NODE, "vfs",
239 SYSCTL_DESCR("Filesystem"),
240 NULL, 0, NULL, 0,
241 CTL_VFS, CTL_EOL);
242 sysctl_createv(clog, 0, NULL, NULL,
243 CTLFLAG_PERMANENT,
244 CTLTYPE_NODE, "net",
245 SYSCTL_DESCR("Networking"),
246 NULL, 0, NULL, 0,
247 CTL_NET, CTL_EOL);
248 sysctl_createv(clog, 0, NULL, NULL,
249 CTLFLAG_PERMANENT,
250 CTLTYPE_NODE, "debug",
251 SYSCTL_DESCR("Debugging"),
252 NULL, 0, NULL, 0,
253 CTL_DEBUG, CTL_EOL);
254 sysctl_createv(clog, 0, NULL, NULL,
255 CTLFLAG_PERMANENT,
256 CTLTYPE_NODE, "hw",
257 SYSCTL_DESCR("Generic CPU, I/O"),
258 NULL, 0, NULL, 0,
259 CTL_HW, CTL_EOL);
260 sysctl_createv(clog, 0, NULL, NULL,
261 CTLFLAG_PERMANENT,
262 CTLTYPE_NODE, "machdep",
263 SYSCTL_DESCR("Machine dependent"),
264 NULL, 0, NULL, 0,
265 CTL_MACHDEP, CTL_EOL);
266 /*
267 * this node is inserted so that the sysctl nodes in libc can
268 * operate.
269 */
270 sysctl_createv(clog, 0, NULL, NULL,
271 CTLFLAG_PERMANENT,
272 CTLTYPE_NODE, "user",
273 SYSCTL_DESCR("User-level"),
274 NULL, 0, NULL, 0,
275 CTL_USER, CTL_EOL);
276 sysctl_createv(clog, 0, NULL, NULL,
277 CTLFLAG_PERMANENT,
278 CTLTYPE_NODE, "ddb",
279 SYSCTL_DESCR("In-kernel debugger"),
280 NULL, 0, NULL, 0,
281 CTL_DDB, CTL_EOL);
282 sysctl_createv(clog, 0, NULL, NULL,
283 CTLFLAG_PERMANENT,
284 CTLTYPE_NODE, "proc",
285 SYSCTL_DESCR("Per-process"),
286 NULL, 0, NULL, 0,
287 CTL_PROC, CTL_EOL);
288 sysctl_createv(clog, 0, NULL, NULL,
289 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
290 CTLTYPE_NODE, "vendor",
291 SYSCTL_DESCR("Vendor specific"),
292 NULL, 0, NULL, 0,
293 CTL_VENDOR, CTL_EOL);
294 sysctl_createv(clog, 0, NULL, NULL,
295 CTLFLAG_PERMANENT,
296 CTLTYPE_NODE, "emul",
297 SYSCTL_DESCR("Emulation settings"),
298 NULL, 0, NULL, 0,
299 CTL_EMUL, CTL_EOL);
300 sysctl_createv(clog, 0, NULL, NULL,
301 CTLFLAG_PERMANENT,
302 CTLTYPE_NODE, "security",
303 SYSCTL_DESCR("Security"),
304 NULL, 0, NULL, 0,
305 CTL_SECURITY, CTL_EOL);
306 }
307
308 /*
309 * this setup routine is a replacement for kern_sysctl()
310 */
311 SYSCTL_SETUP(sysctl_kern_setup, "sysctl kern subtree setup")
312 {
313 extern int kern_logsigexit; /* defined in kern/kern_sig.c */
314 extern fixpt_t ccpu; /* defined in kern/kern_synch.c */
315 extern int dumponpanic; /* defined in kern/subr_prf.c */
316 const struct sysctlnode *rnode;
317
318 sysctl_createv(clog, 0, NULL, NULL,
319 CTLFLAG_PERMANENT,
320 CTLTYPE_NODE, "kern", NULL,
321 NULL, 0, NULL, 0,
322 CTL_KERN, CTL_EOL);
323
324 sysctl_createv(clog, 0, NULL, NULL,
325 CTLFLAG_PERMANENT,
326 CTLTYPE_STRING, "ostype",
327 SYSCTL_DESCR("Operating system type"),
328 NULL, 0, &ostype, 0,
329 CTL_KERN, KERN_OSTYPE, CTL_EOL);
330 sysctl_createv(clog, 0, NULL, NULL,
331 CTLFLAG_PERMANENT,
332 CTLTYPE_STRING, "osrelease",
333 SYSCTL_DESCR("Operating system release"),
334 NULL, 0, &osrelease, 0,
335 CTL_KERN, KERN_OSRELEASE, CTL_EOL);
336 sysctl_createv(clog, 0, NULL, NULL,
337 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
338 CTLTYPE_INT, "osrevision",
339 SYSCTL_DESCR("Operating system revision"),
340 NULL, __NetBSD_Version__, NULL, 0,
341 CTL_KERN, KERN_OSREV, CTL_EOL);
342 sysctl_createv(clog, 0, NULL, NULL,
343 CTLFLAG_PERMANENT,
344 CTLTYPE_STRING, "version",
345 SYSCTL_DESCR("Kernel version"),
346 NULL, 0, &version, 0,
347 CTL_KERN, KERN_VERSION, CTL_EOL);
348 sysctl_createv(clog, 0, NULL, NULL,
349 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
350 CTLTYPE_INT, "maxvnodes",
351 SYSCTL_DESCR("Maximum number of vnodes"),
352 sysctl_kern_maxvnodes, 0, NULL, 0,
353 CTL_KERN, KERN_MAXVNODES, CTL_EOL);
354 sysctl_createv(clog, 0, NULL, NULL,
355 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
356 CTLTYPE_INT, "maxproc",
357 SYSCTL_DESCR("Maximum number of simultaneous processes"),
358 sysctl_kern_maxproc, 0, NULL, 0,
359 CTL_KERN, KERN_MAXPROC, CTL_EOL);
360 sysctl_createv(clog, 0, NULL, NULL,
361 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
362 CTLTYPE_INT, "maxfiles",
363 SYSCTL_DESCR("Maximum number of open files"),
364 NULL, 0, &maxfiles, 0,
365 CTL_KERN, KERN_MAXFILES, CTL_EOL);
366 sysctl_createv(clog, 0, NULL, NULL,
367 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
368 CTLTYPE_INT, "argmax",
369 SYSCTL_DESCR("Maximum number of bytes of arguments to "
370 "execve(2)"),
371 NULL, ARG_MAX, NULL, 0,
372 CTL_KERN, KERN_ARGMAX, CTL_EOL);
373 sysctl_createv(clog, 0, NULL, NULL,
374 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
375 CTLTYPE_STRING, "hostname",
376 SYSCTL_DESCR("System hostname"),
377 sysctl_setlen, 0, &hostname, MAXHOSTNAMELEN,
378 CTL_KERN, KERN_HOSTNAME, CTL_EOL);
379 sysctl_createv(clog, 0, NULL, NULL,
380 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
381 CTLTYPE_INT, "hostid",
382 SYSCTL_DESCR("System host ID number"),
383 sysctl_kern_hostid, 0, NULL, 0,
384 CTL_KERN, KERN_HOSTID, CTL_EOL);
385 sysctl_createv(clog, 0, NULL, NULL,
386 CTLFLAG_PERMANENT,
387 CTLTYPE_STRUCT, "clockrate",
388 SYSCTL_DESCR("Kernel clock rates"),
389 sysctl_kern_clockrate, 0, NULL,
390 sizeof(struct clockinfo),
391 CTL_KERN, KERN_CLOCKRATE, CTL_EOL);
392 sysctl_createv(clog, 0, NULL, NULL,
393 CTLFLAG_PERMANENT,
394 CTLTYPE_INT, "hardclock_ticks",
395 SYSCTL_DESCR("Number of hardclock ticks"),
396 NULL, 0, &hardclock_ticks, sizeof(hardclock_ticks),
397 CTL_KERN, KERN_HARDCLOCK_TICKS, CTL_EOL);
398 sysctl_createv(clog, 0, NULL, NULL,
399 CTLFLAG_PERMANENT,
400 CTLTYPE_STRUCT, "vnode",
401 SYSCTL_DESCR("System vnode table"),
402 sysctl_kern_vnode, 0, NULL, 0,
403 CTL_KERN, KERN_VNODE, CTL_EOL);
404 sysctl_createv(clog, 0, NULL, NULL,
405 CTLFLAG_PERMANENT,
406 CTLTYPE_STRUCT, "file",
407 SYSCTL_DESCR("System open file table"),
408 sysctl_kern_file, 0, NULL, 0,
409 CTL_KERN, KERN_FILE, CTL_EOL);
410 #ifndef GPROF
411 sysctl_createv(clog, 0, NULL, NULL,
412 CTLFLAG_PERMANENT,
413 CTLTYPE_NODE, "profiling",
414 SYSCTL_DESCR("Profiling information (not available)"),
415 sysctl_notavail, 0, NULL, 0,
416 CTL_KERN, KERN_PROF, CTL_EOL);
417 #endif
418 sysctl_createv(clog, 0, NULL, NULL,
419 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
420 CTLTYPE_INT, "posix1version",
421 SYSCTL_DESCR("Version of ISO/IEC 9945 (POSIX 1003.1) "
422 "with which the operating system attempts "
423 "to comply"),
424 NULL, _POSIX_VERSION, NULL, 0,
425 CTL_KERN, KERN_POSIX1, CTL_EOL);
426 sysctl_createv(clog, 0, NULL, NULL,
427 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
428 CTLTYPE_INT, "ngroups",
429 SYSCTL_DESCR("Maximum number of supplemental groups"),
430 NULL, NGROUPS_MAX, NULL, 0,
431 CTL_KERN, KERN_NGROUPS, CTL_EOL);
432 sysctl_createv(clog, 0, NULL, NULL,
433 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
434 CTLTYPE_INT, "job_control",
435 SYSCTL_DESCR("Whether job control is available"),
436 NULL, 1, NULL, 0,
437 CTL_KERN, KERN_JOB_CONTROL, CTL_EOL);
438 sysctl_createv(clog, 0, NULL, NULL,
439 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
440 CTLTYPE_INT, "saved_ids",
441 SYSCTL_DESCR("Whether POSIX saved set-group/user ID is "
442 "available"), NULL,
443 #ifdef _POSIX_SAVED_IDS
444 1,
445 #else /* _POSIX_SAVED_IDS */
446 0,
447 #endif /* _POSIX_SAVED_IDS */
448 NULL, 0, CTL_KERN, KERN_SAVED_IDS, CTL_EOL);
449 sysctl_createv(clog, 0, NULL, NULL,
450 CTLFLAG_PERMANENT,
451 CTLTYPE_STRUCT, "boottime",
452 SYSCTL_DESCR("System boot time"),
453 NULL, 0, &boottime, sizeof(boottime),
454 CTL_KERN, KERN_BOOTTIME, CTL_EOL);
455 #ifdef COMPAT_50
456 {
457 extern struct timeval50 boottime50;
458 sysctl_createv(clog, 0, NULL, NULL,
459 CTLFLAG_PERMANENT,
460 CTLTYPE_STRUCT, "oboottime",
461 SYSCTL_DESCR("System boot time"),
462 NULL, 0, &boottime50, sizeof(boottime50),
463 CTL_KERN, KERN_OBOOTTIME, CTL_EOL);
464 }
465 #endif
466 sysctl_createv(clog, 0, NULL, NULL,
467 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
468 CTLTYPE_STRING, "domainname",
469 SYSCTL_DESCR("YP domain name"),
470 sysctl_setlen, 0, &domainname, MAXHOSTNAMELEN,
471 CTL_KERN, KERN_DOMAINNAME, CTL_EOL);
472 sysctl_createv(clog, 0, NULL, NULL,
473 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
474 CTLTYPE_INT, "maxpartitions",
475 SYSCTL_DESCR("Maximum number of partitions allowed per "
476 "disk"),
477 NULL, MAXPARTITIONS, NULL, 0,
478 CTL_KERN, KERN_MAXPARTITIONS, CTL_EOL);
479 sysctl_createv(clog, 0, NULL, NULL,
480 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
481 CTLTYPE_INT, "rawpartition",
482 SYSCTL_DESCR("Raw partition of a disk"),
483 NULL, RAW_PART, NULL, 0,
484 CTL_KERN, KERN_RAWPARTITION, CTL_EOL);
485 sysctl_createv(clog, 0, NULL, NULL,
486 CTLFLAG_PERMANENT,
487 CTLTYPE_STRUCT, "timex", NULL,
488 sysctl_notavail, 0, NULL, 0,
489 CTL_KERN, KERN_TIMEX, CTL_EOL);
490 sysctl_createv(clog, 0, NULL, NULL,
491 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
492 CTLTYPE_INT, "rtc_offset",
493 SYSCTL_DESCR("Offset of real time clock from UTC in "
494 "minutes"),
495 sysctl_kern_rtc_offset, 0, &rtc_offset, 0,
496 CTL_KERN, KERN_RTC_OFFSET, CTL_EOL);
497 sysctl_createv(clog, 0, NULL, NULL,
498 CTLFLAG_PERMANENT,
499 CTLTYPE_STRING, "root_device",
500 SYSCTL_DESCR("Name of the root device"),
501 sysctl_root_device, 0, NULL, 0,
502 CTL_KERN, KERN_ROOT_DEVICE, CTL_EOL);
503 sysctl_createv(clog, 0, NULL, NULL,
504 CTLFLAG_PERMANENT,
505 CTLTYPE_INT, "msgbufsize",
506 SYSCTL_DESCR("Size of the kernel message buffer"),
507 sysctl_msgbuf, 0, NULL, 0,
508 CTL_KERN, KERN_MSGBUFSIZE, CTL_EOL);
509 sysctl_createv(clog, 0, NULL, NULL,
510 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
511 CTLTYPE_INT, "fsync",
512 SYSCTL_DESCR("Whether the POSIX 1003.1b File "
513 "Synchronization Option is available on "
514 "this system"),
515 NULL, 1, NULL, 0,
516 CTL_KERN, KERN_FSYNC, CTL_EOL);
517 sysctl_createv(clog, 0, NULL, NULL,
518 CTLFLAG_PERMANENT,
519 CTLTYPE_NODE, "ipc",
520 SYSCTL_DESCR("SysV IPC options"),
521 NULL, 0, NULL, 0,
522 CTL_KERN, KERN_SYSVIPC, CTL_EOL);
523 sysctl_createv(clog, 0, NULL, NULL,
524 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
525 CTLTYPE_INT, "sysvmsg",
526 SYSCTL_DESCR("System V style message support available"),
527 NULL,
528 #ifdef SYSVMSG
529 1,
530 #else /* SYSVMSG */
531 0,
532 #endif /* SYSVMSG */
533 NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_MSG, CTL_EOL);
534 sysctl_createv(clog, 0, NULL, NULL,
535 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
536 CTLTYPE_INT, "sysvsem",
537 SYSCTL_DESCR("System V style semaphore support "
538 "available"), NULL,
539 #ifdef SYSVSEM
540 1,
541 #else /* SYSVSEM */
542 0,
543 #endif /* SYSVSEM */
544 NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM, CTL_EOL);
545 sysctl_createv(clog, 0, NULL, NULL,
546 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
547 CTLTYPE_INT, "sysvshm",
548 SYSCTL_DESCR("System V style shared memory support "
549 "available"), NULL,
550 #ifdef SYSVSHM
551 1,
552 #else /* SYSVSHM */
553 0,
554 #endif /* SYSVSHM */
555 NULL, 0, CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SHM, CTL_EOL);
556 sysctl_createv(clog, 0, NULL, NULL,
557 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
558 CTLTYPE_INT, "synchronized_io",
559 SYSCTL_DESCR("Whether the POSIX 1003.1b Synchronized "
560 "I/O Option is available on this system"),
561 NULL, 1, NULL, 0,
562 CTL_KERN, KERN_SYNCHRONIZED_IO, CTL_EOL);
563 sysctl_createv(clog, 0, NULL, NULL,
564 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
565 CTLTYPE_INT, "iov_max",
566 SYSCTL_DESCR("Maximum number of iovec structures per "
567 "process"),
568 NULL, IOV_MAX, NULL, 0,
569 CTL_KERN, KERN_IOV_MAX, CTL_EOL);
570 sysctl_createv(clog, 0, NULL, NULL,
571 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
572 CTLTYPE_INT, "mapped_files",
573 SYSCTL_DESCR("Whether the POSIX 1003.1b Memory Mapped "
574 "Files Option is available on this system"),
575 NULL, 1, NULL, 0,
576 CTL_KERN, KERN_MAPPED_FILES, CTL_EOL);
577 sysctl_createv(clog, 0, NULL, NULL,
578 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
579 CTLTYPE_INT, "memlock",
580 SYSCTL_DESCR("Whether the POSIX 1003.1b Process Memory "
581 "Locking Option is available on this "
582 "system"),
583 NULL, 1, NULL, 0,
584 CTL_KERN, KERN_MEMLOCK, CTL_EOL);
585 sysctl_createv(clog, 0, NULL, NULL,
586 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
587 CTLTYPE_INT, "memlock_range",
588 SYSCTL_DESCR("Whether the POSIX 1003.1b Range Memory "
589 "Locking Option is available on this "
590 "system"),
591 NULL, 1, NULL, 0,
592 CTL_KERN, KERN_MEMLOCK_RANGE, CTL_EOL);
593 sysctl_createv(clog, 0, NULL, NULL,
594 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
595 CTLTYPE_INT, "memory_protection",
596 SYSCTL_DESCR("Whether the POSIX 1003.1b Memory "
597 "Protection Option is available on this "
598 "system"),
599 NULL, 1, NULL, 0,
600 CTL_KERN, KERN_MEMORY_PROTECTION, CTL_EOL);
601 sysctl_createv(clog, 0, NULL, NULL,
602 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
603 CTLTYPE_INT, "login_name_max",
604 SYSCTL_DESCR("Maximum login name length"),
605 NULL, LOGIN_NAME_MAX, NULL, 0,
606 CTL_KERN, KERN_LOGIN_NAME_MAX, CTL_EOL);
607 sysctl_createv(clog, 0, NULL, NULL,
608 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
609 CTLTYPE_STRING, "defcorename",
610 SYSCTL_DESCR("Default core file name"),
611 sysctl_kern_defcorename, 0, defcorename, MAXPATHLEN,
612 CTL_KERN, KERN_DEFCORENAME, CTL_EOL);
613 sysctl_createv(clog, 0, NULL, NULL,
614 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
615 CTLTYPE_INT, "logsigexit",
616 SYSCTL_DESCR("Log process exit when caused by signals"),
617 NULL, 0, &kern_logsigexit, 0,
618 CTL_KERN, KERN_LOGSIGEXIT, CTL_EOL);
619 sysctl_createv(clog, 0, NULL, NULL,
620 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
621 CTLTYPE_INT, "fscale",
622 SYSCTL_DESCR("Kernel fixed-point scale factor"),
623 NULL, FSCALE, NULL, 0,
624 CTL_KERN, KERN_FSCALE, CTL_EOL);
625 sysctl_createv(clog, 0, NULL, NULL,
626 CTLFLAG_PERMANENT,
627 CTLTYPE_INT, "ccpu",
628 SYSCTL_DESCR("Scheduler exponential decay value"),
629 NULL, 0, &ccpu, 0,
630 CTL_KERN, KERN_CCPU, CTL_EOL);
631 sysctl_createv(clog, 0, NULL, NULL,
632 CTLFLAG_PERMANENT,
633 CTLTYPE_STRUCT, "cp_time",
634 SYSCTL_DESCR("Clock ticks spent in different CPU states"),
635 sysctl_kern_cptime, 0, NULL, 0,
636 CTL_KERN, KERN_CP_TIME, CTL_EOL);
637 sysctl_createv(clog, 0, NULL, NULL,
638 CTLFLAG_PERMANENT,
639 CTLTYPE_INT, "msgbuf",
640 SYSCTL_DESCR("Kernel message buffer"),
641 sysctl_msgbuf, 0, NULL, 0,
642 CTL_KERN, KERN_MSGBUF, CTL_EOL);
643 sysctl_createv(clog, 0, NULL, NULL,
644 CTLFLAG_PERMANENT,
645 CTLTYPE_STRUCT, "consdev",
646 SYSCTL_DESCR("Console device"),
647 sysctl_consdev, 0, NULL, sizeof(dev_t),
648 CTL_KERN, KERN_CONSDEV, CTL_EOL);
649 #if NPTY > 0
650 sysctl_createv(clog, 0, NULL, NULL,
651 CTLFLAG_PERMANENT,
652 CTLTYPE_INT, "maxptys",
653 SYSCTL_DESCR("Maximum number of pseudo-ttys"),
654 sysctl_kern_maxptys, 0, NULL, 0,
655 CTL_KERN, KERN_MAXPTYS, CTL_EOL);
656 #endif /* NPTY > 0 */
657 sysctl_createv(clog, 0, NULL, NULL,
658 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
659 CTLTYPE_INT, "maxphys",
660 SYSCTL_DESCR("Maximum raw I/O transfer size"),
661 NULL, MAXPHYS, NULL, 0,
662 CTL_KERN, KERN_MAXPHYS, CTL_EOL);
663 sysctl_createv(clog, 0, NULL, NULL,
664 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
665 CTLTYPE_INT, "sbmax",
666 SYSCTL_DESCR("Maximum socket buffer size"),
667 sysctl_kern_sbmax, 0, NULL, 0,
668 CTL_KERN, KERN_SBMAX, CTL_EOL);
669 sysctl_createv(clog, 0, NULL, NULL,
670 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
671 CTLTYPE_INT, "monotonic_clock",
672 SYSCTL_DESCR("Implementation version of the POSIX "
673 "1003.1b Monotonic Clock Option"),
674 /* XXX _POSIX_VERSION */
675 NULL, _POSIX_MONOTONIC_CLOCK, NULL, 0,
676 CTL_KERN, KERN_MONOTONIC_CLOCK, CTL_EOL);
677 sysctl_createv(clog, 0, NULL, NULL,
678 CTLFLAG_PERMANENT,
679 CTLTYPE_INT, "urandom",
680 SYSCTL_DESCR("Random integer value"),
681 sysctl_kern_urnd, 0, NULL, 0,
682 CTL_KERN, KERN_URND, CTL_EOL);
683 sysctl_createv(clog, 0, NULL, NULL,
684 CTLFLAG_PERMANENT,
685 CTLTYPE_INT, "arandom",
686 SYSCTL_DESCR("n bytes of random data"),
687 sysctl_kern_arnd, 0, NULL, 0,
688 CTL_KERN, KERN_ARND, CTL_EOL);
689 sysctl_createv(clog, 0, NULL, NULL,
690 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
691 CTLTYPE_INT, "labelsector",
692 SYSCTL_DESCR("Sector number containing the disklabel"),
693 NULL, LABELSECTOR, NULL, 0,
694 CTL_KERN, KERN_LABELSECTOR, CTL_EOL);
695 sysctl_createv(clog, 0, NULL, NULL,
696 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
697 CTLTYPE_INT, "labeloffset",
698 SYSCTL_DESCR("Offset of the disklabel within the "
699 "sector"),
700 NULL, LABELOFFSET, NULL, 0,
701 CTL_KERN, KERN_LABELOFFSET, CTL_EOL);
702 sysctl_createv(clog, 0, NULL, NULL,
703 CTLFLAG_PERMANENT,
704 CTLTYPE_NODE, "lwp",
705 SYSCTL_DESCR("System-wide LWP information"),
706 sysctl_kern_lwp, 0, NULL, 0,
707 CTL_KERN, KERN_LWP, CTL_EOL);
708 sysctl_createv(clog, 0, NULL, NULL,
709 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
710 CTLTYPE_INT, "forkfsleep",
711 SYSCTL_DESCR("Milliseconds to sleep on fork failure due "
712 "to process limits"),
713 sysctl_kern_forkfsleep, 0, NULL, 0,
714 CTL_KERN, KERN_FORKFSLEEP, CTL_EOL);
715 sysctl_createv(clog, 0, NULL, NULL,
716 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
717 CTLTYPE_INT, "posix_threads",
718 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
719 "Threads option to which the system "
720 "attempts to conform"),
721 /* XXX _POSIX_VERSION */
722 NULL, _POSIX_THREADS, NULL, 0,
723 CTL_KERN, KERN_POSIX_THREADS, CTL_EOL);
724 sysctl_createv(clog, 0, NULL, NULL,
725 CTLFLAG_PERMANENT,
726 CTLTYPE_INT, "posix_semaphores",
727 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
728 "Semaphores option to which the system "
729 "attempts to conform"), NULL,
730 0, &posix_semaphores,
731 0, CTL_KERN, KERN_POSIX_SEMAPHORES, CTL_EOL);
732 sysctl_createv(clog, 0, NULL, NULL,
733 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
734 CTLTYPE_INT, "posix_barriers",
735 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
736 "Barriers option to which the system "
737 "attempts to conform"),
738 /* XXX _POSIX_VERSION */
739 NULL, _POSIX_BARRIERS, NULL, 0,
740 CTL_KERN, KERN_POSIX_BARRIERS, CTL_EOL);
741 sysctl_createv(clog, 0, NULL, NULL,
742 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
743 CTLTYPE_INT, "posix_timers",
744 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
745 "Timers option to which the system "
746 "attempts to conform"),
747 /* XXX _POSIX_VERSION */
748 NULL, _POSIX_TIMERS, NULL, 0,
749 CTL_KERN, KERN_POSIX_TIMERS, CTL_EOL);
750 sysctl_createv(clog, 0, NULL, NULL,
751 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
752 CTLTYPE_INT, "posix_spin_locks",
753 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its Spin "
754 "Locks option to which the system attempts "
755 "to conform"),
756 /* XXX _POSIX_VERSION */
757 NULL, _POSIX_SPIN_LOCKS, NULL, 0,
758 CTL_KERN, KERN_POSIX_SPIN_LOCKS, CTL_EOL);
759 sysctl_createv(clog, 0, NULL, NULL,
760 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
761 CTLTYPE_INT, "posix_reader_writer_locks",
762 SYSCTL_DESCR("Version of IEEE Std 1003.1 and its "
763 "Read-Write Locks option to which the "
764 "system attempts to conform"),
765 /* XXX _POSIX_VERSION */
766 NULL, _POSIX_READER_WRITER_LOCKS, NULL, 0,
767 CTL_KERN, KERN_POSIX_READER_WRITER_LOCKS, CTL_EOL);
768 sysctl_createv(clog, 0, NULL, NULL,
769 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
770 CTLTYPE_INT, "dump_on_panic",
771 SYSCTL_DESCR("Perform a crash dump on system panic"),
772 NULL, 0, &dumponpanic, 0,
773 CTL_KERN, KERN_DUMP_ON_PANIC, CTL_EOL);
774 #ifdef DIAGNOSTIC
775 sysctl_createv(clog, 0, NULL, NULL,
776 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
777 CTLTYPE_INT, "panic_now",
778 SYSCTL_DESCR("Trigger a panic"),
779 sysctl_kern_trigger_panic, 0, NULL, 0,
780 CTL_KERN, CTL_CREATE, CTL_EOL);
781 #endif
782 sysctl_createv(clog, 0, NULL, NULL,
783 CTLFLAG_PERMANENT,
784 CTLTYPE_INT, "root_partition",
785 SYSCTL_DESCR("Root partition on the root device"),
786 sysctl_kern_root_partition, 0, NULL, 0,
787 CTL_KERN, KERN_ROOT_PARTITION, CTL_EOL);
788 sysctl_createv(clog, 0, NULL, NULL,
789 CTLFLAG_PERMANENT,
790 CTLTYPE_STRUCT, "drivers",
791 SYSCTL_DESCR("List of all drivers with block and "
792 "character device numbers"),
793 sysctl_kern_drivers, 0, NULL, 0,
794 CTL_KERN, KERN_DRIVERS, CTL_EOL);
795 sysctl_createv(clog, 0, NULL, NULL,
796 CTLFLAG_PERMANENT,
797 CTLTYPE_STRUCT, "file2",
798 SYSCTL_DESCR("System open file table"),
799 sysctl_kern_file2, 0, NULL, 0,
800 CTL_KERN, KERN_FILE2, CTL_EOL);
801 sysctl_createv(clog, 0, NULL, NULL,
802 CTLFLAG_PERMANENT,
803 CTLTYPE_STRUCT, "cp_id",
804 SYSCTL_DESCR("Mapping of CPU number to CPU id"),
805 sysctl_kern_cpid, 0, NULL, 0,
806 CTL_KERN, KERN_CP_ID, CTL_EOL);
807 sysctl_createv(clog, 0, NULL, &rnode,
808 CTLFLAG_PERMANENT,
809 CTLTYPE_NODE, "coredump",
810 SYSCTL_DESCR("Coredump settings."),
811 NULL, 0, NULL, 0,
812 CTL_KERN, CTL_CREATE, CTL_EOL);
813 sysctl_createv(clog, 0, &rnode, &rnode,
814 CTLFLAG_PERMANENT,
815 CTLTYPE_NODE, "setid",
816 SYSCTL_DESCR("Set-id processes' coredump settings."),
817 NULL, 0, NULL, 0,
818 CTL_CREATE, CTL_EOL);
819 sysctl_createv(clog, 0, &rnode, NULL,
820 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
821 CTLTYPE_INT, "dump",
822 SYSCTL_DESCR("Allow set-id processes to dump core."),
823 sysctl_security_setidcore, 0, &security_setidcore_dump,
824 sizeof(security_setidcore_dump),
825 CTL_CREATE, CTL_EOL);
826 sysctl_createv(clog, 0, &rnode, NULL,
827 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
828 CTLTYPE_STRING, "path",
829 SYSCTL_DESCR("Path pattern for set-id coredumps."),
830 sysctl_security_setidcorename, 0,
831 &security_setidcore_path,
832 sizeof(security_setidcore_path),
833 CTL_CREATE, CTL_EOL);
834 sysctl_createv(clog, 0, &rnode, NULL,
835 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
836 CTLTYPE_INT, "owner",
837 SYSCTL_DESCR("Owner id for set-id processes' cores."),
838 sysctl_security_setidcore, 0, &security_setidcore_owner,
839 0,
840 CTL_CREATE, CTL_EOL);
841 sysctl_createv(clog, 0, &rnode, NULL,
842 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
843 CTLTYPE_INT, "group",
844 SYSCTL_DESCR("Group id for set-id processes' cores."),
845 sysctl_security_setidcore, 0, &security_setidcore_group,
846 0,
847 CTL_CREATE, CTL_EOL);
848 sysctl_createv(clog, 0, &rnode, NULL,
849 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
850 CTLTYPE_INT, "mode",
851 SYSCTL_DESCR("Mode for set-id processes' cores."),
852 sysctl_security_setidcore, 0, &security_setidcore_mode,
853 0,
854 CTL_CREATE, CTL_EOL);
855 #ifdef KERN_SA
856 sysctl_createv(clog, 0, NULL, NULL,
857 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
858 CTLTYPE_INT, "no_sa_support",
859 SYSCTL_DESCR("0 if the kernel supports SA, otherwise it doesn't"),
860 NULL, 0, &sa_system_disabled, 0,
861 CTL_KERN, CTL_CREATE, CTL_EOL);
862 #else
863 sysctl_createv(clog, 0, NULL, NULL,
864 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
865 CTLTYPE_INT, "no_sa_support",
866 SYSCTL_DESCR("0 if the kernel supports SA, otherwise it doesn't"),
867 NULL, 1, NULL, 0,
868 CTL_KERN, CTL_CREATE, CTL_EOL);
869 #endif
870
871 /* kern.posix. */
872 sysctl_createv(clog, 0, NULL, &rnode,
873 CTLFLAG_PERMANENT,
874 CTLTYPE_NODE, "posix",
875 SYSCTL_DESCR("POSIX options"),
876 NULL, 0, NULL, 0,
877 CTL_KERN, CTL_CREATE, CTL_EOL);
878 sysctl_createv(clog, 0, &rnode, NULL,
879 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
880 CTLTYPE_INT, "semmax",
881 SYSCTL_DESCR("Maximal number of semaphores"),
882 NULL, 0, &ksem_max, 0,
883 CTL_CREATE, CTL_EOL);
884 }
885
886 SYSCTL_SETUP(sysctl_kern_proc_setup,
887 "sysctl kern.proc/proc2/proc_args subtree setup")
888 {
889
890 sysctl_createv(clog, 0, NULL, NULL,
891 CTLFLAG_PERMANENT,
892 CTLTYPE_NODE, "kern", NULL,
893 NULL, 0, NULL, 0,
894 CTL_KERN, CTL_EOL);
895
896 sysctl_createv(clog, 0, NULL, NULL,
897 CTLFLAG_PERMANENT,
898 CTLTYPE_NODE, "proc",
899 SYSCTL_DESCR("System-wide process information"),
900 sysctl_doeproc, 0, NULL, 0,
901 CTL_KERN, KERN_PROC, CTL_EOL);
902 sysctl_createv(clog, 0, NULL, NULL,
903 CTLFLAG_PERMANENT,
904 CTLTYPE_NODE, "proc2",
905 SYSCTL_DESCR("Machine-independent process information"),
906 sysctl_doeproc, 0, NULL, 0,
907 CTL_KERN, KERN_PROC2, CTL_EOL);
908 sysctl_createv(clog, 0, NULL, NULL,
909 CTLFLAG_PERMANENT,
910 CTLTYPE_NODE, "proc_args",
911 SYSCTL_DESCR("Process argument information"),
912 sysctl_kern_proc_args, 0, NULL, 0,
913 CTL_KERN, KERN_PROC_ARGS, CTL_EOL);
914
915 /*
916 "nodes" under these:
917
918 KERN_PROC_ALL
919 KERN_PROC_PID pid
920 KERN_PROC_PGRP pgrp
921 KERN_PROC_SESSION sess
922 KERN_PROC_TTY tty
923 KERN_PROC_UID uid
924 KERN_PROC_RUID uid
925 KERN_PROC_GID gid
926 KERN_PROC_RGID gid
927
928 all in all, probably not worth the effort...
929 */
930 }
931
932 SYSCTL_SETUP(sysctl_hw_setup, "sysctl hw subtree setup")
933 {
934 u_int u;
935 u_quad_t q;
936
937 sysctl_createv(clog, 0, NULL, NULL,
938 CTLFLAG_PERMANENT,
939 CTLTYPE_NODE, "hw", NULL,
940 NULL, 0, NULL, 0,
941 CTL_HW, CTL_EOL);
942
943 sysctl_createv(clog, 0, NULL, NULL,
944 CTLFLAG_PERMANENT,
945 CTLTYPE_STRING, "machine",
946 SYSCTL_DESCR("Machine class"),
947 NULL, 0, machine, 0,
948 CTL_HW, HW_MACHINE, CTL_EOL);
949 sysctl_createv(clog, 0, NULL, NULL,
950 CTLFLAG_PERMANENT,
951 CTLTYPE_STRING, "model",
952 SYSCTL_DESCR("Machine model"),
953 NULL, 0, cpu_model, 0,
954 CTL_HW, HW_MODEL, CTL_EOL);
955 sysctl_createv(clog, 0, NULL, NULL,
956 CTLFLAG_PERMANENT,
957 CTLTYPE_INT, "ncpu",
958 SYSCTL_DESCR("Number of CPUs configured"),
959 NULL, 0, &ncpu, 0,
960 CTL_HW, HW_NCPU, CTL_EOL);
961 sysctl_createv(clog, 0, NULL, NULL,
962 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
963 CTLTYPE_INT, "byteorder",
964 SYSCTL_DESCR("System byte order"),
965 NULL, BYTE_ORDER, NULL, 0,
966 CTL_HW, HW_BYTEORDER, CTL_EOL);
967 u = ((u_int)physmem > (UINT_MAX / PAGE_SIZE)) ?
968 UINT_MAX : physmem * PAGE_SIZE;
969 sysctl_createv(clog, 0, NULL, NULL,
970 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
971 CTLTYPE_INT, "physmem",
972 SYSCTL_DESCR("Bytes of physical memory"),
973 NULL, u, NULL, 0,
974 CTL_HW, HW_PHYSMEM, CTL_EOL);
975 sysctl_createv(clog, 0, NULL, NULL,
976 CTLFLAG_PERMANENT,
977 CTLTYPE_INT, "usermem",
978 SYSCTL_DESCR("Bytes of non-kernel memory"),
979 sysctl_hw_usermem, 0, NULL, 0,
980 CTL_HW, HW_USERMEM, CTL_EOL);
981 sysctl_createv(clog, 0, NULL, NULL,
982 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
983 CTLTYPE_INT, "pagesize",
984 SYSCTL_DESCR("Software page size"),
985 NULL, PAGE_SIZE, NULL, 0,
986 CTL_HW, HW_PAGESIZE, CTL_EOL);
987 sysctl_createv(clog, 0, NULL, NULL,
988 CTLFLAG_PERMANENT,
989 CTLTYPE_STRING, "machine_arch",
990 SYSCTL_DESCR("Machine CPU class"),
991 NULL, 0, machine_arch, 0,
992 CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
993 sysctl_createv(clog, 0, NULL, NULL,
994 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
995 CTLTYPE_INT, "alignbytes",
996 SYSCTL_DESCR("Alignment constraint for all possible "
997 "data types"),
998 NULL, ALIGNBYTES, NULL, 0,
999 CTL_HW, HW_ALIGNBYTES, CTL_EOL);
1000 sysctl_createv(clog, 0, NULL, NULL,
1001 CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_HEX,
1002 CTLTYPE_STRING, "cnmagic",
1003 SYSCTL_DESCR("Console magic key sequence"),
1004 sysctl_hw_cnmagic, 0, NULL, CNS_LEN,
1005 CTL_HW, HW_CNMAGIC, CTL_EOL);
1006 q = (u_quad_t)physmem * PAGE_SIZE;
1007 sysctl_createv(clog, 0, NULL, NULL,
1008 CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
1009 CTLTYPE_QUAD, "physmem64",
1010 SYSCTL_DESCR("Bytes of physical memory"),
1011 NULL, q, NULL, 0,
1012 CTL_HW, HW_PHYSMEM64, CTL_EOL);
1013 sysctl_createv(clog, 0, NULL, NULL,
1014 CTLFLAG_PERMANENT,
1015 CTLTYPE_QUAD, "usermem64",
1016 SYSCTL_DESCR("Bytes of non-kernel memory"),
1017 sysctl_hw_usermem, 0, NULL, 0,
1018 CTL_HW, HW_USERMEM64, CTL_EOL);
1019 sysctl_createv(clog, 0, NULL, NULL,
1020 CTLFLAG_PERMANENT,
1021 CTLTYPE_INT, "ncpuonline",
1022 SYSCTL_DESCR("Number of CPUs online"),
1023 NULL, 0, &ncpuonline, 0,
1024 CTL_HW, HW_NCPUONLINE, CTL_EOL);
1025 }
1026
1027 #ifdef DEBUG
1028 /*
1029 * Debugging related system variables.
1030 */
1031 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
1032 struct ctldebug debug5, debug6, debug7, debug8, debug9;
1033 struct ctldebug debug10, debug11, debug12, debug13, debug14;
1034 struct ctldebug debug15, debug16, debug17, debug18, debug19;
1035 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
1036 &debug0, &debug1, &debug2, &debug3, &debug4,
1037 &debug5, &debug6, &debug7, &debug8, &debug9,
1038 &debug10, &debug11, &debug12, &debug13, &debug14,
1039 &debug15, &debug16, &debug17, &debug18, &debug19,
1040 };
1041
1042 /*
1043 * this setup routine is a replacement for debug_sysctl()
1044 *
1045 * note that it creates several nodes per defined debug variable
1046 */
1047 SYSCTL_SETUP(sysctl_debug_setup, "sysctl debug subtree setup")
1048 {
1049 struct ctldebug *cdp;
1050 char nodename[20];
1051 int i;
1052
1053 /*
1054 * two ways here:
1055 *
1056 * the "old" way (debug.name -> value) which was emulated by
1057 * the sysctl(8) binary
1058 *
1059 * the new way, which the sysctl(8) binary was actually using
1060
1061 node debug
1062 node debug.0
1063 string debug.0.name
1064 int debug.0.value
1065 int debug.name
1066
1067 */
1068
1069 sysctl_createv(clog, 0, NULL, NULL,
1070 CTLFLAG_PERMANENT,
1071 CTLTYPE_NODE, "debug", NULL,
1072 NULL, 0, NULL, 0,
1073 CTL_DEBUG, CTL_EOL);
1074
1075 for (i = 0; i < CTL_DEBUG_MAXID; i++) {
1076 cdp = debugvars[i];
1077 if (cdp->debugname == NULL || cdp->debugvar == NULL)
1078 continue;
1079
1080 snprintf(nodename, sizeof(nodename), "debug%d", i);
1081 sysctl_createv(clog, 0, NULL, NULL,
1082 CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
1083 CTLTYPE_NODE, nodename, NULL,
1084 NULL, 0, NULL, 0,
1085 CTL_DEBUG, i, CTL_EOL);
1086 sysctl_createv(clog, 0, NULL, NULL,
1087 CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
1088 CTLTYPE_STRING, "name", NULL,
1089 /*XXXUNCONST*/
1090 NULL, 0, __UNCONST(cdp->debugname), 0,
1091 CTL_DEBUG, i, CTL_DEBUG_NAME, CTL_EOL);
1092 sysctl_createv(clog, 0, NULL, NULL,
1093 CTLFLAG_PERMANENT|CTLFLAG_HIDDEN,
1094 CTLTYPE_INT, "value", NULL,
1095 NULL, 0, cdp->debugvar, 0,
1096 CTL_DEBUG, i, CTL_DEBUG_VALUE, CTL_EOL);
1097 sysctl_createv(clog, 0, NULL, NULL,
1098 CTLFLAG_PERMANENT,
1099 CTLTYPE_INT, cdp->debugname, NULL,
1100 NULL, 0, cdp->debugvar, 0,
1101 CTL_DEBUG, CTL_CREATE, CTL_EOL);
1102 }
1103 }
1104 #endif /* DEBUG */
1105
1106 /*
1107 * ********************************************************************
1108 * section 2: private node-specific helper routines.
1109 * ********************************************************************
1110 */
1111
1112 #ifdef DIAGNOSTIC
1113 static int
1114 sysctl_kern_trigger_panic(SYSCTLFN_ARGS)
1115 {
1116 int newtrig, error;
1117 struct sysctlnode node;
1118
1119 newtrig = 0;
1120 node = *rnode;
1121 node.sysctl_data = &newtrig;
1122 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1123 if (error || newp == NULL)
1124 return (error);
1125
1126 if (newtrig != 0)
1127 panic("Panic triggered");
1128
1129 return (error);
1130 }
1131 #endif
1132
1133 /*
1134 * sysctl helper routine for kern.maxvnodes. Drain vnodes if
1135 * new value is lower than desiredvnodes and then calls reinit
1136 * routines that needs to adjust to the new value.
1137 */
1138 static int
1139 sysctl_kern_maxvnodes(SYSCTLFN_ARGS)
1140 {
1141 int error, new_vnodes, old_vnodes, new_max;
1142 struct sysctlnode node;
1143
1144 new_vnodes = desiredvnodes;
1145 node = *rnode;
1146 node.sysctl_data = &new_vnodes;
1147 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1148 if (error || newp == NULL)
1149 return (error);
1150
1151 /* Limits: 75% of KVA and physical memory. */
1152 new_max = calc_cache_size(kernel_map, 75, 75) / VNODE_COST;
1153 if (new_vnodes > new_max)
1154 new_vnodes = new_max;
1155
1156 old_vnodes = desiredvnodes;
1157 desiredvnodes = new_vnodes;
1158 if (new_vnodes < old_vnodes) {
1159 error = vfs_drainvnodes(new_vnodes, l);
1160 if (error) {
1161 desiredvnodes = old_vnodes;
1162 return (error);
1163 }
1164 }
1165 vfs_reinit();
1166 nchreinit();
1167
1168 return (0);
1169 }
1170
1171 /*
1172 * sysctl helper routine for rtc_offset - set time after changes
1173 */
1174 static int
1175 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
1176 {
1177 struct timespec ts, delta;
1178 int error, new_rtc_offset;
1179 struct sysctlnode node;
1180
1181 new_rtc_offset = rtc_offset;
1182 node = *rnode;
1183 node.sysctl_data = &new_rtc_offset;
1184 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1185 if (error || newp == NULL)
1186 return (error);
1187
1188 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
1189 KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
1190 KAUTH_ARG(new_rtc_offset), NULL, NULL))
1191 return (EPERM);
1192 if (rtc_offset == new_rtc_offset)
1193 return (0);
1194
1195 /* if we change the offset, adjust the time */
1196 nanotime(&ts);
1197 delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
1198 delta.tv_nsec = 0;
1199 timespecadd(&ts, &delta, &ts);
1200 rtc_offset = new_rtc_offset;
1201 return (settime(l->l_proc, &ts));
1202 }
1203
1204 /*
1205 * sysctl helper routine for kern.maxproc. Ensures that the new
1206 * values are not too low or too high.
1207 */
1208 static int
1209 sysctl_kern_maxproc(SYSCTLFN_ARGS)
1210 {
1211 int error, nmaxproc;
1212 struct sysctlnode node;
1213
1214 nmaxproc = maxproc;
1215 node = *rnode;
1216 node.sysctl_data = &nmaxproc;
1217 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1218 if (error || newp == NULL)
1219 return (error);
1220
1221 if (nmaxproc < 0 || nmaxproc >= PID_MAX)
1222 return (EINVAL);
1223 #ifdef __HAVE_CPU_MAXPROC
1224 if (nmaxproc > cpu_maxproc())
1225 return (EINVAL);
1226 #endif
1227 maxproc = nmaxproc;
1228
1229 return (0);
1230 }
1231
1232 /*
1233 * sysctl helper function for kern.hostid. The hostid is a long, but
1234 * we export it as an int, so we need to give it a little help.
1235 */
1236 static int
1237 sysctl_kern_hostid(SYSCTLFN_ARGS)
1238 {
1239 int error, inthostid;
1240 struct sysctlnode node;
1241
1242 inthostid = hostid; /* XXX assumes sizeof int <= sizeof long */
1243 node = *rnode;
1244 node.sysctl_data = &inthostid;
1245 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1246 if (error || newp == NULL)
1247 return (error);
1248
1249 hostid = (unsigned)inthostid;
1250
1251 return (0);
1252 }
1253
1254 /*
1255 * sysctl helper function for kern.hostname and kern.domainnname.
1256 * resets the relevant recorded length when the underlying name is
1257 * changed.
1258 */
1259 static int
1260 sysctl_setlen(SYSCTLFN_ARGS)
1261 {
1262 int error;
1263
1264 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1265 if (error || newp == NULL)
1266 return (error);
1267
1268 switch (rnode->sysctl_num) {
1269 case KERN_HOSTNAME:
1270 hostnamelen = strlen((const char*)rnode->sysctl_data);
1271 break;
1272 case KERN_DOMAINNAME:
1273 domainnamelen = strlen((const char*)rnode->sysctl_data);
1274 break;
1275 }
1276
1277 return (0);
1278 }
1279
1280 /*
1281 * sysctl helper routine for kern.clockrate. Assembles a struct on
1282 * the fly to be returned to the caller.
1283 */
1284 static int
1285 sysctl_kern_clockrate(SYSCTLFN_ARGS)
1286 {
1287 struct clockinfo clkinfo;
1288 struct sysctlnode node;
1289
1290 clkinfo.tick = tick;
1291 clkinfo.tickadj = tickadj;
1292 clkinfo.hz = hz;
1293 clkinfo.profhz = profhz;
1294 clkinfo.stathz = stathz ? stathz : hz;
1295
1296 node = *rnode;
1297 node.sysctl_data = &clkinfo;
1298 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1299 }
1300
1301
1302 /*
1303 * sysctl helper routine for kern.file pseudo-subtree.
1304 */
1305 static int
1306 sysctl_kern_file(SYSCTLFN_ARGS)
1307 {
1308 int error;
1309 size_t buflen;
1310 struct file *fp, *dp, *np, fbuf;
1311 char *start, *where;
1312
1313 start = where = oldp;
1314 buflen = *oldlenp;
1315 dp = NULL;
1316
1317 if (where == NULL) {
1318 /*
1319 * overestimate by 10 files
1320 */
1321 *oldlenp = sizeof(filehead) + (nfiles + 10) *
1322 sizeof(struct file);
1323 return (0);
1324 }
1325
1326 /*
1327 * first dcopyout filehead
1328 */
1329 if (buflen < sizeof(filehead)) {
1330 *oldlenp = 0;
1331 return (0);
1332 }
1333 sysctl_unlock();
1334 error = dcopyout(l, &filehead, where, sizeof(filehead));
1335 if (error) {
1336 sysctl_relock();
1337 return error;
1338 }
1339 buflen -= sizeof(filehead);
1340 where += sizeof(filehead);
1341
1342 /*
1343 * allocate dummy file descriptor to make position in list
1344 */
1345 if ((dp = fgetdummy()) == NULL) {
1346 sysctl_relock();
1347 return ENOMEM;
1348 }
1349
1350 /*
1351 * followed by an array of file structures
1352 */
1353 mutex_enter(&filelist_lock);
1354 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1355 np = LIST_NEXT(fp, f_list);
1356 mutex_enter(&fp->f_lock);
1357 if (fp->f_count == 0) {
1358 mutex_exit(&fp->f_lock);
1359 continue;
1360 }
1361 /*
1362 * XXX Need to prevent that from being an alternative way
1363 * XXX to getting process information.
1364 */
1365 if (kauth_authorize_generic(l->l_cred,
1366 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
1367 mutex_exit(&fp->f_lock);
1368 continue;
1369 }
1370 if (buflen < sizeof(struct file)) {
1371 *oldlenp = where - start;
1372 mutex_exit(&fp->f_lock);
1373 error = ENOMEM;
1374 break;
1375 }
1376 memcpy(&fbuf, fp, sizeof(fbuf));
1377 LIST_INSERT_AFTER(fp, dp, f_list);
1378 mutex_exit(&fp->f_lock);
1379 mutex_exit(&filelist_lock);
1380 error = dcopyout(l, &fbuf, where, sizeof(fbuf));
1381 if (error) {
1382 mutex_enter(&filelist_lock);
1383 LIST_REMOVE(dp, f_list);
1384 break;
1385 }
1386 buflen -= sizeof(struct file);
1387 where += sizeof(struct file);
1388 mutex_enter(&filelist_lock);
1389 np = LIST_NEXT(dp, f_list);
1390 LIST_REMOVE(dp, f_list);
1391 }
1392 mutex_exit(&filelist_lock);
1393 *oldlenp = where - start;
1394 if (dp != NULL)
1395 fputdummy(dp);
1396 sysctl_relock();
1397 return (error);
1398 }
1399
1400 /*
1401 * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
1402 * former it merely checks the message buffer is set up. For the latter,
1403 * it also copies out the data if necessary.
1404 */
1405 static int
1406 sysctl_msgbuf(SYSCTLFN_ARGS)
1407 {
1408 char *where = oldp;
1409 size_t len, maxlen;
1410 long beg, end;
1411 extern kmutex_t log_lock;
1412 int error;
1413
1414 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1415 msgbufenabled = 0;
1416 return (ENXIO);
1417 }
1418
1419 switch (rnode->sysctl_num) {
1420 case KERN_MSGBUFSIZE: {
1421 struct sysctlnode node = *rnode;
1422 int msg_bufs = (int)msgbufp->msg_bufs;
1423 node.sysctl_data = &msg_bufs;
1424 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1425 }
1426 case KERN_MSGBUF:
1427 break;
1428 default:
1429 return (EOPNOTSUPP);
1430 }
1431
1432 if (newp != NULL)
1433 return (EPERM);
1434
1435 if (oldp == NULL) {
1436 /* always return full buffer size */
1437 *oldlenp = msgbufp->msg_bufs;
1438 return (0);
1439 }
1440
1441 sysctl_unlock();
1442
1443 /*
1444 * First, copy from the write pointer to the end of
1445 * message buffer.
1446 */
1447 error = 0;
1448 mutex_spin_enter(&log_lock);
1449 maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1450 beg = msgbufp->msg_bufx;
1451 end = msgbufp->msg_bufs;
1452 mutex_spin_exit(&log_lock);
1453
1454 while (maxlen > 0) {
1455 len = MIN(end - beg, maxlen);
1456 if (len == 0)
1457 break;
1458 /* XXX unlocked, but hardly matters. */
1459 error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
1460 if (error)
1461 break;
1462 where += len;
1463 maxlen -= len;
1464
1465 /*
1466 * ... then, copy from the beginning of message buffer to
1467 * the write pointer.
1468 */
1469 beg = 0;
1470 end = msgbufp->msg_bufx;
1471 }
1472
1473 sysctl_relock();
1474 return (error);
1475 }
1476
1477 /*
1478 * sysctl helper routine for kern.defcorename. In the case of a new
1479 * string being assigned, check that it's not a zero-length string.
1480 * (XXX the check in -current doesn't work, but do we really care?)
1481 */
1482 static int
1483 sysctl_kern_defcorename(SYSCTLFN_ARGS)
1484 {
1485 int error;
1486 char *newcorename;
1487 struct sysctlnode node;
1488
1489 newcorename = PNBUF_GET();
1490 node = *rnode;
1491 node.sysctl_data = &newcorename[0];
1492 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1493 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1494 if (error || newp == NULL) {
1495 goto done;
1496 }
1497
1498 /*
1499 * when sysctl_lookup() deals with a string, it's guaranteed
1500 * to come back nul terminated. So there. :)
1501 */
1502 if (strlen(newcorename) == 0) {
1503 error = EINVAL;
1504 } else {
1505 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1506 error = 0;
1507 }
1508 done:
1509 PNBUF_PUT(newcorename);
1510 return error;
1511 }
1512
1513 /*
1514 * sysctl helper routine for kern.cp_time node. Adds up cpu time
1515 * across all cpus.
1516 */
1517 static int
1518 sysctl_kern_cptime(SYSCTLFN_ARGS)
1519 {
1520 struct sysctlnode node = *rnode;
1521 uint64_t *cp_time = NULL;
1522 int error, n = ncpu, i;
1523 struct cpu_info *ci;
1524 CPU_INFO_ITERATOR cii;
1525
1526 /*
1527 * if you specifically pass a buffer that is the size of the
1528 * sum, or if you are probing for the size, you get the "sum"
1529 * of cp_time (and the size thereof) across all processors.
1530 *
1531 * alternately, you can pass an additional mib number and get
1532 * cp_time for that particular processor.
1533 */
1534 switch (namelen) {
1535 case 0:
1536 if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
1537 node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1538 n = -1; /* SUM */
1539 }
1540 else {
1541 node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
1542 n = -2; /* ALL */
1543 }
1544 break;
1545 case 1:
1546 if (name[0] < 0 || name[0] >= n)
1547 return (ENOENT); /* ENOSUCHPROCESSOR */
1548 node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1549 n = name[0];
1550 /*
1551 * adjust these so that sysctl_lookup() will be happy
1552 */
1553 name++;
1554 namelen--;
1555 break;
1556 default:
1557 return (EINVAL);
1558 }
1559
1560 cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
1561 if (cp_time == NULL)
1562 return (ENOMEM);
1563 node.sysctl_data = cp_time;
1564 memset(cp_time, 0, node.sysctl_size);
1565
1566 for (CPU_INFO_FOREACH(cii, ci)) {
1567 if (n <= 0) {
1568 for (i = 0; i < CPUSTATES; i++) {
1569 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1570 }
1571 }
1572 /*
1573 * if a specific processor was requested and we just
1574 * did it, we're done here
1575 */
1576 if (n == 0)
1577 break;
1578 /*
1579 * if doing "all", skip to next cp_time set for next processor
1580 */
1581 if (n == -2)
1582 cp_time += CPUSTATES;
1583 /*
1584 * if we're doing a specific processor, we're one
1585 * processor closer
1586 */
1587 if (n > 0)
1588 n--;
1589 }
1590
1591 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1592 kmem_free(node.sysctl_data, node.sysctl_size);
1593 return (error);
1594 }
1595
1596 #if NPTY > 0
1597 /*
1598 * sysctl helper routine for kern.maxptys. Ensures that any new value
1599 * is acceptable to the pty subsystem.
1600 */
1601 static int
1602 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1603 {
1604 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
1605 int error, xmax;
1606 struct sysctlnode node;
1607
1608 /* get current value of maxptys */
1609 xmax = pty_maxptys(0, 0);
1610
1611 node = *rnode;
1612 node.sysctl_data = &xmax;
1613 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1614 if (error || newp == NULL)
1615 return (error);
1616
1617 if (xmax != pty_maxptys(xmax, 1))
1618 return (EINVAL);
1619
1620 return (0);
1621 }
1622 #endif /* NPTY > 0 */
1623
1624 /*
1625 * sysctl helper routine for kern.sbmax. Basically just ensures that
1626 * any new value is not too small.
1627 */
1628 static int
1629 sysctl_kern_sbmax(SYSCTLFN_ARGS)
1630 {
1631 int error, new_sbmax;
1632 struct sysctlnode node;
1633
1634 new_sbmax = sb_max;
1635 node = *rnode;
1636 node.sysctl_data = &new_sbmax;
1637 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1638 if (error || newp == NULL)
1639 return (error);
1640
1641 KERNEL_LOCK(1, NULL);
1642 error = sb_max_set(new_sbmax);
1643 KERNEL_UNLOCK_ONE(NULL);
1644
1645 return (error);
1646 }
1647
1648 /*
1649 * sysctl helper routine for kern.urandom node. Picks a random number
1650 * for you.
1651 */
1652 static int
1653 sysctl_kern_urnd(SYSCTLFN_ARGS)
1654 {
1655 #if NRND > 0
1656 int v, rv;
1657
1658 KERNEL_LOCK(1, NULL);
1659 rv = rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY);
1660 KERNEL_UNLOCK_ONE(NULL);
1661 if (rv == sizeof(v)) {
1662 struct sysctlnode node = *rnode;
1663 node.sysctl_data = &v;
1664 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1665 }
1666 else
1667 return (EIO); /*XXX*/
1668 #else
1669 return (EOPNOTSUPP);
1670 #endif
1671 }
1672
1673 /*
1674 * sysctl helper routine for kern.arandom node. Picks a random number
1675 * for you.
1676 */
1677 static int
1678 sysctl_kern_arnd(SYSCTLFN_ARGS)
1679 {
1680 #if NRND > 0
1681 int error;
1682 void *v;
1683 struct sysctlnode node = *rnode;
1684
1685 if (*oldlenp == 0)
1686 return 0;
1687 if (*oldlenp > 8192)
1688 return E2BIG;
1689
1690 v = kmem_alloc(*oldlenp, KM_SLEEP);
1691 arc4randbytes(v, *oldlenp);
1692 node.sysctl_data = v;
1693 node.sysctl_size = *oldlenp;
1694 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1695 kmem_free(v, *oldlenp);
1696 return error;
1697 #else
1698 return (EOPNOTSUPP);
1699 #endif
1700 }
1701 /*
1702 * sysctl helper routine to do kern.lwp.* work.
1703 */
1704 static int
1705 sysctl_kern_lwp(SYSCTLFN_ARGS)
1706 {
1707 struct kinfo_lwp klwp;
1708 struct proc *p;
1709 struct lwp *l2, *l3;
1710 char *where, *dp;
1711 int pid, elem_size, elem_count;
1712 int buflen, needed, error;
1713 bool gotit;
1714
1715 if (namelen == 1 && name[0] == CTL_QUERY)
1716 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1717
1718 dp = where = oldp;
1719 buflen = where != NULL ? *oldlenp : 0;
1720 error = needed = 0;
1721
1722 if (newp != NULL || namelen != 3)
1723 return (EINVAL);
1724 pid = name[0];
1725 elem_size = name[1];
1726 elem_count = name[2];
1727
1728 sysctl_unlock();
1729 if (pid == -1) {
1730 mutex_enter(proc_lock);
1731 LIST_FOREACH(p, &allproc, p_list) {
1732 /* Grab a hold on the process. */
1733 if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1734 continue;
1735 }
1736 mutex_exit(proc_lock);
1737
1738 mutex_enter(p->p_lock);
1739 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1740 if (buflen >= elem_size && elem_count > 0) {
1741 lwp_lock(l2);
1742 fill_lwp(l2, &klwp);
1743 lwp_unlock(l2);
1744 mutex_exit(p->p_lock);
1745
1746 /*
1747 * Copy out elem_size, but not
1748 * larger than the size of a
1749 * struct kinfo_proc2.
1750 */
1751 error = dcopyout(l, &klwp, dp,
1752 min(sizeof(klwp), elem_size));
1753 if (error) {
1754 rw_exit(&p->p_reflock);
1755 goto cleanup;
1756 }
1757 mutex_enter(p->p_lock);
1758 LIST_FOREACH(l3, &p->p_lwps,
1759 l_sibling) {
1760 if (l2 == l3)
1761 break;
1762 }
1763 if (l3 == NULL) {
1764 mutex_exit(p->p_lock);
1765 rw_exit(&p->p_reflock);
1766 error = EAGAIN;
1767 goto cleanup;
1768 }
1769 dp += elem_size;
1770 buflen -= elem_size;
1771 elem_count--;
1772 }
1773 needed += elem_size;
1774 }
1775 mutex_exit(p->p_lock);
1776
1777 /* Drop reference to process. */
1778 mutex_enter(proc_lock);
1779 rw_exit(&p->p_reflock);
1780 }
1781 mutex_exit(proc_lock);
1782 } else {
1783 mutex_enter(proc_lock);
1784 p = p_find(pid, PFIND_LOCKED);
1785 if (p == NULL) {
1786 error = ESRCH;
1787 mutex_exit(proc_lock);
1788 goto cleanup;
1789 }
1790 /* Grab a hold on the process. */
1791 gotit = rw_tryenter(&p->p_reflock, RW_READER);
1792 mutex_exit(proc_lock);
1793 if (!gotit) {
1794 error = ESRCH;
1795 goto cleanup;
1796 }
1797
1798 mutex_enter(p->p_lock);
1799 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1800 if (buflen >= elem_size && elem_count > 0) {
1801 lwp_lock(l2);
1802 fill_lwp(l2, &klwp);
1803 lwp_unlock(l2);
1804 mutex_exit(p->p_lock);
1805 /*
1806 * Copy out elem_size, but not larger than
1807 * the size of a struct kinfo_proc2.
1808 */
1809 error = dcopyout(l, &klwp, dp,
1810 min(sizeof(klwp), elem_size));
1811 if (error) {
1812 rw_exit(&p->p_reflock);
1813 goto cleanup;
1814 }
1815 mutex_enter(p->p_lock);
1816 LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1817 if (l2 == l3)
1818 break;
1819 }
1820 if (l3 == NULL) {
1821 mutex_exit(p->p_lock);
1822 rw_exit(&p->p_reflock);
1823 error = EAGAIN;
1824 goto cleanup;
1825 }
1826 dp += elem_size;
1827 buflen -= elem_size;
1828 elem_count--;
1829 }
1830 needed += elem_size;
1831 }
1832 mutex_exit(p->p_lock);
1833
1834 /* Drop reference to process. */
1835 rw_exit(&p->p_reflock);
1836 }
1837
1838 if (where != NULL) {
1839 *oldlenp = dp - where;
1840 if (needed > *oldlenp) {
1841 sysctl_relock();
1842 return (ENOMEM);
1843 }
1844 } else {
1845 needed += KERN_LWPSLOP;
1846 *oldlenp = needed;
1847 }
1848 error = 0;
1849 cleanup:
1850 sysctl_relock();
1851 return (error);
1852 }
1853
1854 /*
1855 * sysctl helper routine for kern.forkfsleep node. Ensures that the
1856 * given value is not too large or two small, and is at least one
1857 * timer tick if not zero.
1858 */
1859 static int
1860 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1861 {
1862 /* userland sees value in ms, internally is in ticks */
1863 extern int forkfsleep; /* defined in kern/kern_fork.c */
1864 int error, timo, lsleep;
1865 struct sysctlnode node;
1866
1867 lsleep = forkfsleep * 1000 / hz;
1868 node = *rnode;
1869 node.sysctl_data = &lsleep;
1870 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1871 if (error || newp == NULL)
1872 return (error);
1873
1874 /* refuse negative values, and overly 'long time' */
1875 if (lsleep < 0 || lsleep > MAXSLP * 1000)
1876 return (EINVAL);
1877
1878 timo = mstohz(lsleep);
1879
1880 /* if the interval is >0 ms && <1 tick, use 1 tick */
1881 if (lsleep != 0 && timo == 0)
1882 forkfsleep = 1;
1883 else
1884 forkfsleep = timo;
1885
1886 return (0);
1887 }
1888
1889 /*
1890 * sysctl helper routine for kern.root_partition
1891 */
1892 static int
1893 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1894 {
1895 int rootpart = DISKPART(rootdev);
1896 struct sysctlnode node = *rnode;
1897
1898 node.sysctl_data = &rootpart;
1899 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1900 }
1901
1902 /*
1903 * sysctl helper function for kern.drivers
1904 */
1905 static int
1906 sysctl_kern_drivers(SYSCTLFN_ARGS)
1907 {
1908 int error;
1909 size_t buflen;
1910 struct kinfo_drivers kd;
1911 char *start, *where;
1912 const char *dname;
1913 int i;
1914 extern struct devsw_conv *devsw_conv;
1915 extern int max_devsw_convs;
1916
1917 if (newp != NULL || namelen != 0)
1918 return (EINVAL);
1919
1920 start = where = oldp;
1921 buflen = *oldlenp;
1922 if (where == NULL) {
1923 *oldlenp = max_devsw_convs * sizeof kd;
1924 return 0;
1925 }
1926
1927 /*
1928 * An array of kinfo_drivers structures
1929 */
1930 error = 0;
1931 sysctl_unlock();
1932 mutex_enter(&specfs_lock);
1933 for (i = 0; i < max_devsw_convs; i++) {
1934 dname = devsw_conv[i].d_name;
1935 if (dname == NULL)
1936 continue;
1937 if (buflen < sizeof kd) {
1938 error = ENOMEM;
1939 break;
1940 }
1941 memset(&kd, 0, sizeof(kd));
1942 kd.d_bmajor = devsw_conv[i].d_bmajor;
1943 kd.d_cmajor = devsw_conv[i].d_cmajor;
1944 strlcpy(kd.d_name, dname, sizeof kd.d_name);
1945 mutex_exit(&specfs_lock);
1946 error = dcopyout(l, &kd, where, sizeof kd);
1947 mutex_enter(&specfs_lock);
1948 if (error != 0)
1949 break;
1950 buflen -= sizeof kd;
1951 where += sizeof kd;
1952 }
1953 mutex_exit(&specfs_lock);
1954 sysctl_relock();
1955 *oldlenp = where - start;
1956 return error;
1957 }
1958
1959 /*
1960 * sysctl helper function for kern.file2
1961 */
1962 static int
1963 sysctl_kern_file2(SYSCTLFN_ARGS)
1964 {
1965 struct proc *p;
1966 struct file *fp, *tp, *np;
1967 struct filedesc *fd;
1968 struct kinfo_file kf;
1969 char *dp;
1970 u_int i, op;
1971 size_t len, needed, elem_size, out_size;
1972 int error, arg, elem_count;
1973 fdfile_t *ff;
1974
1975 if (namelen == 1 && name[0] == CTL_QUERY)
1976 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1977
1978 if (namelen != 4)
1979 return (EINVAL);
1980
1981 error = 0;
1982 dp = oldp;
1983 len = (oldp != NULL) ? *oldlenp : 0;
1984 op = name[0];
1985 arg = name[1];
1986 elem_size = name[2];
1987 elem_count = name[3];
1988 out_size = MIN(sizeof(kf), elem_size);
1989 needed = 0;
1990
1991 if (elem_size < 1 || elem_count < 0)
1992 return (EINVAL);
1993
1994 switch (op) {
1995 case KERN_FILE_BYFILE:
1996 /*
1997 * doesn't use arg so it must be zero
1998 */
1999 if (arg != 0)
2000 return (EINVAL);
2001 sysctl_unlock();
2002 /*
2003 * allocate dummy file descriptor to make position in list
2004 */
2005 if ((tp = fgetdummy()) == NULL) {
2006 sysctl_relock();
2007 return ENOMEM;
2008 }
2009 mutex_enter(&filelist_lock);
2010 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
2011 np = LIST_NEXT(fp, f_list);
2012 mutex_enter(&fp->f_lock);
2013 if (fp->f_count == 0) {
2014 mutex_exit(&fp->f_lock);
2015 continue;
2016 }
2017 /*
2018 * XXX Need to prevent that from being an alternative
2019 * XXX way for getting process information.
2020 */
2021 if (kauth_authorize_generic(l->l_cred,
2022 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
2023 mutex_exit(&fp->f_lock);
2024 continue;
2025 }
2026 if (len >= elem_size && elem_count > 0) {
2027 fill_file(&kf, fp, NULL, 0, 0);
2028 LIST_INSERT_AFTER(fp, tp, f_list);
2029 mutex_exit(&fp->f_lock);
2030 mutex_exit(&filelist_lock);
2031 error = dcopyout(l, &kf, dp, out_size);
2032 mutex_enter(&filelist_lock);
2033 np = LIST_NEXT(tp, f_list);
2034 LIST_REMOVE(tp, f_list);
2035 if (error) {
2036 break;
2037 }
2038 dp += elem_size;
2039 len -= elem_size;
2040 } else {
2041 mutex_exit(&fp->f_lock);
2042 }
2043 if (elem_count > 0) {
2044 needed += elem_size;
2045 if (elem_count != INT_MAX)
2046 elem_count--;
2047 }
2048 }
2049 mutex_exit(&filelist_lock);
2050 fputdummy(tp);
2051 sysctl_relock();
2052 break;
2053 case KERN_FILE_BYPID:
2054 if (arg < -1)
2055 /* -1 means all processes */
2056 return (EINVAL);
2057 sysctl_unlock();
2058 mutex_enter(proc_lock);
2059 LIST_FOREACH(p, &allproc, p_list) {
2060 if (p->p_stat == SIDL) {
2061 /* skip embryonic processes */
2062 continue;
2063 }
2064 if (arg > 0 && p->p_pid != arg) {
2065 /* pick only the one we want */
2066 /* XXX want 0 to mean "kernel files" */
2067 continue;
2068 }
2069 mutex_enter(p->p_lock);
2070 error = kauth_authorize_process(l->l_cred,
2071 KAUTH_PROCESS_CANSEE, p,
2072 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
2073 NULL, NULL);
2074 mutex_exit(p->p_lock);
2075 if (error != 0) {
2076 continue;
2077 }
2078
2079 /*
2080 * Grab a hold on the process.
2081 */
2082 if (!rw_tryenter(&p->p_reflock, RW_READER)) {
2083 continue;
2084 }
2085 mutex_exit(proc_lock);
2086
2087 /* XXX Do we need to check permission per file? */
2088 fd = p->p_fd;
2089 mutex_enter(&fd->fd_lock);
2090 for (i = 0; i < fd->fd_nfiles; i++) {
2091 if ((ff = fd->fd_ofiles[i]) == NULL) {
2092 continue;
2093 }
2094 mutex_enter(&ff->ff_lock);
2095 if ((fp = ff->ff_file) == NULL) {
2096 mutex_exit(&ff->ff_lock);
2097 continue;
2098 }
2099 if (len >= elem_size && elem_count > 0) {
2100 mutex_enter(&fp->f_lock);
2101 fill_file(&kf, fp, ff, i, p->p_pid);
2102 mutex_exit(&fp->f_lock);
2103 mutex_exit(&ff->ff_lock);
2104 mutex_exit(&fd->fd_lock);
2105 error = dcopyout(l, &kf, dp, out_size);
2106 mutex_enter(&fd->fd_lock);
2107 if (error)
2108 break;
2109 dp += elem_size;
2110 len -= elem_size;
2111 } else {
2112 mutex_exit(&ff->ff_lock);
2113 }
2114 if (elem_count > 0) {
2115 needed += elem_size;
2116 if (elem_count != INT_MAX)
2117 elem_count--;
2118 }
2119 }
2120 mutex_exit(&fd->fd_lock);
2121
2122 /*
2123 * Release reference to process.
2124 */
2125 mutex_enter(proc_lock);
2126 rw_exit(&p->p_reflock);
2127 }
2128 mutex_exit(proc_lock);
2129 sysctl_relock();
2130 break;
2131 default:
2132 return (EINVAL);
2133 }
2134
2135 if (oldp == NULL)
2136 needed += KERN_FILESLOP * elem_size;
2137 *oldlenp = needed;
2138
2139 return (error);
2140 }
2141
2142 static void
2143 fill_file(struct kinfo_file *kp, const file_t *fp, const fdfile_t *ff,
2144 int i, pid_t pid)
2145 {
2146
2147 memset(kp, 0, sizeof(*kp));
2148
2149 kp->ki_fileaddr = PTRTOUINT64(fp);
2150 kp->ki_flag = fp->f_flag;
2151 kp->ki_iflags = fp->f_iflags;
2152 kp->ki_ftype = fp->f_type;
2153 kp->ki_count = fp->f_count;
2154 kp->ki_msgcount = fp->f_msgcount;
2155 kp->ki_fucred = PTRTOUINT64(fp->f_cred);
2156 kp->ki_fuid = kauth_cred_geteuid(fp->f_cred);
2157 kp->ki_fgid = kauth_cred_getegid(fp->f_cred);
2158 kp->ki_fops = PTRTOUINT64(fp->f_ops);
2159 kp->ki_foffset = fp->f_offset;
2160 kp->ki_fdata = PTRTOUINT64(fp->f_data);
2161
2162 /* vnode information to glue this file to something */
2163 if (fp->f_type == DTYPE_VNODE) {
2164 struct vnode *vp = (struct vnode *)fp->f_data;
2165
2166 kp->ki_vun = PTRTOUINT64(vp->v_un.vu_socket);
2167 kp->ki_vsize = vp->v_size;
2168 kp->ki_vtype = vp->v_type;
2169 kp->ki_vtag = vp->v_tag;
2170 kp->ki_vdata = PTRTOUINT64(vp->v_data);
2171 }
2172
2173 /* process information when retrieved via KERN_FILE_BYPID */
2174 if (ff != NULL) {
2175 kp->ki_pid = pid;
2176 kp->ki_fd = i;
2177 kp->ki_ofileflags = ff->ff_exclose;
2178 kp->ki_usecount = ff->ff_refcnt;
2179 }
2180 }
2181
2182 static int
2183 sysctl_doeproc(SYSCTLFN_ARGS)
2184 {
2185 struct eproc *eproc;
2186 struct kinfo_proc2 *kproc2;
2187 struct kinfo_proc *dp;
2188 struct proc *p, *next, *marker;
2189 char *where, *dp2;
2190 int type, op, arg, error;
2191 u_int elem_size, elem_count;
2192 size_t buflen, needed;
2193 bool match, zombie, mmmbrains;
2194
2195 if (namelen == 1 && name[0] == CTL_QUERY)
2196 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2197
2198 dp = oldp;
2199 dp2 = where = oldp;
2200 buflen = where != NULL ? *oldlenp : 0;
2201 error = 0;
2202 needed = 0;
2203 type = rnode->sysctl_num;
2204
2205 if (type == KERN_PROC) {
2206 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
2207 return (EINVAL);
2208 op = name[0];
2209 if (op != KERN_PROC_ALL)
2210 arg = name[1];
2211 else
2212 arg = 0; /* Quell compiler warning */
2213 elem_size = elem_count = 0; /* Ditto */
2214 } else {
2215 if (namelen != 4)
2216 return (EINVAL);
2217 op = name[0];
2218 arg = name[1];
2219 elem_size = name[2];
2220 elem_count = name[3];
2221 }
2222
2223 sysctl_unlock();
2224
2225 if (type == KERN_PROC) {
2226 eproc = kmem_alloc(sizeof(*eproc), KM_SLEEP);
2227 kproc2 = NULL;
2228 } else {
2229 eproc = NULL;
2230 kproc2 = kmem_alloc(sizeof(*kproc2), KM_SLEEP);
2231 }
2232 marker = kmem_alloc(sizeof(*marker), KM_SLEEP);
2233
2234 mutex_enter(proc_lock);
2235 mmmbrains = false;
2236 for (p = LIST_FIRST(&allproc);; p = next) {
2237 if (p == NULL) {
2238 if (!mmmbrains) {
2239 p = LIST_FIRST(&zombproc);
2240 mmmbrains = true;
2241 }
2242 if (p == NULL)
2243 break;
2244 }
2245 next = LIST_NEXT(p, p_list);
2246
2247 /*
2248 * Skip embryonic processes.
2249 */
2250 if (p->p_stat == SIDL)
2251 continue;
2252
2253 mutex_enter(p->p_lock);
2254 error = kauth_authorize_process(l->l_cred,
2255 KAUTH_PROCESS_CANSEE, p,
2256 KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
2257 if (error != 0) {
2258 mutex_exit(p->p_lock);
2259 continue;
2260 }
2261
2262 /*
2263 * TODO - make more efficient (see notes below).
2264 * do by session.
2265 */
2266 switch (op) {
2267 case KERN_PROC_PID:
2268 /* could do this with just a lookup */
2269 match = (p->p_pid == (pid_t)arg);
2270 break;
2271
2272 case KERN_PROC_PGRP:
2273 /* could do this by traversing pgrp */
2274 match = (p->p_pgrp->pg_id == (pid_t)arg);
2275 break;
2276
2277 case KERN_PROC_SESSION:
2278 match = (p->p_session->s_sid == (pid_t)arg);
2279 break;
2280
2281 case KERN_PROC_TTY:
2282 match = true;
2283 if (arg == (int) KERN_PROC_TTY_REVOKE) {
2284 if ((p->p_lflag & PL_CONTROLT) == 0 ||
2285 p->p_session->s_ttyp == NULL ||
2286 p->p_session->s_ttyvp != NULL) {
2287 match = false;
2288 }
2289 } else if ((p->p_lflag & PL_CONTROLT) == 0 ||
2290 p->p_session->s_ttyp == NULL) {
2291 if ((dev_t)arg != KERN_PROC_TTY_NODEV) {
2292 match = false;
2293 }
2294 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg) {
2295 match = false;
2296 }
2297 break;
2298
2299 case KERN_PROC_UID:
2300 match = (kauth_cred_geteuid(p->p_cred) == (uid_t)arg);
2301 break;
2302
2303 case KERN_PROC_RUID:
2304 match = (kauth_cred_getuid(p->p_cred) == (uid_t)arg);
2305 break;
2306
2307 case KERN_PROC_GID:
2308 match = (kauth_cred_getegid(p->p_cred) == (uid_t)arg);
2309 break;
2310
2311 case KERN_PROC_RGID:
2312 match = (kauth_cred_getgid(p->p_cred) == (uid_t)arg);
2313 break;
2314
2315 case KERN_PROC_ALL:
2316 match = true;
2317 /* allow everything */
2318 break;
2319
2320 default:
2321 error = EINVAL;
2322 mutex_exit(p->p_lock);
2323 goto cleanup;
2324 }
2325 if (!match) {
2326 mutex_exit(p->p_lock);
2327 continue;
2328 }
2329
2330 /*
2331 * Grab a hold on the process.
2332 */
2333 if (mmmbrains) {
2334 zombie = true;
2335 } else {
2336 zombie = !rw_tryenter(&p->p_reflock, RW_READER);
2337 }
2338 if (zombie) {
2339 LIST_INSERT_AFTER(p, marker, p_list);
2340 }
2341
2342 if (type == KERN_PROC) {
2343 if (buflen >= sizeof(struct kinfo_proc)) {
2344 fill_eproc(p, eproc, zombie);
2345 mutex_exit(p->p_lock);
2346 mutex_exit(proc_lock);
2347 error = dcopyout(l, p, &dp->kp_proc,
2348 sizeof(struct proc));
2349 mutex_enter(proc_lock);
2350 if (error) {
2351 goto bah;
2352 }
2353 error = dcopyout(l, eproc, &dp->kp_eproc,
2354 sizeof(*eproc));
2355 if (error) {
2356 goto bah;
2357 }
2358 dp++;
2359 buflen -= sizeof(struct kinfo_proc);
2360 } else {
2361 mutex_exit(p->p_lock);
2362 }
2363 needed += sizeof(struct kinfo_proc);
2364 } else { /* KERN_PROC2 */
2365 if (buflen >= elem_size && elem_count > 0) {
2366 fill_kproc2(p, kproc2, zombie);
2367 mutex_exit(p->p_lock);
2368 mutex_exit(proc_lock);
2369 /*
2370 * Copy out elem_size, but not larger than
2371 * the size of a struct kinfo_proc2.
2372 */
2373 error = dcopyout(l, kproc2, dp2,
2374 min(sizeof(*kproc2), elem_size));
2375 mutex_enter(proc_lock);
2376 if (error) {
2377 goto bah;
2378 }
2379 dp2 += elem_size;
2380 buflen -= elem_size;
2381 elem_count--;
2382 } else {
2383 mutex_exit(p->p_lock);
2384 }
2385 needed += elem_size;
2386 }
2387
2388 /*
2389 * Release reference to process.
2390 */
2391 if (zombie) {
2392 next = LIST_NEXT(marker, p_list);
2393 LIST_REMOVE(marker, p_list);
2394 } else {
2395 rw_exit(&p->p_reflock);
2396 }
2397 }
2398 mutex_exit(proc_lock);
2399
2400 if (where != NULL) {
2401 if (type == KERN_PROC)
2402 *oldlenp = (char *)dp - where;
2403 else
2404 *oldlenp = dp2 - where;
2405 if (needed > *oldlenp) {
2406 error = ENOMEM;
2407 goto out;
2408 }
2409 } else {
2410 needed += KERN_PROCSLOP;
2411 *oldlenp = needed;
2412 }
2413 if (kproc2)
2414 kmem_free(kproc2, sizeof(*kproc2));
2415 if (eproc)
2416 kmem_free(eproc, sizeof(*eproc));
2417 if (marker)
2418 kmem_free(marker, sizeof(*marker));
2419 sysctl_relock();
2420 return 0;
2421 bah:
2422 if (zombie)
2423 LIST_REMOVE(marker, p_list);
2424 else
2425 rw_exit(&p->p_reflock);
2426 cleanup:
2427 mutex_exit(proc_lock);
2428 out:
2429 if (kproc2)
2430 kmem_free(kproc2, sizeof(*kproc2));
2431 if (eproc)
2432 kmem_free(eproc, sizeof(*eproc));
2433 if (marker)
2434 kmem_free(marker, sizeof(*marker));
2435 sysctl_relock();
2436 return error;
2437 }
2438
2439 /*
2440 * sysctl helper routine for kern.proc_args pseudo-subtree.
2441 */
2442 static int
2443 sysctl_kern_proc_args(SYSCTLFN_ARGS)
2444 {
2445 struct ps_strings pss;
2446 struct proc *p;
2447 size_t len, i;
2448 struct uio auio;
2449 struct iovec aiov;
2450 pid_t pid;
2451 int nargv, type, error, argvlen;
2452 char *arg;
2453 char **argv = NULL;
2454 char *tmp;
2455 struct vmspace *vmspace;
2456 vaddr_t psstr_addr;
2457 vaddr_t offsetn;
2458 vaddr_t offsetv;
2459
2460 if (namelen == 1 && name[0] == CTL_QUERY)
2461 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2462
2463 if (newp != NULL || namelen != 2)
2464 return (EINVAL);
2465 pid = name[0];
2466 type = name[1];
2467 argv = NULL;
2468 argvlen = 0;
2469
2470 switch (type) {
2471 case KERN_PROC_ARGV:
2472 case KERN_PROC_NARGV:
2473 case KERN_PROC_ENV:
2474 case KERN_PROC_NENV:
2475 /* ok */
2476 break;
2477 default:
2478 return (EINVAL);
2479 }
2480
2481 sysctl_unlock();
2482
2483 /* check pid */
2484 mutex_enter(proc_lock);
2485 if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
2486 error = EINVAL;
2487 goto out_locked;
2488 }
2489 mutex_enter(p->p_lock);
2490
2491 /* Check permission. */
2492 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
2493 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
2494 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ARGS), NULL, NULL);
2495 else if (type == KERN_PROC_ENV || type == KERN_PROC_NENV)
2496 error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
2497 p, KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENV), NULL, NULL);
2498 else
2499 error = EINVAL; /* XXXGCC */
2500 if (error) {
2501 mutex_exit(p->p_lock);
2502 goto out_locked;
2503 }
2504
2505 if (oldp == NULL) {
2506 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
2507 *oldlenp = sizeof (int);
2508 else
2509 *oldlenp = ARG_MAX; /* XXX XXX XXX */
2510 error = 0;
2511 mutex_exit(p->p_lock);
2512 goto out_locked;
2513 }
2514
2515 /*
2516 * Zombies don't have a stack, so we can't read their psstrings.
2517 * System processes also don't have a user stack.
2518 */
2519 if (P_ZOMBIE(p) || (p->p_flag & PK_SYSTEM) != 0) {
2520 error = EINVAL;
2521 mutex_exit(p->p_lock);
2522 goto out_locked;
2523 }
2524
2525 /*
2526 * Lock the process down in memory.
2527 */
2528 psstr_addr = (vaddr_t)p->p_psstr;
2529 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
2530 offsetn = p->p_psnargv;
2531 offsetv = p->p_psargv;
2532 } else {
2533 offsetn = p->p_psnenv;
2534 offsetv = p->p_psenv;
2535 }
2536 vmspace = p->p_vmspace;
2537 uvmspace_addref(vmspace);
2538 mutex_exit(p->p_lock);
2539 mutex_exit(proc_lock);
2540
2541 /*
2542 * Allocate a temporary buffer to hold the arguments.
2543 */
2544 arg = kmem_alloc(PAGE_SIZE, KM_SLEEP);
2545
2546 /*
2547 * Read in the ps_strings structure.
2548 */
2549 aiov.iov_base = &pss;
2550 aiov.iov_len = sizeof(pss);
2551 auio.uio_iov = &aiov;
2552 auio.uio_iovcnt = 1;
2553 auio.uio_offset = psstr_addr;
2554 auio.uio_resid = sizeof(pss);
2555 auio.uio_rw = UIO_READ;
2556 UIO_SETUP_SYSSPACE(&auio);
2557 error = uvm_io(&vmspace->vm_map, &auio);
2558 if (error)
2559 goto done;
2560
2561 memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
2562 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
2563 error = dcopyout(l, &nargv, oldp, sizeof(nargv));
2564 *oldlenp = sizeof(nargv);
2565 goto done;
2566 }
2567 /*
2568 * Now read the address of the argument vector.
2569 */
2570 switch (type) {
2571 case KERN_PROC_ARGV:
2572 /* FALLTHROUGH */
2573 case KERN_PROC_ENV:
2574 memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
2575 break;
2576 default:
2577 error = EINVAL;
2578 goto done;
2579 }
2580
2581 #ifdef COMPAT_NETBSD32
2582 if (p->p_flag & PK_32)
2583 len = sizeof(netbsd32_charp) * nargv;
2584 else
2585 #endif
2586 len = sizeof(char *) * nargv;
2587
2588 if ((argvlen = len) != 0)
2589 argv = kmem_alloc(len, KM_SLEEP);
2590
2591 aiov.iov_base = argv;
2592 aiov.iov_len = len;
2593 auio.uio_iov = &aiov;
2594 auio.uio_iovcnt = 1;
2595 auio.uio_offset = (off_t)(unsigned long)tmp;
2596 auio.uio_resid = len;
2597 auio.uio_rw = UIO_READ;
2598 UIO_SETUP_SYSSPACE(&auio);
2599 error = uvm_io(&vmspace->vm_map, &auio);
2600 if (error)
2601 goto done;
2602
2603 /*
2604 * Now copy each string.
2605 */
2606 len = 0; /* bytes written to user buffer */
2607 for (i = 0; i < nargv; i++) {
2608 int finished = 0;
2609 vaddr_t base;
2610 size_t xlen;
2611 int j;
2612
2613 #ifdef COMPAT_NETBSD32
2614 if (p->p_flag & PK_32) {
2615 netbsd32_charp *argv32;
2616
2617 argv32 = (netbsd32_charp *)argv;
2618 base = (vaddr_t)NETBSD32PTR64(argv32[i]);
2619 } else
2620 #endif
2621 base = (vaddr_t)argv[i];
2622
2623 /*
2624 * The program has messed around with its arguments,
2625 * possibly deleting some, and replacing them with
2626 * NULL's. Treat this as the last argument and not
2627 * a failure.
2628 */
2629 if (base == 0)
2630 break;
2631
2632 while (!finished) {
2633 xlen = PAGE_SIZE - (base & PAGE_MASK);
2634
2635 aiov.iov_base = arg;
2636 aiov.iov_len = PAGE_SIZE;
2637 auio.uio_iov = &aiov;
2638 auio.uio_iovcnt = 1;
2639 auio.uio_offset = base;
2640 auio.uio_resid = xlen;
2641 auio.uio_rw = UIO_READ;
2642 UIO_SETUP_SYSSPACE(&auio);
2643 error = uvm_io(&vmspace->vm_map, &auio);
2644 if (error)
2645 goto done;
2646
2647 /* Look for the end of the string */
2648 for (j = 0; j < xlen; j++) {
2649 if (arg[j] == '\0') {
2650 xlen = j + 1;
2651 finished = 1;
2652 break;
2653 }
2654 }
2655
2656 /* Check for user buffer overflow */
2657 if (len + xlen > *oldlenp) {
2658 finished = 1;
2659 if (len > *oldlenp)
2660 xlen = 0;
2661 else
2662 xlen = *oldlenp - len;
2663 }
2664
2665 /* Copyout the page */
2666 error = dcopyout(l, arg, (char *)oldp + len, xlen);
2667 if (error)
2668 goto done;
2669
2670 len += xlen;
2671 base += xlen;
2672 }
2673 }
2674 *oldlenp = len;
2675
2676 done:
2677 if (argvlen != 0)
2678 kmem_free(argv, argvlen);
2679 uvmspace_free(vmspace);
2680 kmem_free(arg, PAGE_SIZE);
2681 sysctl_relock();
2682 return error;
2683
2684 out_locked:
2685 mutex_exit(proc_lock);
2686 sysctl_relock();
2687 return error;
2688 }
2689
2690 static int
2691 sysctl_security_setidcore(SYSCTLFN_ARGS)
2692 {
2693 int newsize, error;
2694 struct sysctlnode node;
2695
2696 node = *rnode;
2697 node.sysctl_data = &newsize;
2698 newsize = *(int *)rnode->sysctl_data;
2699 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2700 if (error || newp == NULL)
2701 return error;
2702
2703 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
2704 0, NULL, NULL, NULL))
2705 return (EPERM);
2706
2707 *(int *)rnode->sysctl_data = newsize;
2708
2709 return 0;
2710 }
2711
2712 static int
2713 sysctl_security_setidcorename(SYSCTLFN_ARGS)
2714 {
2715 int error;
2716 char *newsetidcorename;
2717 struct sysctlnode node;
2718
2719 newsetidcorename = PNBUF_GET();
2720 node = *rnode;
2721 node.sysctl_data = newsetidcorename;
2722 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
2723 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2724 if (error || newp == NULL) {
2725 goto out;
2726 }
2727 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_SETIDCORE,
2728 0, NULL, NULL, NULL)) {
2729 error = EPERM;
2730 goto out;
2731 }
2732 if (strlen(newsetidcorename) == 0) {
2733 error = EINVAL;
2734 goto out;
2735 }
2736 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
2737 out:
2738 PNBUF_PUT(newsetidcorename);
2739 return error;
2740 }
2741
2742 /*
2743 * sysctl helper routine for kern.cp_id node. Maps cpus to their
2744 * cpuids.
2745 */
2746 static int
2747 sysctl_kern_cpid(SYSCTLFN_ARGS)
2748 {
2749 struct sysctlnode node = *rnode;
2750 uint64_t *cp_id = NULL;
2751 int error, n = ncpu;
2752 struct cpu_info *ci;
2753 CPU_INFO_ITERATOR cii;
2754
2755 /*
2756 * Here you may either retrieve a single cpu id or the whole
2757 * set. The size you get back when probing depends on what
2758 * you ask for.
2759 */
2760 switch (namelen) {
2761 case 0:
2762 node.sysctl_size = n * sizeof(uint64_t);
2763 n = -2; /* ALL */
2764 break;
2765 case 1:
2766 if (name[0] < 0 || name[0] >= n)
2767 return (ENOENT); /* ENOSUCHPROCESSOR */
2768 node.sysctl_size = sizeof(uint64_t);
2769 n = name[0];
2770 /*
2771 * adjust these so that sysctl_lookup() will be happy
2772 */
2773 name++;
2774 namelen--;
2775 break;
2776 default:
2777 return (EINVAL);
2778 }
2779
2780 cp_id = kmem_alloc(node.sysctl_size, KM_SLEEP);
2781 if (cp_id == NULL)
2782 return (ENOMEM);
2783 node.sysctl_data = cp_id;
2784 memset(cp_id, 0, node.sysctl_size);
2785
2786 for (CPU_INFO_FOREACH(cii, ci)) {
2787 if (n <= 0)
2788 cp_id[0] = cpu_index(ci);
2789 /*
2790 * if a specific processor was requested and we just
2791 * did it, we're done here
2792 */
2793 if (n == 0)
2794 break;
2795 /*
2796 * if doing "all", skip to next cp_id slot for next processor
2797 */
2798 if (n == -2)
2799 cp_id++;
2800 /*
2801 * if we're doing a specific processor, we're one
2802 * processor closer
2803 */
2804 if (n > 0)
2805 n--;
2806 }
2807
2808 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2809 kmem_free(node.sysctl_data, node.sysctl_size);
2810 return (error);
2811 }
2812
2813 /*
2814 * sysctl helper routine for hw.usermem and hw.usermem64. Values are
2815 * calculate on the fly taking into account integer overflow and the
2816 * current wired count.
2817 */
2818 static int
2819 sysctl_hw_usermem(SYSCTLFN_ARGS)
2820 {
2821 u_int ui;
2822 u_quad_t uq;
2823 struct sysctlnode node;
2824
2825 node = *rnode;
2826 switch (rnode->sysctl_num) {
2827 case HW_USERMEM:
2828 if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
2829 ui = UINT_MAX;
2830 else
2831 ui *= PAGE_SIZE;
2832 node.sysctl_data = &ui;
2833 break;
2834 case HW_USERMEM64:
2835 uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
2836 node.sysctl_data = &uq;
2837 break;
2838 default:
2839 return (EINVAL);
2840 }
2841
2842 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2843 }
2844
2845 /*
2846 * sysctl helper routine for kern.cnmagic node. Pulls the old value
2847 * out, encoded, and stuffs the new value in for decoding.
2848 */
2849 static int
2850 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
2851 {
2852 char magic[CNS_LEN];
2853 int error;
2854 struct sysctlnode node;
2855
2856 if (oldp)
2857 cn_get_magic(magic, CNS_LEN);
2858 node = *rnode;
2859 node.sysctl_data = &magic[0];
2860 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2861 if (error || newp == NULL)
2862 return (error);
2863
2864 return (cn_set_magic(magic));
2865 }
2866
2867 /*
2868 * ********************************************************************
2869 * section 3: public helper routines that are used for more than one
2870 * node
2871 * ********************************************************************
2872 */
2873
2874 /*
2875 * sysctl helper routine for the kern.root_device node and some ports'
2876 * machdep.root_device nodes.
2877 */
2878 int
2879 sysctl_root_device(SYSCTLFN_ARGS)
2880 {
2881 struct sysctlnode node;
2882
2883 node = *rnode;
2884 node.sysctl_data = root_device->dv_xname;
2885 node.sysctl_size = strlen(device_xname(root_device)) + 1;
2886 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2887 }
2888
2889 /*
2890 * sysctl helper routine for kern.consdev, dependent on the current
2891 * state of the console. Also used for machdep.console_device on some
2892 * ports.
2893 */
2894 int
2895 sysctl_consdev(SYSCTLFN_ARGS)
2896 {
2897 dev_t consdev;
2898 struct sysctlnode node;
2899
2900 if (cn_tab != NULL)
2901 consdev = cn_tab->cn_dev;
2902 else
2903 consdev = NODEV;
2904 node = *rnode;
2905 node.sysctl_data = &consdev;
2906 node.sysctl_size = sizeof(consdev);
2907 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2908 }
2909
2910 /*
2911 * ********************************************************************
2912 * section 4: support for some helpers
2913 * ********************************************************************
2914 */
2915 /*
2916 * Find the most ``active'' lwp of a process and return it for ps display
2917 * purposes
2918 */
2919 static struct lwp *
2920 proc_active_lwp(struct proc *p)
2921 {
2922 static const int ostat[] = {
2923 0,
2924 2, /* LSIDL */
2925 6, /* LSRUN */
2926 5, /* LSSLEEP */
2927 4, /* LSSTOP */
2928 0, /* LSZOMB */
2929 1, /* LSDEAD */
2930 7, /* LSONPROC */
2931 3 /* LSSUSPENDED */
2932 };
2933
2934 struct lwp *l, *lp = NULL;
2935 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2936 KASSERT(l->l_stat >= 0 && l->l_stat < __arraycount(ostat));
2937 if (lp == NULL ||
2938 ostat[l->l_stat] > ostat[lp->l_stat] ||
2939 (ostat[l->l_stat] == ostat[lp->l_stat] &&
2940 l->l_cpticks > lp->l_cpticks)) {
2941 lp = l;
2942 continue;
2943 }
2944 }
2945 return lp;
2946 }
2947
2948
2949 /*
2950 * Fill in a kinfo_proc2 structure for the specified process.
2951 */
2952 static void
2953 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki, bool zombie)
2954 {
2955 struct tty *tp;
2956 struct lwp *l, *l2;
2957 struct timeval ut, st, rt;
2958 sigset_t ss1, ss2;
2959 struct rusage ru;
2960 struct vmspace *vm;
2961
2962 KASSERT(mutex_owned(proc_lock));
2963 KASSERT(mutex_owned(p->p_lock));
2964
2965 sigemptyset(&ss1);
2966 sigemptyset(&ss2);
2967 memset(ki, 0, sizeof(*ki));
2968
2969 ki->p_paddr = PTRTOUINT64(p);
2970 ki->p_fd = PTRTOUINT64(p->p_fd);
2971 ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
2972 ki->p_stats = PTRTOUINT64(p->p_stats);
2973 ki->p_limit = PTRTOUINT64(p->p_limit);
2974 ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
2975 ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
2976 ki->p_sess = PTRTOUINT64(p->p_session);
2977 ki->p_tsess = 0; /* may be changed if controlling tty below */
2978 ki->p_ru = PTRTOUINT64(&p->p_stats->p_ru);
2979 ki->p_eflag = 0;
2980 ki->p_exitsig = p->p_exitsig;
2981 ki->p_flag = sysctl_map_flags(sysctl_flagmap, p->p_flag);
2982 ki->p_flag |= sysctl_map_flags(sysctl_sflagmap, p->p_sflag);
2983 ki->p_flag |= sysctl_map_flags(sysctl_slflagmap, p->p_slflag);
2984 ki->p_flag |= sysctl_map_flags(sysctl_lflagmap, p->p_lflag);
2985 ki->p_flag |= sysctl_map_flags(sysctl_stflagmap, p->p_stflag);
2986 ki->p_pid = p->p_pid;
2987 if (p->p_pptr)
2988 ki->p_ppid = p->p_pptr->p_pid;
2989 else
2990 ki->p_ppid = 0;
2991 ki->p_uid = kauth_cred_geteuid(p->p_cred);
2992 ki->p_ruid = kauth_cred_getuid(p->p_cred);
2993 ki->p_gid = kauth_cred_getegid(p->p_cred);
2994 ki->p_rgid = kauth_cred_getgid(p->p_cred);
2995 ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
2996 ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
2997 ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
2998 kauth_cred_getgroups(p->p_cred, ki->p_groups,
2999 min(ki->p_ngroups, sizeof(ki->p_groups) / sizeof(ki->p_groups[0])),
3000 UIO_SYSSPACE);
3001
3002 ki->p_uticks = p->p_uticks;
3003 ki->p_sticks = p->p_sticks;
3004 ki->p_iticks = p->p_iticks;
3005 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */
3006 ki->p_tracep = PTRTOUINT64(p->p_tracep);
3007 ki->p_traceflag = p->p_traceflag;
3008
3009 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
3010 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
3011
3012 ki->p_cpticks = 0;
3013 ki->p_pctcpu = p->p_pctcpu;
3014 ki->p_estcpu = 0;
3015 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
3016 ki->p_realstat = p->p_stat;
3017 ki->p_nice = p->p_nice;
3018 ki->p_xstat = p->p_xstat;
3019 ki->p_acflag = p->p_acflag;
3020
3021 strncpy(ki->p_comm, p->p_comm,
3022 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
3023 strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
3024
3025 ki->p_nlwps = p->p_nlwps;
3026 ki->p_realflag = ki->p_flag;
3027
3028 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
3029 vm = p->p_vmspace;
3030 ki->p_vm_rssize = vm_resident_count(vm);
3031 ki->p_vm_tsize = vm->vm_tsize;
3032 ki->p_vm_dsize = vm->vm_dsize;
3033 ki->p_vm_ssize = vm->vm_ssize;
3034
3035 /* Pick the primary (first) LWP */
3036 l = proc_active_lwp(p);
3037 KASSERT(l != NULL);
3038 lwp_lock(l);
3039 ki->p_nrlwps = p->p_nrlwps;
3040 ki->p_forw = 0;
3041 ki->p_back = 0;
3042 ki->p_addr = PTRTOUINT64(l->l_addr);
3043 ki->p_stat = l->l_stat;
3044 ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
3045 ki->p_swtime = l->l_swtime;
3046 ki->p_slptime = l->l_slptime;
3047 if (l->l_stat == LSONPROC)
3048 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
3049 else
3050 ki->p_schedflags = 0;
3051 ki->p_holdcnt = l->l_holdcnt;
3052 ki->p_priority = lwp_eprio(l);
3053 ki->p_usrpri = l->l_priority;
3054 if (l->l_wchan)
3055 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
3056 ki->p_wchan = PTRTOUINT64(l->l_wchan);
3057 ki->p_cpuid = cpu_index(l->l_cpu);
3058 lwp_unlock(l);
3059 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
3060 /* This is hardly correct, but... */
3061 sigplusset(&l->l_sigpend.sp_set, &ss1);
3062 sigplusset(&l->l_sigmask, &ss2);
3063 ki->p_cpticks += l->l_cpticks;
3064 ki->p_pctcpu += l->l_pctcpu;
3065 ki->p_estcpu += l->l_estcpu;
3066 }
3067 }
3068 sigplusset(&p->p_sigpend.sp_set, &ss2);
3069 memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
3070 memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
3071
3072 if (p->p_session != NULL) {
3073 ki->p_sid = p->p_session->s_sid;
3074 ki->p__pgid = p->p_pgrp->pg_id;
3075 if (p->p_session->s_ttyvp)
3076 ki->p_eflag |= EPROC_CTTY;
3077 if (SESS_LEADER(p))
3078 ki->p_eflag |= EPROC_SLEADER;
3079 strncpy(ki->p_login, p->p_session->s_login,
3080 min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
3081 ki->p_jobc = p->p_pgrp->pg_jobc;
3082 if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
3083 ki->p_tdev = tp->t_dev;
3084 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
3085 ki->p_tsess = PTRTOUINT64(tp->t_session);
3086 } else {
3087 ki->p_tdev = (int32_t)NODEV;
3088 }
3089 }
3090
3091 if (!P_ZOMBIE(p) && !zombie) {
3092 ki->p_uvalid = 1;
3093 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
3094 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
3095
3096 calcru(p, &ut, &st, NULL, &rt);
3097 ki->p_rtime_sec = rt.tv_sec;
3098 ki->p_rtime_usec = rt.tv_usec;
3099 ki->p_uutime_sec = ut.tv_sec;
3100 ki->p_uutime_usec = ut.tv_usec;
3101 ki->p_ustime_sec = st.tv_sec;
3102 ki->p_ustime_usec = st.tv_usec;
3103
3104 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
3105 ki->p_uru_nvcsw = 0;
3106 ki->p_uru_nivcsw = 0;
3107 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
3108 ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
3109 ki->p_uru_nivcsw += l2->l_nivcsw;
3110 ruadd(&ru, &l2->l_ru);
3111 }
3112 ki->p_uru_maxrss = ru.ru_maxrss;
3113 ki->p_uru_ixrss = ru.ru_ixrss;
3114 ki->p_uru_idrss = ru.ru_idrss;
3115 ki->p_uru_isrss = ru.ru_isrss;
3116 ki->p_uru_minflt = ru.ru_minflt;
3117 ki->p_uru_majflt = ru.ru_majflt;
3118 ki->p_uru_nswap = ru.ru_nswap;
3119 ki->p_uru_inblock = ru.ru_inblock;
3120 ki->p_uru_oublock = ru.ru_oublock;
3121 ki->p_uru_msgsnd = ru.ru_msgsnd;
3122 ki->p_uru_msgrcv = ru.ru_msgrcv;
3123 ki->p_uru_nsignals = ru.ru_nsignals;
3124
3125 timeradd(&p->p_stats->p_cru.ru_utime,
3126 &p->p_stats->p_cru.ru_stime, &ut);
3127 ki->p_uctime_sec = ut.tv_sec;
3128 ki->p_uctime_usec = ut.tv_usec;
3129 }
3130 }
3131
3132 /*
3133 * Fill in a kinfo_lwp structure for the specified lwp.
3134 */
3135 static void
3136 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
3137 {
3138 struct proc *p = l->l_proc;
3139 struct timeval tv;
3140
3141 KASSERT(lwp_locked(l, NULL));
3142
3143 kl->l_forw = 0;
3144 kl->l_back = 0;
3145 kl->l_laddr = PTRTOUINT64(l);
3146 kl->l_addr = PTRTOUINT64(l->l_addr);
3147 kl->l_stat = l->l_stat;
3148 kl->l_lid = l->l_lid;
3149 kl->l_flag = sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
3150 kl->l_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
3151
3152 kl->l_swtime = l->l_swtime;
3153 kl->l_slptime = l->l_slptime;
3154 if (l->l_stat == LSONPROC)
3155 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
3156 else
3157 kl->l_schedflags = 0;
3158 kl->l_holdcnt = l->l_holdcnt;
3159 kl->l_priority = lwp_eprio(l);
3160 kl->l_usrpri = l->l_priority;
3161 if (l->l_wchan)
3162 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
3163 kl->l_wchan = PTRTOUINT64(l->l_wchan);
3164 kl->l_cpuid = cpu_index(l->l_cpu);
3165 bintime2timeval(&l->l_rtime, &tv);
3166 kl->l_rtime_sec = tv.tv_sec;
3167 kl->l_rtime_usec = tv.tv_usec;
3168 kl->l_cpticks = l->l_cpticks;
3169 kl->l_pctcpu = l->l_pctcpu;
3170 kl->l_pid = p->p_pid;
3171 if (l->l_name == NULL)
3172 kl->l_name[0] = '\0';
3173 else
3174 strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
3175 }
3176
3177 /*
3178 * Fill in an eproc structure for the specified process.
3179 */
3180 void
3181 fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
3182 {
3183 struct tty *tp;
3184 struct lwp *l;
3185
3186 KASSERT(mutex_owned(proc_lock));
3187 KASSERT(mutex_owned(p->p_lock));
3188
3189 memset(ep, 0, sizeof(*ep));
3190
3191 ep->e_paddr = p;
3192 ep->e_sess = p->p_session;
3193 if (p->p_cred) {
3194 kauth_cred_topcred(p->p_cred, &ep->e_pcred);
3195 kauth_cred_toucred(p->p_cred, &ep->e_ucred);
3196 }
3197 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
3198 struct vmspace *vm = p->p_vmspace;
3199
3200 ep->e_vm.vm_rssize = vm_resident_count(vm);
3201 ep->e_vm.vm_tsize = vm->vm_tsize;
3202 ep->e_vm.vm_dsize = vm->vm_dsize;
3203 ep->e_vm.vm_ssize = vm->vm_ssize;
3204
3205 /* Pick the primary (first) LWP */
3206 l = proc_active_lwp(p);
3207 KASSERT(l != NULL);
3208 lwp_lock(l);
3209 if (l->l_wchan)
3210 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
3211 lwp_unlock(l);
3212 }
3213 if (p->p_pptr)
3214 ep->e_ppid = p->p_pptr->p_pid;
3215 if (p->p_pgrp && p->p_session) {
3216 ep->e_pgid = p->p_pgrp->pg_id;
3217 ep->e_jobc = p->p_pgrp->pg_jobc;
3218 ep->e_sid = p->p_session->s_sid;
3219 if ((p->p_lflag & PL_CONTROLT) &&
3220 (tp = ep->e_sess->s_ttyp)) {
3221 ep->e_tdev = tp->t_dev;
3222 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
3223 ep->e_tsess = tp->t_session;
3224 } else
3225 ep->e_tdev = NODEV;
3226 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
3227 if (SESS_LEADER(p))
3228 ep->e_flag |= EPROC_SLEADER;
3229 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
3230 }
3231 ep->e_xsize = ep->e_xrssize = 0;
3232 ep->e_xccount = ep->e_xswrss = 0;
3233 }
3234
3235 u_int
3236 sysctl_map_flags(const u_int *map, u_int word)
3237 {
3238 u_int rv;
3239
3240 for (rv = 0; *map != 0; map += 2)
3241 if ((word & map[0]) != 0)
3242 rv |= map[1];
3243
3244 return rv;
3245 }
3246