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