kern_sysctl.c revision 1.141 1 /* $NetBSD: kern_sysctl.c,v 1.141 2003/08/14 05:14:52 itojun Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Mike Karels at Berkeley Software Design, Inc.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
35 */
36
37 /*
38 * sysctl system call.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: kern_sysctl.c,v 1.141 2003/08/14 05:14:52 itojun Exp $");
43
44 #include "opt_ddb.h"
45 #include "opt_insecure.h"
46 #include "opt_defcorename.h"
47 #include "opt_multiprocessor.h"
48 #include "opt_pipe.h"
49 #include "opt_sysv.h"
50 #include "pty.h"
51 #include "rnd.h"
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/buf.h>
57 #include <sys/device.h>
58 #include <sys/disklabel.h>
59 #include <sys/dkstat.h>
60 #include <sys/exec.h>
61 #include <sys/file.h>
62 #include <sys/ioctl.h>
63 #include <sys/malloc.h>
64 #include <sys/mount.h>
65 #include <sys/msgbuf.h>
66 #include <sys/pool.h>
67 #include <sys/proc.h>
68 #include <sys/resource.h>
69 #include <sys/resourcevar.h>
70 #include <sys/sa.h>
71 #include <sys/syscallargs.h>
72 #include <sys/tty.h>
73 #include <sys/unistd.h>
74 #include <sys/vnode.h>
75 #include <sys/socketvar.h>
76 #define __SYSCTL_PRIVATE
77 #include <sys/sysctl.h>
78 #include <sys/lock.h>
79 #include <sys/namei.h>
80
81 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
82 #include <sys/ipc.h>
83 #endif
84 #ifdef SYSVMSG
85 #include <sys/msg.h>
86 #endif
87 #ifdef SYSVSEM
88 #include <sys/sem.h>
89 #endif
90 #ifdef SYSVSHM
91 #include <sys/shm.h>
92 #endif
93
94 #include <dev/cons.h>
95
96 #if defined(DDB)
97 #include <ddb/ddbvar.h>
98 #endif
99
100 #ifndef PIPE_SOCKETPAIR
101 #include <sys/pipe.h>
102 #endif
103
104 #if NRND > 0
105 #include <sys/rnd.h>
106 #endif
107
108 #define PTRTOINT64(foo) ((u_int64_t)(uintptr_t)(foo))
109
110 static int sysctl_file(void *, size_t *);
111 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
112 static int sysctl_sysvipc(int *, u_int, void *, size_t *);
113 #endif
114 static int sysctl_msgbuf(void *, size_t *);
115 static int sysctl_doeproc(int *, u_int, void *, size_t *);
116 static int sysctl_dolwp(int *, u_int, void *, size_t *);
117 static int sysctl_dotkstat(int *, u_int, void *, size_t *, void *);
118 #ifdef MULTIPROCESSOR
119 static int sysctl_docptime(void *, size_t *, void *);
120 static int sysctl_ncpus(void);
121 #endif
122 static void fill_kproc2(struct proc *, struct kinfo_proc2 *);
123 static void fill_lwp(struct lwp *, struct kinfo_lwp *);
124 static int sysctl_procargs(int *, u_int, void *, size_t *, struct proc *);
125 #if NPTY > 0
126 static int sysctl_pty(void *, size_t *, void *, size_t);
127 #endif
128
129 /*
130 * The `sysctl_memlock' is intended to keep too many processes from
131 * locking down memory by doing sysctls at once. Whether or not this
132 * is really a good idea to worry about it probably a subject of some
133 * debate.
134 */
135 struct lock sysctl_memlock;
136
137 void
138 sysctl_init(void)
139 {
140
141 lockinit(&sysctl_memlock, PRIBIO|PCATCH, "sysctl", 0, 0);
142 }
143
144 int
145 sys___sysctl(struct lwp *l, void *v, register_t *retval)
146 {
147 struct sys___sysctl_args /* {
148 syscallarg(int *) name;
149 syscallarg(u_int) namelen;
150 syscallarg(void *) old;
151 syscallarg(size_t *) oldlenp;
152 syscallarg(void *) new;
153 syscallarg(size_t) newlen;
154 } */ *uap = v;
155 struct proc *p = l->l_proc;
156 int error;
157 size_t savelen = 0, oldlen = 0;
158 sysctlfn *fn;
159 int name[CTL_MAXNAME];
160
161 /*
162 * all top-level sysctl names are non-terminal
163 */
164 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
165 return (EINVAL);
166 error = copyin(SCARG(uap, name), &name,
167 SCARG(uap, namelen) * sizeof(int));
168 if (error)
169 return (error);
170
171 /*
172 * For all but CTL_PROC, must be root to change a value.
173 * For CTL_PROC, must be root, or owner of the proc (and not suid),
174 * this is checked in proc_sysctl() (once we know the targer proc).
175 */
176 if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
177 (error = suser(p->p_ucred, &p->p_acflag)))
178 return (error);
179
180 switch (name[0]) {
181 case CTL_KERN:
182 fn = kern_sysctl;
183 break;
184 case CTL_HW:
185 fn = hw_sysctl;
186 break;
187 case CTL_VM:
188 fn = uvm_sysctl;
189 break;
190 case CTL_NET:
191 fn = net_sysctl;
192 break;
193 case CTL_VFS:
194 fn = vfs_sysctl;
195 break;
196 case CTL_MACHDEP:
197 fn = cpu_sysctl;
198 break;
199 #ifdef DEBUG
200 case CTL_DEBUG:
201 fn = debug_sysctl;
202 break;
203 #endif
204 #ifdef DDB
205 case CTL_DDB:
206 fn = ddb_sysctl;
207 break;
208 #endif
209 case CTL_PROC:
210 fn = proc_sysctl;
211 break;
212
213 case CTL_EMUL:
214 fn = emul_sysctl;
215 break;
216 default:
217 return (EOPNOTSUPP);
218 }
219
220 /*
221 * XXX Hey, we wire `old', but what about `new'?
222 */
223
224 if (SCARG(uap, oldlenp)) {
225 if ((error = copyin(SCARG(uap, oldlenp), &oldlen,
226 sizeof(oldlen))))
227 return (error);
228 }
229 if (SCARG(uap, old) != NULL) {
230 error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
231 if (error)
232 return (error);
233 error = uvm_vslock(p, SCARG(uap, old), oldlen, VM_PROT_WRITE);
234 if (error) {
235 (void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
236 return (error);
237 }
238 savelen = oldlen;
239 }
240 error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
241 &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
242 if (SCARG(uap, old) != NULL) {
243 uvm_vsunlock(p, SCARG(uap, old), savelen);
244 (void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
245 }
246 if (error)
247 return (error);
248 if (SCARG(uap, oldlenp))
249 error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
250 return (error);
251 }
252
253 /*
254 * Attributes stored in the kernel.
255 */
256 char hostname[MAXHOSTNAMELEN];
257 int hostnamelen;
258
259 char domainname[MAXHOSTNAMELEN];
260 int domainnamelen;
261
262 long hostid;
263
264 #ifdef INSECURE
265 int securelevel = -1;
266 #else
267 int securelevel = 0;
268 #endif
269
270 #ifndef DEFCORENAME
271 #define DEFCORENAME "%n.core"
272 #endif
273 char defcorename[MAXPATHLEN] = DEFCORENAME;
274 int defcorenamelen = sizeof(DEFCORENAME);
275
276 extern int kern_logsigexit;
277 extern fixpt_t ccpu;
278 extern int forkfsleep;
279 extern int dumponpanic;
280
281 #ifndef MULTIPROCESSOR
282 #define sysctl_ncpus() 1
283 #endif
284
285 #ifdef MULTIPROCESSOR
286
287 #ifndef CPU_INFO_FOREACH
288 #define CPU_INFO_ITERATOR int
289 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = curcpu(); ci != NULL; ci = NULL
290 #endif
291
292 static int
293 sysctl_docptime(void *oldp, size_t *oldlenp, void *newp)
294 {
295 u_int64_t cp_time[CPUSTATES];
296 int i;
297 struct cpu_info *ci;
298 CPU_INFO_ITERATOR cii;
299
300 for (i = 0; i < CPUSTATES; i++)
301 cp_time[i] = 0;
302
303 for (CPU_INFO_FOREACH(cii, ci)) {
304 for (i = 0; i < CPUSTATES; i++)
305 cp_time[i] += ci->ci_schedstate.spc_cp_time[i];
306 }
307 return (sysctl_rdstruct(oldp, oldlenp, newp,
308 cp_time, sizeof(cp_time)));
309 }
310
311 static int
312 sysctl_ncpus(void)
313 {
314 struct cpu_info *ci;
315 CPU_INFO_ITERATOR cii;
316
317 int ncpus = 0;
318 for (CPU_INFO_FOREACH(cii, ci))
319 ncpus++;
320 return (ncpus);
321 }
322
323 #endif
324
325 /*
326 * kernel related system variables.
327 */
328 int
329 kern_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
330 void *newp, size_t newlen, struct proc *p)
331 {
332 int error, level, inthostid;
333 int old_autonicetime;
334 int old_vnodes;
335 dev_t consdev;
336 #if NRND > 0
337 int v;
338 #endif
339
340 /* All sysctl names at this level, except for a few, are terminal. */
341 switch (name[0]) {
342 case KERN_PROC:
343 case KERN_PROC2:
344 case KERN_LWP:
345 case KERN_PROF:
346 case KERN_MBUF:
347 case KERN_PROC_ARGS:
348 case KERN_SYSVIPC_INFO:
349 case KERN_PIPE:
350 case KERN_TKSTAT:
351 /* Not terminal. */
352 break;
353 default:
354 if (namelen != 1)
355 return (ENOTDIR); /* overloaded */
356 }
357
358 switch (name[0]) {
359 case KERN_OSTYPE:
360 return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
361 case KERN_OSRELEASE:
362 return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
363 case KERN_OSREV:
364 return (sysctl_rdint(oldp, oldlenp, newp, __NetBSD_Version__));
365 case KERN_VERSION:
366 return (sysctl_rdstring(oldp, oldlenp, newp, version));
367 case KERN_MAXVNODES:
368 old_vnodes = desiredvnodes;
369 error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
370 if (newp && !error) {
371 if (old_vnodes > desiredvnodes) {
372 error = vfs_drainvnodes(desiredvnodes, p);
373 if (error) {
374 desiredvnodes = old_vnodes;
375 return error;
376 }
377 }
378 vfs_reinit();
379 nchreinit();
380 }
381 return (error);
382 case KERN_MAXPROC:
383 {
384 int nmaxproc = maxproc;
385
386 error = sysctl_int(oldp, oldlenp, newp, newlen, &nmaxproc);
387
388 if (!error && newp) {
389 if (nmaxproc < 0 || nmaxproc >= PID_MAX)
390 return (EINVAL);
391
392 #ifdef __HAVE_CPU_MAXPROC
393 if (nmaxproc > cpu_maxproc())
394 return (EINVAL);
395 #endif
396 maxproc = nmaxproc;
397 }
398
399 return (error);
400 }
401 case KERN_MAXFILES:
402 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
403 case KERN_ARGMAX:
404 return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
405 case KERN_SECURELVL:
406 level = securelevel;
407 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
408 newp == NULL)
409 return (error);
410 if (level < securelevel && p->p_pid != 1)
411 return (EPERM);
412 securelevel = level;
413 return (0);
414 case KERN_HOSTNAME:
415 error = sysctl_string(oldp, oldlenp, newp, newlen,
416 hostname, sizeof(hostname));
417 if (newp && !error)
418 hostnamelen = newlen;
419 return (error);
420 case KERN_DOMAINNAME:
421 error = sysctl_string(oldp, oldlenp, newp, newlen,
422 domainname, sizeof(domainname));
423 if (newp && !error)
424 domainnamelen = newlen;
425 return (error);
426 case KERN_HOSTID:
427 inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
428 error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
429 if (newp && !error)
430 hostid = inthostid;
431 return (error);
432 case KERN_CLOCKRATE:
433 return (sysctl_clockrate(oldp, oldlenp));
434 case KERN_BOOTTIME:
435 return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
436 sizeof(struct timeval)));
437 case KERN_VNODE:
438 return (sysctl_vnode(oldp, oldlenp, p));
439 case KERN_PROC:
440 case KERN_PROC2:
441 return (sysctl_doeproc(name, namelen, oldp, oldlenp));
442 case KERN_LWP:
443 return (sysctl_dolwp(name, namelen, oldp, oldlenp));
444 case KERN_PROC_ARGS:
445 return (sysctl_procargs(name + 1, namelen - 1,
446 oldp, oldlenp, p));
447 case KERN_FILE:
448 return (sysctl_file(oldp, oldlenp));
449 #ifdef GPROF
450 case KERN_PROF:
451 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
452 newp, newlen));
453 #endif
454 case KERN_POSIX1:
455 return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
456 case KERN_NGROUPS:
457 return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
458 case KERN_JOB_CONTROL:
459 return (sysctl_rdint(oldp, oldlenp, newp, 1));
460 case KERN_SAVED_IDS:
461 #ifdef _POSIX_SAVED_IDS
462 return (sysctl_rdint(oldp, oldlenp, newp, 1));
463 #else
464 return (sysctl_rdint(oldp, oldlenp, newp, 0));
465 #endif
466 case KERN_MAXPARTITIONS:
467 return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
468 case KERN_RAWPARTITION:
469 return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
470 #ifdef NTP
471 case KERN_NTPTIME:
472 return (sysctl_ntptime(oldp, oldlenp));
473 #endif
474 case KERN_AUTONICETIME:
475 old_autonicetime = autonicetime;
476 error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
477 if (autonicetime < 0)
478 autonicetime = old_autonicetime;
479 return (error);
480 case KERN_AUTONICEVAL:
481 error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
482 if (autoniceval < PRIO_MIN)
483 autoniceval = PRIO_MIN;
484 if (autoniceval > PRIO_MAX)
485 autoniceval = PRIO_MAX;
486 return (error);
487 case KERN_RTC_OFFSET:
488 return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
489 case KERN_ROOT_DEVICE:
490 return (sysctl_rdstring(oldp, oldlenp, newp,
491 root_device->dv_xname));
492 case KERN_MSGBUFSIZE:
493 /*
494 * deal with cases where the message buffer has
495 * become corrupted.
496 */
497 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
498 msgbufenabled = 0;
499 return (ENXIO);
500 }
501 return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
502 case KERN_FSYNC:
503 return (sysctl_rdint(oldp, oldlenp, newp, 1));
504 case KERN_SYSVMSG:
505 #ifdef SYSVMSG
506 return (sysctl_rdint(oldp, oldlenp, newp, 1));
507 #else
508 return (sysctl_rdint(oldp, oldlenp, newp, 0));
509 #endif
510 case KERN_SYSVSEM:
511 #ifdef SYSVSEM
512 return (sysctl_rdint(oldp, oldlenp, newp, 1));
513 #else
514 return (sysctl_rdint(oldp, oldlenp, newp, 0));
515 #endif
516 case KERN_SYSVSHM:
517 #ifdef SYSVSHM
518 return (sysctl_rdint(oldp, oldlenp, newp, 1));
519 #else
520 return (sysctl_rdint(oldp, oldlenp, newp, 0));
521 #endif
522 case KERN_DEFCORENAME:
523 if (newp && newlen < 1)
524 return (EINVAL);
525 error = sysctl_string(oldp, oldlenp, newp, newlen,
526 defcorename, sizeof(defcorename));
527 if (newp && !error)
528 defcorenamelen = newlen;
529 return (error);
530 case KERN_SYNCHRONIZED_IO:
531 return (sysctl_rdint(oldp, oldlenp, newp, 1));
532 case KERN_IOV_MAX:
533 return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
534 case KERN_MBUF:
535 return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
536 newp, newlen));
537 case KERN_MAPPED_FILES:
538 return (sysctl_rdint(oldp, oldlenp, newp, 1));
539 case KERN_MEMLOCK:
540 return (sysctl_rdint(oldp, oldlenp, newp, 1));
541 case KERN_MEMLOCK_RANGE:
542 return (sysctl_rdint(oldp, oldlenp, newp, 1));
543 case KERN_MEMORY_PROTECTION:
544 return (sysctl_rdint(oldp, oldlenp, newp, 1));
545 case KERN_LOGIN_NAME_MAX:
546 return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
547 case KERN_LOGSIGEXIT:
548 return (sysctl_int(oldp, oldlenp, newp, newlen,
549 &kern_logsigexit));
550 case KERN_FSCALE:
551 return (sysctl_rdint(oldp, oldlenp, newp, FSCALE));
552 case KERN_CCPU:
553 return (sysctl_rdint(oldp, oldlenp, newp, ccpu));
554 case KERN_CP_TIME:
555 #ifndef MULTIPROCESSOR
556 return (sysctl_rdstruct(oldp, oldlenp, newp,
557 curcpu()->ci_schedstate.spc_cp_time,
558 sizeof(curcpu()->ci_schedstate.spc_cp_time)));
559 #else
560 return (sysctl_docptime(oldp, oldlenp, newp));
561 #endif
562 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
563 case KERN_SYSVIPC_INFO:
564 return (sysctl_sysvipc(name + 1, namelen - 1, oldp, oldlenp));
565 #endif
566 case KERN_MSGBUF:
567 return (sysctl_msgbuf(oldp, oldlenp));
568 case KERN_CONSDEV:
569 if (cn_tab != NULL)
570 consdev = cn_tab->cn_dev;
571 else
572 consdev = NODEV;
573 return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
574 sizeof consdev));
575 #if NPTY > 0
576 case KERN_MAXPTYS:
577 return (sysctl_pty(oldp, oldlenp, newp, newlen));
578 #endif
579 #ifndef PIPE_SOCKETPAIR
580 case KERN_PIPE:
581 return (sysctl_dopipe(name + 1, namelen - 1, oldp, oldlenp,
582 newp, newlen));
583 #endif
584 case KERN_MAXPHYS:
585 return (sysctl_rdint(oldp, oldlenp, newp, MAXPHYS));
586 case KERN_SOMAXKVA:
587 {
588 int new_somaxkva = somaxkva;
589
590 error = sysctl_int(oldp, oldlenp, newp, newlen, &new_somaxkva);
591 if (newp && !error) {
592 if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */
593 return (EINVAL);
594 somaxkva = new_somaxkva;
595 }
596 return (error);
597 }
598 case KERN_SBMAX:
599 {
600 int new_sbmax = sb_max;
601
602 error = sysctl_int(oldp, oldlenp, newp, newlen, &new_sbmax);
603 if (newp && !error) {
604 if (new_sbmax < (16 * 1024)) /* sanity */
605 return (EINVAL);
606 sb_max = new_sbmax;
607 }
608 return (error);
609 }
610 case KERN_TKSTAT:
611 return (sysctl_dotkstat(name + 1, namelen - 1, oldp, oldlenp,
612 newp));
613 case KERN_MONOTONIC_CLOCK: /* XXX _POSIX_VERSION */
614 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
615 case KERN_URND:
616 #if NRND > 0
617 if (rnd_extract_data(&v, sizeof(v), RND_EXTRACT_ANY) ==
618 sizeof(v))
619 return (sysctl_rdint(oldp, oldlenp, newp, v));
620 else
621 return (EIO); /*XXX*/
622 #else
623 return (EOPNOTSUPP);
624 #endif
625 case KERN_LABELSECTOR:
626 return (sysctl_rdint(oldp, oldlenp, newp, LABELSECTOR));
627 case KERN_LABELOFFSET:
628 return (sysctl_rdint(oldp, oldlenp, newp, LABELOFFSET));
629 case KERN_FORKFSLEEP:
630 {
631 /* userland sees value in ms, internally is in ticks */
632 int timo, lsleep = forkfsleep * 1000 / hz;
633
634 error = sysctl_int(oldp, oldlenp, newp, newlen, &lsleep);
635 if (newp && !error) {
636 /* refuse negative values, and overly 'long time' */
637 if (lsleep < 0 || lsleep > MAXSLP * 1000)
638 return (EINVAL);
639
640 timo = mstohz(lsleep);
641
642 /* if the interval is >0 ms && <1 tick, use 1 tick */
643 if (lsleep != 0 && timo == 0)
644 forkfsleep = 1;
645 else
646 forkfsleep = timo;
647 }
648 return (error);
649 }
650 case KERN_POSIX_THREADS: /* XXX _POSIX_VERSION */
651 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
652 case KERN_POSIX_SEMAPHORES: /* XXX _POSIX_VERSION */
653 #ifdef P1003_1B_SEMAPHORE
654 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
655 #else
656 return (sysctl_rdint(oldp, oldlenp, newp, 0));
657 #endif
658 case KERN_POSIX_BARRIERS: /* XXX _POSIX_VERSION */
659 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
660 case KERN_POSIX_TIMERS: /* XXX _POSIX_VERSION */
661 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
662 case KERN_POSIX_SPIN_LOCKS: /* XXX _POSIX_VERSION */
663 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
664 case KERN_POSIX_READER_WRITER_LOCKS: /* XXX _POSIX_VERSION */
665 return (sysctl_rdint(oldp, oldlenp, newp, 200112));
666 case KERN_DUMP_ON_PANIC:
667 return (sysctl_int(oldp, oldlenp, newp, newlen, &dumponpanic));
668
669 default:
670 return (EOPNOTSUPP);
671 }
672 /* NOTREACHED */
673 }
674
675 /*
676 * hardware related system variables.
677 */
678 int
679 hw_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
680 void *newp, size_t newlen, struct proc *p)
681 {
682
683 /* All sysctl names at this level, except for a few, are terminal. */
684 switch (name[0]) {
685 case HW_DISKSTATS:
686 /* Not terminal. */
687 break;
688 default:
689 if (namelen != 1)
690 return (ENOTDIR); /* overloaded */
691 }
692
693 switch (name[0]) {
694 case HW_MACHINE:
695 return (sysctl_rdstring(oldp, oldlenp, newp, machine));
696 case HW_MACHINE_ARCH:
697 return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
698 case HW_MODEL:
699 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
700 case HW_NCPU:
701 return (sysctl_rdint(oldp, oldlenp, newp, sysctl_ncpus()));
702 case HW_BYTEORDER:
703 return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
704 case HW_PHYSMEM:
705 {
706 u_int rval;
707
708 if ((u_int)physmem > (UINT_MAX / PAGE_SIZE))
709 rval = UINT_MAX;
710 else
711 rval = physmem * PAGE_SIZE;
712 return (sysctl_rdint(oldp, oldlenp, newp, rval));
713 }
714 case HW_PHYSMEM64:
715 return (sysctl_rdquad(oldp, oldlenp, newp,
716 (u_quad_t)physmem * PAGE_SIZE));
717 case HW_USERMEM:
718 {
719 u_int rval;
720
721 if ((u_int)(physmem - uvmexp.wired) > (UINT_MAX / PAGE_SIZE))
722 rval = UINT_MAX;
723 else
724 rval = (physmem - uvmexp.wired) * PAGE_SIZE;
725 return (sysctl_rdint(oldp, oldlenp, newp, rval));
726 }
727 case HW_USERMEM64:
728 return (sysctl_rdquad(oldp, oldlenp, newp,
729 (u_quad_t)(physmem - uvmexp.wired) * PAGE_SIZE));
730 case HW_PAGESIZE:
731 return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
732 case HW_ALIGNBYTES:
733 return (sysctl_rdint(oldp, oldlenp, newp, ALIGNBYTES));
734 case HW_DISKNAMES:
735 return (sysctl_disknames(oldp, oldlenp));
736 case HW_DISKSTATS:
737 return (sysctl_diskstats(name + 1, namelen - 1, oldp, oldlenp));
738 case HW_CNMAGIC: {
739 char magic[CNS_LEN];
740 int error;
741
742 if (oldp)
743 cn_get_magic(magic, CNS_LEN);
744 error = sysctl_string(oldp, oldlenp, newp, newlen,
745 magic, sizeof(magic));
746 if (newp && !error) {
747 error = cn_set_magic(magic);
748 }
749 return (error);
750 }
751 default:
752 return (EOPNOTSUPP);
753 }
754 /* NOTREACHED */
755 }
756
757 #ifdef DEBUG
758 /*
759 * Debugging related system variables.
760 */
761 struct ctldebug /* debug0, */ /* debug1, */ debug2, debug3, debug4;
762 struct ctldebug debug5, debug6, debug7, debug8, debug9;
763 struct ctldebug debug10, debug11, debug12, debug13, debug14;
764 struct ctldebug debug15, debug16, debug17, debug18, debug19;
765 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
766 &debug0, &debug1, &debug2, &debug3, &debug4,
767 &debug5, &debug6, &debug7, &debug8, &debug9,
768 &debug10, &debug11, &debug12, &debug13, &debug14,
769 &debug15, &debug16, &debug17, &debug18, &debug19,
770 };
771
772 int
773 debug_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
774 void *newp, size_t newlen, struct proc *p)
775 {
776 struct ctldebug *cdp;
777
778 /* all sysctl names at this level are name and field */
779 if (namelen != 2)
780 return (ENOTDIR); /* overloaded */
781 if (name[0] >= CTL_DEBUG_MAXID)
782 return (EOPNOTSUPP);
783 cdp = debugvars[name[0]];
784 if (cdp->debugname == 0)
785 return (EOPNOTSUPP);
786 switch (name[1]) {
787 case CTL_DEBUG_NAME:
788 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
789 case CTL_DEBUG_VALUE:
790 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
791 default:
792 return (EOPNOTSUPP);
793 }
794 /* NOTREACHED */
795 }
796 #endif /* DEBUG */
797
798 int
799 proc_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
800 void *newp, size_t newlen, struct proc *p)
801 {
802 struct proc *ptmp = NULL;
803 const struct proclist_desc *pd;
804 int error = 0;
805 struct rlimit alim;
806 struct plimit *newplim;
807 char *tmps = NULL;
808 size_t len, curlen;
809 u_int i;
810
811 if (namelen < 2)
812 return (EINVAL);
813
814 if (name[0] == PROC_CURPROC) {
815 ptmp = p;
816 } else {
817 proclist_lock_read();
818 for (pd = proclists; pd->pd_list != NULL; pd++) {
819 for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
820 ptmp = LIST_NEXT(ptmp, p_list)) {
821 /* Skip embryonic processes. */
822 if (ptmp->p_stat == SIDL)
823 continue;
824 if (ptmp->p_pid == (pid_t)name[0])
825 break;
826 }
827 if (ptmp != NULL)
828 break;
829 }
830 proclist_unlock_read();
831 if (ptmp == NULL)
832 return (ESRCH);
833 if (p->p_ucred->cr_uid != 0) {
834 if (p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
835 p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
836 return (EPERM);
837 if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
838 return (EPERM); /* sgid proc */
839 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
840 if (p->p_ucred->cr_groups[i] ==
841 ptmp->p_cred->p_rgid)
842 break;
843 }
844 if (i == p->p_ucred->cr_ngroups)
845 return (EPERM);
846 }
847 }
848 switch (name[1]) {
849 case PROC_PID_STOPFORK:
850 if (namelen != 2)
851 return (EINVAL);
852 i = ((ptmp->p_flag & P_STOPFORK) != 0);
853 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &i)) != 0)
854 return (error);
855 if (i != 0)
856 ptmp->p_flag |= P_STOPFORK;
857 else
858 ptmp->p_flag &= ~P_STOPFORK;
859 return (0);
860 break;
861
862 case PROC_PID_STOPEXEC:
863 if (namelen != 2)
864 return (EINVAL);
865 i = ((ptmp->p_flag & P_STOPEXEC) != 0);
866 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &i)) != 0)
867 return (error);
868 if (i != 0)
869 ptmp->p_flag |= P_STOPEXEC;
870 else
871 ptmp->p_flag &= ~P_STOPEXEC;
872 return (0);
873 break;
874
875 case PROC_PID_CORENAME:
876 if (namelen != 2)
877 return (EINVAL);
878 /*
879 * Can't use sysctl_string() here because we may malloc a new
880 * area during the process, so we have to do it by hand.
881 */
882 curlen = strlen(ptmp->p_limit->pl_corename) + 1;
883 if (oldlenp && *oldlenp < curlen) {
884 if (!oldp)
885 *oldlenp = curlen;
886 return (ENOMEM);
887 }
888 if (newp) {
889 if (securelevel > 2)
890 return (EPERM);
891 if (newlen > MAXPATHLEN)
892 return (ENAMETOOLONG);
893 tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
894 if (tmps == NULL)
895 return (ENOMEM);
896 error = copyin(newp, tmps, newlen + 1);
897 tmps[newlen] = '\0';
898 if (error)
899 goto cleanup;
900 /* Enforce to be either 'core' for end with '.core' */
901 if (newlen < 4) { /* c.o.r.e */
902 error = EINVAL;
903 goto cleanup;
904 }
905 len = newlen - 4;
906 if (len > 0) {
907 if (tmps[len - 1] != '.' &&
908 tmps[len - 1] != '/') {
909 error = EINVAL;
910 goto cleanup;
911 }
912 }
913 if (strcmp(&tmps[len], "core") != 0) {
914 error = EINVAL;
915 goto cleanup;
916 }
917 }
918 if (oldp && oldlenp) {
919 *oldlenp = curlen;
920 error = copyout(ptmp->p_limit->pl_corename, oldp,
921 curlen);
922 }
923 if (newp && error == 0) {
924 /* if the 2 strings are identical, don't limcopy() */
925 if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
926 error = 0;
927 goto cleanup;
928 }
929 if (ptmp->p_limit->p_refcnt > 1 &&
930 (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
931 newplim = limcopy(ptmp->p_limit);
932 limfree(ptmp->p_limit);
933 ptmp->p_limit = newplim;
934 }
935 if (ptmp->p_limit->pl_corename != defcorename) {
936 free(ptmp->p_limit->pl_corename, M_TEMP);
937 }
938 ptmp->p_limit->pl_corename = tmps;
939 return (0);
940 }
941 cleanup:
942 if (tmps)
943 free(tmps, M_TEMP);
944 return (error);
945 break;
946
947 case PROC_PID_LIMIT:
948 if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
949 return (EINVAL);
950 memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
951 if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
952 error = sysctl_quad(oldp, oldlenp, newp, newlen,
953 &alim.rlim_max);
954 else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
955 error = sysctl_quad(oldp, oldlenp, newp, newlen,
956 &alim.rlim_cur);
957 else
958 error = (EINVAL);
959
960 if (error)
961 return (error);
962
963 if (newp)
964 error = dosetrlimit(ptmp, p->p_cred,
965 name[2] - 1, &alim);
966 return (error);
967 break;
968
969 default:
970 return (EINVAL);
971 break;
972 }
973 /* NOTREACHED */
974 return (EINVAL);
975 }
976
977 int
978 emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
979 void *newp, size_t newlen, struct proc *p)
980 {
981 static struct {
982 const char *name;
983 int type;
984 } emulations[] = CTL_EMUL_NAMES;
985 const struct emul *e;
986 const char *ename;
987 #ifdef LKM
988 extern struct lock exec_lock; /* XXX */
989 int error;
990 #else
991 extern int nexecs_builtin;
992 extern const struct execsw execsw_builtin[];
993 int i;
994 #endif
995
996 /* all sysctl names at this level are name and field */
997 if (namelen < 2)
998 return (ENOTDIR); /* overloaded */
999
1000 if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
1001 return (EOPNOTSUPP);
1002
1003 ename = emulations[name[0]].name;
1004
1005 #ifdef LKM
1006 lockmgr(&exec_lock, LK_SHARED, NULL);
1007 if ((e = emul_search(ename))) {
1008 error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
1009 newp, newlen, p);
1010 } else
1011 error = EOPNOTSUPP;
1012 lockmgr(&exec_lock, LK_RELEASE, NULL);
1013
1014 return (error);
1015 #else
1016 for (i = 0; i < nexecs_builtin; i++) {
1017 e = execsw_builtin[i].es_emul;
1018 /*
1019 * In order to match e.g. e->e_name "irix o32"
1020 * with ename "irix", we limit the comparison
1021 * to the length of ename.
1022 */
1023 if (e == NULL ||
1024 strncmp(ename, e->e_name, strlen(ename)) != 0 ||
1025 e->e_sysctl == NULL)
1026 continue;
1027
1028 return ((*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
1029 newp, newlen, p));
1030 }
1031
1032 return (EOPNOTSUPP);
1033 #endif
1034 }
1035 /*
1036 * Convenience macros.
1037 */
1038
1039 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) \
1040 if (oldlenp) { \
1041 if (!oldp) \
1042 *oldlenp = len; \
1043 else { \
1044 if (*oldlenp < len) \
1045 return (ENOMEM); \
1046 *oldlenp = len; \
1047 error = copyout((caddr_t)valp, oldp, len); \
1048 } \
1049 }
1050
1051 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
1052 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
1053
1054 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len) \
1055 if (newp && newlen != len) \
1056 return (EINVAL);
1057
1058 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ) \
1059 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
1060
1061 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len) \
1062 if (error == 0 && newp) \
1063 error = copyin(newp, valp, len);
1064
1065 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ) \
1066 SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
1067
1068 #define SYSCTL_STRING_CORE(oldp, oldlenp, str) \
1069 if (oldlenp) { \
1070 len = strlen(str) + 1; \
1071 if (!oldp) \
1072 *oldlenp = len; \
1073 else { \
1074 if (*oldlenp < len) { \
1075 err2 = ENOMEM; \
1076 len = *oldlenp; \
1077 } else \
1078 *oldlenp = len; \
1079 error = copyout(str, oldp, len);\
1080 if (error == 0) \
1081 error = err2; \
1082 } \
1083 }
1084
1085 /*
1086 * Validate parameters and get old / set new parameters
1087 * for an integer-valued sysctl function.
1088 */
1089 int
1090 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
1091 {
1092 int error = 0;
1093
1094 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
1095 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
1096 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
1097
1098 return (error);
1099 }
1100
1101
1102 /*
1103 * As above, but read-only.
1104 */
1105 int
1106 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
1107 {
1108 int error = 0;
1109
1110 if (newp)
1111 return (EPERM);
1112
1113 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
1114
1115 return (error);
1116 }
1117
1118 /*
1119 * Validate parameters and get old / set new parameters
1120 * for an quad-valued sysctl function.
1121 */
1122 int
1123 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
1124 quad_t *valp)
1125 {
1126 int error = 0;
1127
1128 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
1129 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
1130 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
1131
1132 return (error);
1133 }
1134
1135 /*
1136 * As above, but read-only.
1137 */
1138 int
1139 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
1140 {
1141 int error = 0;
1142
1143 if (newp)
1144 return (EPERM);
1145
1146 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
1147
1148 return (error);
1149 }
1150
1151 /*
1152 * Validate parameters and get old / set new parameters
1153 * for a string-valued sysctl function.
1154 */
1155 int
1156 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
1157 size_t maxlen)
1158 {
1159 int error = 0, err2 = 0;
1160 size_t len;
1161
1162 if (newp && newlen >= maxlen)
1163 return (EINVAL);
1164
1165 SYSCTL_STRING_CORE(oldp, oldlenp, str);
1166
1167 if (error == 0 && newp) {
1168 error = copyin(newp, str, newlen);
1169 str[newlen] = 0;
1170 }
1171 return (error);
1172 }
1173
1174 /*
1175 * As above, but read-only.
1176 */
1177 int
1178 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
1179 {
1180 int error = 0, err2 = 0;
1181 size_t len;
1182
1183 if (newp)
1184 return (EPERM);
1185
1186 SYSCTL_STRING_CORE(oldp, oldlenp, str);
1187
1188 return (error);
1189 }
1190
1191 /*
1192 * Validate parameters and get old / set new parameters
1193 * for a structure oriented sysctl function.
1194 */
1195 int
1196 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
1197 size_t len)
1198 {
1199 int error = 0;
1200
1201 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
1202 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1203 SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
1204
1205 return (error);
1206 }
1207
1208 /*
1209 * Validate parameters and get old parameters
1210 * for a structure oriented sysctl function.
1211 */
1212 int
1213 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1214 size_t len)
1215 {
1216 int error = 0;
1217
1218 if (newp)
1219 return (EPERM);
1220
1221 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1222
1223 return (error);
1224 }
1225
1226 /*
1227 * As above, but can return a truncated result.
1228 */
1229 int
1230 sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1231 size_t len)
1232 {
1233 int error = 0;
1234
1235 if (newp)
1236 return (EPERM);
1237
1238 len = min(*oldlenp, len);
1239 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1240
1241 return (error);
1242 }
1243
1244 /*
1245 * Get file structures.
1246 */
1247 static int
1248 sysctl_file(void *vwhere, size_t *sizep)
1249 {
1250 int error;
1251 size_t buflen;
1252 struct file *fp;
1253 char *start, *where;
1254
1255 start = where = vwhere;
1256 buflen = *sizep;
1257 if (where == NULL) {
1258 /*
1259 * overestimate by 10 files
1260 */
1261 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
1262 return (0);
1263 }
1264
1265 /*
1266 * first copyout filehead
1267 */
1268 if (buflen < sizeof(filehead)) {
1269 *sizep = 0;
1270 return (0);
1271 }
1272 error = copyout((caddr_t)&filehead, where, sizeof(filehead));
1273 if (error)
1274 return (error);
1275 buflen -= sizeof(filehead);
1276 where += sizeof(filehead);
1277
1278 /*
1279 * followed by an array of file structures
1280 */
1281 LIST_FOREACH(fp, &filehead, f_list) {
1282 if (buflen < sizeof(struct file)) {
1283 *sizep = where - start;
1284 return (ENOMEM);
1285 }
1286 error = copyout((caddr_t)fp, where, sizeof(struct file));
1287 if (error)
1288 return (error);
1289 buflen -= sizeof(struct file);
1290 where += sizeof(struct file);
1291 }
1292 *sizep = where - start;
1293 return (0);
1294 }
1295
1296 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
1297 #define FILL_PERM(src, dst) do { \
1298 (dst)._key = (src)._key; \
1299 (dst).uid = (src).uid; \
1300 (dst).gid = (src).gid; \
1301 (dst).cuid = (src).cuid; \
1302 (dst).cgid = (src).cgid; \
1303 (dst).mode = (src).mode; \
1304 (dst)._seq = (src)._seq; \
1305 } while (/*CONSTCOND*/ 0);
1306 #define FILL_MSG(src, dst) do { \
1307 FILL_PERM((src).msg_perm, (dst).msg_perm); \
1308 (dst).msg_qnum = (src).msg_qnum; \
1309 (dst).msg_qbytes = (src).msg_qbytes; \
1310 (dst)._msg_cbytes = (src)._msg_cbytes; \
1311 (dst).msg_lspid = (src).msg_lspid; \
1312 (dst).msg_lrpid = (src).msg_lrpid; \
1313 (dst).msg_stime = (src).msg_stime; \
1314 (dst).msg_rtime = (src).msg_rtime; \
1315 (dst).msg_ctime = (src).msg_ctime; \
1316 } while (/*CONSTCOND*/ 0)
1317 #define FILL_SEM(src, dst) do { \
1318 FILL_PERM((src).sem_perm, (dst).sem_perm); \
1319 (dst).sem_nsems = (src).sem_nsems; \
1320 (dst).sem_otime = (src).sem_otime; \
1321 (dst).sem_ctime = (src).sem_ctime; \
1322 } while (/*CONSTCOND*/ 0)
1323 #define FILL_SHM(src, dst) do { \
1324 FILL_PERM((src).shm_perm, (dst).shm_perm); \
1325 (dst).shm_segsz = (src).shm_segsz; \
1326 (dst).shm_lpid = (src).shm_lpid; \
1327 (dst).shm_cpid = (src).shm_cpid; \
1328 (dst).shm_atime = (src).shm_atime; \
1329 (dst).shm_dtime = (src).shm_dtime; \
1330 (dst).shm_ctime = (src).shm_ctime; \
1331 (dst).shm_nattch = (src).shm_nattch; \
1332 } while (/*CONSTCOND*/ 0)
1333
1334 static int
1335 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
1336 {
1337 #ifdef SYSVMSG
1338 struct msg_sysctl_info *msgsi = NULL;
1339 #endif
1340 #ifdef SYSVSEM
1341 struct sem_sysctl_info *semsi = NULL;
1342 #endif
1343 #ifdef SYSVSHM
1344 struct shm_sysctl_info *shmsi = NULL;
1345 #endif
1346 size_t infosize, dssize, tsize, buflen;
1347 void *buf = NULL;
1348 char *start;
1349 int32_t nds;
1350 int i, error, ret;
1351
1352 if (namelen != 1)
1353 return (EINVAL);
1354
1355 start = where;
1356 buflen = *sizep;
1357
1358 switch (*name) {
1359 case KERN_SYSVIPC_MSG_INFO:
1360 #ifdef SYSVMSG
1361 infosize = sizeof(msgsi->msginfo);
1362 nds = msginfo.msgmni;
1363 dssize = sizeof(msgsi->msgids[0]);
1364 break;
1365 #else
1366 return (EINVAL);
1367 #endif
1368 case KERN_SYSVIPC_SEM_INFO:
1369 #ifdef SYSVSEM
1370 infosize = sizeof(semsi->seminfo);
1371 nds = seminfo.semmni;
1372 dssize = sizeof(semsi->semids[0]);
1373 break;
1374 #else
1375 return (EINVAL);
1376 #endif
1377 case KERN_SYSVIPC_SHM_INFO:
1378 #ifdef SYSVSHM
1379 infosize = sizeof(shmsi->shminfo);
1380 nds = shminfo.shmmni;
1381 dssize = sizeof(shmsi->shmids[0]);
1382 break;
1383 #else
1384 return (EINVAL);
1385 #endif
1386 default:
1387 return (EINVAL);
1388 }
1389 /*
1390 * Round infosize to 64 bit boundary if requesting more than just
1391 * the info structure or getting the total data size.
1392 */
1393 if (where == NULL || *sizep > infosize)
1394 infosize = ((infosize + 7) / 8) * 8;
1395 tsize = infosize + nds * dssize;
1396
1397 /* Return just the total size required. */
1398 if (where == NULL) {
1399 *sizep = tsize;
1400 return (0);
1401 }
1402
1403 /* Not enough room for even the info struct. */
1404 if (buflen < infosize) {
1405 *sizep = 0;
1406 return (ENOMEM);
1407 }
1408 buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
1409 memset(buf, 0, min(tsize, buflen));
1410
1411 switch (*name) {
1412 #ifdef SYSVMSG
1413 case KERN_SYSVIPC_MSG_INFO:
1414 msgsi = (struct msg_sysctl_info *)buf;
1415 msgsi->msginfo = msginfo;
1416 break;
1417 #endif
1418 #ifdef SYSVSEM
1419 case KERN_SYSVIPC_SEM_INFO:
1420 semsi = (struct sem_sysctl_info *)buf;
1421 semsi->seminfo = seminfo;
1422 break;
1423 #endif
1424 #ifdef SYSVSHM
1425 case KERN_SYSVIPC_SHM_INFO:
1426 shmsi = (struct shm_sysctl_info *)buf;
1427 shmsi->shminfo = shminfo;
1428 break;
1429 #endif
1430 }
1431 buflen -= infosize;
1432
1433 ret = 0;
1434 if (buflen > 0) {
1435 /* Fill in the IPC data structures. */
1436 for (i = 0; i < nds; i++) {
1437 if (buflen < dssize) {
1438 ret = ENOMEM;
1439 break;
1440 }
1441 switch (*name) {
1442 #ifdef SYSVMSG
1443 case KERN_SYSVIPC_MSG_INFO:
1444 FILL_MSG(msqids[i], msgsi->msgids[i]);
1445 break;
1446 #endif
1447 #ifdef SYSVSEM
1448 case KERN_SYSVIPC_SEM_INFO:
1449 FILL_SEM(sema[i], semsi->semids[i]);
1450 break;
1451 #endif
1452 #ifdef SYSVSHM
1453 case KERN_SYSVIPC_SHM_INFO:
1454 FILL_SHM(shmsegs[i], shmsi->shmids[i]);
1455 break;
1456 #endif
1457 }
1458 buflen -= dssize;
1459 }
1460 }
1461 *sizep -= buflen;
1462 error = copyout(buf, start, *sizep);
1463 /* If copyout succeeded, use return code set earlier. */
1464 if (error == 0)
1465 error = ret;
1466 if (buf)
1467 free(buf, M_TEMP);
1468 return (error);
1469 }
1470 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
1471
1472 static int
1473 sysctl_msgbuf(void *vwhere, size_t *sizep)
1474 {
1475 char *where = vwhere;
1476 size_t len, maxlen = *sizep;
1477 long beg, end;
1478 int error;
1479
1480 /*
1481 * deal with cases where the message buffer has
1482 * become corrupted.
1483 */
1484 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1485 msgbufenabled = 0;
1486 return (ENXIO);
1487 }
1488
1489 if (where == NULL) {
1490 /* always return full buffer size */
1491 *sizep = msgbufp->msg_bufs;
1492 return (0);
1493 }
1494
1495 error = 0;
1496 maxlen = min(msgbufp->msg_bufs, maxlen);
1497
1498 /*
1499 * First, copy from the write pointer to the end of
1500 * message buffer.
1501 */
1502 beg = msgbufp->msg_bufx;
1503 end = msgbufp->msg_bufs;
1504 while (maxlen > 0) {
1505 len = min(end - beg, maxlen);
1506 if (len == 0)
1507 break;
1508 error = copyout(&msgbufp->msg_bufc[beg], where, len);
1509 if (error)
1510 break;
1511 where += len;
1512 maxlen -= len;
1513
1514 /*
1515 * ... then, copy from the beginning of message buffer to
1516 * the write pointer.
1517 */
1518 beg = 0;
1519 end = msgbufp->msg_bufx;
1520 }
1521 return (error);
1522 }
1523
1524 /*
1525 * try over estimating by 5 procs
1526 */
1527 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
1528
1529 static int
1530 sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
1531 {
1532 struct eproc eproc;
1533 struct kinfo_proc2 kproc2;
1534 struct kinfo_proc *dp;
1535 struct proc *p;
1536 const struct proclist_desc *pd;
1537 char *where, *dp2;
1538 int type, op, arg;
1539 u_int elem_size, elem_count;
1540 size_t buflen, needed;
1541 int error;
1542
1543 dp = vwhere;
1544 dp2 = where = vwhere;
1545 buflen = where != NULL ? *sizep : 0;
1546 error = 0;
1547 needed = 0;
1548 type = name[0];
1549
1550 if (type == KERN_PROC) {
1551 if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
1552 return (EINVAL);
1553 op = name[1];
1554 if (op != KERN_PROC_ALL)
1555 arg = name[2];
1556 else
1557 arg = 0; /* Quell compiler warning */
1558 elem_size = elem_count = 0; /* Ditto */
1559 } else {
1560 if (namelen != 5)
1561 return (EINVAL);
1562 op = name[1];
1563 arg = name[2];
1564 elem_size = name[3];
1565 elem_count = name[4];
1566 }
1567
1568 proclist_lock_read();
1569
1570 pd = proclists;
1571 again:
1572 for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
1573 /*
1574 * Skip embryonic processes.
1575 */
1576 if (p->p_stat == SIDL)
1577 continue;
1578 /*
1579 * TODO - make more efficient (see notes below).
1580 * do by session.
1581 */
1582 switch (op) {
1583
1584 case KERN_PROC_PID:
1585 /* could do this with just a lookup */
1586 if (p->p_pid != (pid_t)arg)
1587 continue;
1588 break;
1589
1590 case KERN_PROC_PGRP:
1591 /* could do this by traversing pgrp */
1592 if (p->p_pgrp->pg_id != (pid_t)arg)
1593 continue;
1594 break;
1595
1596 case KERN_PROC_SESSION:
1597 if (p->p_session->s_sid != (pid_t)arg)
1598 continue;
1599 break;
1600
1601 case KERN_PROC_TTY:
1602 if (arg == (int) KERN_PROC_TTY_REVOKE) {
1603 if ((p->p_flag & P_CONTROLT) == 0 ||
1604 p->p_session->s_ttyp == NULL ||
1605 p->p_session->s_ttyvp != NULL)
1606 continue;
1607 } else if ((p->p_flag & P_CONTROLT) == 0 ||
1608 p->p_session->s_ttyp == NULL) {
1609 if ((dev_t)arg != KERN_PROC_TTY_NODEV)
1610 continue;
1611 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
1612 continue;
1613 break;
1614
1615 case KERN_PROC_UID:
1616 if (p->p_ucred->cr_uid != (uid_t)arg)
1617 continue;
1618 break;
1619
1620 case KERN_PROC_RUID:
1621 if (p->p_cred->p_ruid != (uid_t)arg)
1622 continue;
1623 break;
1624
1625 case KERN_PROC_GID:
1626 if (p->p_ucred->cr_gid != (uid_t)arg)
1627 continue;
1628 break;
1629
1630 case KERN_PROC_RGID:
1631 if (p->p_cred->p_rgid != (uid_t)arg)
1632 continue;
1633 break;
1634
1635 case KERN_PROC_ALL:
1636 /* allow everything */
1637 break;
1638
1639 default:
1640 error = EINVAL;
1641 goto cleanup;
1642 }
1643 if (type == KERN_PROC) {
1644 if (buflen >= sizeof(struct kinfo_proc)) {
1645 fill_eproc(p, &eproc);
1646 error = copyout((caddr_t)p, &dp->kp_proc,
1647 sizeof(struct proc));
1648 if (error)
1649 goto cleanup;
1650 error = copyout((caddr_t)&eproc, &dp->kp_eproc,
1651 sizeof(eproc));
1652 if (error)
1653 goto cleanup;
1654 dp++;
1655 buflen -= sizeof(struct kinfo_proc);
1656 }
1657 needed += sizeof(struct kinfo_proc);
1658 } else { /* KERN_PROC2 */
1659 if (buflen >= elem_size && elem_count > 0) {
1660 fill_kproc2(p, &kproc2);
1661 /*
1662 * Copy out elem_size, but not larger than
1663 * the size of a struct kinfo_proc2.
1664 */
1665 error = copyout(&kproc2, dp2,
1666 min(sizeof(kproc2), elem_size));
1667 if (error)
1668 goto cleanup;
1669 dp2 += elem_size;
1670 buflen -= elem_size;
1671 elem_count--;
1672 }
1673 needed += elem_size;
1674 }
1675 }
1676 pd++;
1677 if (pd->pd_list != NULL)
1678 goto again;
1679 proclist_unlock_read();
1680
1681 if (where != NULL) {
1682 if (type == KERN_PROC)
1683 *sizep = (caddr_t)dp - where;
1684 else
1685 *sizep = dp2 - where;
1686 if (needed > *sizep)
1687 return (ENOMEM);
1688 } else {
1689 needed += KERN_PROCSLOP;
1690 *sizep = needed;
1691 }
1692 return (0);
1693 cleanup:
1694 proclist_unlock_read();
1695 return (error);
1696 }
1697
1698
1699 /*
1700 * try over estimating by 5 LWPs
1701 */
1702 #define KERN_LWPSLOP (5 * sizeof(struct kinfo_lwp))
1703
1704 static int
1705 sysctl_dolwp(int *name, u_int namelen, void *vwhere, size_t *sizep)
1706 {
1707 struct kinfo_lwp klwp;
1708 struct proc *p;
1709 struct lwp *l;
1710 char *where, *dp;
1711 int type, pid, elem_size, elem_count;
1712 int buflen, needed, error;
1713
1714 dp = where = vwhere;
1715 buflen = where != NULL ? *sizep : 0;
1716 error = needed = 0;
1717 type = name[0];
1718
1719 if (namelen != 4)
1720 return (EINVAL);
1721 pid = name[1];
1722 elem_size = name[2];
1723 elem_count = name[3];
1724
1725 p = pfind(pid);
1726 if (p == NULL)
1727 return (ESRCH);
1728 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1729 if (buflen >= elem_size && elem_count > 0) {
1730 fill_lwp(l, &klwp);
1731 /*
1732 * Copy out elem_size, but not larger than
1733 * the size of a struct kinfo_proc2.
1734 */
1735 error = copyout(&klwp, dp,
1736 min(sizeof(klwp), elem_size));
1737 if (error)
1738 goto cleanup;
1739 dp += elem_size;
1740 buflen -= elem_size;
1741 elem_count--;
1742 }
1743 needed += elem_size;
1744 }
1745
1746 if (where != NULL) {
1747 *sizep = dp - where;
1748 if (needed > *sizep)
1749 return (ENOMEM);
1750 } else {
1751 needed += KERN_PROCSLOP;
1752 *sizep = needed;
1753 }
1754 return (0);
1755 cleanup:
1756 return (error);
1757 }
1758
1759 /*
1760 * Fill in an eproc structure for the specified process.
1761 */
1762 void
1763 fill_eproc(struct proc *p, struct eproc *ep)
1764 {
1765 struct tty *tp;
1766 struct lwp *l;
1767
1768 ep->e_paddr = p;
1769 ep->e_sess = p->p_session;
1770 ep->e_pcred = *p->p_cred;
1771 ep->e_ucred = *p->p_ucred;
1772 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1773 ep->e_vm.vm_rssize = 0;
1774 ep->e_vm.vm_tsize = 0;
1775 ep->e_vm.vm_dsize = 0;
1776 ep->e_vm.vm_ssize = 0;
1777 /* ep->e_vm.vm_pmap = XXX; */
1778 } else {
1779 struct vmspace *vm = p->p_vmspace;
1780
1781 ep->e_vm.vm_rssize = vm_resident_count(vm);
1782 ep->e_vm.vm_tsize = vm->vm_tsize;
1783 ep->e_vm.vm_dsize = vm->vm_dsize;
1784 ep->e_vm.vm_ssize = vm->vm_ssize;
1785
1786 /* Pick a "representative" LWP */
1787 l = proc_representative_lwp(p);
1788
1789 if (l->l_wmesg)
1790 strncpy(ep->e_wmesg, l->l_wmesg, WMESGLEN);
1791 }
1792 if (p->p_pptr)
1793 ep->e_ppid = p->p_pptr->p_pid;
1794 else
1795 ep->e_ppid = 0;
1796 ep->e_pgid = p->p_pgrp->pg_id;
1797 ep->e_sid = ep->e_sess->s_sid;
1798 ep->e_jobc = p->p_pgrp->pg_jobc;
1799 if ((p->p_flag & P_CONTROLT) &&
1800 (tp = ep->e_sess->s_ttyp)) {
1801 ep->e_tdev = tp->t_dev;
1802 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
1803 ep->e_tsess = tp->t_session;
1804 } else
1805 ep->e_tdev = NODEV;
1806
1807 ep->e_xsize = ep->e_xrssize = 0;
1808 ep->e_xccount = ep->e_xswrss = 0;
1809 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
1810 if (SESS_LEADER(p))
1811 ep->e_flag |= EPROC_SLEADER;
1812 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
1813 }
1814
1815 /*
1816 * Fill in an eproc structure for the specified process.
1817 */
1818 static void
1819 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
1820 {
1821 struct tty *tp;
1822 struct lwp *l;
1823 struct timeval ut, st;
1824
1825 memset(ki, 0, sizeof(*ki));
1826
1827 ki->p_paddr = PTRTOINT64(p);
1828 ki->p_fd = PTRTOINT64(p->p_fd);
1829 ki->p_cwdi = PTRTOINT64(p->p_cwdi);
1830 ki->p_stats = PTRTOINT64(p->p_stats);
1831 ki->p_limit = PTRTOINT64(p->p_limit);
1832 ki->p_vmspace = PTRTOINT64(p->p_vmspace);
1833 ki->p_sigacts = PTRTOINT64(p->p_sigacts);
1834 ki->p_sess = PTRTOINT64(p->p_session);
1835 ki->p_tsess = 0; /* may be changed if controlling tty below */
1836 ki->p_ru = PTRTOINT64(p->p_ru);
1837
1838 ki->p_eflag = 0;
1839 ki->p_exitsig = p->p_exitsig;
1840 ki->p_flag = p->p_flag;
1841
1842 ki->p_pid = p->p_pid;
1843 if (p->p_pptr)
1844 ki->p_ppid = p->p_pptr->p_pid;
1845 else
1846 ki->p_ppid = 0;
1847 ki->p_sid = p->p_session->s_sid;
1848 ki->p__pgid = p->p_pgrp->pg_id;
1849
1850 ki->p_tpgid = NO_PGID; /* may be changed if controlling tty below */
1851
1852 ki->p_uid = p->p_ucred->cr_uid;
1853 ki->p_ruid = p->p_cred->p_ruid;
1854 ki->p_gid = p->p_ucred->cr_gid;
1855 ki->p_rgid = p->p_cred->p_rgid;
1856 ki->p_svuid = p->p_cred->p_svuid;
1857 ki->p_svgid = p->p_cred->p_svgid;
1858
1859 memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
1860 min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
1861 ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
1862
1863 ki->p_jobc = p->p_pgrp->pg_jobc;
1864 if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
1865 ki->p_tdev = tp->t_dev;
1866 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
1867 ki->p_tsess = PTRTOINT64(tp->t_session);
1868 } else {
1869 ki->p_tdev = NODEV;
1870 }
1871
1872 ki->p_estcpu = p->p_estcpu;
1873 ki->p_rtime_sec = p->p_rtime.tv_sec;
1874 ki->p_rtime_usec = p->p_rtime.tv_usec;
1875 ki->p_cpticks = p->p_cpticks;
1876 ki->p_pctcpu = p->p_pctcpu;
1877
1878 ki->p_uticks = p->p_uticks;
1879 ki->p_sticks = p->p_sticks;
1880 ki->p_iticks = p->p_iticks;
1881
1882 ki->p_tracep = PTRTOINT64(p->p_tracep);
1883 ki->p_traceflag = p->p_traceflag;
1884
1885
1886 memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
1887 memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
1888 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
1889 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
1890
1891 ki->p_stat = p->p_stat; /* Will likely be overridden by LWP status */
1892 ki->p_realstat = p->p_stat;
1893 ki->p_nice = p->p_nice;
1894
1895 ki->p_xstat = p->p_xstat;
1896 ki->p_acflag = p->p_acflag;
1897
1898 strncpy(ki->p_comm, p->p_comm,
1899 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
1900
1901 strncpy(ki->p_login, p->p_session->s_login,
1902 min(sizeof ki->p_login - 1, sizeof p->p_session->s_login));
1903
1904 ki->p_nlwps = p->p_nlwps;
1905 ki->p_nrlwps = p->p_nrlwps;
1906 ki->p_realflag = p->p_flag;
1907
1908 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1909 ki->p_vm_rssize = 0;
1910 ki->p_vm_tsize = 0;
1911 ki->p_vm_dsize = 0;
1912 ki->p_vm_ssize = 0;
1913 l = NULL;
1914 } else {
1915 struct vmspace *vm = p->p_vmspace;
1916
1917 ki->p_vm_rssize = vm_resident_count(vm);
1918 ki->p_vm_tsize = vm->vm_tsize;
1919 ki->p_vm_dsize = vm->vm_dsize;
1920 ki->p_vm_ssize = vm->vm_ssize;
1921
1922 /* Pick a "representative" LWP */
1923 l = proc_representative_lwp(p);
1924 ki->p_forw = PTRTOINT64(l->l_forw);
1925 ki->p_back = PTRTOINT64(l->l_back);
1926 ki->p_addr = PTRTOINT64(l->l_addr);
1927 ki->p_stat = l->l_stat;
1928 ki->p_flag |= l->l_flag;
1929 ki->p_swtime = l->l_swtime;
1930 ki->p_slptime = l->l_slptime;
1931 if (l->l_stat == LSONPROC) {
1932 KDASSERT(l->l_cpu != NULL);
1933 ki->p_schedflags = l->l_cpu->ci_schedstate.spc_flags;
1934 } else
1935 ki->p_schedflags = 0;
1936 ki->p_holdcnt = l->l_holdcnt;
1937 ki->p_priority = l->l_priority;
1938 ki->p_usrpri = l->l_usrpri;
1939 if (l->l_wmesg)
1940 strncpy(ki->p_wmesg, l->l_wmesg, sizeof(ki->p_wmesg));
1941 ki->p_wchan = PTRTOINT64(l->l_wchan);
1942
1943 }
1944
1945 if (p->p_session->s_ttyvp)
1946 ki->p_eflag |= EPROC_CTTY;
1947 if (SESS_LEADER(p))
1948 ki->p_eflag |= EPROC_SLEADER;
1949
1950 /* XXX Is this double check necessary? */
1951 if (P_ZOMBIE(p)) {
1952 ki->p_uvalid = 0;
1953 } else {
1954 ki->p_uvalid = 1;
1955
1956 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
1957 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
1958
1959 calcru(p, &ut, &st, 0);
1960 ki->p_uutime_sec = ut.tv_sec;
1961 ki->p_uutime_usec = ut.tv_usec;
1962 ki->p_ustime_sec = st.tv_sec;
1963 ki->p_ustime_usec = st.tv_usec;
1964
1965 ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
1966 ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
1967 ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
1968 ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
1969 ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
1970 ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
1971 ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
1972 ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
1973 ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
1974 ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
1975 ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
1976 ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
1977 ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
1978 ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
1979
1980 timeradd(&p->p_stats->p_cru.ru_utime,
1981 &p->p_stats->p_cru.ru_stime, &ut);
1982 ki->p_uctime_sec = ut.tv_sec;
1983 ki->p_uctime_usec = ut.tv_usec;
1984 }
1985 #ifdef MULTIPROCESSOR
1986 if (l && l->l_cpu != NULL)
1987 ki->p_cpuid = l->l_cpu->ci_cpuid;
1988 else
1989 #endif
1990 ki->p_cpuid = KI_NOCPU;
1991
1992 }
1993
1994 /*
1995 * Fill in a kinfo_lwp structure for the specified lwp.
1996 */
1997 static void
1998 fill_lwp(struct lwp *l, struct kinfo_lwp *kl)
1999 {
2000
2001 kl->l_forw = PTRTOINT64(l->l_forw);
2002 kl->l_back = PTRTOINT64(l->l_back);
2003 kl->l_laddr = PTRTOINT64(l);
2004 kl->l_addr = PTRTOINT64(l->l_addr);
2005 kl->l_stat = l->l_stat;
2006 kl->l_lid = l->l_lid;
2007 kl->l_flag = l->l_flag;
2008
2009 kl->l_swtime = l->l_swtime;
2010 kl->l_slptime = l->l_slptime;
2011 if (l->l_stat == LSONPROC) {
2012 KDASSERT(l->l_cpu != NULL);
2013 kl->l_schedflags = l->l_cpu->ci_schedstate.spc_flags;
2014 } else
2015 kl->l_schedflags = 0;
2016 kl->l_holdcnt = l->l_holdcnt;
2017 kl->l_priority = l->l_priority;
2018 kl->l_usrpri = l->l_usrpri;
2019 if (l->l_wmesg)
2020 strncpy(kl->l_wmesg, l->l_wmesg, sizeof(kl->l_wmesg));
2021 kl->l_wchan = PTRTOINT64(l->l_wchan);
2022 #ifdef MULTIPROCESSOR
2023 if (l->l_cpu != NULL)
2024 kl->l_cpuid = l->l_cpu->ci_cpuid;
2025 else
2026 #endif
2027 kl->l_cpuid = KI_NOCPU;
2028 }
2029
2030 int
2031 sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
2032 struct proc *up)
2033 {
2034 struct ps_strings pss;
2035 struct proc *p;
2036 size_t len, upper_bound, xlen, i;
2037 struct uio auio;
2038 struct iovec aiov;
2039 vaddr_t argv;
2040 pid_t pid;
2041 int nargv, type, error;
2042 char *arg;
2043 char *tmp;
2044
2045 if (namelen != 2)
2046 return (EINVAL);
2047 pid = name[0];
2048 type = name[1];
2049
2050 switch (type) {
2051 case KERN_PROC_ARGV:
2052 case KERN_PROC_NARGV:
2053 case KERN_PROC_ENV:
2054 case KERN_PROC_NENV:
2055 /* ok */
2056 break;
2057 default:
2058 return (EINVAL);
2059 }
2060
2061 /* check pid */
2062 if ((p = pfind(pid)) == NULL)
2063 return (EINVAL);
2064
2065 /* only root or same user change look at the environment */
2066 if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
2067 if (up->p_ucred->cr_uid != 0) {
2068 if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
2069 up->p_cred->p_ruid != p->p_cred->p_svuid)
2070 return (EPERM);
2071 }
2072 }
2073
2074 if (sizep != NULL && where == NULL) {
2075 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
2076 *sizep = sizeof (int);
2077 else
2078 *sizep = ARG_MAX; /* XXX XXX XXX */
2079 return (0);
2080 }
2081 if (where == NULL || sizep == NULL)
2082 return (EINVAL);
2083
2084 /*
2085 * Zombies don't have a stack, so we can't read their psstrings.
2086 * System processes also don't have a user stack.
2087 */
2088 if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
2089 return (EINVAL);
2090
2091 /*
2092 * Lock the process down in memory.
2093 */
2094 /* XXXCDC: how should locking work here? */
2095 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
2096 return (EFAULT);
2097
2098 p->p_vmspace->vm_refcnt++; /* XXX */
2099
2100 /*
2101 * Allocate a temporary buffer to hold the arguments.
2102 */
2103 arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
2104
2105 /*
2106 * Read in the ps_strings structure.
2107 */
2108 aiov.iov_base = &pss;
2109 aiov.iov_len = sizeof(pss);
2110 auio.uio_iov = &aiov;
2111 auio.uio_iovcnt = 1;
2112 auio.uio_offset = (vaddr_t)p->p_psstr;
2113 auio.uio_resid = sizeof(pss);
2114 auio.uio_segflg = UIO_SYSSPACE;
2115 auio.uio_rw = UIO_READ;
2116 auio.uio_procp = NULL;
2117 error = uvm_io(&p->p_vmspace->vm_map, &auio);
2118 if (error)
2119 goto done;
2120
2121 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
2122 memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
2123 else
2124 memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
2125 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
2126 error = copyout(&nargv, where, sizeof(nargv));
2127 *sizep = sizeof(nargv);
2128 goto done;
2129 }
2130 /*
2131 * Now read the address of the argument vector.
2132 */
2133 switch (type) {
2134 case KERN_PROC_ARGV:
2135 /* XXX compat32 stuff here */
2136 memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
2137 break;
2138 case KERN_PROC_ENV:
2139 memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
2140 break;
2141 default:
2142 return (EINVAL);
2143 }
2144 auio.uio_offset = (off_t)(long)tmp;
2145 aiov.iov_base = &argv;
2146 aiov.iov_len = sizeof(argv);
2147 auio.uio_iov = &aiov;
2148 auio.uio_iovcnt = 1;
2149 auio.uio_resid = sizeof(argv);
2150 auio.uio_segflg = UIO_SYSSPACE;
2151 auio.uio_rw = UIO_READ;
2152 auio.uio_procp = NULL;
2153 error = uvm_io(&p->p_vmspace->vm_map, &auio);
2154 if (error)
2155 goto done;
2156
2157 /*
2158 * Now copy in the actual argument vector, one page at a time,
2159 * since we don't know how long the vector is (though, we do
2160 * know how many NUL-terminated strings are in the vector).
2161 */
2162 len = 0;
2163 upper_bound = *sizep;
2164 for (; nargv != 0 && len < upper_bound; len += xlen) {
2165 aiov.iov_base = arg;
2166 aiov.iov_len = PAGE_SIZE;
2167 auio.uio_iov = &aiov;
2168 auio.uio_iovcnt = 1;
2169 auio.uio_offset = argv + len;
2170 xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
2171 auio.uio_resid = xlen;
2172 auio.uio_segflg = UIO_SYSSPACE;
2173 auio.uio_rw = UIO_READ;
2174 auio.uio_procp = NULL;
2175 error = uvm_io(&p->p_vmspace->vm_map, &auio);
2176 if (error)
2177 goto done;
2178
2179 for (i = 0; i < xlen && nargv != 0; i++) {
2180 if (arg[i] == '\0')
2181 nargv--; /* one full string */
2182 }
2183
2184 /*
2185 * Make sure we don't copyout past the end of the user's
2186 * buffer.
2187 */
2188 if (len + i > upper_bound)
2189 i = upper_bound - len;
2190
2191 error = copyout(arg, (char *)where + len, i);
2192 if (error)
2193 break;
2194
2195 if (nargv == 0) {
2196 len += i;
2197 break;
2198 }
2199 }
2200 *sizep = len;
2201
2202 done:
2203 uvmspace_free(p->p_vmspace);
2204
2205 free(arg, M_TEMP);
2206 return (error);
2207 }
2208
2209 #if NPTY > 0
2210 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
2211
2212 /*
2213 * Validate parameters and get old / set new parameters
2214 * for pty sysctl function.
2215 */
2216 static int
2217 sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
2218 {
2219 int error = 0;
2220 int oldmax = 0, newmax = 0;
2221
2222 /* get current value of maxptys */
2223 oldmax = pty_maxptys(0, 0);
2224
2225 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
2226
2227 if (!error && newp) {
2228 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
2229 SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
2230
2231 if (newmax != pty_maxptys(newmax, (newp != NULL)))
2232 return (EINVAL);
2233
2234 }
2235
2236 return (error);
2237 }
2238 #endif /* NPTY > 0 */
2239
2240 static int
2241 sysctl_dotkstat(int *name, u_int namelen, void *where, size_t *sizep,
2242 void *newp)
2243 {
2244
2245 /* all sysctl names at this level are terminal */
2246 if (namelen != 1)
2247 return (ENOTDIR); /* overloaded */
2248
2249 switch (name[0]) {
2250 case KERN_TKSTAT_NIN:
2251 return (sysctl_rdquad(where, sizep, newp, tk_nin));
2252 case KERN_TKSTAT_NOUT:
2253 return (sysctl_rdquad(where, sizep, newp, tk_nout));
2254 case KERN_TKSTAT_CANCC:
2255 return (sysctl_rdquad(where, sizep, newp, tk_cancc));
2256 case KERN_TKSTAT_RAWCC:
2257 return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
2258 default:
2259 return (EOPNOTSUPP);
2260 }
2261 }
2262