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