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