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