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