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