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