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