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