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