init_sysctl.c revision 1.140 1 /* $NetBSD: init_sysctl.c,v 1.140 2008/05/31 21:34:42 ad 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.140 2008/05/31 21:34:42 ad 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_PPWAIT, P_PPWAIT,
98 PS_WEXIT, P_WEXIT,
99 PS_STOPFORK, P_STOPFORK,
100 PS_STOPEXEC, P_STOPEXEC,
101 PS_STOPEXIT, P_STOPEXIT,
102 0
103 };
104
105 static const u_int sysctl_slflagmap[] = {
106 PSL_TRACED, P_TRACED,
107 PSL_FSTRACE, P_FSTRACE,
108 PSL_CHTRACED, P_CHTRACED,
109 PSL_SYSCALL, P_SYSCALL,
110 0
111 };
112
113 static const u_int sysctl_lflagmap[] = {
114 PL_CONTROLT, P_CONTROLT,
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;
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 old_vnodes = desiredvnodes;
1106 desiredvnodes = new_vnodes;
1107 if (new_vnodes < old_vnodes) {
1108 error = vfs_drainvnodes(new_vnodes, l);
1109 if (error) {
1110 desiredvnodes = old_vnodes;
1111 return (error);
1112 }
1113 }
1114 vfs_reinit();
1115 nchreinit();
1116
1117 return (0);
1118 }
1119
1120 /*
1121 * sysctl helper routine for rtc_offset - set time after changes
1122 */
1123 static int
1124 sysctl_kern_rtc_offset(SYSCTLFN_ARGS)
1125 {
1126 struct timespec ts, delta;
1127 int error, new_rtc_offset;
1128 struct sysctlnode node;
1129
1130 new_rtc_offset = rtc_offset;
1131 node = *rnode;
1132 node.sysctl_data = &new_rtc_offset;
1133 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1134 if (error || newp == NULL)
1135 return (error);
1136
1137 if (kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_TIME,
1138 KAUTH_REQ_SYSTEM_TIME_RTCOFFSET,
1139 KAUTH_ARG(new_rtc_offset), NULL, NULL))
1140 return (EPERM);
1141 if (rtc_offset == new_rtc_offset)
1142 return (0);
1143
1144 /* if we change the offset, adjust the time */
1145 nanotime(&ts);
1146 delta.tv_sec = 60 * (new_rtc_offset - rtc_offset);
1147 delta.tv_nsec = 0;
1148 timespecadd(&ts, &delta, &ts);
1149 rtc_offset = new_rtc_offset;
1150 return (settime(l->l_proc, &ts));
1151 }
1152
1153 /*
1154 * sysctl helper routine for kern.maxproc. Ensures that the new
1155 * values are not too low or too high.
1156 */
1157 static int
1158 sysctl_kern_maxproc(SYSCTLFN_ARGS)
1159 {
1160 int error, nmaxproc;
1161 struct sysctlnode node;
1162
1163 nmaxproc = maxproc;
1164 node = *rnode;
1165 node.sysctl_data = &nmaxproc;
1166 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1167 if (error || newp == NULL)
1168 return (error);
1169
1170 if (nmaxproc < 0 || nmaxproc >= PID_MAX)
1171 return (EINVAL);
1172 #ifdef __HAVE_CPU_MAXPROC
1173 if (nmaxproc > cpu_maxproc())
1174 return (EINVAL);
1175 #endif
1176 maxproc = nmaxproc;
1177
1178 return (0);
1179 }
1180
1181 /*
1182 * sysctl helper function for kern.hostid. The hostid is a long, but
1183 * we export it as an int, so we need to give it a little help.
1184 */
1185 static int
1186 sysctl_kern_hostid(SYSCTLFN_ARGS)
1187 {
1188 int error, inthostid;
1189 struct sysctlnode node;
1190
1191 inthostid = hostid; /* XXX assumes sizeof int <= sizeof long */
1192 node = *rnode;
1193 node.sysctl_data = &inthostid;
1194 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1195 if (error || newp == NULL)
1196 return (error);
1197
1198 hostid = (unsigned)inthostid;
1199
1200 return (0);
1201 }
1202
1203 /*
1204 * sysctl helper function for kern.hostname and kern.domainnname.
1205 * resets the relevant recorded length when the underlying name is
1206 * changed.
1207 */
1208 static int
1209 sysctl_setlen(SYSCTLFN_ARGS)
1210 {
1211 int error;
1212
1213 error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1214 if (error || newp == NULL)
1215 return (error);
1216
1217 switch (rnode->sysctl_num) {
1218 case KERN_HOSTNAME:
1219 hostnamelen = strlen((const char*)rnode->sysctl_data);
1220 break;
1221 case KERN_DOMAINNAME:
1222 domainnamelen = strlen((const char*)rnode->sysctl_data);
1223 break;
1224 }
1225
1226 return (0);
1227 }
1228
1229 /*
1230 * sysctl helper routine for kern.clockrate. Assembles a struct on
1231 * the fly to be returned to the caller.
1232 */
1233 static int
1234 sysctl_kern_clockrate(SYSCTLFN_ARGS)
1235 {
1236 struct clockinfo clkinfo;
1237 struct sysctlnode node;
1238
1239 clkinfo.tick = tick;
1240 clkinfo.tickadj = tickadj;
1241 clkinfo.hz = hz;
1242 clkinfo.profhz = profhz;
1243 clkinfo.stathz = stathz ? stathz : hz;
1244
1245 node = *rnode;
1246 node.sysctl_data = &clkinfo;
1247 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1248 }
1249
1250
1251 /*
1252 * sysctl helper routine for kern.file pseudo-subtree.
1253 */
1254 static int
1255 sysctl_kern_file(SYSCTLFN_ARGS)
1256 {
1257 int error;
1258 size_t buflen;
1259 struct file *fp, *dp, *np, fbuf;
1260 char *start, *where;
1261
1262 start = where = oldp;
1263 buflen = *oldlenp;
1264 dp = NULL;
1265
1266 if (where == NULL) {
1267 /*
1268 * overestimate by 10 files
1269 */
1270 *oldlenp = sizeof(filehead) + (nfiles + 10) *
1271 sizeof(struct file);
1272 return (0);
1273 }
1274
1275 /*
1276 * first dcopyout filehead
1277 */
1278 if (buflen < sizeof(filehead)) {
1279 *oldlenp = 0;
1280 return (0);
1281 }
1282 sysctl_unlock();
1283 error = dcopyout(l, &filehead, where, sizeof(filehead));
1284 if (error) {
1285 sysctl_relock();
1286 return error;
1287 }
1288 buflen -= sizeof(filehead);
1289 where += sizeof(filehead);
1290
1291 /*
1292 * allocate dummy file descriptor to make position in list
1293 */
1294 if ((dp = fgetdummy()) == NULL) {
1295 sysctl_relock();
1296 return ENOMEM;
1297 }
1298
1299 /*
1300 * followed by an array of file structures
1301 */
1302 mutex_enter(&filelist_lock);
1303 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) {
1304 np = LIST_NEXT(fp, f_list);
1305 mutex_enter(&fp->f_lock);
1306 if (fp->f_count == 0) {
1307 mutex_exit(&fp->f_lock);
1308 continue;
1309 }
1310 /*
1311 * XXX Need to prevent that from being an alternative way
1312 * XXX to getting process information.
1313 */
1314 if (kauth_authorize_generic(l->l_cred,
1315 KAUTH_GENERIC_CANSEE, fp->f_cred) != 0) {
1316 mutex_exit(&fp->f_lock);
1317 continue;
1318 }
1319 if (buflen < sizeof(struct file)) {
1320 *oldlenp = where - start;
1321 mutex_exit(&fp->f_lock);
1322 error = ENOMEM;
1323 break;
1324 }
1325 memcpy(&fbuf, fp, sizeof(fbuf));
1326 LIST_INSERT_AFTER(fp, dp, f_list);
1327 mutex_exit(&fp->f_lock);
1328 mutex_exit(&filelist_lock);
1329 error = dcopyout(l, &fbuf, where, sizeof(fbuf));
1330 if (error) {
1331 mutex_enter(&filelist_lock);
1332 LIST_REMOVE(dp, f_list);
1333 break;
1334 }
1335 buflen -= sizeof(struct file);
1336 where += sizeof(struct file);
1337 mutex_enter(&filelist_lock);
1338 np = LIST_NEXT(dp, f_list);
1339 LIST_REMOVE(dp, f_list);
1340 }
1341 mutex_exit(&filelist_lock);
1342 *oldlenp = where - start;
1343 if (dp != NULL)
1344 fputdummy(dp);
1345 sysctl_relock();
1346 return (error);
1347 }
1348
1349 /*
1350 * sysctl helper routine for kern.msgbufsize and kern.msgbuf. For the
1351 * former it merely checks the message buffer is set up. For the latter,
1352 * it also copies out the data if necessary.
1353 */
1354 static int
1355 sysctl_msgbuf(SYSCTLFN_ARGS)
1356 {
1357 char *where = oldp;
1358 size_t len, maxlen;
1359 long beg, end;
1360 extern kmutex_t log_lock;
1361 int error;
1362
1363 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1364 msgbufenabled = 0;
1365 return (ENXIO);
1366 }
1367
1368 switch (rnode->sysctl_num) {
1369 case KERN_MSGBUFSIZE: {
1370 struct sysctlnode node = *rnode;
1371 int msg_bufs = (int)msgbufp->msg_bufs;
1372 node.sysctl_data = &msg_bufs;
1373 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1374 }
1375 case KERN_MSGBUF:
1376 break;
1377 default:
1378 return (EOPNOTSUPP);
1379 }
1380
1381 if (newp != NULL)
1382 return (EPERM);
1383
1384 if (oldp == NULL) {
1385 /* always return full buffer size */
1386 *oldlenp = msgbufp->msg_bufs;
1387 return (0);
1388 }
1389
1390 sysctl_unlock();
1391
1392 /*
1393 * First, copy from the write pointer to the end of
1394 * message buffer.
1395 */
1396 error = 0;
1397 mutex_spin_enter(&log_lock);
1398 maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1399 beg = msgbufp->msg_bufx;
1400 end = msgbufp->msg_bufs;
1401 mutex_spin_exit(&log_lock);
1402
1403 while (maxlen > 0) {
1404 len = MIN(end - beg, maxlen);
1405 if (len == 0)
1406 break;
1407 /* XXX unlocked, but hardly matters. */
1408 error = dcopyout(l, &msgbufp->msg_bufc[beg], where, len);
1409 if (error)
1410 break;
1411 where += len;
1412 maxlen -= len;
1413
1414 /*
1415 * ... then, copy from the beginning of message buffer to
1416 * the write pointer.
1417 */
1418 beg = 0;
1419 end = msgbufp->msg_bufx;
1420 }
1421
1422 sysctl_relock();
1423 return (error);
1424 }
1425
1426 /*
1427 * sysctl helper routine for kern.defcorename. In the case of a new
1428 * string being assigned, check that it's not a zero-length string.
1429 * (XXX the check in -current doesn't work, but do we really care?)
1430 */
1431 static int
1432 sysctl_kern_defcorename(SYSCTLFN_ARGS)
1433 {
1434 int error;
1435 char *newcorename;
1436 struct sysctlnode node;
1437
1438 newcorename = PNBUF_GET();
1439 node = *rnode;
1440 node.sysctl_data = &newcorename[0];
1441 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1442 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1443 if (error || newp == NULL) {
1444 goto done;
1445 }
1446
1447 /*
1448 * when sysctl_lookup() deals with a string, it's guaranteed
1449 * to come back nul terminated. So there. :)
1450 */
1451 if (strlen(newcorename) == 0) {
1452 error = EINVAL;
1453 } else {
1454 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1455 error = 0;
1456 }
1457 done:
1458 PNBUF_PUT(newcorename);
1459 return error;
1460 }
1461
1462 /*
1463 * sysctl helper routine for kern.cp_time node. Adds up cpu time
1464 * across all cpus.
1465 */
1466 static int
1467 sysctl_kern_cptime(SYSCTLFN_ARGS)
1468 {
1469 struct sysctlnode node = *rnode;
1470 uint64_t *cp_time = NULL;
1471 int error, n = ncpu, i;
1472 struct cpu_info *ci;
1473 CPU_INFO_ITERATOR cii;
1474
1475 /*
1476 * if you specifically pass a buffer that is the size of the
1477 * sum, or if you are probing for the size, you get the "sum"
1478 * of cp_time (and the size thereof) across all processors.
1479 *
1480 * alternately, you can pass an additional mib number and get
1481 * cp_time for that particular processor.
1482 */
1483 switch (namelen) {
1484 case 0:
1485 if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
1486 node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1487 n = -1; /* SUM */
1488 }
1489 else {
1490 node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
1491 n = -2; /* ALL */
1492 }
1493 break;
1494 case 1:
1495 if (name[0] < 0 || name[0] >= n)
1496 return (ENOENT); /* ENOSUCHPROCESSOR */
1497 node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1498 n = name[0];
1499 /*
1500 * adjust these so that sysctl_lookup() will be happy
1501 */
1502 name++;
1503 namelen--;
1504 break;
1505 default:
1506 return (EINVAL);
1507 }
1508
1509 cp_time = kmem_alloc(node.sysctl_size, KM_SLEEP);
1510 if (cp_time == NULL)
1511 return (ENOMEM);
1512 node.sysctl_data = cp_time;
1513 memset(cp_time, 0, node.sysctl_size);
1514
1515 for (CPU_INFO_FOREACH(cii, ci)) {
1516 if (n <= 0) {
1517 for (i = 0; i < CPUSTATES; i++) {
1518 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1519 }
1520 }
1521 /*
1522 * if a specific processor was requested and we just
1523 * did it, we're done here
1524 */
1525 if (n == 0)
1526 break;
1527 /*
1528 * if doing "all", skip to next cp_time set for next processor
1529 */
1530 if (n == -2)
1531 cp_time += CPUSTATES;
1532 /*
1533 * if we're doing a specific processor, we're one
1534 * processor closer
1535 */
1536 if (n > 0)
1537 n--;
1538 }
1539
1540 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1541 kmem_free(node.sysctl_data, node.sysctl_size);
1542 return (error);
1543 }
1544
1545 #if NPTY > 0
1546 /*
1547 * sysctl helper routine for kern.maxptys. Ensures that any new value
1548 * is acceptable to the pty subsystem.
1549 */
1550 static int
1551 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1552 {
1553 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
1554 int error, xmax;
1555 struct sysctlnode node;
1556
1557 /* get current value of maxptys */
1558 xmax = pty_maxptys(0, 0);
1559
1560 node = *rnode;
1561 node.sysctl_data = &xmax;
1562 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1563 if (error || newp == NULL)
1564 return (error);
1565
1566 if (xmax != pty_maxptys(xmax, 1))
1567 return (EINVAL);
1568
1569 return (0);
1570 }
1571 #endif /* NPTY > 0 */
1572
1573 /*
1574 * sysctl helper routine for kern.sbmax. Basically just ensures that
1575 * any new value is not too small.
1576 */
1577 static int
1578 sysctl_kern_sbmax(SYSCTLFN_ARGS)
1579 {
1580 int error, new_sbmax;
1581 struct sysctlnode node;
1582
1583 new_sbmax = sb_max;
1584 node = *rnode;
1585 node.sysctl_data = &new_sbmax;
1586 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1587 if (error || newp == NULL)
1588 return (error);
1589
1590 KERNEL_LOCK(1, NULL);
1591 error = sb_max_set(new_sbmax);
1592 KERNEL_UNLOCK_ONE(NULL);
1593
1594 return (error);
1595 }
1596
1597 /*
1598 * sysctl helper routine for kern.urandom node. Picks a random number
1599 * for you.
1600 */
1601 static int
1602 sysctl_kern_urnd(SYSCTLFN_ARGS)
1603 {
1604 #if NRND > 0
1605 int v, rv;
1606
1607 KERNEL_LOCK(1, NULL);
1608 rv = rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY);
1609 KERNEL_UNLOCK_ONE(NULL);
1610 if (rv == sizeof(v)) {
1611 struct sysctlnode node = *rnode;
1612 node.sysctl_data = &v;
1613 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1614 }
1615 else
1616 return (EIO); /*XXX*/
1617 #else
1618 return (EOPNOTSUPP);
1619 #endif
1620 }
1621
1622 /*
1623 * sysctl helper routine for kern.arandom node. Picks a random number
1624 * for you.
1625 */
1626 static int
1627 sysctl_kern_arnd(SYSCTLFN_ARGS)
1628 {
1629 #if NRND > 0
1630 int error;
1631 void *v;
1632 struct sysctlnode node = *rnode;
1633
1634 if (*oldlenp == 0)
1635 return 0;
1636 if (*oldlenp > 8192)
1637 return E2BIG;
1638
1639 v = kmem_alloc(*oldlenp, KM_SLEEP);
1640 arc4randbytes(v, *oldlenp);
1641 node.sysctl_data = v;
1642 node.sysctl_size = *oldlenp;
1643 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1644 kmem_free(v, *oldlenp);
1645 return error;
1646 #else
1647 return (EOPNOTSUPP);
1648 #endif
1649 }
1650 /*
1651 * sysctl helper routine to do kern.lwp.* work.
1652 */
1653 static int
1654 sysctl_kern_lwp(SYSCTLFN_ARGS)
1655 {
1656 struct kinfo_lwp klwp;
1657 struct proc *p;
1658 struct lwp *l2, *l3;
1659 char *where, *dp;
1660 int pid, elem_size, elem_count;
1661 int buflen, needed, error;
1662 bool gotit;
1663
1664 if (namelen == 1 && name[0] == CTL_QUERY)
1665 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1666
1667 dp = where = oldp;
1668 buflen = where != NULL ? *oldlenp : 0;
1669 error = needed = 0;
1670
1671 if (newp != NULL || namelen != 3)
1672 return (EINVAL);
1673 pid = name[0];
1674 elem_size = name[1];
1675 elem_count = name[2];
1676
1677 sysctl_unlock();
1678 if (pid == -1) {
1679 mutex_enter(proc_lock);
1680 LIST_FOREACH(p, &allproc, p_list) {
1681 /* Grab a hold on the process. */
1682 if (!rw_tryenter(&p->p_reflock, RW_READER)) {
1683 continue;
1684 }
1685 mutex_exit(proc_lock);
1686
1687 mutex_enter(p->p_lock);
1688 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1689 if (buflen >= elem_size && elem_count > 0) {
1690 lwp_lock(l2);
1691 fill_lwp(l2, &klwp);
1692 lwp_unlock(l2);
1693 mutex_exit(p->p_lock);
1694
1695 /*
1696 * Copy out elem_size, but not
1697 * larger than the size of a
1698 * struct kinfo_proc2.
1699 */
1700 error = dcopyout(l, &klwp, dp,
1701 min(sizeof(klwp), elem_size));
1702 if (error) {
1703 rw_exit(&p->p_reflock);
1704 goto cleanup;
1705 }
1706 mutex_enter(p->p_lock);
1707 LIST_FOREACH(l3, &p->p_lwps,
1708 l_sibling) {
1709 if (l2 == l3)
1710 break;
1711 }
1712 if (l3 == NULL) {
1713 mutex_exit(p->p_lock);
1714 rw_exit(&p->p_reflock);
1715 error = EAGAIN;
1716 goto cleanup;
1717 }
1718 dp += elem_size;
1719 buflen -= elem_size;
1720 elem_count--;
1721 }
1722 needed += elem_size;
1723 }
1724 mutex_exit(p->p_lock);
1725
1726 /* Drop reference to process. */
1727 mutex_enter(proc_lock);
1728 rw_exit(&p->p_reflock);
1729 }
1730 mutex_exit(proc_lock);
1731 } else {
1732 mutex_enter(proc_lock);
1733 p = p_find(pid, PFIND_LOCKED);
1734 if (p == NULL) {
1735 error = ESRCH;
1736 mutex_exit(proc_lock);
1737 goto cleanup;
1738 }
1739 /* Grab a hold on the process. */
1740 gotit = rw_tryenter(&p->p_reflock, RW_READER);
1741 mutex_exit(proc_lock);
1742 if (!gotit) {
1743 error = ESRCH;
1744 goto cleanup;
1745 }
1746
1747 mutex_enter(p->p_lock);
1748 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1749 if (buflen >= elem_size && elem_count > 0) {
1750 lwp_lock(l2);
1751 fill_lwp(l2, &klwp);
1752 lwp_unlock(l2);
1753 mutex_exit(p->p_lock);
1754 /*
1755 * Copy out elem_size, but not larger than
1756 * the size of a struct kinfo_proc2.
1757 */
1758 error = dcopyout(l, &klwp, dp,
1759 min(sizeof(klwp), elem_size));
1760 if (error) {
1761 rw_exit(&p->p_reflock);
1762 goto cleanup;
1763 }
1764 mutex_enter(p->p_lock);
1765 LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
1766 if (l2 == l3)
1767 break;
1768 }
1769 if (l3 == NULL) {
1770 mutex_exit(p->p_lock);
1771 rw_exit(&p->p_reflock);
1772 error = EAGAIN;
1773 goto cleanup;
1774 }
1775 dp += elem_size;
1776 buflen -= elem_size;
1777 elem_count--;
1778 }
1779 needed += elem_size;
1780 }
1781 mutex_exit(p->p_lock);
1782
1783 /* Drop reference to process. */
1784 rw_exit(&p->p_reflock);
1785 }
1786
1787 if (where != NULL) {
1788 *oldlenp = dp - where;
1789 if (needed > *oldlenp) {
1790 sysctl_relock();
1791 return (ENOMEM);
1792 }
1793 } else {
1794 needed += KERN_LWPSLOP;
1795 *oldlenp = needed;
1796 }
1797 error = 0;
1798 cleanup:
1799 sysctl_relock();
1800 return (error);
1801 }
1802
1803 /*
1804 * sysctl helper routine for kern.forkfsleep node. Ensures that the
1805 * given value is not too large or two small, and is at least one
1806 * timer tick if not zero.
1807 */
1808 static int
1809 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1810 {
1811 /* userland sees value in ms, internally is in ticks */
1812 extern int forkfsleep; /* defined in kern/kern_fork.c */
1813 int error, timo, lsleep;
1814 struct sysctlnode node;
1815
1816 lsleep = forkfsleep * 1000 / hz;
1817 node = *rnode;
1818 node.sysctl_data = &lsleep;
1819 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1820 if (error || newp == NULL)
1821 return (error);
1822
1823 /* refuse negative values, and overly 'long time' */
1824 if (lsleep < 0 || lsleep > MAXSLP * 1000)
1825 return (EINVAL);
1826
1827 timo = mstohz(lsleep);
1828
1829 /* if the interval is >0 ms && <1 tick, use 1 tick */
1830 if (lsleep != 0 && timo == 0)
1831 forkfsleep = 1;
1832 else
1833 forkfsleep = timo;
1834
1835 return (0);
1836 }
1837
1838 /*
1839 * sysctl helper routine for kern.root_partition
1840 */
1841 static int
1842 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1843 {
1844 int rootpart = DISKPART(rootdev);
1845 struct sysctlnode node = *rnode;
1846
1847 node.sysctl_data = &rootpart;
1848 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1849 }
1850
1851 /*
1852 * sysctl helper function for kern.drivers
1853 */
1854 static int
1855 sysctl_kern_drivers(SYSCTLFN_ARGS)
1856 {
1857 int error;
1858 size_t buflen;
1859 struct kinfo_drivers kd;
1860 char *start, *where;
1861 const char *dname;
1862 int i;
1863 extern struct devsw_conv *devsw_conv;
1864 extern int max_devsw_convs;
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(&specfs_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(&specfs_lock);
1895 error = dcopyout(l, &kd, where, sizeof kd);
1896 mutex_enter(&specfs_lock);
1897 if (error != 0)
1898 break;
1899 buflen -= sizeof kd;
1900 where += sizeof kd;
1901 }
1902 mutex_exit(&specfs_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] = cpu_index(ci);
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 strncpy(ki->p_ename, p->p_emul->e_name, sizeof(ki->p_ename));
2941
2942 ki->p_nlwps = p->p_nlwps;
2943 ki->p_realflag = ki->p_flag;
2944
2945 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
2946 vm = p->p_vmspace;
2947 ki->p_vm_rssize = vm_resident_count(vm);
2948 ki->p_vm_tsize = vm->vm_tsize;
2949 ki->p_vm_dsize = vm->vm_dsize;
2950 ki->p_vm_ssize = vm->vm_ssize;
2951
2952 /* Pick a "representative" LWP */
2953 l = proc_representative_lwp(p, &tmp, 1);
2954 lwp_lock(l);
2955 ki->p_nrlwps = tmp;
2956 ki->p_forw = 0;
2957 ki->p_back = 0;
2958 ki->p_addr = PTRTOUINT64(l->l_addr);
2959 ki->p_stat = l->l_stat;
2960 ki->p_flag |= sysctl_map_flags(sysctl_lwpflagmap, l->l_flag);
2961 ki->p_swtime = l->l_swtime;
2962 ki->p_slptime = l->l_slptime;
2963 if (l->l_stat == LSONPROC)
2964 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2965 else
2966 ki->p_schedflags = 0;
2967 ki->p_holdcnt = l->l_holdcnt;
2968 ki->p_priority = lwp_eprio(l);
2969 ki->p_usrpri = l->l_priority;
2970 if (l->l_wchan)
2971 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
2972 ki->p_wchan = PTRTOUINT64(l->l_wchan);
2973 ki->p_cpuid = cpu_index(l->l_cpu);
2974 lwp_unlock(l);
2975 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
2976 /* This is hardly correct, but... */
2977 sigplusset(&l->l_sigpend.sp_set, &ss1);
2978 sigplusset(&l->l_sigmask, &ss2);
2979 ki->p_cpticks += l->l_cpticks;
2980 ki->p_pctcpu += l->l_pctcpu;
2981 ki->p_estcpu += l->l_estcpu;
2982 }
2983 }
2984 sigplusset(&p->p_sigpend.sp_set, &ss2);
2985 memcpy(&ki->p_siglist, &ss1, sizeof(ki_sigset_t));
2986 memcpy(&ki->p_sigmask, &ss2, sizeof(ki_sigset_t));
2987
2988 if (p->p_session != NULL) {
2989 ki->p_sid = p->p_session->s_sid;
2990 ki->p__pgid = p->p_pgrp->pg_id;
2991 if (p->p_session->s_ttyvp)
2992 ki->p_eflag |= EPROC_CTTY;
2993 if (SESS_LEADER(p))
2994 ki->p_eflag |= EPROC_SLEADER;
2995 strncpy(ki->p_login, p->p_session->s_login,
2996 min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
2997 ki->p_jobc = p->p_pgrp->pg_jobc;
2998 if ((p->p_lflag & PL_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2999 ki->p_tdev = tp->t_dev;
3000 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
3001 ki->p_tsess = PTRTOUINT64(tp->t_session);
3002 } else {
3003 ki->p_tdev = NODEV;
3004 }
3005 }
3006
3007 if (!P_ZOMBIE(p) && !zombie) {
3008 ki->p_uvalid = 1;
3009 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
3010 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
3011
3012 calcru(p, &ut, &st, NULL, &rt);
3013 ki->p_rtime_sec = rt.tv_sec;
3014 ki->p_rtime_usec = rt.tv_usec;
3015 ki->p_uutime_sec = ut.tv_sec;
3016 ki->p_uutime_usec = ut.tv_usec;
3017 ki->p_ustime_sec = st.tv_sec;
3018 ki->p_ustime_usec = st.tv_usec;
3019
3020 memcpy(&ru, &p->p_stats->p_ru, sizeof(ru));
3021 ki->p_uru_nvcsw = 0;
3022 ki->p_uru_nivcsw = 0;
3023 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
3024 ki->p_uru_nvcsw += (l2->l_ncsw - l2->l_nivcsw);
3025 ki->p_uru_nivcsw += l2->l_nivcsw;
3026 ruadd(&ru, &l2->l_ru);
3027 }
3028 ki->p_uru_maxrss = ru.ru_maxrss;
3029 ki->p_uru_ixrss = ru.ru_ixrss;
3030 ki->p_uru_idrss = ru.ru_idrss;
3031 ki->p_uru_isrss = ru.ru_isrss;
3032 ki->p_uru_minflt = ru.ru_minflt;
3033 ki->p_uru_majflt = ru.ru_majflt;
3034 ki->p_uru_nswap = ru.ru_nswap;
3035 ki->p_uru_inblock = ru.ru_inblock;
3036 ki->p_uru_oublock = ru.ru_oublock;
3037 ki->p_uru_msgsnd = ru.ru_msgsnd;
3038 ki->p_uru_msgrcv = ru.ru_msgrcv;
3039 ki->p_uru_nsignals = ru.ru_nsignals;
3040
3041 timeradd(&p->p_stats->p_cru.ru_utime,
3042 &p->p_stats->p_cru.ru_stime, &ut);
3043 ki->p_uctime_sec = ut.tv_sec;
3044 ki->p_uctime_usec = ut.tv_usec;
3045 }
3046 }
3047
3048 /*
3049 * Fill in a kinfo_lwp structure for the specified lwp.
3050 */
3051 static void
3052 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
3053 {
3054 struct proc *p = l->l_proc;
3055 struct timeval tv;
3056
3057 KASSERT(lwp_locked(l, NULL));
3058
3059 kl->l_forw = 0;
3060 kl->l_back = 0;
3061 kl->l_laddr = PTRTOUINT64(l);
3062 kl->l_addr = PTRTOUINT64(l->l_addr);
3063 kl->l_stat = l->l_stat;
3064 kl->l_lid = l->l_lid;
3065 kl->l_flag = sysctl_map_flags(sysctl_lwpprflagmap, l->l_prflag);
3066
3067 kl->l_swtime = l->l_swtime;
3068 kl->l_slptime = l->l_slptime;
3069 if (l->l_stat == LSONPROC)
3070 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
3071 else
3072 kl->l_schedflags = 0;
3073 kl->l_holdcnt = l->l_holdcnt;
3074 kl->l_priority = lwp_eprio(l);
3075 kl->l_usrpri = l->l_priority;
3076 if (l->l_wchan)
3077 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
3078 kl->l_wchan = PTRTOUINT64(l->l_wchan);
3079 kl->l_cpuid = cpu_index(l->l_cpu);
3080 bintime2timeval(&l->l_rtime, &tv);
3081 kl->l_rtime_sec = tv.tv_sec;
3082 kl->l_rtime_usec = tv.tv_usec;
3083 kl->l_cpticks = l->l_cpticks;
3084 kl->l_pctcpu = l->l_pctcpu;
3085 kl->l_pid = p->p_pid;
3086 if (l->l_name == NULL)
3087 kl->l_name[0] = '\0';
3088 else
3089 strlcpy(kl->l_name, l->l_name, sizeof(kl->l_name));
3090 }
3091
3092 /*
3093 * Fill in an eproc structure for the specified process.
3094 */
3095 void
3096 fill_eproc(struct proc *p, struct eproc *ep, bool zombie)
3097 {
3098 struct tty *tp;
3099 struct lwp *l;
3100
3101 KASSERT(mutex_owned(proc_lock));
3102 KASSERT(mutex_owned(p->p_lock));
3103
3104 memset(ep, 0, sizeof(*ep));
3105
3106 ep->e_paddr = p;
3107 ep->e_sess = p->p_session;
3108 if (p->p_cred) {
3109 kauth_cred_topcred(p->p_cred, &ep->e_pcred);
3110 kauth_cred_toucred(p->p_cred, &ep->e_ucred);
3111 }
3112 if (p->p_stat != SIDL && !P_ZOMBIE(p) && !zombie) {
3113 struct vmspace *vm = p->p_vmspace;
3114
3115 ep->e_vm.vm_rssize = vm_resident_count(vm);
3116 ep->e_vm.vm_tsize = vm->vm_tsize;
3117 ep->e_vm.vm_dsize = vm->vm_dsize;
3118 ep->e_vm.vm_ssize = vm->vm_ssize;
3119
3120 /* Pick a "representative" LWP */
3121 l = proc_representative_lwp(p, NULL, 1);
3122 lwp_lock(l);
3123 if (l->l_wchan)
3124 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
3125 lwp_unlock(l);
3126 }
3127 if (p->p_pptr)
3128 ep->e_ppid = p->p_pptr->p_pid;
3129 if (p->p_pgrp && p->p_session) {
3130 ep->e_pgid = p->p_pgrp->pg_id;
3131 ep->e_jobc = p->p_pgrp->pg_jobc;
3132 ep->e_sid = p->p_session->s_sid;
3133 if ((p->p_lflag & PL_CONTROLT) &&
3134 (tp = ep->e_sess->s_ttyp)) {
3135 ep->e_tdev = tp->t_dev;
3136 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
3137 ep->e_tsess = tp->t_session;
3138 } else
3139 ep->e_tdev = NODEV;
3140 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
3141 if (SESS_LEADER(p))
3142 ep->e_flag |= EPROC_SLEADER;
3143 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
3144 }
3145 ep->e_xsize = ep->e_xrssize = 0;
3146 ep->e_xccount = ep->e_xswrss = 0;
3147 }
3148
3149 u_int
3150 sysctl_map_flags(const u_int *map, u_int word)
3151 {
3152 u_int rv;
3153
3154 for (rv = 0; *map != 0; map += 2)
3155 if ((word & map[0]) != 0)
3156 rv |= map[1];
3157
3158 return rv;
3159 }
3160