kern_sysctl.c revision 1.108.4.4 1 /* $NetBSD: kern_sysctl.c,v 1.108.4.4 2003/10/02 08:59:10 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.4 2003/10/02 08:59:10 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
155 /*
156 * all top-level sysctl names are non-terminal
157 */
158 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
159 return (EINVAL);
160 error = copyin(SCARG(uap, name), &name,
161 SCARG(uap, namelen) * sizeof(int));
162 if (error)
163 return (error);
164
165 /*
166 * For all but CTL_PROC, must be root to change a value.
167 * For CTL_PROC, must be root, or owner of the proc (and not suid),
168 * this is checked in proc_sysctl() (once we know the targer proc).
169 */
170 if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
171 (error = suser(p->p_ucred, &p->p_acflag)))
172 return error;
173
174 switch (name[0]) {
175 case CTL_KERN:
176 fn = kern_sysctl;
177 break;
178 case CTL_HW:
179 fn = hw_sysctl;
180 break;
181 case CTL_VM:
182 fn = uvm_sysctl;
183 break;
184 case CTL_NET:
185 fn = net_sysctl;
186 break;
187 case CTL_VFS:
188 fn = vfs_sysctl;
189 break;
190 case CTL_MACHDEP:
191 fn = cpu_sysctl;
192 break;
193 #ifdef DEBUG
194 case CTL_DEBUG:
195 fn = debug_sysctl;
196 break;
197 #endif
198 #ifdef DDB
199 case CTL_DDB:
200 fn = ddb_sysctl;
201 break;
202 #endif
203 case CTL_PROC:
204 fn = proc_sysctl;
205 break;
206
207 case CTL_EMUL:
208 fn = emul_sysctl;
209 break;
210 default:
211 return (EOPNOTSUPP);
212 }
213
214 /*
215 * XXX Hey, we wire `old', but what about `new'?
216 */
217
218 if (SCARG(uap, oldlenp)) {
219 if ((error = copyin(SCARG(uap, oldlenp), &oldlen,
220 sizeof(oldlen))))
221 return (error);
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 &oldlen, 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 if (name[0] < 0 || name[0] >= CTL_DEBUG_MAXID)
661 return (EOPNOTSUPP);
662 cdp = debugvars[name[0]];
663 if (cdp->debugname == 0)
664 return (EOPNOTSUPP);
665 switch (name[1]) {
666 case CTL_DEBUG_NAME:
667 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
668 case CTL_DEBUG_VALUE:
669 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
670 default:
671 return (EOPNOTSUPP);
672 }
673 /* NOTREACHED */
674 }
675 #endif /* DEBUG */
676
677 int
678 proc_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
679 void *newp, size_t newlen, struct proc *p)
680 {
681 struct proc *ptmp = NULL;
682 int error = 0;
683 struct rlimit alim;
684 struct plimit *newplim;
685 char *tmps = NULL;
686 int i, curlen, len;
687
688 if (namelen < 2)
689 return EINVAL;
690
691 if (name[0] == PROC_CURPROC) {
692 ptmp = p;
693 } else if ((ptmp = pfind((pid_t)name[0])) == NULL) {
694 return (ESRCH);
695 } else {
696 if (p->p_ucred->cr_uid != 0) {
697 if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
698 p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
699 return EPERM;
700 if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
701 return EPERM; /* sgid proc */
702 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
703 if (p->p_ucred->cr_groups[i] ==
704 ptmp->p_cred->p_rgid)
705 break;
706 }
707 if (i == p->p_ucred->cr_ngroups)
708 return EPERM;
709 }
710 }
711 if (name[1] == PROC_PID_CORENAME) {
712 if (namelen != 2)
713 return EINVAL;
714 /*
715 * Can't use sysctl_string() here because we may malloc a new
716 * area during the process, so we have to do it by hand.
717 */
718 curlen = strlen(ptmp->p_limit->pl_corename) + 1;
719 if (oldlenp && *oldlenp < curlen) {
720 if (!oldp)
721 *oldlenp = curlen;
722 return (ENOMEM);
723 }
724 if (newp) {
725 if (securelevel > 2)
726 return EPERM;
727 if (newlen > MAXPATHLEN)
728 return ENAMETOOLONG;
729 tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
730 if (tmps == NULL)
731 return ENOMEM;
732 error = copyin(newp, tmps, newlen + 1);
733 tmps[newlen] = '\0';
734 if (error)
735 goto cleanup;
736 /* Enforce to be either 'core' for end with '.core' */
737 if (newlen < 4) { /* c.o.r.e */
738 error = EINVAL;
739 goto cleanup;
740 }
741 len = newlen - 4;
742 if (len > 0) {
743 if (tmps[len - 1] != '.' &&
744 tmps[len - 1] != '/') {
745 error = EINVAL;
746 goto cleanup;
747 }
748 }
749 if (strcmp(&tmps[len], "core") != 0) {
750 error = EINVAL;
751 goto cleanup;
752 }
753 }
754 if (oldp && oldlenp) {
755 *oldlenp = curlen;
756 error = copyout(ptmp->p_limit->pl_corename, oldp,
757 curlen);
758 }
759 if (newp && error == 0) {
760 /* if the 2 strings are identical, don't limcopy() */
761 if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
762 error = 0;
763 goto cleanup;
764 }
765 if (ptmp->p_limit->p_refcnt > 1 &&
766 (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
767 newplim = limcopy(ptmp->p_limit);
768 limfree(ptmp->p_limit);
769 ptmp->p_limit = newplim;
770 }
771 if (ptmp->p_limit->pl_corename != defcorename) {
772 free(ptmp->p_limit->pl_corename, M_TEMP);
773 }
774 ptmp->p_limit->pl_corename = tmps;
775 return (0);
776 }
777 cleanup:
778 if (tmps)
779 free(tmps, M_TEMP);
780 return (error);
781 }
782 if (name[1] == PROC_PID_LIMIT) {
783 if (namelen != 4 || name[2] < 1 ||
784 name[2] >= PROC_PID_LIMIT_MAXID)
785 return EINVAL;
786 memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
787 if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
788 error = sysctl_quad(oldp, oldlenp, newp, newlen,
789 &alim.rlim_max);
790 else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
791 error = sysctl_quad(oldp, oldlenp, newp, newlen,
792 &alim.rlim_cur);
793 else
794 error = EINVAL;
795
796 if (error)
797 return error;
798
799 if (newp)
800 error = dosetrlimit(ptmp, p->p_cred,
801 name[2] - 1, &alim);
802 return error;
803 }
804 return (EINVAL);
805 }
806
807 int
808 emul_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
809 void *newp, size_t newlen, struct proc *p)
810 {
811 static struct {
812 const char *name;
813 int type;
814 } emulations[] = CTL_EMUL_NAMES;
815 const struct emul *e;
816 const char *ename;
817 #ifdef LKM
818 extern struct lock exec_lock; /* XXX */
819 int error;
820 #else
821 extern int nexecs_builtin;
822 extern const struct execsw execsw_builtin[];
823 int i;
824 #endif
825
826 /* all sysctl names at this level are name and field */
827 if (namelen < 2)
828 return (ENOTDIR); /* overloaded */
829
830 if ((u_int) name[0] >= EMUL_MAXID || name[0] == 0)
831 return (EOPNOTSUPP);
832
833 ename = emulations[name[0]].name;
834
835 #ifdef LKM
836 lockmgr(&exec_lock, LK_SHARED, NULL);
837 if ((e = emul_search(ename))) {
838 error = (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
839 newp, newlen, p);
840 } else
841 error = EOPNOTSUPP;
842 lockmgr(&exec_lock, LK_RELEASE, NULL);
843
844 return (error);
845 #else
846 for (i = 0; i < nexecs_builtin; i++) {
847 e = execsw_builtin[i].es_emul;
848 if (e == NULL || strcmp(ename, e->e_name) != 0 ||
849 e->e_sysctl != NULL)
850 continue;
851
852 return (*e->e_sysctl)(name + 1, namelen - 1, oldp, oldlenp,
853 newp, newlen, p);
854 }
855
856 return (EOPNOTSUPP);
857 #endif
858 }
859 /*
860 * Convenience macros.
861 */
862
863 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) \
864 if (oldlenp) { \
865 if (!oldp) \
866 *oldlenp = len; \
867 else { \
868 if (*oldlenp < len) \
869 return(ENOMEM); \
870 *oldlenp = len; \
871 error = copyout((caddr_t)valp, oldp, len); \
872 } \
873 }
874
875 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
876 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
877
878 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len) \
879 if (newp && newlen != len) \
880 return (EINVAL);
881
882 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ) \
883 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
884
885 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len) \
886 if (error == 0 && newp) \
887 error = copyin(newp, valp, len);
888
889 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ) \
890 SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
891
892 #define SYSCTL_STRING_CORE(oldp, oldlenp, str) \
893 if (oldlenp) { \
894 len = strlen(str) + 1; \
895 if (!oldp) \
896 *oldlenp = len; \
897 else { \
898 if (*oldlenp < len) { \
899 err2 = ENOMEM; \
900 len = *oldlenp; \
901 } else \
902 *oldlenp = len; \
903 error = copyout(str, oldp, len);\
904 if (error == 0) \
905 error = err2; \
906 } \
907 }
908
909 /*
910 * Validate parameters and get old / set new parameters
911 * for an integer-valued sysctl function.
912 */
913 int
914 sysctl_int(void *oldp, size_t *oldlenp, void *newp, size_t newlen, int *valp)
915 {
916 int error = 0;
917
918 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
919 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
920 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
921
922 return (error);
923 }
924
925
926 /*
927 * As above, but read-only.
928 */
929 int
930 sysctl_rdint(void *oldp, size_t *oldlenp, void *newp, int val)
931 {
932 int error = 0;
933
934 if (newp)
935 return (EPERM);
936
937 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
938
939 return (error);
940 }
941
942 /*
943 * Validate parameters and get old / set new parameters
944 * for an quad-valued sysctl function.
945 */
946 int
947 sysctl_quad(void *oldp, size_t *oldlenp, void *newp, size_t newlen,
948 quad_t *valp)
949 {
950 int error = 0;
951
952 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
953 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
954 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
955
956 return (error);
957 }
958
959 /*
960 * As above, but read-only.
961 */
962 int
963 sysctl_rdquad(void *oldp, size_t *oldlenp, void *newp, quad_t val)
964 {
965 int error = 0;
966
967 if (newp)
968 return (EPERM);
969
970 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
971
972 return (error);
973 }
974
975 /*
976 * Validate parameters and get old / set new parameters
977 * for a string-valued sysctl function.
978 */
979 int
980 sysctl_string(void *oldp, size_t *oldlenp, void *newp, size_t newlen, char *str,
981 int maxlen)
982 {
983 int len, error = 0, err2 = 0;
984
985 if (newp && newlen >= maxlen)
986 return (EINVAL);
987
988 SYSCTL_STRING_CORE(oldp, oldlenp, str);
989
990 if (error == 0 && newp) {
991 error = copyin(newp, str, newlen);
992 str[newlen] = 0;
993 }
994 return (error);
995 }
996
997 /*
998 * As above, but read-only.
999 */
1000 int
1001 sysctl_rdstring(void *oldp, size_t *oldlenp, void *newp, const char *str)
1002 {
1003 int len, error = 0, err2 = 0;
1004
1005 if (newp)
1006 return (EPERM);
1007
1008 SYSCTL_STRING_CORE(oldp, oldlenp, str);
1009
1010 return (error);
1011 }
1012
1013 /*
1014 * Validate parameters and get old / set new parameters
1015 * for a structure oriented sysctl function.
1016 */
1017 int
1018 sysctl_struct(void *oldp, size_t *oldlenp, void *newp, size_t newlen, void *sp,
1019 int len)
1020 {
1021 int error = 0;
1022
1023 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
1024 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1025 SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
1026
1027 return (error);
1028 }
1029
1030 /*
1031 * Validate parameters and get old parameters
1032 * for a structure oriented sysctl function.
1033 */
1034 int
1035 sysctl_rdstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1036 int len)
1037 {
1038 int error = 0;
1039
1040 if (newp)
1041 return (EPERM);
1042
1043 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1044
1045 return (error);
1046 }
1047
1048 /*
1049 * As above, but can return a truncated result.
1050 */
1051 int
1052 sysctl_rdminstruct(void *oldp, size_t *oldlenp, void *newp, const void *sp,
1053 int len)
1054 {
1055 int error = 0;
1056
1057 if (newp)
1058 return (EPERM);
1059
1060 len = min(*oldlenp, len);
1061 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
1062
1063 return (error);
1064 }
1065
1066 /*
1067 * Get file structures.
1068 */
1069 static int
1070 sysctl_file(void *vwhere, size_t *sizep)
1071 {
1072 int buflen, error;
1073 struct file *fp;
1074 char *start, *where;
1075
1076 start = where = vwhere;
1077 buflen = *sizep;
1078 if (where == NULL) {
1079 /*
1080 * overestimate by 10 files
1081 */
1082 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
1083 return (0);
1084 }
1085
1086 /*
1087 * first copyout filehead
1088 */
1089 if (buflen < sizeof(filehead)) {
1090 *sizep = 0;
1091 return (0);
1092 }
1093 error = copyout((caddr_t)&filehead, where, sizeof(filehead));
1094 if (error)
1095 return (error);
1096 buflen -= sizeof(filehead);
1097 where += sizeof(filehead);
1098
1099 /*
1100 * followed by an array of file structures
1101 */
1102 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
1103 if (buflen < sizeof(struct file)) {
1104 *sizep = where - start;
1105 return (ENOMEM);
1106 }
1107 error = copyout((caddr_t)fp, where, sizeof(struct file));
1108 if (error)
1109 return (error);
1110 buflen -= sizeof(struct file);
1111 where += sizeof(struct file);
1112 }
1113 *sizep = where - start;
1114 return (0);
1115 }
1116
1117 #if defined(SYSVMSG) || defined(SYSVSEM) || defined(SYSVSHM)
1118 #define FILL_PERM(src, dst) do { \
1119 (dst)._key = (src)._key; \
1120 (dst).uid = (src).uid; \
1121 (dst).gid = (src).gid; \
1122 (dst).cuid = (src).cuid; \
1123 (dst).cgid = (src).cgid; \
1124 (dst).mode = (src).mode; \
1125 (dst)._seq = (src)._seq; \
1126 } while (0);
1127 #define FILL_MSG(src, dst) do { \
1128 FILL_PERM((src).msg_perm, (dst).msg_perm); \
1129 (dst).msg_qnum = (src).msg_qnum; \
1130 (dst).msg_qbytes = (src).msg_qbytes; \
1131 (dst)._msg_cbytes = (src)._msg_cbytes; \
1132 (dst).msg_lspid = (src).msg_lspid; \
1133 (dst).msg_lrpid = (src).msg_lrpid; \
1134 (dst).msg_stime = (src).msg_stime; \
1135 (dst).msg_rtime = (src).msg_rtime; \
1136 (dst).msg_ctime = (src).msg_ctime; \
1137 } while (0)
1138 #define FILL_SEM(src, dst) do { \
1139 FILL_PERM((src).sem_perm, (dst).sem_perm); \
1140 (dst).sem_nsems = (src).sem_nsems; \
1141 (dst).sem_otime = (src).sem_otime; \
1142 (dst).sem_ctime = (src).sem_ctime; \
1143 } while (0)
1144 #define FILL_SHM(src, dst) do { \
1145 FILL_PERM((src).shm_perm, (dst).shm_perm); \
1146 (dst).shm_segsz = (src).shm_segsz; \
1147 (dst).shm_lpid = (src).shm_lpid; \
1148 (dst).shm_cpid = (src).shm_cpid; \
1149 (dst).shm_atime = (src).shm_atime; \
1150 (dst).shm_dtime = (src).shm_dtime; \
1151 (dst).shm_ctime = (src).shm_ctime; \
1152 (dst).shm_nattch = (src).shm_nattch; \
1153 } while (0)
1154
1155 static int
1156 sysctl_sysvipc(int *name, u_int namelen, void *where, size_t *sizep)
1157 {
1158 #ifdef SYSVMSG
1159 struct msg_sysctl_info *msgsi;
1160 #endif
1161 #ifdef SYSVSEM
1162 struct sem_sysctl_info *semsi;
1163 #endif
1164 #ifdef SYSVSHM
1165 struct shm_sysctl_info *shmsi;
1166 #endif
1167 size_t infosize, dssize, tsize, buflen;
1168 void *buf = NULL;
1169 char *start;
1170 int32_t nds;
1171 int i, error, ret;
1172
1173 if (namelen != 1)
1174 return (EINVAL);
1175
1176 start = where;
1177 buflen = *sizep;
1178
1179 switch (*name) {
1180 case KERN_SYSVIPC_MSG_INFO:
1181 #ifdef SYSVMSG
1182 infosize = sizeof(msgsi->msginfo);
1183 nds = msginfo.msgmni;
1184 dssize = sizeof(msgsi->msgids[0]);
1185 break;
1186 #else
1187 return (EINVAL);
1188 #endif
1189 case KERN_SYSVIPC_SEM_INFO:
1190 #ifdef SYSVSEM
1191 infosize = sizeof(semsi->seminfo);
1192 nds = seminfo.semmni;
1193 dssize = sizeof(semsi->semids[0]);
1194 break;
1195 #else
1196 return (EINVAL);
1197 #endif
1198 case KERN_SYSVIPC_SHM_INFO:
1199 #ifdef SYSVSHM
1200 infosize = sizeof(shmsi->shminfo);
1201 nds = shminfo.shmmni;
1202 dssize = sizeof(shmsi->shmids[0]);
1203 break;
1204 #else
1205 return (EINVAL);
1206 #endif
1207 default:
1208 return (EINVAL);
1209 }
1210 /*
1211 * Round infosize to 64 bit boundary if requesting more than just
1212 * the info structure or getting the total data size.
1213 */
1214 if (where == NULL || *sizep > infosize)
1215 infosize = ((infosize + 7) / 8) * 8;
1216 tsize = infosize + nds * dssize;
1217
1218 /* Return just the total size required. */
1219 if (where == NULL) {
1220 *sizep = tsize;
1221 return (0);
1222 }
1223
1224 /* Not enough room for even the info struct. */
1225 if (buflen < infosize) {
1226 *sizep = 0;
1227 return (ENOMEM);
1228 }
1229 buf = malloc(min(tsize, buflen), M_TEMP, M_WAITOK);
1230 memset(buf, 0, min(tsize, buflen));
1231
1232 switch (*name) {
1233 #ifdef SYSVMSG
1234 case KERN_SYSVIPC_MSG_INFO:
1235 msgsi = (struct msg_sysctl_info *)buf;
1236 msgsi->msginfo = msginfo;
1237 break;
1238 #endif
1239 #ifdef SYSVSEM
1240 case KERN_SYSVIPC_SEM_INFO:
1241 semsi = (struct sem_sysctl_info *)buf;
1242 semsi->seminfo = seminfo;
1243 break;
1244 #endif
1245 #ifdef SYSVSHM
1246 case KERN_SYSVIPC_SHM_INFO:
1247 shmsi = (struct shm_sysctl_info *)buf;
1248 shmsi->shminfo = shminfo;
1249 break;
1250 #endif
1251 }
1252 buflen -= infosize;
1253
1254 ret = 0;
1255 if (buflen > 0) {
1256 /* Fill in the IPC data structures. */
1257 for (i = 0; i < nds; i++) {
1258 if (buflen < dssize) {
1259 ret = ENOMEM;
1260 break;
1261 }
1262 switch (*name) {
1263 #ifdef SYSVMSG
1264 case KERN_SYSVIPC_MSG_INFO:
1265 FILL_MSG(msqids[i], msgsi->msgids[i]);
1266 break;
1267 #endif
1268 #ifdef SYSVSEM
1269 case KERN_SYSVIPC_SEM_INFO:
1270 FILL_SEM(sema[i], semsi->semids[i]);
1271 break;
1272 #endif
1273 #ifdef SYSVSHM
1274 case KERN_SYSVIPC_SHM_INFO:
1275 FILL_SHM(shmsegs[i], shmsi->shmids[i]);
1276 break;
1277 #endif
1278 }
1279 buflen -= dssize;
1280 }
1281 }
1282 *sizep -= buflen;
1283 error = copyout(buf, start, *sizep);
1284 /* If copyout succeeded, use return code set earlier. */
1285 if (error == 0)
1286 error = ret;
1287 if (buf)
1288 free(buf, M_TEMP);
1289 return (error);
1290 }
1291 #endif /* SYSVMSG || SYSVSEM || SYSVSHM */
1292
1293 static int
1294 sysctl_msgbuf(void *vwhere, size_t *sizep)
1295 {
1296 char *where = vwhere;
1297 size_t len, maxlen = *sizep;
1298 long beg, end;
1299 int error;
1300
1301 /*
1302 * deal with cases where the message buffer has
1303 * become corrupted.
1304 */
1305 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
1306 msgbufenabled = 0;
1307 return (ENXIO);
1308 }
1309
1310 if (where == NULL) {
1311 /* always return full buffer size */
1312 *sizep = msgbufp->msg_bufs;
1313 return (0);
1314 }
1315
1316 error = 0;
1317 maxlen = min(msgbufp->msg_bufs, maxlen);
1318
1319 /*
1320 * First, copy from the write pointer to the end of
1321 * message buffer.
1322 */
1323 beg = msgbufp->msg_bufx;
1324 end = msgbufp->msg_bufs;
1325 while (maxlen > 0) {
1326 len = min(end - beg, maxlen);
1327 if (len == 0)
1328 break;
1329 error = copyout(&msgbufp->msg_bufc[beg], where, len);
1330 if (error)
1331 break;
1332 where += len;
1333 maxlen -= len;
1334
1335 /*
1336 * ... then, copy from the beginning of message buffer to
1337 * the write pointer.
1338 */
1339 beg = 0;
1340 end = msgbufp->msg_bufx;
1341 }
1342 return (error);
1343 }
1344
1345 /*
1346 * try over estimating by 5 procs
1347 */
1348 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
1349
1350 static int
1351 sysctl_doeproc(int *name, u_int namelen, void *vwhere, size_t *sizep)
1352 {
1353 struct eproc eproc;
1354 struct kinfo_proc2 kproc2;
1355 struct kinfo_proc *dp;
1356 struct proc *p;
1357 const struct proclist_desc *pd;
1358 char *where, *dp2;
1359 int type, op, arg, elem_size, elem_count;
1360 int buflen, needed, error;
1361
1362 dp = vwhere;
1363 dp2 = where = vwhere;
1364 buflen = where != NULL ? *sizep : 0;
1365 error = needed = 0;
1366 type = name[0];
1367
1368 if (type == KERN_PROC) {
1369 if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
1370 return (EINVAL);
1371 op = name[1];
1372 if (op != KERN_PROC_ALL)
1373 arg = name[2];
1374 } else {
1375 if (namelen != 5)
1376 return (EINVAL);
1377 op = name[1];
1378 arg = name[2];
1379 elem_size = name[3];
1380 elem_count = name[4];
1381 }
1382
1383 proclist_lock_read();
1384
1385 pd = proclists;
1386 again:
1387 for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
1388 /*
1389 * Skip embryonic processes.
1390 */
1391 if (p->p_stat == SIDL)
1392 continue;
1393 /*
1394 * TODO - make more efficient (see notes below).
1395 * do by session.
1396 */
1397 switch (op) {
1398
1399 case KERN_PROC_PID:
1400 /* could do this with just a lookup */
1401 if (p->p_pid != (pid_t)arg)
1402 continue;
1403 break;
1404
1405 case KERN_PROC_PGRP:
1406 /* could do this by traversing pgrp */
1407 if (p->p_pgrp->pg_id != (pid_t)arg)
1408 continue;
1409 break;
1410
1411 case KERN_PROC_SESSION:
1412 if (p->p_session->s_sid != (pid_t)arg)
1413 continue;
1414 break;
1415
1416 case KERN_PROC_TTY:
1417 if (arg == KERN_PROC_TTY_REVOKE) {
1418 if ((p->p_flag & P_CONTROLT) == 0 ||
1419 p->p_session->s_ttyp == NULL ||
1420 p->p_session->s_ttyvp != NULL)
1421 continue;
1422 } else if ((p->p_flag & P_CONTROLT) == 0 ||
1423 p->p_session->s_ttyp == NULL) {
1424 if ((dev_t)arg != KERN_PROC_TTY_NODEV)
1425 continue;
1426 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
1427 continue;
1428 break;
1429
1430 case KERN_PROC_UID:
1431 if (p->p_ucred->cr_uid != (uid_t)arg)
1432 continue;
1433 break;
1434
1435 case KERN_PROC_RUID:
1436 if (p->p_cred->p_ruid != (uid_t)arg)
1437 continue;
1438 break;
1439
1440 case KERN_PROC_GID:
1441 if (p->p_ucred->cr_gid != (uid_t)arg)
1442 continue;
1443 break;
1444
1445 case KERN_PROC_RGID:
1446 if (p->p_cred->p_rgid != (uid_t)arg)
1447 continue;
1448 break;
1449
1450 case KERN_PROC_ALL:
1451 /* allow everything */
1452 break;
1453
1454 default:
1455 error = EINVAL;
1456 goto cleanup;
1457 }
1458 if (type == KERN_PROC) {
1459 if (buflen >= sizeof(struct kinfo_proc)) {
1460 fill_eproc(p, &eproc);
1461 error = copyout((caddr_t)p, &dp->kp_proc,
1462 sizeof(struct proc));
1463 if (error)
1464 goto cleanup;
1465 error = copyout((caddr_t)&eproc, &dp->kp_eproc,
1466 sizeof(eproc));
1467 if (error)
1468 goto cleanup;
1469 dp++;
1470 buflen -= sizeof(struct kinfo_proc);
1471 }
1472 needed += sizeof(struct kinfo_proc);
1473 } else { /* KERN_PROC2 */
1474 if (buflen >= elem_size && elem_count > 0) {
1475 fill_kproc2(p, &kproc2);
1476 /*
1477 * Copy out elem_size, but not larger than
1478 * the size of a struct kinfo_proc2.
1479 */
1480 error = copyout(&kproc2, dp2,
1481 min(sizeof(kproc2), elem_size));
1482 if (error)
1483 goto cleanup;
1484 dp2 += elem_size;
1485 buflen -= elem_size;
1486 elem_count--;
1487 }
1488 needed += elem_size;
1489 }
1490 }
1491 pd++;
1492 if (pd->pd_list != NULL)
1493 goto again;
1494 proclist_unlock_read();
1495
1496 if (where != NULL) {
1497 if (type == KERN_PROC)
1498 *sizep = (caddr_t)dp - where;
1499 else
1500 *sizep = dp2 - where;
1501 if (needed > *sizep)
1502 return (ENOMEM);
1503 } else {
1504 needed += KERN_PROCSLOP;
1505 *sizep = needed;
1506 }
1507 return (0);
1508 cleanup:
1509 proclist_unlock_read();
1510 return (error);
1511 }
1512
1513 /*
1514 * Fill in an eproc structure for the specified process.
1515 */
1516 void
1517 fill_eproc(struct proc *p, struct eproc *ep)
1518 {
1519 struct tty *tp;
1520
1521 ep->e_paddr = p;
1522 ep->e_sess = p->p_session;
1523 ep->e_pcred = *p->p_cred;
1524 ep->e_ucred = *p->p_ucred;
1525 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1526 ep->e_vm.vm_rssize = 0;
1527 ep->e_vm.vm_tsize = 0;
1528 ep->e_vm.vm_dsize = 0;
1529 ep->e_vm.vm_ssize = 0;
1530 /* ep->e_vm.vm_pmap = XXX; */
1531 } else {
1532 struct vmspace *vm = p->p_vmspace;
1533
1534 ep->e_vm.vm_rssize = vm_resident_count(vm);
1535 ep->e_vm.vm_tsize = vm->vm_tsize;
1536 ep->e_vm.vm_dsize = vm->vm_dsize;
1537 ep->e_vm.vm_ssize = vm->vm_ssize;
1538 }
1539 if (p->p_pptr)
1540 ep->e_ppid = p->p_pptr->p_pid;
1541 else
1542 ep->e_ppid = 0;
1543 ep->e_pgid = p->p_pgrp->pg_id;
1544 ep->e_sid = ep->e_sess->s_sid;
1545 ep->e_jobc = p->p_pgrp->pg_jobc;
1546 if ((p->p_flag & P_CONTROLT) &&
1547 (tp = ep->e_sess->s_ttyp)) {
1548 ep->e_tdev = tp->t_dev;
1549 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1550 ep->e_tsess = tp->t_session;
1551 } else
1552 ep->e_tdev = NODEV;
1553 if (p->p_wmesg)
1554 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
1555 ep->e_xsize = ep->e_xrssize = 0;
1556 ep->e_xccount = ep->e_xswrss = 0;
1557 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
1558 if (SESS_LEADER(p))
1559 ep->e_flag |= EPROC_SLEADER;
1560 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
1561 }
1562
1563 /*
1564 * Fill in an eproc structure for the specified process.
1565 */
1566 static void
1567 fill_kproc2(struct proc *p, struct kinfo_proc2 *ki)
1568 {
1569 struct tty *tp;
1570
1571 memset(ki, 0, sizeof(*ki));
1572
1573 ki->p_forw = PTRTOINT64(p->p_forw);
1574 ki->p_back = PTRTOINT64(p->p_back);
1575 ki->p_paddr = PTRTOINT64(p);
1576
1577 ki->p_addr = PTRTOINT64(p->p_addr);
1578 ki->p_fd = PTRTOINT64(p->p_fd);
1579 ki->p_cwdi = PTRTOINT64(p->p_cwdi);
1580 ki->p_stats = PTRTOINT64(p->p_stats);
1581 ki->p_limit = PTRTOINT64(p->p_limit);
1582 ki->p_vmspace = PTRTOINT64(p->p_vmspace);
1583 ki->p_sigacts = PTRTOINT64(p->p_sigacts);
1584 ki->p_sess = PTRTOINT64(p->p_session);
1585 ki->p_tsess = 0; /* may be changed if controlling tty below */
1586 ki->p_ru = PTRTOINT64(p->p_ru);
1587
1588 ki->p_eflag = 0;
1589 ki->p_exitsig = p->p_exitsig;
1590 ki->p_flag = p->p_flag;
1591
1592 ki->p_pid = p->p_pid;
1593 if (p->p_pptr)
1594 ki->p_ppid = p->p_pptr->p_pid;
1595 else
1596 ki->p_ppid = 0;
1597 ki->p_sid = p->p_session->s_sid;
1598 ki->p__pgid = p->p_pgrp->pg_id;
1599
1600 ki->p_tpgid = NO_PID; /* may be changed if controlling tty below */
1601
1602 ki->p_uid = p->p_ucred->cr_uid;
1603 ki->p_ruid = p->p_cred->p_ruid;
1604 ki->p_gid = p->p_ucred->cr_gid;
1605 ki->p_rgid = p->p_cred->p_rgid;
1606
1607 memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
1608 min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
1609 ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
1610
1611 ki->p_jobc = p->p_pgrp->pg_jobc;
1612 if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
1613 ki->p_tdev = tp->t_dev;
1614 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1615 ki->p_tsess = PTRTOINT64(tp->t_session);
1616 } else {
1617 ki->p_tdev = NODEV;
1618 }
1619
1620 ki->p_estcpu = p->p_estcpu;
1621 ki->p_rtime_sec = p->p_rtime.tv_sec;
1622 ki->p_rtime_usec = p->p_rtime.tv_usec;
1623 ki->p_cpticks = p->p_cpticks;
1624 ki->p_pctcpu = p->p_pctcpu;
1625 ki->p_swtime = p->p_swtime;
1626 ki->p_slptime = p->p_slptime;
1627 if (p->p_stat == SONPROC) {
1628 KDASSERT(p->p_cpu != NULL);
1629 ki->p_schedflags = p->p_cpu->ci_schedstate.spc_flags;
1630 } else
1631 ki->p_schedflags = 0;
1632
1633 ki->p_uticks = p->p_uticks;
1634 ki->p_sticks = p->p_sticks;
1635 ki->p_iticks = p->p_iticks;
1636
1637 ki->p_tracep = PTRTOINT64(p->p_tracep);
1638 ki->p_traceflag = p->p_traceflag;
1639
1640 ki->p_holdcnt = p->p_holdcnt;
1641
1642 memcpy(&ki->p_siglist, &p->p_sigctx.ps_siglist, sizeof(ki_sigset_t));
1643 memcpy(&ki->p_sigmask, &p->p_sigctx.ps_sigmask, sizeof(ki_sigset_t));
1644 memcpy(&ki->p_sigignore, &p->p_sigctx.ps_sigignore,sizeof(ki_sigset_t));
1645 memcpy(&ki->p_sigcatch, &p->p_sigctx.ps_sigcatch, sizeof(ki_sigset_t));
1646
1647 ki->p_stat = p->p_stat;
1648 ki->p_priority = p->p_priority;
1649 ki->p_usrpri = p->p_usrpri;
1650 ki->p_nice = p->p_nice;
1651
1652 ki->p_xstat = p->p_xstat;
1653 ki->p_acflag = p->p_acflag;
1654
1655 strncpy(ki->p_comm, p->p_comm,
1656 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
1657
1658 if (p->p_wmesg)
1659 strncpy(ki->p_wmesg, p->p_wmesg, sizeof(ki->p_wmesg));
1660 ki->p_wchan = PTRTOINT64(p->p_wchan);
1661
1662 strncpy(ki->p_login, p->p_session->s_login, sizeof(ki->p_login));
1663
1664 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1665 ki->p_vm_rssize = 0;
1666 ki->p_vm_tsize = 0;
1667 ki->p_vm_dsize = 0;
1668 ki->p_vm_ssize = 0;
1669 } else {
1670 struct vmspace *vm = p->p_vmspace;
1671
1672 ki->p_vm_rssize = vm_resident_count(vm);
1673 ki->p_vm_tsize = vm->vm_tsize;
1674 ki->p_vm_dsize = vm->vm_dsize;
1675 ki->p_vm_ssize = vm->vm_ssize;
1676 }
1677
1678 if (p->p_session->s_ttyvp)
1679 ki->p_eflag |= EPROC_CTTY;
1680 if (SESS_LEADER(p))
1681 ki->p_eflag |= EPROC_SLEADER;
1682
1683 /* XXX Is this double check necessary? */
1684 if ((p->p_flag & P_INMEM) == 0 || P_ZOMBIE(p)) {
1685 ki->p_uvalid = 0;
1686 } else {
1687 ki->p_uvalid = 1;
1688
1689 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
1690 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
1691
1692 ki->p_uutime_sec = p->p_stats->p_ru.ru_utime.tv_sec;
1693 ki->p_uutime_usec = p->p_stats->p_ru.ru_utime.tv_usec;
1694 ki->p_ustime_sec = p->p_stats->p_ru.ru_stime.tv_sec;
1695 ki->p_ustime_usec = p->p_stats->p_ru.ru_stime.tv_usec;
1696
1697 ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
1698 ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
1699 ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
1700 ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
1701 ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
1702 ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
1703 ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
1704 ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
1705 ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
1706 ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
1707 ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
1708 ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
1709 ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
1710 ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
1711
1712 ki->p_uctime_sec = p->p_stats->p_cru.ru_utime.tv_sec +
1713 p->p_stats->p_cru.ru_stime.tv_sec;
1714 ki->p_uctime_usec = p->p_stats->p_cru.ru_utime.tv_usec +
1715 p->p_stats->p_cru.ru_stime.tv_usec;
1716 }
1717 #ifdef MULTIPROCESSOR
1718 if (p->p_cpu != NULL)
1719 ki->p_cpuid = p->p_cpu->ci_cpuid;
1720 else
1721 #endif
1722 ki->p_cpuid = KI_NOCPU;
1723 }
1724
1725 int
1726 sysctl_procargs(int *name, u_int namelen, void *where, size_t *sizep,
1727 struct proc *up)
1728 {
1729 struct ps_strings pss;
1730 struct proc *p;
1731 size_t len, upper_bound, xlen;
1732 struct uio auio;
1733 struct iovec aiov;
1734 vaddr_t argv;
1735 pid_t pid;
1736 int nargv, type, error, i;
1737 char *arg;
1738 char *tmp;
1739
1740 if (namelen != 2)
1741 return (EINVAL);
1742 pid = name[0];
1743 type = name[1];
1744
1745 switch (type) {
1746 case KERN_PROC_ARGV:
1747 case KERN_PROC_NARGV:
1748 case KERN_PROC_ENV:
1749 case KERN_PROC_NENV:
1750 /* ok */
1751 break;
1752 default:
1753 return (EINVAL);
1754 }
1755
1756 /* check pid */
1757 if ((p = pfind(pid)) == NULL)
1758 return (EINVAL);
1759
1760 /* only root or same user change look at the environment */
1761 if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
1762 if (up->p_ucred->cr_uid != 0) {
1763 if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
1764 up->p_cred->p_ruid != p->p_cred->p_svuid)
1765 return (EPERM);
1766 }
1767 }
1768
1769 if (sizep != NULL && where == NULL) {
1770 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
1771 *sizep = sizeof (int);
1772 else
1773 *sizep = ARG_MAX; /* XXX XXX XXX */
1774 return (0);
1775 }
1776 if (where == NULL || sizep == NULL)
1777 return (EINVAL);
1778
1779 /*
1780 * Zombies don't have a stack, so we can't read their psstrings.
1781 * System processes also don't have a user stack.
1782 */
1783 if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
1784 return (EINVAL);
1785
1786 /*
1787 * Lock the process down in memory.
1788 */
1789 /* XXXCDC: how should locking work here? */
1790 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
1791 return (EFAULT);
1792 p->p_vmspace->vm_refcnt++; /* XXX */
1793
1794 /*
1795 * Allocate a temporary buffer to hold the arguments.
1796 */
1797 arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
1798
1799 /*
1800 * Read in the ps_strings structure.
1801 */
1802 aiov.iov_base = &pss;
1803 aiov.iov_len = sizeof(pss);
1804 auio.uio_iov = &aiov;
1805 auio.uio_iovcnt = 1;
1806 auio.uio_offset = (vaddr_t)p->p_psstr;
1807 auio.uio_resid = sizeof(pss);
1808 auio.uio_segflg = UIO_SYSSPACE;
1809 auio.uio_rw = UIO_READ;
1810 auio.uio_procp = NULL;
1811 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1812 if (error)
1813 goto done;
1814
1815 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
1816 memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
1817 else
1818 memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
1819 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
1820 error = copyout(&nargv, where, sizeof(nargv));
1821 *sizep = sizeof(nargv);
1822 goto done;
1823 }
1824 /*
1825 * Now read the address of the argument vector.
1826 */
1827 switch (type) {
1828 case KERN_PROC_ARGV:
1829 /* XXX compat32 stuff here */
1830 memcpy(&tmp, (char *)&pss + p->p_psargv, sizeof(tmp));
1831 break;
1832 case KERN_PROC_ENV:
1833 memcpy(&tmp, (char *)&pss + p->p_psenv, sizeof(tmp));
1834 break;
1835 default:
1836 return (EINVAL);
1837 }
1838 auio.uio_offset = (off_t)(long)tmp;
1839 aiov.iov_base = &argv;
1840 aiov.iov_len = sizeof(argv);
1841 auio.uio_iov = &aiov;
1842 auio.uio_iovcnt = 1;
1843 auio.uio_resid = sizeof(argv);
1844 auio.uio_segflg = UIO_SYSSPACE;
1845 auio.uio_rw = UIO_READ;
1846 auio.uio_procp = NULL;
1847 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1848 if (error)
1849 goto done;
1850
1851 /*
1852 * Now copy in the actual argument vector, one page at a time,
1853 * since we don't know how long the vector is (though, we do
1854 * know how many NUL-terminated strings are in the vector).
1855 */
1856 len = 0;
1857 upper_bound = *sizep;
1858 for (; nargv != 0 && len < upper_bound; len += xlen) {
1859 aiov.iov_base = arg;
1860 aiov.iov_len = PAGE_SIZE;
1861 auio.uio_iov = &aiov;
1862 auio.uio_iovcnt = 1;
1863 auio.uio_offset = argv + len;
1864 xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
1865 auio.uio_resid = xlen;
1866 auio.uio_segflg = UIO_SYSSPACE;
1867 auio.uio_rw = UIO_READ;
1868 auio.uio_procp = NULL;
1869 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1870 if (error)
1871 goto done;
1872
1873 for (i = 0; i < xlen && nargv != 0; i++) {
1874 if (arg[i] == '\0')
1875 nargv--; /* one full string */
1876 }
1877
1878 /* make sure we don't copyout past the end of the user's buffer */
1879 if (len + i > upper_bound)
1880 i = upper_bound - len;
1881
1882 error = copyout(arg, (char *)where + len, i);
1883 if (error)
1884 break;
1885
1886 if (nargv == 0) {
1887 len += i;
1888 break;
1889 }
1890 }
1891 *sizep = len;
1892
1893 done:
1894 uvmspace_free(p->p_vmspace);
1895
1896 free(arg, M_TEMP);
1897 return (error);
1898 }
1899
1900 #if NPTY > 0
1901 int pty_maxptys(int, int); /* defined in kern/tty_pty.c */
1902
1903 /*
1904 * Validate parameters and get old / set new parameters
1905 * for pty sysctl function.
1906 */
1907 static int
1908 sysctl_pty(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
1909 {
1910 int error = 0;
1911 int oldmax = 0, newmax = 0;
1912
1913 /* get current value of maxptys */
1914 oldmax = pty_maxptys(0, 0);
1915
1916 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &oldmax, int)
1917
1918 if (!error && newp) {
1919 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
1920 SYSCTL_SCALAR_NEWPCOP_TYP(newp, &newmax, int)
1921
1922 if (newmax != pty_maxptys(newmax, (newp != NULL)))
1923 return (EINVAL);
1924
1925 }
1926
1927 return (error);
1928 }
1929 #endif /* NPTY > 0 */
1930
1931 static int
1932 sysctl_dotkstat(name, namelen, where, sizep, newp)
1933 int *name;
1934 u_int namelen;
1935 void *where;
1936 size_t *sizep;
1937 void *newp;
1938 {
1939 /* all sysctl names at this level are terminal */
1940 if (namelen != 1)
1941 return (ENOTDIR); /* overloaded */
1942
1943 switch (name[0]) {
1944 case KERN_TKSTAT_NIN:
1945 return (sysctl_rdquad(where, sizep, newp, tk_nin));
1946 case KERN_TKSTAT_NOUT:
1947 return (sysctl_rdquad(where, sizep, newp, tk_nout));
1948 case KERN_TKSTAT_CANCC:
1949 return (sysctl_rdquad(where, sizep, newp, tk_cancc));
1950 case KERN_TKSTAT_RAWCC:
1951 return (sysctl_rdquad(where, sizep, newp, tk_rawcc));
1952 default:
1953 return (EOPNOTSUPP);
1954 }
1955 }
1956