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