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