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