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