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