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