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