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