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