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