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