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