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