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