init_sysctl.c revision 1.63.4.2 1 /* $NetBSD: init_sysctl.c,v 1.63.4.2 2006/03/14 02:52:47 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.2 2006/03/14 02:52:47 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 (CURTAIN(kauth_cred_geteuid(l->l_proc->p_cred),
1325 kauth_cred_geteuid(fp->f_cred)))
1326 continue;
1327 if (buflen < sizeof(struct file)) {
1328 *oldlenp = where - start;
1329 return (ENOMEM);
1330 }
1331 error = copyout(fp, where, sizeof(struct file));
1332 if (error)
1333 return (error);
1334 buflen -= sizeof(struct file);
1335 where += sizeof(struct file);
1336 }
1337 *oldlenp = where - start;
1338 return (0);
1339 }
1340
1341 /*
1342 * sysctl helper routine for kern.autonicetime and kern.autoniceval.
1343 * asserts that the assigned value is in the correct range.
1344 */
1345 static int
1346 sysctl_kern_autonice(SYSCTLFN_ARGS)
1347 {
1348 int error, t = 0;
1349 struct sysctlnode node;
1350
1351 node = *rnode;
1352 t = *(int*)node.sysctl_data;
1353 node.sysctl_data = &t;
1354 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1355 if (error || newp == NULL)
1356 return (error);
1357
1358 switch (node.sysctl_num) {
1359 case KERN_AUTONICETIME:
1360 if (t >= 0)
1361 autonicetime = t;
1362 break;
1363 case KERN_AUTONICEVAL:
1364 if (t < PRIO_MIN)
1365 t = PRIO_MIN;
1366 else if (t > PRIO_MAX)
1367 t = PRIO_MAX;
1368 autoniceval = t;
1369 break;
1370 }
1371
1372 return (0);
1373 }
1374
1375 /*
1376 * sysctl helper routine for kern.msgbufsize and kern.msgbuf. for the
1377 * former it merely checks the message buffer is set up. for the latter,
1378 * it also copies out the data if necessary.
1379 */
1380 static int
1381 sysctl_msgbuf(SYSCTLFN_ARGS)
1382 {
1383 char *where = oldp;
1384 size_t len, maxlen;
1385 long beg, end;
1386 int error;
1387
1388 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1389 msgbufenabled = 0;
1390 return (ENXIO);
1391 }
1392
1393 switch (rnode->sysctl_num) {
1394 case KERN_MSGBUFSIZE: {
1395 struct sysctlnode node = *rnode;
1396 int msg_bufs = (int)msgbufp->msg_bufs;
1397 node.sysctl_data = &msg_bufs;
1398 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1399 }
1400 case KERN_MSGBUF:
1401 break;
1402 default:
1403 return (EOPNOTSUPP);
1404 }
1405
1406 if (newp != NULL)
1407 return (EPERM);
1408
1409 if (oldp == NULL) {
1410 /* always return full buffer size */
1411 *oldlenp = msgbufp->msg_bufs;
1412 return (0);
1413 }
1414
1415 error = 0;
1416 maxlen = MIN(msgbufp->msg_bufs, *oldlenp);
1417
1418 /*
1419 * First, copy from the write pointer to the end of
1420 * message buffer.
1421 */
1422 beg = msgbufp->msg_bufx;
1423 end = msgbufp->msg_bufs;
1424 while (maxlen > 0) {
1425 len = MIN(end - beg, maxlen);
1426 if (len == 0)
1427 break;
1428 error = copyout(&msgbufp->msg_bufc[beg], where, len);
1429 if (error)
1430 break;
1431 where += len;
1432 maxlen -= len;
1433
1434 /*
1435 * ... then, copy from the beginning of message buffer to
1436 * the write pointer.
1437 */
1438 beg = 0;
1439 end = msgbufp->msg_bufx;
1440 }
1441
1442 return (error);
1443 }
1444
1445 /*
1446 * sysctl helper routine for kern.defcorename. in the case of a new
1447 * string being assigned, check that it's not a zero-length string.
1448 * (XXX the check in -current doesn't work, but do we really care?)
1449 */
1450 static int
1451 sysctl_kern_defcorename(SYSCTLFN_ARGS)
1452 {
1453 int error;
1454 char *newcorename;
1455 struct sysctlnode node;
1456
1457 newcorename = PNBUF_GET();
1458 node = *rnode;
1459 node.sysctl_data = &newcorename[0];
1460 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
1461 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1462 if (error || newp == NULL) {
1463 goto done;
1464 }
1465
1466 /*
1467 * when sysctl_lookup() deals with a string, it's guaranteed
1468 * to come back nul terminated. so there. :)
1469 */
1470 if (strlen(newcorename) == 0) {
1471 error = EINVAL;
1472 } else {
1473 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
1474 error = 0;
1475 }
1476 done:
1477 PNBUF_PUT(newcorename);
1478 return error;
1479 }
1480
1481 /*
1482 * sysctl helper routine for kern.cp_time node. adds up cpu time
1483 * across all cpus.
1484 */
1485 static int
1486 sysctl_kern_cptime(SYSCTLFN_ARGS)
1487 {
1488 struct sysctlnode node = *rnode;
1489
1490 #ifndef MULTIPROCESSOR
1491
1492 if (namelen == 1) {
1493 if (name[0] != 0)
1494 return (ENOENT);
1495 /*
1496 * you're allowed to ask for the zero'th processor
1497 */
1498 name++;
1499 namelen--;
1500 }
1501 node.sysctl_data = curcpu()->ci_schedstate.spc_cp_time;
1502 node.sysctl_size = sizeof(curcpu()->ci_schedstate.spc_cp_time);
1503 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1504
1505 #else /* MULTIPROCESSOR */
1506
1507 uint64_t *cp_time = NULL;
1508 int error, n = sysctl_ncpus(), i;
1509 struct cpu_info *ci;
1510 CPU_INFO_ITERATOR cii;
1511
1512 /*
1513 * if you specifically pass a buffer that is the size of the
1514 * sum, or if you are probing for the size, you get the "sum"
1515 * of cp_time (and the size thereof) across all processors.
1516 *
1517 * alternately, you can pass an additional mib number and get
1518 * cp_time for that particular processor.
1519 */
1520 switch (namelen) {
1521 case 0:
1522 if (*oldlenp == sizeof(uint64_t) * CPUSTATES || oldp == NULL) {
1523 node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1524 n = -1; /* SUM */
1525 }
1526 else {
1527 node.sysctl_size = n * sizeof(uint64_t) * CPUSTATES;
1528 n = -2; /* ALL */
1529 }
1530 break;
1531 case 1:
1532 if (name[0] < 0 || name[0] >= n)
1533 return (ENOENT); /* ENOSUCHPROCESSOR */
1534 node.sysctl_size = sizeof(uint64_t) * CPUSTATES;
1535 n = name[0];
1536 /*
1537 * adjust these so that sysctl_lookup() will be happy
1538 */
1539 name++;
1540 namelen--;
1541 break;
1542 default:
1543 return (EINVAL);
1544 }
1545
1546 cp_time = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
1547 if (cp_time == NULL)
1548 return (ENOMEM);
1549 node.sysctl_data = cp_time;
1550 memset(cp_time, 0, node.sysctl_size);
1551
1552 for (CPU_INFO_FOREACH(cii, ci)) {
1553 if (n <= 0)
1554 for (i = 0; i < CPUSTATES; i++)
1555 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
1556 /*
1557 * if a specific processor was requested and we just
1558 * did it, we're done here
1559 */
1560 if (n == 0)
1561 break;
1562 /*
1563 * if doing "all", skip to next cp_time set for next processor
1564 */
1565 if (n == -2)
1566 cp_time += CPUSTATES;
1567 /*
1568 * if we're doing a specific processor, we're one
1569 * processor closer
1570 */
1571 if (n > 0)
1572 n--;
1573 }
1574
1575 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1576 free(node.sysctl_data, M_TEMP);
1577 return (error);
1578
1579 #endif /* MULTIPROCESSOR */
1580 }
1581
1582 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
1583 /*
1584 * sysctl helper routine for kern.sysvipc_info subtree.
1585 */
1586
1587 #define FILL_PERM(src, dst) do { \
1588 (dst)._key = (src)._key; \
1589 (dst).uid = (src).uid; \
1590 (dst).gid = (src).gid; \
1591 (dst).cuid = (src).cuid; \
1592 (dst).cgid = (src).cgid; \
1593 (dst).mode = (src).mode; \
1594 (dst)._seq = (src)._seq; \
1595 } while (/*CONSTCOND*/ 0);
1596 #define FILL_MSG(src, dst) do { \
1597 FILL_PERM((src).msg_perm, (dst).msg_perm); \
1598 (dst).msg_qnum = (src).msg_qnum; \
1599 (dst).msg_qbytes = (src).msg_qbytes; \
1600 (dst)._msg_cbytes = (src)._msg_cbytes; \
1601 (dst).msg_lspid = (src).msg_lspid; \
1602 (dst).msg_lrpid = (src).msg_lrpid; \
1603 (dst).msg_stime = (src).msg_stime; \
1604 (dst).msg_rtime = (src).msg_rtime; \
1605 (dst).msg_ctime = (src).msg_ctime; \
1606 } while (/*CONSTCOND*/ 0)
1607 #define FILL_SEM(src, dst) do { \
1608 FILL_PERM((src).sem_perm, (dst).sem_perm); \
1609 (dst).sem_nsems = (src).sem_nsems; \
1610 (dst).sem_otime = (src).sem_otime; \
1611 (dst).sem_ctime = (src).sem_ctime; \
1612 } while (/*CONSTCOND*/ 0)
1613 #define FILL_SHM(src, dst) do { \
1614 FILL_PERM((src).shm_perm, (dst).shm_perm); \
1615 (dst).shm_segsz = (src).shm_segsz; \
1616 (dst).shm_lpid = (src).shm_lpid; \
1617 (dst).shm_cpid = (src).shm_cpid; \
1618 (dst).shm_atime = (src).shm_atime; \
1619 (dst).shm_dtime = (src).shm_dtime; \
1620 (dst).shm_ctime = (src).shm_ctime; \
1621 (dst).shm_nattch = (src).shm_nattch; \
1622 } while (/*CONSTCOND*/ 0)
1623
1624 static int
1625 sysctl_kern_sysvipc(SYSCTLFN_ARGS)
1626 {
1627 void *where = oldp;
1628 size_t *sizep = oldlenp;
1629 #ifdef SYSVMSG
1630 struct msg_sysctl_info *msgsi = NULL;
1631 #endif
1632 #ifdef SYSVSEM
1633 struct sem_sysctl_info *semsi = NULL;
1634 #endif
1635 #ifdef SYSVSHM
1636 struct shm_sysctl_info *shmsi = NULL;
1637 #endif
1638 size_t infosize, dssize, tsize, buflen;
1639 void *bf = NULL;
1640 char *start;
1641 int32_t nds;
1642 int i, error, ret;
1643
1644 if (namelen != 1)
1645 return (EINVAL);
1646
1647 start = where;
1648 buflen = *sizep;
1649
1650 switch (*name) {
1651 case KERN_SYSVIPC_MSG_INFO:
1652 #ifdef SYSVMSG
1653 infosize = sizeof(msgsi->msginfo);
1654 nds = msginfo.msgmni;
1655 dssize = sizeof(msgsi->msgids[0]);
1656 break;
1657 #else
1658 return (EINVAL);
1659 #endif
1660 case KERN_SYSVIPC_SEM_INFO:
1661 #ifdef SYSVSEM
1662 infosize = sizeof(semsi->seminfo);
1663 nds = seminfo.semmni;
1664 dssize = sizeof(semsi->semids[0]);
1665 break;
1666 #else
1667 return (EINVAL);
1668 #endif
1669 case KERN_SYSVIPC_SHM_INFO:
1670 #ifdef SYSVSHM
1671 infosize = sizeof(shmsi->shminfo);
1672 nds = shminfo.shmmni;
1673 dssize = sizeof(shmsi->shmids[0]);
1674 break;
1675 #else
1676 return (EINVAL);
1677 #endif
1678 default:
1679 return (EINVAL);
1680 }
1681 /*
1682 * Round infosize to 64 bit boundary if requesting more than just
1683 * the info structure or getting the total data size.
1684 */
1685 if (where == NULL || *sizep > infosize)
1686 infosize = ((infosize + 7) / 8) * 8;
1687 tsize = infosize + nds * dssize;
1688
1689 /* Return just the total size required. */
1690 if (where == NULL) {
1691 *sizep = tsize;
1692 return (0);
1693 }
1694
1695 /* Not enough room for even the info struct. */
1696 if (buflen < infosize) {
1697 *sizep = 0;
1698 return (ENOMEM);
1699 }
1700 bf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
1701 memset(bf, 0, min(tsize, buflen));
1702
1703 switch (*name) {
1704 #ifdef SYSVMSG
1705 case KERN_SYSVIPC_MSG_INFO:
1706 msgsi = (struct msg_sysctl_info *)bf;
1707 msgsi->msginfo = msginfo;
1708 break;
1709 #endif
1710 #ifdef SYSVSEM
1711 case KERN_SYSVIPC_SEM_INFO:
1712 semsi = (struct sem_sysctl_info *)bf;
1713 semsi->seminfo = seminfo;
1714 break;
1715 #endif
1716 #ifdef SYSVSHM
1717 case KERN_SYSVIPC_SHM_INFO:
1718 shmsi = (struct shm_sysctl_info *)bf;
1719 shmsi->shminfo = shminfo;
1720 break;
1721 #endif
1722 }
1723 buflen -= infosize;
1724
1725 ret = 0;
1726 if (buflen > 0) {
1727 /* Fill in the IPC data structures. */
1728 for (i = 0; i < nds; i++) {
1729 if (buflen < dssize) {
1730 ret = ENOMEM;
1731 break;
1732 }
1733 switch (*name) {
1734 #ifdef SYSVMSG
1735 case KERN_SYSVIPC_MSG_INFO:
1736 FILL_MSG(msqids[i], msgsi->msgids[i]);
1737 break;
1738 #endif
1739 #ifdef SYSVSEM
1740 case KERN_SYSVIPC_SEM_INFO:
1741 FILL_SEM(sema[i], semsi->semids[i]);
1742 break;
1743 #endif
1744 #ifdef SYSVSHM
1745 case KERN_SYSVIPC_SHM_INFO:
1746 FILL_SHM(shmsegs[i], shmsi->shmids[i]);
1747 break;
1748 #endif
1749 }
1750 buflen -= dssize;
1751 }
1752 }
1753 *sizep -= buflen;
1754 error = copyout(bf, start, *sizep);
1755 /* If copyout succeeded, use return code set earlier. */
1756 if (error == 0)
1757 error = ret;
1758 if (bf)
1759 free(bf, M_TEMP);
1760 return (error);
1761 }
1762
1763 #undef FILL_PERM
1764 #undef FILL_MSG
1765 #undef FILL_SEM
1766 #undef FILL_SHM
1767
1768 #endif /* defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM) */
1769
1770 #if NPTY > 0
1771 /*
1772 * sysctl helper routine for kern.maxptys. ensures that any new value
1773 * is acceptable to the pty subsystem.
1774 */
1775 static int
1776 sysctl_kern_maxptys(SYSCTLFN_ARGS)
1777 {
1778 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
1779 int error, xmax;
1780 struct sysctlnode node;
1781
1782 /* get current value of maxptys */
1783 xmax = pty_maxptys(0, 0);
1784
1785 node = *rnode;
1786 node.sysctl_data = &xmax;
1787 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1788 if (error || newp == NULL)
1789 return (error);
1790
1791 if (xmax != pty_maxptys(xmax, 1))
1792 return (EINVAL);
1793
1794 return (0);
1795 }
1796 #endif /* NPTY > 0 */
1797
1798 /*
1799 * sysctl helper routine for kern.sbmax. basically just ensures that
1800 * any new value is not too small.
1801 */
1802 static int
1803 sysctl_kern_sbmax(SYSCTLFN_ARGS)
1804 {
1805 int error, new_sbmax;
1806 struct sysctlnode node;
1807
1808 new_sbmax = sb_max;
1809 node = *rnode;
1810 node.sysctl_data = &new_sbmax;
1811 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1812 if (error || newp == NULL)
1813 return (error);
1814
1815 error = sb_max_set(new_sbmax);
1816
1817 return (error);
1818 }
1819
1820 /*
1821 * sysctl helper routine for kern.urandom node. picks a random number
1822 * for you.
1823 */
1824 static int
1825 sysctl_kern_urnd(SYSCTLFN_ARGS)
1826 {
1827 #if NRND > 0
1828 int v;
1829
1830 if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) == sizeof(v)) {
1831 struct sysctlnode node = *rnode;
1832 node.sysctl_data = &v;
1833 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1834 }
1835 else
1836 return (EIO); /*XXX*/
1837 #else
1838 return (EOPNOTSUPP);
1839 #endif
1840 }
1841
1842 /*
1843 * sysctl helper routine to do kern.lwp.* work.
1844 */
1845 static int
1846 sysctl_kern_lwp(SYSCTLFN_ARGS)
1847 {
1848 struct kinfo_lwp klwp;
1849 struct proc *p;
1850 struct lwp *l2;
1851 char *where, *dp;
1852 int pid, elem_size, elem_count;
1853 int buflen, needed, error;
1854
1855 if (namelen == 1 && name[0] == CTL_QUERY)
1856 return (sysctl_query(SYSCTLFN_CALL(rnode)));
1857
1858 dp = where = oldp;
1859 buflen = where != NULL ? *oldlenp : 0;
1860 error = needed = 0;
1861
1862 if (newp != NULL || namelen != 3)
1863 return (EINVAL);
1864 pid = name[0];
1865 elem_size = name[1];
1866 elem_count = name[2];
1867
1868 p = pfind(pid);
1869 if (p == NULL)
1870 return (ESRCH);
1871 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
1872 if (buflen >= elem_size && elem_count > 0) {
1873 fill_lwp(l2, &klwp);
1874 /*
1875 * Copy out elem_size, but not larger than
1876 * the size of a struct kinfo_proc2.
1877 */
1878 error = copyout(&klwp, dp,
1879 min(sizeof(klwp), elem_size));
1880 if (error)
1881 goto cleanup;
1882 dp += elem_size;
1883 buflen -= elem_size;
1884 elem_count--;
1885 }
1886 needed += elem_size;
1887 }
1888
1889 if (where != NULL) {
1890 *oldlenp = dp - where;
1891 if (needed > *oldlenp)
1892 return (ENOMEM);
1893 } else {
1894 needed += KERN_LWPSLOP;
1895 *oldlenp = needed;
1896 }
1897 return (0);
1898 cleanup:
1899 return (error);
1900 }
1901
1902 /*
1903 * sysctl helper routine for kern.forkfsleep node. ensures that the
1904 * given value is not too large or two small, and is at least one
1905 * timer tick if not zero.
1906 */
1907 static int
1908 sysctl_kern_forkfsleep(SYSCTLFN_ARGS)
1909 {
1910 /* userland sees value in ms, internally is in ticks */
1911 extern int forkfsleep; /* defined in kern/kern_fork.c */
1912 int error, timo, lsleep;
1913 struct sysctlnode node;
1914
1915 lsleep = forkfsleep * 1000 / hz;
1916 node = *rnode;
1917 node.sysctl_data = &lsleep;
1918 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1919 if (error || newp == NULL)
1920 return (error);
1921
1922 /* refuse negative values, and overly 'long time' */
1923 if (lsleep < 0 || lsleep > MAXSLP * 1000)
1924 return (EINVAL);
1925
1926 timo = mstohz(lsleep);
1927
1928 /* if the interval is >0 ms && <1 tick, use 1 tick */
1929 if (lsleep != 0 && timo == 0)
1930 forkfsleep = 1;
1931 else
1932 forkfsleep = timo;
1933
1934 return (0);
1935 }
1936
1937 /*
1938 * sysctl helper routine for kern.root_partition
1939 */
1940 static int
1941 sysctl_kern_root_partition(SYSCTLFN_ARGS)
1942 {
1943 int rootpart = DISKPART(rootdev);
1944 struct sysctlnode node = *rnode;
1945
1946 node.sysctl_data = &rootpart;
1947 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1948 }
1949
1950 /*
1951 * sysctl helper function for kern.drivers
1952 */
1953 static int
1954 sysctl_kern_drivers(SYSCTLFN_ARGS)
1955 {
1956 int error;
1957 size_t buflen;
1958 struct kinfo_drivers kd;
1959 char *start, *where;
1960 const char *dname;
1961 int i;
1962 extern struct devsw_conv *devsw_conv;
1963 extern int max_devsw_convs;
1964
1965 if (newp != NULL || namelen != 0)
1966 return (EINVAL);
1967
1968 start = where = oldp;
1969 buflen = *oldlenp;
1970 if (where == NULL) {
1971 *oldlenp = max_devsw_convs * sizeof kd;
1972 return 0;
1973 }
1974
1975 /*
1976 * An array of kinfo_drivers structures
1977 */
1978 error = 0;
1979 for (i = 0; i < max_devsw_convs; i++) {
1980 dname = devsw_conv[i].d_name;
1981 if (dname == NULL)
1982 continue;
1983 if (buflen < sizeof kd) {
1984 error = ENOMEM;
1985 break;
1986 }
1987 memset(&kd, 0, sizeof(kd));
1988 kd.d_bmajor = devsw_conv[i].d_bmajor;
1989 kd.d_cmajor = devsw_conv[i].d_cmajor;
1990 strlcpy(kd.d_name, dname, sizeof kd.d_name);
1991 error = copyout(&kd, where, sizeof kd);
1992 if (error != 0)
1993 break;
1994 buflen -= sizeof kd;
1995 where += sizeof kd;
1996 }
1997 *oldlenp = where - start;
1998 return error;
1999 }
2000
2001 /*
2002 * sysctl helper function for kern.file2
2003 */
2004 static int
2005 sysctl_kern_file2(SYSCTLFN_ARGS)
2006 {
2007 struct proc *p;
2008 struct file *fp;
2009 struct filedesc *fd;
2010 struct kinfo_file kf;
2011 char *dp;
2012 u_int i, op;
2013 size_t len, needed, elem_size, out_size;
2014 int error, arg, elem_count;
2015
2016 if (namelen == 1 && name[0] == CTL_QUERY)
2017 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2018
2019 if (namelen != 4)
2020 return (EINVAL);
2021
2022 error = 0;
2023 dp = oldp;
2024 len = (oldp != NULL) ? *oldlenp : 0;
2025 op = name[0];
2026 arg = name[1];
2027 elem_size = name[2];
2028 elem_count = name[3];
2029 out_size = MIN(sizeof(kf), elem_size);
2030 needed = 0;
2031
2032 if (elem_size < 1 || elem_count < 0)
2033 return (EINVAL);
2034
2035 switch (op) {
2036 case KERN_FILE_BYFILE:
2037 /*
2038 * doesn't use arg so it must be zero
2039 */
2040 if (arg != 0)
2041 return (EINVAL);
2042 LIST_FOREACH(fp, &filehead, f_list) {
2043 if (CURTAIN(kauth_cred_geteuid(l->l_proc->p_cred),
2044 kauth_cred_geteuid(fp->f_cred)))
2045 continue;
2046 if (len >= elem_size && elem_count > 0) {
2047 fill_file(&kf, fp, NULL, 0);
2048 error = copyout(&kf, dp, out_size);
2049 if (error)
2050 break;
2051 dp += elem_size;
2052 len -= elem_size;
2053 }
2054 if (elem_count > 0) {
2055 needed += elem_size;
2056 if (elem_count != INT_MAX)
2057 elem_count--;
2058 }
2059 }
2060 break;
2061 case KERN_FILE_BYPID:
2062 if (arg < -1)
2063 /* -1 means all processes */
2064 return (EINVAL);
2065 proclist_lock_read();
2066 PROCLIST_FOREACH(p, &allproc) {
2067 if (p->p_stat == SIDL)
2068 /* skip embryonic processes */
2069 continue;
2070 if (CURTAIN(kauth_cred_geteuid(l->l_proc->p_cred),
2071 kauth_cred_geteuid(p->p_cred)))
2072 continue;
2073 if (arg > 0 && p->p_pid != arg)
2074 /* pick only the one we want */
2075 /* XXX want 0 to mean "kernel files" */
2076 continue;
2077 fd = p->p_fd;
2078 for (i = 0; i < fd->fd_nfiles; i++) {
2079 fp = fd->fd_ofiles[i];
2080 if (fp == NULL || !FILE_IS_USABLE(fp))
2081 continue;
2082 if (len >= elem_size && elem_count > 0) {
2083 fill_file(&kf, fd->fd_ofiles[i],
2084 p, i);
2085 error = copyout(&kf, dp, out_size);
2086 if (error)
2087 break;
2088 dp += elem_size;
2089 len -= elem_size;
2090 }
2091 if (elem_count > 0) {
2092 needed += elem_size;
2093 if (elem_count != INT_MAX)
2094 elem_count--;
2095 }
2096 }
2097 }
2098 proclist_unlock_read();
2099 break;
2100 default:
2101 return (EINVAL);
2102 }
2103
2104 if (oldp == NULL)
2105 needed += KERN_FILESLOP * elem_size;
2106 *oldlenp = needed;
2107
2108 return (error);
2109 }
2110
2111 static void
2112 fill_file(struct kinfo_file *kp, const struct file *fp, struct proc *p, int i)
2113 {
2114
2115 memset(kp, 0, sizeof(*kp));
2116
2117 kp->ki_fileaddr = PTRTOUINT64(fp);
2118 kp->ki_flag = fp->f_flag;
2119 kp->ki_iflags = fp->f_iflags;
2120 kp->ki_ftype = fp->f_type;
2121 kp->ki_count = fp->f_count;
2122 kp->ki_msgcount = fp->f_msgcount;
2123 kp->ki_usecount = fp->f_usecount;
2124 kp->ki_fucred = PTRTOUINT64(fp->f_cred);
2125 kp->ki_fuid = kauth_cred_geteuid(fp->f_cred);
2126 kp->ki_fgid = kauth_cred_getegid(fp->f_cred);
2127 kp->ki_fops = PTRTOUINT64(fp->f_ops);
2128 kp->ki_foffset = fp->f_offset;
2129 kp->ki_fdata = PTRTOUINT64(fp->f_data);
2130
2131 /* vnode information to glue this file to something */
2132 if (fp->f_type == DTYPE_VNODE) {
2133 struct vnode *vp = (struct vnode *)fp->f_data;
2134
2135 kp->ki_vun = PTRTOUINT64(vp->v_un.vu_socket);
2136 kp->ki_vsize = vp->v_size;
2137 kp->ki_vtype = vp->v_type;
2138 kp->ki_vtag = vp->v_tag;
2139 kp->ki_vdata = PTRTOUINT64(vp->v_data);
2140 }
2141
2142 /* process information when retrieved via KERN_FILE_BYPID */
2143 if (p) {
2144 kp->ki_pid = p->p_pid;
2145 kp->ki_fd = i;
2146 kp->ki_ofileflags = p->p_fd->fd_ofileflags[i];
2147 }
2148 }
2149
2150 static int
2151 sysctl_doeproc(SYSCTLFN_ARGS)
2152 {
2153 struct eproc eproc;
2154 struct kinfo_proc2 kproc2;
2155 struct kinfo_proc *dp;
2156 struct proc *p;
2157 const struct proclist_desc *pd;
2158 char *where, *dp2;
2159 int type, op, arg;
2160 u_int elem_size, elem_count;
2161 size_t buflen, needed;
2162 int error;
2163
2164 if (namelen == 1 && name[0] == CTL_QUERY)
2165 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2166
2167 dp = oldp;
2168 dp2 = where = oldp;
2169 buflen = where != NULL ? *oldlenp : 0;
2170 error = 0;
2171 needed = 0;
2172 type = rnode->sysctl_num;
2173
2174 if (type == KERN_PROC) {
2175 if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
2176 return (EINVAL);
2177 op = name[0];
2178 if (op != KERN_PROC_ALL)
2179 arg = name[1];
2180 else
2181 arg = 0; /* Quell compiler warning */
2182 elem_size = elem_count = 0; /* Ditto */
2183 } else {
2184 if (namelen != 4)
2185 return (EINVAL);
2186 op = name[0];
2187 arg = name[1];
2188 elem_size = name[2];
2189 elem_count = name[3];
2190 }
2191
2192 proclist_lock_read();
2193
2194 pd = proclists;
2195 again:
2196 PROCLIST_FOREACH(p, pd->pd_list) {
2197 /*
2198 * Skip embryonic processes.
2199 */
2200 if (p->p_stat == SIDL)
2201 continue;
2202
2203 if (CURTAIN(kauth_cred_geteuid(l->l_proc->p_cred),
2204 kauth_cred_geteuid(p->p_cred)))
2205 continue;
2206
2207 /*
2208 * TODO - make more efficient (see notes below).
2209 * do by session.
2210 */
2211 switch (op) {
2212
2213 case KERN_PROC_PID:
2214 /* could do this with just a lookup */
2215 if (p->p_pid != (pid_t)arg)
2216 continue;
2217 break;
2218
2219 case KERN_PROC_PGRP:
2220 /* could do this by traversing pgrp */
2221 if (p->p_pgrp->pg_id != (pid_t)arg)
2222 continue;
2223 break;
2224
2225 case KERN_PROC_SESSION:
2226 if (p->p_session->s_sid != (pid_t)arg)
2227 continue;
2228 break;
2229
2230 case KERN_PROC_TTY:
2231 if (arg == (int) KERN_PROC_TTY_REVOKE) {
2232 if ((p->p_flag & P_CONTROLT) == 0 ||
2233 p->p_session->s_ttyp == NULL ||
2234 p->p_session->s_ttyvp != NULL)
2235 continue;
2236 } else if ((p->p_flag & P_CONTROLT) == 0 ||
2237 p->p_session->s_ttyp == NULL) {
2238 if ((dev_t)arg != KERN_PROC_TTY_NODEV)
2239 continue;
2240 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
2241 continue;
2242 break;
2243
2244 case KERN_PROC_UID:
2245 if (kauth_cred_geteuid(p->p_cred) != (uid_t)arg)
2246 continue;
2247 break;
2248
2249 case KERN_PROC_RUID:
2250 if (kauth_cred_getuid(p->p_cred) != (uid_t)arg)
2251 continue;
2252 break;
2253
2254 case KERN_PROC_GID:
2255 if (kauth_cred_getegid(p->p_cred) != (uid_t)arg)
2256 continue;
2257 break;
2258
2259 case KERN_PROC_RGID:
2260 if (kauth_cred_getgid(p->p_cred) != (uid_t)arg)
2261 continue;
2262 break;
2263
2264 case KERN_PROC_ALL:
2265 /* allow everything */
2266 break;
2267
2268 default:
2269 error = EINVAL;
2270 goto cleanup;
2271 }
2272 if (type == KERN_PROC) {
2273 if (buflen >= sizeof(struct kinfo_proc)) {
2274 fill_eproc(p, &eproc);
2275 error = copyout(p, &dp->kp_proc,
2276 sizeof(struct proc));
2277 if (error)
2278 goto cleanup;
2279 error = copyout(&eproc, &dp->kp_eproc,
2280 sizeof(eproc));
2281 if (error)
2282 goto cleanup;
2283 dp++;
2284 buflen -= sizeof(struct kinfo_proc);
2285 }
2286 needed += sizeof(struct kinfo_proc);
2287 } else { /* KERN_PROC2 */
2288 if (buflen >= elem_size && elem_count > 0) {
2289 fill_kproc2(p, &kproc2);
2290 /*
2291 * Copy out elem_size, but not larger than
2292 * the size of a struct kinfo_proc2.
2293 */
2294 error = copyout(&kproc2, dp2,
2295 min(sizeof(kproc2), elem_size));
2296 if (error)
2297 goto cleanup;
2298 dp2 += elem_size;
2299 buflen -= elem_size;
2300 elem_count--;
2301 }
2302 needed += elem_size;
2303 }
2304 }
2305 pd++;
2306 if (pd->pd_list != NULL)
2307 goto again;
2308 proclist_unlock_read();
2309
2310 if (where != NULL) {
2311 if (type == KERN_PROC)
2312 *oldlenp = (char *)dp - where;
2313 else
2314 *oldlenp = dp2 - where;
2315 if (needed > *oldlenp)
2316 return (ENOMEM);
2317 } else {
2318 needed += KERN_PROCSLOP;
2319 *oldlenp = needed;
2320 }
2321 return (0);
2322 cleanup:
2323 proclist_unlock_read();
2324 return (error);
2325 }
2326
2327 /*
2328 * sysctl helper routine for kern.proc_args pseudo-subtree.
2329 */
2330 static int
2331 sysctl_kern_proc_args(SYSCTLFN_ARGS)
2332 {
2333 struct ps_strings pss;
2334 struct proc *p, *up = l->l_proc;
2335 size_t len, upper_bound, xlen, i;
2336 struct uio auio;
2337 struct iovec aiov;
2338 vaddr_t argv;
2339 pid_t pid;
2340 int nargv, type, error;
2341 char *arg;
2342 char *tmp;
2343 struct vmspace *vmspace;
2344 vaddr_t psstr_addr;
2345 vaddr_t offsetn;
2346 vaddr_t offsetv;
2347
2348 if (namelen == 1 && name[0] == CTL_QUERY)
2349 return (sysctl_query(SYSCTLFN_CALL(rnode)));
2350
2351 if (newp != NULL || namelen != 2)
2352 return (EINVAL);
2353 pid = name[0];
2354 type = name[1];
2355
2356 switch (type) {
2357 case KERN_PROC_ARGV:
2358 case KERN_PROC_NARGV:
2359 case KERN_PROC_ENV:
2360 case KERN_PROC_NENV:
2361 /* ok */
2362 break;
2363 default:
2364 return (EINVAL);
2365 }
2366
2367 proclist_lock_read();
2368
2369 /* check pid */
2370 if ((p = p_find(pid, PFIND_LOCKED)) == NULL) {
2371 error = EINVAL;
2372 goto out_locked;
2373 }
2374
2375 if (CURTAIN(kauth_cred_geteuid(l->l_proc->p_cred),
2376 kauth_cred_geteuid(p->p_cred))) {
2377 error = EPERM;
2378 goto out_locked;
2379 }
2380
2381 /* only root or same user change look at the environment */
2382 if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
2383 if (kauth_cred_geteuid(up->p_cred) != 0) {
2384 if (kauth_cred_getuid(up->p_cred) != kauth_cred_getuid(p->p_cred) ||
2385 kauth_cred_getuid(up->p_cred) != kauth_cred_getsvuid(p->p_cred)) {
2386 error = EPERM;
2387 goto out_locked;
2388 }
2389 }
2390 }
2391
2392 if (oldp == NULL) {
2393 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
2394 *oldlenp = sizeof (int);
2395 else
2396 *oldlenp = ARG_MAX; /* XXX XXX XXX */
2397 error = 0;
2398 goto out_locked;
2399 }
2400
2401 /*
2402 * Zombies don't have a stack, so we can't read their psstrings.
2403 * System processes also don't have a user stack.
2404 */
2405 if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0) {
2406 error = EINVAL;
2407 goto out_locked;
2408 }
2409
2410 /*
2411 * Lock the process down in memory.
2412 */
2413 /* XXXCDC: how should locking work here? */
2414 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1)) {
2415 error = EFAULT;
2416 goto out_locked;
2417 }
2418
2419 psstr_addr = (vaddr_t)p->p_psstr;
2420 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV) {
2421 offsetn = p->p_psnargv;
2422 offsetv = p->p_psargv;
2423 } else {
2424 offsetn = p->p_psnenv;
2425 offsetv = p->p_psenv;
2426 }
2427 vmspace = p->p_vmspace;
2428 vmspace->vm_refcnt++; /* XXX */
2429
2430 proclist_unlock_read();
2431
2432 /*
2433 * Allocate a temporary buffer to hold the arguments.
2434 */
2435 arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
2436
2437 /*
2438 * Read in the ps_strings structure.
2439 */
2440 aiov.iov_base = &pss;
2441 aiov.iov_len = sizeof(pss);
2442 auio.uio_iov = &aiov;
2443 auio.uio_iovcnt = 1;
2444 auio.uio_offset = psstr_addr;
2445 auio.uio_resid = sizeof(pss);
2446 auio.uio_rw = UIO_READ;
2447 UIO_SETUP_SYSSPACE(&auio);
2448 error = uvm_io(&vmspace->vm_map, &auio);
2449 if (error)
2450 goto done;
2451
2452 memcpy(&nargv, (char *)&pss + offsetn, sizeof(nargv));
2453 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
2454 error = copyout(&nargv, oldp, sizeof(nargv));
2455 *oldlenp = sizeof(nargv);
2456 goto done;
2457 }
2458 /*
2459 * Now read the address of the argument vector.
2460 */
2461 switch (type) {
2462 case KERN_PROC_ARGV:
2463 /* XXX compat32 stuff here */
2464 /* FALLTHROUGH */
2465 case KERN_PROC_ENV:
2466 memcpy(&tmp, (char *)&pss + offsetv, sizeof(tmp));
2467 break;
2468 default:
2469 return (EINVAL);
2470 }
2471 auio.uio_offset = (off_t)(unsigned long)tmp;
2472 aiov.iov_base = &argv;
2473 aiov.iov_len = sizeof(argv);
2474 auio.uio_iov = &aiov;
2475 auio.uio_iovcnt = 1;
2476 auio.uio_resid = sizeof(argv);
2477 auio.uio_rw = UIO_READ;
2478 UIO_SETUP_SYSSPACE(&auio);
2479 error = uvm_io(&vmspace->vm_map, &auio);
2480 if (error)
2481 goto done;
2482
2483 /*
2484 * Now copy in the actual argument vector, one page at a time,
2485 * since we don't know how long the vector is (though, we do
2486 * know how many NUL-terminated strings are in the vector).
2487 */
2488 len = 0;
2489 upper_bound = *oldlenp;
2490 for (; nargv != 0 && len < upper_bound; len += xlen) {
2491 aiov.iov_base = arg;
2492 aiov.iov_len = PAGE_SIZE;
2493 auio.uio_iov = &aiov;
2494 auio.uio_iovcnt = 1;
2495 auio.uio_offset = argv + len;
2496 xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
2497 auio.uio_resid = xlen;
2498 auio.uio_rw = UIO_READ;
2499 UIO_SETUP_SYSSPACE(&auio);
2500 error = uvm_io(&vmspace->vm_map, &auio);
2501 if (error)
2502 goto done;
2503
2504 for (i = 0; i < xlen && nargv != 0; i++) {
2505 if (arg[i] == '\0')
2506 nargv--; /* one full string */
2507 }
2508
2509 /*
2510 * Make sure we don't copyout past the end of the user's
2511 * buffer.
2512 */
2513 if (len + i > upper_bound)
2514 i = upper_bound - len;
2515
2516 error = copyout(arg, (char *)oldp + len, i);
2517 if (error)
2518 break;
2519
2520 if (nargv == 0) {
2521 len += i;
2522 break;
2523 }
2524 }
2525 *oldlenp = len;
2526
2527 done:
2528 uvmspace_free(vmspace);
2529
2530 free(arg, M_TEMP);
2531 return error;
2532
2533 out_locked:
2534 proclist_unlock_read();
2535 return error;
2536 }
2537
2538 /*
2539 * Sysctl helper routine for Verified Exec.
2540 */
2541 #ifdef VERIFIED_EXEC
2542 static int
2543 sysctl_kern_veriexec(SYSCTLFN_ARGS)
2544 {
2545 int newval, error;
2546 int *var = NULL, raise_only = 0;
2547 struct sysctlnode node;
2548
2549 node = *rnode;
2550
2551 switch (rnode->sysctl_num) {
2552 case VERIEXEC_STRICT:
2553 raise_only = 1;
2554 var = &veriexec_strict;
2555 break;
2556 case VERIEXEC_ALGORITHMS:
2557 node.sysctl_data = veriexec_fp_names;
2558 node.sysctl_size = strlen(veriexec_fp_names) + 1;
2559 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2560 default:
2561 return (EINVAL);
2562 }
2563
2564 newval = *var;
2565
2566 node.sysctl_data = &newval;
2567 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2568 if (error || newp == NULL) {
2569 return (error);
2570 }
2571
2572 if (raise_only && (newval < *var))
2573 return (EPERM);
2574
2575 *var = newval;
2576
2577 return (error);
2578 }
2579 #endif /* VERIFIED_EXEC */
2580
2581 static int
2582 sysctl_security_setidcore(SYSCTLFN_ARGS)
2583 {
2584 int newsize, error;
2585 struct sysctlnode node;
2586
2587 node = *rnode;
2588 node.sysctl_data = &newsize;
2589 newsize = *(int *)rnode->sysctl_data;
2590 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2591 if (error || newp == NULL)
2592 return error;
2593
2594 if (securelevel > 0)
2595 return (EPERM);
2596
2597 *(int *)rnode->sysctl_data = newsize;
2598
2599 return 0;
2600 }
2601
2602 static int
2603 sysctl_security_setidcorename(SYSCTLFN_ARGS)
2604 {
2605 int error;
2606 char newsetidcorename[MAXPATHLEN];
2607 struct sysctlnode node;
2608
2609 node = *rnode;
2610 node.sysctl_data = &newsetidcorename[0];
2611 memcpy(node.sysctl_data, rnode->sysctl_data, MAXPATHLEN);
2612 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2613 if (error || newp == NULL)
2614 return (error);
2615
2616 if (securelevel > 0)
2617 return (EPERM);
2618
2619 if (strlen(newsetidcorename) == 0)
2620 return (EINVAL);
2621
2622 memcpy(rnode->sysctl_data, node.sysctl_data, MAXPATHLEN);
2623
2624 return (0);
2625 }
2626
2627 /*
2628 * sysctl helper routine for kern.cp_id node. maps cpus to their
2629 * cpuids.
2630 */
2631 static int
2632 sysctl_kern_cpid(SYSCTLFN_ARGS)
2633 {
2634 struct sysctlnode node = *rnode;
2635
2636 #ifndef MULTIPROCESSOR
2637 uint64_t id;
2638
2639 if (namelen == 1) {
2640 if (name[0] != 0)
2641 return (ENOENT);
2642 /*
2643 * you're allowed to ask for the zero'th processor
2644 */
2645 name++;
2646 namelen--;
2647 }
2648 node.sysctl_data = &id;
2649 node.sysctl_size = sizeof(id);
2650 id = cpu_number();
2651 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2652
2653 #else /* MULTIPROCESSOR */
2654 uint64_t *cp_id = NULL;
2655 int error, n = sysctl_ncpus();
2656 struct cpu_info *ci;
2657 CPU_INFO_ITERATOR cii;
2658
2659 /*
2660 * here you may either retrieve a single cpu id or the whole
2661 * set. the size you get back when probing depends on what
2662 * you ask for.
2663 */
2664 switch (namelen) {
2665 case 0:
2666 node.sysctl_size = n * sizeof(uint64_t);
2667 n = -2; /* ALL */
2668 break;
2669 case 1:
2670 if (name[0] < 0 || name[0] >= n)
2671 return (ENOENT); /* ENOSUCHPROCESSOR */
2672 node.sysctl_size = sizeof(uint64_t);
2673 n = name[0];
2674 /*
2675 * adjust these so that sysctl_lookup() will be happy
2676 */
2677 name++;
2678 namelen--;
2679 break;
2680 default:
2681 return (EINVAL);
2682 }
2683
2684 cp_id = malloc(node.sysctl_size, M_TEMP, M_WAITOK|M_CANFAIL);
2685 if (cp_id == NULL)
2686 return (ENOMEM);
2687 node.sysctl_data = cp_id;
2688 memset(cp_id, 0, node.sysctl_size);
2689
2690 for (CPU_INFO_FOREACH(cii, ci)) {
2691 if (n <= 0)
2692 cp_id[0] = ci->ci_cpuid;
2693 /*
2694 * if a specific processor was requested and we just
2695 * did it, we're done here
2696 */
2697 if (n == 0)
2698 break;
2699 /*
2700 * if doing "all", skip to next cp_id slot for next processor
2701 */
2702 if (n == -2)
2703 cp_id++;
2704 /*
2705 * if we're doing a specific processor, we're one
2706 * processor closer
2707 */
2708 if (n > 0)
2709 n--;
2710 }
2711
2712 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2713 free(node.sysctl_data, M_TEMP);
2714 return (error);
2715
2716 #endif /* MULTIPROCESSOR */
2717 }
2718
2719 /*
2720 * sysctl helper routine for hw.usermem and hw.usermem64. values are
2721 * calculate on the fly taking into account integer overflow and the
2722 * current wired count.
2723 */
2724 static int
2725 sysctl_hw_usermem(SYSCTLFN_ARGS)
2726 {
2727 u_int ui;
2728 u_quad_t uq;
2729 struct sysctlnode node;
2730
2731 node = *rnode;
2732 switch (rnode->sysctl_num) {
2733 case HW_USERMEM:
2734 if ((ui = physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
2735 ui = UINT_MAX;
2736 else
2737 ui *= PAGE_SIZE;
2738 node.sysctl_data = &ui;
2739 break;
2740 case HW_USERMEM64:
2741 uq = (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE;
2742 node.sysctl_data = &uq;
2743 break;
2744 default:
2745 return (EINVAL);
2746 }
2747
2748 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2749 }
2750
2751 /*
2752 * sysctl helper routine for kern.cnmagic node. pulls the old value
2753 * out, encoded, and stuffs the new value in for decoding.
2754 */
2755 static int
2756 sysctl_hw_cnmagic(SYSCTLFN_ARGS)
2757 {
2758 char magic[CNS_LEN];
2759 int error;
2760 struct sysctlnode node;
2761
2762 if (oldp)
2763 cn_get_magic(magic, CNS_LEN);
2764 node = *rnode;
2765 node.sysctl_data = &magic[0];
2766 error = sysctl_lookup(SYSCTLFN_CALL(&node));
2767 if (error || newp == NULL)
2768 return (error);
2769
2770 return (cn_set_magic(magic));
2771 }
2772
2773 static int
2774 sysctl_hw_ncpu(SYSCTLFN_ARGS)
2775 {
2776 int ncpu;
2777 struct sysctlnode node;
2778
2779 ncpu = sysctl_ncpus();
2780 node = *rnode;
2781 node.sysctl_data = &ncpu;
2782
2783 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2784 }
2785
2786
2787 /*
2788 * ********************************************************************
2789 * section 3: public helper routines that are used for more than one
2790 * node
2791 * ********************************************************************
2792 */
2793
2794 /*
2795 * sysctl helper routine for the kern.root_device node and some ports'
2796 * machdep.root_device nodes.
2797 */
2798 int
2799 sysctl_root_device(SYSCTLFN_ARGS)
2800 {
2801 struct sysctlnode node;
2802
2803 node = *rnode;
2804 node.sysctl_data = root_device->dv_xname;
2805 node.sysctl_size = strlen(root_device->dv_xname) + 1;
2806 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2807 }
2808
2809 /*
2810 * sysctl helper routine for kern.consdev, dependent on the current
2811 * state of the console. also used for machdep.console_device on some
2812 * ports.
2813 */
2814 int
2815 sysctl_consdev(SYSCTLFN_ARGS)
2816 {
2817 dev_t consdev;
2818 struct sysctlnode node;
2819
2820 if (cn_tab != NULL)
2821 consdev = cn_tab->cn_dev;
2822 else
2823 consdev = NODEV;
2824 node = *rnode;
2825 node.sysctl_data = &consdev;
2826 node.sysctl_size = sizeof(consdev);
2827 return (sysctl_lookup(SYSCTLFN_CALL(&node)));
2828 }
2829
2830 /*
2831 * ********************************************************************
2832 * section 4: support for some helpers
2833 * ********************************************************************
2834 */
2835
2836 /*
2837 * Fill in a kinfo_proc2 structure for the specified process.
2838 */
2839 static void
2840 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
2841 {
2842 struct tty *tp;
2843 struct lwp *l;
2844 struct timeval ut, st;
2845
2846 memset(ki, 0, sizeof(*ki));
2847
2848 ki->p_paddr = PTRTOUINT64(p);
2849 ki->p_fd = PTRTOUINT64(p->p_fd);
2850 ki->p_cwdi = PTRTOUINT64(p->p_cwdi);
2851 ki->p_stats = PTRTOUINT64(p->p_stats);
2852 ki->p_limit = PTRTOUINT64(p->p_limit);
2853 ki->p_vmspace = PTRTOUINT64(p->p_vmspace);
2854 ki->p_sigacts = PTRTOUINT64(p->p_sigacts);
2855 ki->p_sess = PTRTOUINT64(p->p_session);
2856 ki->p_tsess = 0; /* may be changed if controlling tty below */
2857 ki->p_ru = PTRTOUINT64(p->p_ru);
2858
2859 ki->p_eflag = 0;
2860 ki->p_exitsig = p->p_exitsig;
2861 ki->p_flag = p->p_flag;
2862
2863 ki->p_pid = p->p_pid;
2864 if (p->p_pptr)
2865 ki->p_ppid = p->p_pptr->p_pid;
2866 else
2867 ki->p_ppid = 0;
2868 ki->p_sid = p->p_session->s_sid;
2869 ki->p__pgid = p->p_pgrp->pg_id;
2870
2871 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */
2872
2873 ki->p_uid = kauth_cred_geteuid(p->p_cred);
2874 ki->p_ruid = kauth_cred_getuid(p->p_cred);
2875 ki->p_gid = kauth_cred_getegid(p->p_cred);
2876 ki->p_rgid = kauth_cred_getgid(p->p_cred);
2877 ki->p_svuid = kauth_cred_getsvuid(p->p_cred);
2878 ki->p_svgid = kauth_cred_getsvgid(p->p_cred);
2879
2880 ki->p_ngroups = kauth_cred_ngroups(p->p_cred);
2881 kauth_cred_getgroups(p->p_cred, ki->p_groups,
2882 sizeof(ki->p_groups) / sizeof(ki->p_groups[0]));
2883
2884 ki->p_jobc = p->p_pgrp->pg_jobc;
2885 if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
2886 ki->p_tdev = tp->t_dev;
2887 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
2888 ki->p_tsess = PTRTOUINT64(tp->t_session);
2889 } else {
2890 ki->p_tdev = NODEV;
2891 }
2892
2893 ki->p_estcpu = p->p_estcpu;
2894 ki->p_rtime_sec = p->p_rtime.tv_sec;
2895 ki->p_rtime_usec = p->p_rtime.tv_usec;
2896 ki->p_cpticks = p->p_cpticks;
2897 ki->p_pctcpu = p->p_pctcpu;
2898
2899 ki->p_uticks = p->p_uticks;
2900 ki->p_sticks = p->p_sticks;
2901 ki->p_iticks = p->p_iticks;
2902
2903 ki->p_tracep = PTRTOUINT64(p->p_tracep);
2904 ki->p_traceflag = p->p_traceflag;
2905
2906
2907 memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
2908 memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
2909 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
2910 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
2911
2912 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
2913 ki->p_realstat = p->p_stat;
2914 ki->p_nice = p->p_nice;
2915
2916 ki->p_xstat = p->p_xstat;
2917 ki->p_acflag = p->p_acflag;
2918
2919 strncpy(ki->p_comm, p->p_comm,
2920 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
2921
2922 strncpy(ki->p_login, p->p_session->s_login,
2923 min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
2924
2925 ki->p_nlwps = p->p_nlwps;
2926 ki->p_nrlwps = p->p_nrlwps;
2927 ki->p_realflag = p->p_flag;
2928
2929 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
2930 ki->p_vm_rssize = 0;
2931 ki->p_vm_tsize = 0;
2932 ki->p_vm_dsize = 0;
2933 ki->p_vm_ssize = 0;
2934 l = NULL;
2935 } else {
2936 struct vmspace *vm = p->p_vmspace;
2937
2938 ki->p_vm_rssize = vm_resident_count(vm);
2939 ki->p_vm_tsize = vm->vm_tsize;
2940 ki->p_vm_dsize = vm->vm_dsize;
2941 ki->p_vm_ssize = vm->vm_ssize;
2942
2943 /* Pick a "representative" LWP */
2944 l = proc_representative_lwp(p);
2945 ki->p_forw = PTRTOUINT64(l->l_forw);
2946 ki->p_back = PTRTOUINT64(l->l_back);
2947 ki->p_addr = PTRTOUINT64(l->l_addr);
2948 ki->p_stat = l->l_stat;
2949 ki->p_flag |= l->l_flag;
2950 ki->p_swtime = l->l_swtime;
2951 ki->p_slptime = l->l_slptime;
2952 if (l->l_stat == LSONPROC) {
2953 KDASSERT(l->l_cpu != NULL);
2954 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2955 } else
2956 ki->p_schedflags = 0;
2957 ki->p_holdcnt = l->l_holdcnt;
2958 ki->p_priority = l->l_priority;
2959 ki->p_usrpri = l->l_usrpri;
2960 if (l->l_wmesg)
2961 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
2962 ki->p_wchan = PTRTOUINT64(l->l_wchan);
2963
2964 }
2965
2966 if (p->p_session->s_ttyvp)
2967 ki->p_eflag |= EPROC_CTTY;
2968 if (SESS_LEADER(p))
2969 ki->p_eflag |= EPROC_SLEADER;
2970
2971 /* XXX Is this double check necessary? */
2972 if (P_ZOMBIE(p)) {
2973 ki->p_uvalid = 0;
2974 } else {
2975 ki->p_uvalid = 1;
2976
2977 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
2978 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
2979
2980 calcru(p, &ut, &st, 0);
2981 ki->p_uutime_sec = ut.tv_sec;
2982 ki->p_uutime_usec = ut.tv_usec;
2983 ki->p_ustime_sec = st.tv_sec;
2984 ki->p_ustime_usec = st.tv_usec;
2985
2986 ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
2987 ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
2988 ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
2989 ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
2990 ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
2991 ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
2992 ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
2993 ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
2994 ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
2995 ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
2996 ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
2997 ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
2998 ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
2999 ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
3000
3001 timeradd(&p->p_stats->p_cru.ru_utime,
3002 &p->p_stats->p_cru.ru_stime, &ut);
3003 ki->p_uctime_sec = ut.tv_sec;
3004 ki->p_uctime_usec = ut.tv_usec;
3005 }
3006 #ifdef MULTIPROCESSOR
3007 if (l && l->l_cpu != NULL)
3008 ki->p_cpuid = l->l_cpu->ci_cpuid;
3009 else
3010 #endif
3011 ki->p_cpuid = KI_NOCPU;
3012 }
3013
3014 /*
3015 * Fill in a kinfo_lwp structure for the specified lwp.
3016 */
3017 static void
3018 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
3019 {
3020
3021 kl->l_forw = PTRTOUINT64(l->l_forw);
3022 kl->l_back = PTRTOUINT64(l->l_back);
3023 kl->l_laddr = PTRTOUINT64(l);
3024 kl->l_addr = PTRTOUINT64(l->l_addr);
3025 kl->l_stat = l->l_stat;
3026 kl->l_lid = l->l_lid;
3027 kl->l_flag = l->l_flag;
3028
3029 kl->l_swtime = l->l_swtime;
3030 kl->l_slptime = l->l_slptime;
3031 if (l->l_stat == LSONPROC) {
3032 KDASSERT(l->l_cpu != NULL);
3033 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
3034 } else
3035 kl->l_schedflags = 0;
3036 kl->l_holdcnt = l->l_holdcnt;
3037 kl->l_priority = l->l_priority;
3038 kl->l_usrpri = l->l_usrpri;
3039 if (l->l_wmesg)
3040 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
3041 kl->l_wchan = PTRTOUINT64(l->l_wchan);
3042 #ifdef MULTIPROCESSOR
3043 if (l->l_cpu != NULL)
3044 kl->l_cpuid = l->l_cpu->ci_cpuid;
3045 else
3046 #endif
3047 kl->l_cpuid = KI_NOCPU;
3048 }
3049
3050 /*
3051 * Fill in an eproc structure for the specified process.
3052 */
3053 void
3054 fill_eproc(struct proc *p, struct eproc *ep)
3055 {
3056 struct tty *tp;
3057 struct lwp *l;
3058 struct pcred pc;
3059 struct ucred uc;
3060
3061 ep->e_paddr = p;
3062 ep->e_sess = p->p_session;
3063 kauth_cred_topcred(p->p_cred, &pc);
3064 kauth_cred_toucred(p->p_cred, &uc);
3065 ep->e_pcred = pc;
3066 ep->e_ucred = uc;
3067 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
3068 ep->e_vm.vm_rssize = 0;
3069 ep->e_vm.vm_tsize = 0;
3070 ep->e_vm.vm_dsize = 0;
3071 ep->e_vm.vm_ssize = 0;
3072 /* ep->e_vm.vm_pmap = XXX; */
3073 } else {
3074 struct vmspace *vm = p->p_vmspace;
3075
3076 ep->e_vm.vm_rssize = vm_resident_count(vm);
3077 ep->e_vm.vm_tsize = vm->vm_tsize;
3078 ep->e_vm.vm_dsize = vm->vm_dsize;
3079 ep->e_vm.vm_ssize = vm->vm_ssize;
3080
3081 /* Pick a "representative" LWP */
3082 l = proc_representative_lwp(p);
3083
3084 if (l->l_wmesg)
3085 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
3086 }
3087 if (p->p_pptr)
3088 ep->e_ppid = p->p_pptr->p_pid;
3089 else
3090 ep->e_ppid = 0;
3091 ep->e_pgid = p->p_pgrp->pg_id;
3092 ep->e_sid = ep->e_sess->s_sid;
3093 ep->e_jobc = p->p_pgrp->pg_jobc;
3094 if ((p->p_flag & P_CONTROLT) &&
3095 (tp = ep->e_sess->s_ttyp)) {
3096 ep->e_tdev = tp->t_dev;
3097 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
3098 ep->e_tsess = tp->t_session;
3099 } else
3100 ep->e_tdev = NODEV;
3101
3102 ep->e_xsize = ep->e_xrssize = 0;
3103 ep->e_xccount = ep->e_xswrss = 0;
3104 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
3105 if (SESS_LEADER(p))
3106 ep->e_flag |= EPROC_SLEADER;
3107 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
3108 }
3109