kern_sysctl.c revision 1.64 1 /* $NetBSD: kern_sysctl.c,v 1.64 2000/05/27 03:24:50 simonb 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
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/buf.h>
54 #include <sys/device.h>
55 #include <sys/disklabel.h>
56 #include <sys/dkstat.h>
57 #include <sys/exec.h>
58 #include <sys/file.h>
59 #include <sys/ioctl.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/msgbuf.h>
63 #include <sys/pool.h>
64 #include <sys/proc.h>
65 #include <sys/resource.h>
66 #include <sys/resourcevar.h>
67 #include <sys/syscallargs.h>
68 #include <sys/tty.h>
69 #include <sys/unistd.h>
70 #include <sys/vnode.h>
71 #include <sys/sysctl.h>
72
73 #if defined(DDB)
74 #include <ddb/ddbvar.h>
75 #endif
76
77 #define PTRTOINT64(foo) ((u_int64_t)(uintptr_t)(foo))
78
79 /*
80 * Locking and stats
81 */
82 static struct sysctl_lock {
83 int sl_lock;
84 int sl_want;
85 int sl_locked;
86 } memlock;
87
88 static int sysctl_doeproc __P((int *, u_int, char *, size_t *));
89 static void fill_kproc2 __P((struct proc *, struct kinfo_proc2 *));
90 static int sysctl_procargs __P((int *, u_int, void *, size_t *, struct proc *));
91
92 int
93 sys___sysctl(p, v, retval)
94 struct proc *p;
95 void *v;
96 register_t *retval;
97 {
98 struct sys___sysctl_args /* {
99 syscallarg(int *) name;
100 syscallarg(u_int) namelen;
101 syscallarg(void *) old;
102 syscallarg(size_t *) oldlenp;
103 syscallarg(void *) new;
104 syscallarg(size_t) newlen;
105 } */ *uap = v;
106 int error, dolock = 1;
107 size_t savelen = 0, oldlen = 0;
108 sysctlfn *fn;
109 int name[CTL_MAXNAME];
110 size_t *oldlenp;
111
112 /*
113 * all top-level sysctl names are non-terminal
114 */
115 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
116 return (EINVAL);
117 error = copyin(SCARG(uap, name), &name,
118 SCARG(uap, namelen) * sizeof(int));
119 if (error)
120 return (error);
121
122 /*
123 * For all but CTL_PROC, must be root to change a value.
124 * For CTL_PROC, must be root, or owner of the proc (and not suid),
125 * this is checked in proc_sysctl() (once we know the targer proc).
126 */
127 if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
128 (error = suser(p->p_ucred, &p->p_acflag)))
129 return error;
130
131 switch (name[0]) {
132 case CTL_KERN:
133 fn = kern_sysctl;
134 if (name[2] != KERN_VNODE) /* XXX */
135 dolock = 0;
136 break;
137 case CTL_HW:
138 fn = hw_sysctl;
139 break;
140 case CTL_VM:
141 fn = uvm_sysctl;
142 break;
143 case CTL_NET:
144 fn = net_sysctl;
145 break;
146 case CTL_VFS:
147 fn = vfs_sysctl;
148 break;
149 case CTL_MACHDEP:
150 fn = cpu_sysctl;
151 break;
152 #ifdef DEBUG
153 case CTL_DEBUG:
154 fn = debug_sysctl;
155 break;
156 #endif
157 #ifdef DDB
158 case CTL_DDB:
159 fn = ddb_sysctl;
160 break;
161 #endif
162 case CTL_PROC:
163 fn = proc_sysctl;
164 break;
165 default:
166 return (EOPNOTSUPP);
167 }
168
169 oldlenp = SCARG(uap, oldlenp);
170 if (oldlenp) {
171 if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
172 return (error);
173 oldlenp = &oldlen;
174 }
175 if (SCARG(uap, old) != NULL) {
176 if (!uvm_useracc(SCARG(uap, old), oldlen, B_WRITE))
177 return (EFAULT);
178 while (memlock.sl_lock) {
179 memlock.sl_want = 1;
180 sleep((caddr_t)&memlock, PRIBIO+1);
181 memlock.sl_locked++;
182 }
183 memlock.sl_lock = 1;
184 if (dolock) {
185 /*
186 * XXX Um, this is kind of evil. What should we
187 * XXX be passing here?
188 */
189 if (uvm_vslock(p, SCARG(uap, old), oldlen,
190 VM_PROT_NONE) != KERN_SUCCESS) {
191 memlock.sl_lock = 0;
192 if (memlock.sl_want) {
193 memlock.sl_want = 0;
194 wakeup((caddr_t)&memlock);
195 return (EFAULT);
196 }
197 }
198 }
199 savelen = oldlen;
200 }
201 error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
202 oldlenp, SCARG(uap, new), SCARG(uap, newlen), p);
203 if (SCARG(uap, old) != NULL) {
204 if (dolock)
205 uvm_vsunlock(p, SCARG(uap, old), savelen);
206 memlock.sl_lock = 0;
207 if (memlock.sl_want) {
208 memlock.sl_want = 0;
209 wakeup((caddr_t)&memlock);
210 }
211 }
212 if (error)
213 return (error);
214 if (SCARG(uap, oldlenp))
215 error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
216 return (error);
217 }
218
219 /*
220 * Attributes stored in the kernel.
221 */
222 char hostname[MAXHOSTNAMELEN];
223 int hostnamelen;
224 char domainname[MAXHOSTNAMELEN];
225 int domainnamelen;
226 long hostid;
227 #ifdef INSECURE
228 int securelevel = -1;
229 #else
230 int securelevel = 0;
231 #endif
232 #ifdef DEFCORENAME
233 char defcorename[MAXPATHLEN] = DEFCORENAME;
234 int defcorenamelen = sizeof(DEFCORENAME);
235 #else
236 char defcorename[MAXPATHLEN] = "%n.core";
237 int defcorenamelen = sizeof("%n.core");
238 #endif
239 extern int kern_logsigexit;
240 extern fixpt_t ccpu;
241
242 /*
243 * kernel related system variables.
244 */
245 int
246 kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
247 int *name;
248 u_int namelen;
249 void *oldp;
250 size_t *oldlenp;
251 void *newp;
252 size_t newlen;
253 struct proc *p;
254 {
255 int error, level, inthostid;
256 int old_autonicetime;
257 int old_vnodes;
258
259 /* All sysctl names at this level, except for a few, are terminal. */
260 switch (name[0]) {
261 case KERN_PROC:
262 case KERN_PROC2:
263 case KERN_PROF:
264 case KERN_MBUF:
265 case KERN_PROC_ARGS:
266 /* Not terminal. */
267 break;
268 default:
269 if (namelen != 1)
270 return (ENOTDIR); /* overloaded */
271 }
272
273 switch (name[0]) {
274 case KERN_OSTYPE:
275 return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
276 case KERN_OSRELEASE:
277 return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
278 case KERN_OSREV:
279 return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
280 case KERN_VERSION:
281 return (sysctl_rdstring(oldp, oldlenp, newp, version));
282 case KERN_MAXVNODES:
283 old_vnodes = desiredvnodes;
284 error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
285 if (old_vnodes > desiredvnodes) {
286 desiredvnodes = old_vnodes;
287 return (EINVAL);
288 }
289 return (error);
290 case KERN_MAXPROC:
291 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
292 case KERN_MAXFILES:
293 return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
294 case KERN_ARGMAX:
295 return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
296 case KERN_SECURELVL:
297 level = securelevel;
298 if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
299 newp == NULL)
300 return (error);
301 if (level < securelevel && p->p_pid != 1)
302 return (EPERM);
303 securelevel = level;
304 return (0);
305 case KERN_HOSTNAME:
306 error = sysctl_string(oldp, oldlenp, newp, newlen,
307 hostname, sizeof(hostname));
308 if (newp && !error)
309 hostnamelen = newlen;
310 return (error);
311 case KERN_DOMAINNAME:
312 error = sysctl_string(oldp, oldlenp, newp, newlen,
313 domainname, sizeof(domainname));
314 if (newp && !error)
315 domainnamelen = newlen;
316 return (error);
317 case KERN_HOSTID:
318 inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
319 error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
320 hostid = inthostid;
321 return (error);
322 case KERN_CLOCKRATE:
323 return (sysctl_clockrate(oldp, oldlenp));
324 case KERN_BOOTTIME:
325 return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
326 sizeof(struct timeval)));
327 case KERN_VNODE:
328 return (sysctl_vnode(oldp, oldlenp, p));
329 case KERN_PROC:
330 case KERN_PROC2:
331 return (sysctl_doeproc(name, namelen, oldp, oldlenp));
332 case KERN_PROC_ARGS:
333 return (sysctl_procargs(name + 1, namelen - 1,
334 oldp, oldlenp, p));
335 case KERN_FILE:
336 return (sysctl_file(oldp, oldlenp));
337 #ifdef GPROF
338 case KERN_PROF:
339 return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
340 newp, newlen));
341 #endif
342 case KERN_POSIX1:
343 return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
344 case KERN_NGROUPS:
345 return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
346 case KERN_JOB_CONTROL:
347 return (sysctl_rdint(oldp, oldlenp, newp, 1));
348 case KERN_SAVED_IDS:
349 #ifdef _POSIX_SAVED_IDS
350 return (sysctl_rdint(oldp, oldlenp, newp, 1));
351 #else
352 return (sysctl_rdint(oldp, oldlenp, newp, 0));
353 #endif
354 case KERN_MAXPARTITIONS:
355 return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
356 case KERN_RAWPARTITION:
357 return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
358 #ifdef NTP
359 case KERN_NTPTIME:
360 return (sysctl_ntptime(oldp, oldlenp));
361 #endif
362 case KERN_AUTONICETIME:
363 old_autonicetime = autonicetime;
364 error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
365 if (autonicetime < 0)
366 autonicetime = old_autonicetime;
367 return (error);
368 case KERN_AUTONICEVAL:
369 error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
370 if (autoniceval < PRIO_MIN)
371 autoniceval = PRIO_MIN;
372 if (autoniceval > PRIO_MAX)
373 autoniceval = PRIO_MAX;
374 return (error);
375 case KERN_RTC_OFFSET:
376 return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
377 case KERN_ROOT_DEVICE:
378 return (sysctl_rdstring(oldp, oldlenp, newp,
379 root_device->dv_xname));
380 case KERN_MSGBUFSIZE:
381 /*
382 * deal with cases where the message buffer has
383 * become corrupted.
384 */
385 if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
386 msgbufenabled = 0;
387 return (ENXIO);
388 }
389 return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
390 case KERN_FSYNC:
391 return (sysctl_rdint(oldp, oldlenp, newp, 1));
392 case KERN_SYSVMSG:
393 #ifdef SYSVMSG
394 return (sysctl_rdint(oldp, oldlenp, newp, 1));
395 #else
396 return (sysctl_rdint(oldp, oldlenp, newp, 0));
397 #endif
398 case KERN_SYSVSEM:
399 #ifdef SYSVSEM
400 return (sysctl_rdint(oldp, oldlenp, newp, 1));
401 #else
402 return (sysctl_rdint(oldp, oldlenp, newp, 0));
403 #endif
404 case KERN_SYSVSHM:
405 #ifdef SYSVSHM
406 return (sysctl_rdint(oldp, oldlenp, newp, 1));
407 #else
408 return (sysctl_rdint(oldp, oldlenp, newp, 0));
409 #endif
410 case KERN_DEFCORENAME:
411 if (newp && newlen < 1)
412 return (EINVAL);
413 error = sysctl_string(oldp, oldlenp, newp, newlen,
414 defcorename, sizeof(defcorename));
415 if (newp && !error)
416 defcorenamelen = newlen;
417 return (error);
418 case KERN_SYNCHRONIZED_IO:
419 return (sysctl_rdint(oldp, oldlenp, newp, 1));
420 case KERN_IOV_MAX:
421 return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
422 case KERN_MBUF:
423 return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
424 newp, newlen));
425 case KERN_MAPPED_FILES:
426 return (sysctl_rdint(oldp, oldlenp, newp, 1));
427 case KERN_MEMLOCK:
428 return (sysctl_rdint(oldp, oldlenp, newp, 1));
429 case KERN_MEMLOCK_RANGE:
430 return (sysctl_rdint(oldp, oldlenp, newp, 1));
431 case KERN_MEMORY_PROTECTION:
432 return (sysctl_rdint(oldp, oldlenp, newp, 1));
433 case KERN_LOGIN_NAME_MAX:
434 return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
435 case KERN_LOGSIGEXIT:
436 return (sysctl_int(oldp, oldlenp, newp, newlen,
437 &kern_logsigexit));
438 case KERN_FSCALE:
439 return (sysctl_rdint(oldp, oldlenp, newp, FSCALE));
440 case KERN_CCPU:
441 return (sysctl_rdint(oldp, oldlenp, newp, ccpu));
442 case KERN_CP_TIME:
443 return (sysctl_rdstruct(oldp, oldlenp, newp, cp_time,
444 sizeof(cp_time)));
445 default:
446 return (EOPNOTSUPP);
447 }
448 /* NOTREACHED */
449 }
450
451 /*
452 * hardware related system variables.
453 */
454 int
455 hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
456 int *name;
457 u_int namelen;
458 void *oldp;
459 size_t *oldlenp;
460 void *newp;
461 size_t newlen;
462 struct proc *p;
463 {
464
465 /* all sysctl names at this level are terminal */
466 if (namelen != 1)
467 return (ENOTDIR); /* overloaded */
468
469 switch (name[0]) {
470 case HW_MACHINE:
471 return (sysctl_rdstring(oldp, oldlenp, newp, machine));
472 case HW_MACHINE_ARCH:
473 return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
474 case HW_MODEL:
475 return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
476 case HW_NCPU:
477 return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */
478 case HW_BYTEORDER:
479 return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
480 case HW_PHYSMEM:
481 return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
482 case HW_USERMEM:
483 return (sysctl_rdint(oldp, oldlenp, newp,
484 ctob(physmem - uvmexp.wired)));
485 case HW_PAGESIZE:
486 return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
487 case HW_ALIGNBYTES:
488 return (sysctl_rdint(oldp, oldlenp, newp, ALIGNBYTES));
489 default:
490 return (EOPNOTSUPP);
491 }
492 /* NOTREACHED */
493 }
494
495 #ifdef DEBUG
496 /*
497 * Debugging related system variables.
498 */
499 struct ctldebug debug0, debug1, debug2, debug3, debug4;
500 struct ctldebug debug5, debug6, debug7, debug8, debug9;
501 struct ctldebug debug10, debug11, debug12, debug13, debug14;
502 struct ctldebug debug15, debug16, debug17, debug18, debug19;
503 static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
504 &debug0, &debug1, &debug2, &debug3, &debug4,
505 &debug5, &debug6, &debug7, &debug8, &debug9,
506 &debug10, &debug11, &debug12, &debug13, &debug14,
507 &debug15, &debug16, &debug17, &debug18, &debug19,
508 };
509 int
510 debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
511 int *name;
512 u_int namelen;
513 void *oldp;
514 size_t *oldlenp;
515 void *newp;
516 size_t newlen;
517 struct proc *p;
518 {
519 struct ctldebug *cdp;
520
521 /* all sysctl names at this level are name and field */
522 if (namelen != 2)
523 return (ENOTDIR); /* overloaded */
524 cdp = debugvars[name[0]];
525 if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
526 return (EOPNOTSUPP);
527 switch (name[1]) {
528 case CTL_DEBUG_NAME:
529 return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
530 case CTL_DEBUG_VALUE:
531 return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
532 default:
533 return (EOPNOTSUPP);
534 }
535 /* NOTREACHED */
536 }
537 #endif /* DEBUG */
538
539 int
540 proc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
541 int *name;
542 u_int namelen;
543 void *oldp;
544 size_t *oldlenp;
545 void *newp;
546 size_t newlen;
547 struct proc *p;
548 {
549 struct proc *ptmp = NULL;
550 const struct proclist_desc *pd;
551 int error = 0;
552 struct rlimit alim;
553 struct plimit *newplim;
554 char *tmps = NULL;
555 int i, curlen, len;
556
557 if (namelen < 2)
558 return EINVAL;
559
560 if (name[0] == PROC_CURPROC) {
561 ptmp = p;
562 } else {
563 proclist_lock_read();
564 for (pd = proclists; pd->pd_list != NULL; pd++) {
565 for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
566 ptmp = LIST_NEXT(ptmp, p_list)) {
567 /* Skip embryonic processes. */
568 if (ptmp->p_stat == SIDL)
569 continue;
570 if (ptmp->p_pid == (pid_t)name[0])
571 break;
572 }
573 if (ptmp != NULL)
574 break;
575 }
576 proclist_unlock_read();
577 if (ptmp == NULL)
578 return(ESRCH);
579 if (p->p_ucred->cr_uid != 0) {
580 if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
581 p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
582 return EPERM;
583 if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
584 return EPERM; /* sgid proc */
585 for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
586 if (p->p_ucred->cr_groups[i] ==
587 ptmp->p_cred->p_rgid)
588 break;
589 }
590 if (i == p->p_ucred->cr_ngroups)
591 return EPERM;
592 }
593 }
594 if (name[1] == PROC_PID_CORENAME) {
595 if (namelen != 2)
596 return EINVAL;
597 /*
598 * Can't use sysctl_string() here because we may malloc a new
599 * area during the process, so we have to do it by hand.
600 */
601 curlen = strlen(ptmp->p_limit->pl_corename) + 1;
602 if (oldlenp && *oldlenp < curlen) {
603 if (!oldp)
604 *oldlenp = curlen;
605 return (ENOMEM);
606 }
607 if (newp) {
608 if (securelevel > 2)
609 return EPERM;
610 if (newlen > MAXPATHLEN)
611 return ENAMETOOLONG;
612 tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
613 if (tmps == NULL)
614 return ENOMEM;
615 error = copyin(newp, tmps, newlen + 1);
616 tmps[newlen] = '\0';
617 if (error)
618 goto cleanup;
619 /* Enforce to be either 'core' for end with '.core' */
620 if (newlen < 4) { /* c.o.r.e */
621 error = EINVAL;
622 goto cleanup;
623 }
624 len = newlen - 4;
625 if (len > 0) {
626 if (tmps[len - 1] != '.' &&
627 tmps[len - 1] != '/') {
628 error = EINVAL;
629 goto cleanup;
630 }
631 }
632 if (strcmp(&tmps[len], "core") != 0) {
633 error = EINVAL;
634 goto cleanup;
635 }
636 }
637 if (oldp && oldlenp) {
638 *oldlenp = curlen;
639 error = copyout(ptmp->p_limit->pl_corename, oldp,
640 curlen);
641 }
642 if (newp && error == 0) {
643 /* if the 2 strings are identical, don't limcopy() */
644 if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
645 error = 0;
646 goto cleanup;
647 }
648 if (ptmp->p_limit->p_refcnt > 1 &&
649 (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
650 newplim = limcopy(ptmp->p_limit);
651 limfree(ptmp->p_limit);
652 ptmp->p_limit = newplim;
653 } else if (ptmp->p_limit->pl_corename != defcorename) {
654 free(ptmp->p_limit->pl_corename, M_TEMP);
655 }
656 ptmp->p_limit->pl_corename = tmps;
657 return (0);
658 }
659 cleanup:
660 if (tmps)
661 free(tmps, M_TEMP);
662 return (error);
663 }
664 if (name[1] == PROC_PID_LIMIT) {
665 if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
666 return EINVAL;
667 memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
668 if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
669 error = sysctl_quad(oldp, oldlenp, newp, newlen,
670 &alim.rlim_max);
671 else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
672 error = sysctl_quad(oldp, oldlenp, newp, newlen,
673 &alim.rlim_cur);
674 else
675 error = EINVAL;
676
677 if (error)
678 return error;
679
680 if (newp)
681 error = dosetrlimit(ptmp, p->p_cred,
682 name[2] - 1, &alim);
683 return error;
684 }
685 return (EINVAL);
686 }
687
688 /*
689 * Convenience macros.
690 */
691
692 #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) \
693 if (oldlenp) { \
694 if (!oldp) \
695 *oldlenp = len; \
696 else { \
697 if (*oldlenp < len) \
698 return(ENOMEM); \
699 *oldlenp = len; \
700 error = copyout((caddr_t)valp, oldp, len); \
701 } \
702 }
703
704 #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
705 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
706
707 #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len) \
708 if (newp && newlen != len) \
709 return (EINVAL);
710
711 #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ) \
712 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
713
714 #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len) \
715 if (error == 0 && newp) \
716 error = copyin(newp, valp, len);
717
718 #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ) \
719 SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
720
721 #define SYSCTL_STRING_CORE(oldp, oldlenp, str) \
722 if (oldlenp) { \
723 len = strlen(str) + 1; \
724 if (!oldp) \
725 *oldlenp = len; \
726 else { \
727 if (*oldlenp < len) { \
728 err2 = ENOMEM; \
729 len = *oldlenp; \
730 } else \
731 *oldlenp = len; \
732 error = copyout(str, oldp, len);\
733 if (error == 0) \
734 error = err2; \
735 } \
736 }
737
738 /*
739 * Validate parameters and get old / set new parameters
740 * for an integer-valued sysctl function.
741 */
742 int
743 sysctl_int(oldp, oldlenp, newp, newlen, valp)
744 void *oldp;
745 size_t *oldlenp;
746 void *newp;
747 size_t newlen;
748 int *valp;
749 {
750 int error = 0;
751
752 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
753 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
754 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
755
756 return (error);
757 }
758
759
760 /*
761 * As above, but read-only.
762 */
763 int
764 sysctl_rdint(oldp, oldlenp, newp, val)
765 void *oldp;
766 size_t *oldlenp;
767 void *newp;
768 int val;
769 {
770 int error = 0;
771
772 if (newp)
773 return (EPERM);
774
775 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
776
777 return (error);
778 }
779
780 /*
781 * Validate parameters and get old / set new parameters
782 * for an quad-valued sysctl function.
783 */
784 int
785 sysctl_quad(oldp, oldlenp, newp, newlen, valp)
786 void *oldp;
787 size_t *oldlenp;
788 void *newp;
789 size_t newlen;
790 quad_t *valp;
791 {
792 int error = 0;
793
794 SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
795 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
796 SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
797
798 return (error);
799 }
800
801 /*
802 * As above, but read-only.
803 */
804 int
805 sysctl_rdquad(oldp, oldlenp, newp, val)
806 void *oldp;
807 size_t *oldlenp;
808 void *newp;
809 quad_t val;
810 {
811 int error = 0;
812
813 if (newp)
814 return (EPERM);
815
816 SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
817
818 return (error);
819 }
820
821 /*
822 * Validate parameters and get old / set new parameters
823 * for a string-valued sysctl function.
824 */
825 int
826 sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
827 void *oldp;
828 size_t *oldlenp;
829 void *newp;
830 size_t newlen;
831 char *str;
832 int maxlen;
833 {
834 int len, error = 0, err2 = 0;
835
836 if (newp && newlen >= maxlen)
837 return (EINVAL);
838
839 SYSCTL_STRING_CORE(oldp, oldlenp, str);
840
841 if (error == 0 && newp) {
842 error = copyin(newp, str, newlen);
843 str[newlen] = 0;
844 }
845 return (error);
846 }
847
848 /*
849 * As above, but read-only.
850 */
851 int
852 sysctl_rdstring(oldp, oldlenp, newp, str)
853 void *oldp;
854 size_t *oldlenp;
855 void *newp;
856 char *str;
857 {
858 int len, error = 0, err2 = 0;
859
860 if (newp)
861 return (EPERM);
862
863 SYSCTL_STRING_CORE(oldp, oldlenp, str);
864
865 return (error);
866 }
867
868 /*
869 * Validate parameters and get old / set new parameters
870 * for a structure oriented sysctl function.
871 */
872 int
873 sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
874 void *oldp;
875 size_t *oldlenp;
876 void *newp;
877 size_t newlen;
878 void *sp;
879 int len;
880 {
881 int error = 0;
882
883 SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
884 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
885 SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
886
887 return (error);
888 }
889
890 /*
891 * Validate parameters and get old parameters
892 * for a structure oriented sysctl function.
893 */
894 int
895 sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
896 void *oldp;
897 size_t *oldlenp;
898 void *newp, *sp;
899 int len;
900 {
901 int error = 0;
902
903 if (newp)
904 return (EPERM);
905
906 SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
907
908 return (error);
909 }
910
911 /*
912 * Get file structures.
913 */
914 int
915 sysctl_file(where, sizep)
916 char *where;
917 size_t *sizep;
918 {
919 int buflen, error;
920 struct file *fp;
921 char *start = where;
922
923 buflen = *sizep;
924 if (where == NULL) {
925 /*
926 * overestimate by 10 files
927 */
928 *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
929 return (0);
930 }
931
932 /*
933 * first copyout filehead
934 */
935 if (buflen < sizeof(filehead)) {
936 *sizep = 0;
937 return (0);
938 }
939 error = copyout((caddr_t)&filehead, where, sizeof(filehead));
940 if (error)
941 return (error);
942 buflen -= sizeof(filehead);
943 where += sizeof(filehead);
944
945 /*
946 * followed by an array of file structures
947 */
948 for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
949 if (buflen < sizeof(struct file)) {
950 *sizep = where - start;
951 return (ENOMEM);
952 }
953 error = copyout((caddr_t)fp, where, sizeof(struct file));
954 if (error)
955 return (error);
956 buflen -= sizeof(struct file);
957 where += sizeof(struct file);
958 }
959 *sizep = where - start;
960 return (0);
961 }
962
963 /*
964 * try over estimating by 5 procs
965 */
966 #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
967
968 int
969 sysctl_doeproc(name, namelen, where, sizep)
970 int *name;
971 u_int namelen;
972 char *where;
973 size_t *sizep;
974 {
975 struct eproc eproc;
976 struct kinfo_proc2 kproc2;
977 struct kinfo_proc *dp = (struct kinfo_proc *)where;
978 struct proc *p;
979 const struct proclist_desc *pd;
980 char *dp2;
981 int type, op, arg, elem_size, elem_count;
982 int buflen, needed, error;
983
984 dp2 = where;
985 buflen = where != NULL ? *sizep : 0;
986 error = needed = 0;
987 type = name[0];
988
989 if (type == KERN_PROC) {
990 if (namelen != 3 && !(namelen == 2 && name[1] == KERN_PROC_ALL))
991 return (EINVAL);
992 op = name[1];
993 if (op != KERN_PROC_ALL)
994 arg = name[2];
995 } else {
996 if (namelen != 5)
997 return (EINVAL);
998 op = name[1];
999 arg = name[2];
1000 elem_size = name[3];
1001 elem_count = name[4];
1002 }
1003
1004 proclist_lock_read();
1005
1006 pd = proclists;
1007 again:
1008 for (p = LIST_FIRST(pd->pd_list); p != NULL; p = LIST_NEXT(p, p_list)) {
1009 /*
1010 * Skip embryonic processes.
1011 */
1012 if (p->p_stat == SIDL)
1013 continue;
1014 /*
1015 * TODO - make more efficient (see notes below).
1016 * do by session.
1017 */
1018 switch (op) {
1019
1020 case KERN_PROC_PID:
1021 /* could do this with just a lookup */
1022 if (p->p_pid != (pid_t)arg)
1023 continue;
1024 break;
1025
1026 case KERN_PROC_PGRP:
1027 /* could do this by traversing pgrp */
1028 if (p->p_pgrp->pg_id != (pid_t)arg)
1029 continue;
1030 break;
1031
1032 case KERN_PROC_TTY:
1033 if (arg == KERN_PROC_TTY_REVOKE) {
1034 if ((p->p_flag & P_CONTROLT) == 0 ||
1035 p->p_session->s_ttyp == NULL ||
1036 p->p_session->s_ttyvp != NULL)
1037 continue;
1038 } else if ((p->p_flag & P_CONTROLT) == 0 ||
1039 p->p_session->s_ttyp == NULL) {
1040 if ((dev_t)arg != KERN_PROC_TTY_NODEV)
1041 continue;
1042 } else if (p->p_session->s_ttyp->t_dev != (dev_t)arg)
1043 continue;
1044 break;
1045
1046 case KERN_PROC_UID:
1047 if (p->p_ucred->cr_uid != (uid_t)arg)
1048 continue;
1049 break;
1050
1051 case KERN_PROC_RUID:
1052 if (p->p_cred->p_ruid != (uid_t)arg)
1053 continue;
1054 break;
1055 }
1056 if (type == KERN_PROC) {
1057 if (buflen >= sizeof(struct kinfo_proc)) {
1058 fill_eproc(p, &eproc);
1059 error = copyout((caddr_t)p, &dp->kp_proc,
1060 sizeof(struct proc));
1061 if (error)
1062 goto cleanup;
1063 error = copyout((caddr_t)&eproc, &dp->kp_eproc,
1064 sizeof(eproc));
1065 if (error)
1066 goto cleanup;
1067 dp++;
1068 buflen -= sizeof(struct kinfo_proc);
1069 }
1070 needed += sizeof(struct kinfo_proc);
1071 } else { /* KERN_PROC2 */
1072 if (buflen >= elem_size && elem_count > 0) {
1073 fill_kproc2(p, &kproc2);
1074 /*
1075 * Copy out elem_size, but not larger than
1076 * the size of a struct kinfo_proc2.
1077 */
1078 error = copyout(&kproc2, dp2,
1079 min(sizeof(kproc2), elem_size));
1080 if (error)
1081 goto cleanup;
1082 dp2 += elem_size;
1083 buflen -= elem_size;
1084 elem_count--;
1085 }
1086 needed += elem_size;
1087 }
1088 }
1089 pd++;
1090 if (pd->pd_list != NULL)
1091 goto again;
1092 proclist_unlock_read();
1093
1094 if (where != NULL) {
1095 if (type == KERN_PROC)
1096 *sizep = (caddr_t)dp - where;
1097 else
1098 *sizep = dp2 - where;
1099 if (needed > *sizep)
1100 return (ENOMEM);
1101 } else {
1102 needed += KERN_PROCSLOP;
1103 *sizep = needed;
1104 }
1105 return (0);
1106 cleanup:
1107 proclist_unlock_read();
1108 return (error);
1109 }
1110
1111 /*
1112 * Fill in an eproc structure for the specified process.
1113 */
1114 void
1115 fill_eproc(p, ep)
1116 struct proc *p;
1117 struct eproc *ep;
1118 {
1119 struct tty *tp;
1120
1121 ep->e_paddr = p;
1122 ep->e_sess = p->p_session;
1123 ep->e_pcred = *p->p_cred;
1124 ep->e_ucred = *p->p_ucred;
1125 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1126 ep->e_vm.vm_rssize = 0;
1127 ep->e_vm.vm_tsize = 0;
1128 ep->e_vm.vm_dsize = 0;
1129 ep->e_vm.vm_ssize = 0;
1130 /* ep->e_vm.vm_pmap = XXX; */
1131 } else {
1132 struct vmspace *vm = p->p_vmspace;
1133
1134 ep->e_vm.vm_rssize = vm_resident_count(vm);
1135 ep->e_vm.vm_tsize = vm->vm_tsize;
1136 ep->e_vm.vm_dsize = vm->vm_dsize;
1137 ep->e_vm.vm_ssize = vm->vm_ssize;
1138 }
1139 if (p->p_pptr)
1140 ep->e_ppid = p->p_pptr->p_pid;
1141 else
1142 ep->e_ppid = 0;
1143 ep->e_pgid = p->p_pgrp->pg_id;
1144 ep->e_sid = ep->e_sess->s_sid;
1145 ep->e_jobc = p->p_pgrp->pg_jobc;
1146 if ((p->p_flag & P_CONTROLT) &&
1147 (tp = ep->e_sess->s_ttyp)) {
1148 ep->e_tdev = tp->t_dev;
1149 ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1150 ep->e_tsess = tp->t_session;
1151 } else
1152 ep->e_tdev = NODEV;
1153 if (p->p_wmesg)
1154 strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
1155 ep->e_xsize = ep->e_xrssize = 0;
1156 ep->e_xccount = ep->e_xswrss = 0;
1157 ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
1158 if (SESS_LEADER(p))
1159 ep->e_flag |= EPROC_SLEADER;
1160 strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
1161 }
1162
1163 /*
1164 * Fill in an eproc structure for the specified process.
1165 */
1166 static void
1167 fill_kproc2(p, ki)
1168 struct proc *p;
1169 struct kinfo_proc2 *ki;
1170 {
1171 struct tty *tp;
1172
1173 memset(ki, 0, sizeof(*ki));
1174
1175 ki->p_forw = PTRTOINT64(p->p_forw);
1176 ki->p_back = PTRTOINT64(p->p_back);
1177 ki->p_paddr = PTRTOINT64(p);
1178
1179 ki->p_addr = PTRTOINT64(p->p_addr);
1180 ki->p_fd = PTRTOINT64(p->p_fd);
1181 ki->p_cwdi = PTRTOINT64(p->p_cwdi);
1182 ki->p_stats = PTRTOINT64(p->p_stats);
1183 ki->p_limit = PTRTOINT64(p->p_limit);
1184 ki->p_vmspace = PTRTOINT64(p->p_vmspace);
1185 ki->p_sigacts = PTRTOINT64(p->p_sigacts);
1186 ki->p_sess = PTRTOINT64(p->p_session);
1187 ki->p_tsess = 0; /* may be changed if controlling tty below */
1188 ki->p_ru = PTRTOINT64(p->p_ru);
1189
1190 ki->p_eflag = 0;
1191 ki->p_exitsig = p->p_exitsig;
1192 ki->p_flag = p->p_flag;
1193
1194 ki->p_pid = p->p_pid;
1195 if (p->p_pptr)
1196 ki->p_ppid = p->p_pptr->p_pid;
1197 else
1198 ki->p_ppid = 0;
1199 ki->p_sid = p->p_session->s_sid;
1200 ki->p__pgid = p->p_pgrp->pg_id;
1201
1202 ki->p_tpgid = NO_PID; /* may be changed if controlling tty below */
1203
1204 ki->p_uid = p->p_ucred->cr_uid;
1205 ki->p_ruid = p->p_cred->p_ruid;
1206 ki->p_gid = p->p_ucred->cr_gid;
1207 ki->p_rgid = p->p_cred->p_rgid;
1208
1209 memcpy(ki->p_groups, p->p_cred->pc_ucred->cr_groups,
1210 min(sizeof(ki->p_groups), sizeof(p->p_cred->pc_ucred->cr_groups)));
1211 ki->p_ngroups = p->p_cred->pc_ucred->cr_ngroups;
1212
1213 ki->p_jobc = p->p_pgrp->pg_jobc;
1214 if ((p->p_flag & P_CONTROLT) && (tp = p->p_session->s_ttyp)) {
1215 ki->p_tdev = tp->t_dev;
1216 ki->p_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1217 ki->p_tsess = PTRTOINT64(tp->t_session);
1218 } else {
1219 ki->p_tdev = NODEV;
1220 }
1221
1222 ki->p_estcpu = p->p_estcpu;
1223 ki->p_rtime_sec = p->p_rtime.tv_sec;
1224 ki->p_rtime_usec = p->p_rtime.tv_usec;
1225 ki->p_cpticks = p->p_cpticks;
1226 ki->p_pctcpu = p->p_pctcpu;
1227 ki->p_swtime = p->p_swtime;
1228 ki->p_slptime = p->p_slptime;
1229 /*
1230 * XXX curcpu() is wrong; should be CPU process is running on.
1231 * XXX --thorpej
1232 */
1233 ki->p_schedflags = (p->p_stat == SONPROC) ?
1234 curcpu()->ci_schedstate.spc_flags : 0;
1235
1236 ki->p_uticks = p->p_uticks;
1237 ki->p_sticks = p->p_sticks;
1238 ki->p_iticks = p->p_iticks;
1239
1240 ki->p_tracep = PTRTOINT64(p->p_tracep);
1241 ki->p_traceflag = p->p_traceflag;
1242
1243 ki->p_holdcnt = p->p_holdcnt;
1244
1245 memcpy(&ki->p_siglist, &p->p_siglist, sizeof(ki_sigset_t));
1246 memcpy(&ki->p_sigmask, &p->p_sigmask, sizeof(ki_sigset_t));
1247 memcpy(&ki->p_sigignore, &p->p_sigignore, sizeof(ki_sigset_t));
1248 memcpy(&ki->p_sigcatch, &p->p_sigcatch, sizeof(ki_sigset_t));
1249
1250 ki->p_stat = p->p_stat;
1251 ki->p_priority = p->p_priority;
1252 ki->p_usrpri = p->p_usrpri;
1253 ki->p_nice = p->p_nice;
1254
1255 ki->p_xstat = p->p_xstat;
1256 ki->p_acflag = p->p_acflag;
1257
1258 strncpy(ki->p_comm, p->p_comm,
1259 min(sizeof(ki->p_comm), sizeof(p->p_comm)));
1260
1261 if (p->p_wmesg)
1262 strncpy(ki->p_wmesg, p->p_wmesg, sizeof(ki->p_wmesg));
1263 ki->p_wchan = PTRTOINT64(p->p_wchan);
1264
1265 strncpy(ki->p_login, p->p_session->s_login, sizeof(ki->p_login));
1266
1267 if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1268 ki->p_vm_rssize = 0;
1269 ki->p_vm_tsize = 0;
1270 ki->p_vm_dsize = 0;
1271 ki->p_vm_ssize = 0;
1272 } else {
1273 struct vmspace *vm = p->p_vmspace;
1274
1275 ki->p_vm_rssize = vm_resident_count(vm);
1276 ki->p_vm_tsize = vm->vm_tsize;
1277 ki->p_vm_dsize = vm->vm_dsize;
1278 ki->p_vm_ssize = vm->vm_ssize;
1279 }
1280
1281 if (p->p_session->s_ttyvp)
1282 ki->p_eflag |= EPROC_CTTY;
1283 if (SESS_LEADER(p))
1284 ki->p_eflag |= EPROC_SLEADER;
1285
1286 /* XXX Is this double check necessary? */
1287 if (P_ZOMBIE(p) || p->p_addr == NULL) {
1288 ki->p_uvalid = 0;
1289 } else {
1290 ki->p_uvalid = 1;
1291
1292 ki->p_ustart_sec = p->p_stats->p_start.tv_sec;
1293 ki->p_ustart_usec = p->p_stats->p_start.tv_usec;
1294
1295 ki->p_uutime_sec = p->p_stats->p_ru.ru_utime.tv_sec;
1296 ki->p_uutime_usec = p->p_stats->p_ru.ru_utime.tv_usec;
1297 ki->p_ustime_sec = p->p_stats->p_ru.ru_stime.tv_sec;
1298 ki->p_ustime_usec = p->p_stats->p_ru.ru_stime.tv_usec;
1299
1300 ki->p_uru_maxrss = p->p_stats->p_ru.ru_maxrss;
1301 ki->p_uru_ixrss = p->p_stats->p_ru.ru_ixrss;
1302 ki->p_uru_idrss = p->p_stats->p_ru.ru_idrss;
1303 ki->p_uru_isrss = p->p_stats->p_ru.ru_isrss;
1304 ki->p_uru_minflt = p->p_stats->p_ru.ru_minflt;
1305 ki->p_uru_majflt = p->p_stats->p_ru.ru_majflt;
1306 ki->p_uru_nswap = p->p_stats->p_ru.ru_nswap;
1307 ki->p_uru_inblock = p->p_stats->p_ru.ru_inblock;
1308 ki->p_uru_oublock = p->p_stats->p_ru.ru_oublock;
1309 ki->p_uru_msgsnd = p->p_stats->p_ru.ru_msgsnd;
1310 ki->p_uru_msgrcv = p->p_stats->p_ru.ru_msgrcv;
1311 ki->p_uru_nsignals = p->p_stats->p_ru.ru_nsignals;
1312 ki->p_uru_nvcsw = p->p_stats->p_ru.ru_nvcsw;
1313 ki->p_uru_nivcsw = p->p_stats->p_ru.ru_nivcsw;
1314
1315 ki->p_uctime_sec = p->p_stats->p_cru.ru_utime.tv_sec +
1316 p->p_stats->p_cru.ru_stime.tv_sec;
1317 ki->p_uctime_usec = p->p_stats->p_cru.ru_utime.tv_usec +
1318 p->p_stats->p_cru.ru_stime.tv_usec;
1319 }
1320 }
1321
1322 int
1323 sysctl_procargs(name, namelen, where, sizep, up)
1324 int *name;
1325 u_int namelen;
1326 void *where;
1327 size_t *sizep;
1328 struct proc *up;
1329 {
1330 struct ps_strings pss;
1331 struct proc *p;
1332 size_t len, upper_bound, xlen;
1333 struct uio auio;
1334 struct iovec aiov;
1335 vaddr_t argv;
1336 pid_t pid;
1337 int nargv, type, error, i;
1338 char *arg;
1339
1340 if (namelen != 2)
1341 return (EINVAL);
1342 pid = name[0];
1343 type = name[1];
1344
1345 switch (type) {
1346 case KERN_PROC_ARGV:
1347 case KERN_PROC_NARGV:
1348 case KERN_PROC_ENV:
1349 case KERN_PROC_NENV:
1350 /* ok */
1351 break;
1352 default:
1353 return (EINVAL);
1354 }
1355
1356 /* check pid */
1357 if ((p = pfind(pid)) == NULL)
1358 return (EINVAL);
1359
1360 /* only root or same user change look at the environment */
1361 if (type == KERN_PROC_ENV || type == KERN_PROC_NENV) {
1362 if (up->p_ucred->cr_uid != 0) {
1363 if (up->p_cred->p_ruid != p->p_cred->p_ruid ||
1364 up->p_cred->p_ruid != p->p_cred->p_svuid)
1365 return (EPERM);
1366 }
1367 }
1368
1369 if (sizep != NULL && where == NULL) {
1370 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV)
1371 *sizep = sizeof (int);
1372 else
1373 *sizep = ARG_MAX; /* XXX XXX XXX */
1374 return (0);
1375 }
1376 if (where == NULL || sizep == NULL)
1377 return (EINVAL);
1378 /*
1379 * Allocate a temporary buffer to hold the arguments.
1380 */
1381 arg = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
1382
1383 /*
1384 * Zombies don't have a stack, so we can't read their psstrings.
1385 * System processes also don't have a user stack.
1386 */
1387 if (P_ZOMBIE(p) || (p->p_flag & P_SYSTEM) != 0)
1388 return (EINVAL);
1389
1390 /*
1391 * Lock the process down in memory.
1392 */
1393 /* XXXCDC: how should locking work here? */
1394 if ((p->p_flag & P_WEXIT) || (p->p_vmspace->vm_refcnt < 1))
1395 return (EFAULT);
1396 PHOLD(p);
1397 p->p_vmspace->vm_refcnt++; /* XXX */
1398
1399 /*
1400 * Read in the ps_strings structure.
1401 */
1402 aiov.iov_base = &pss;
1403 aiov.iov_len = sizeof(pss);
1404 auio.uio_iov = &aiov;
1405 auio.uio_iovcnt = 1;
1406 auio.uio_offset = (vaddr_t)p->p_psstr;
1407 auio.uio_resid = sizeof(pss);
1408 auio.uio_segflg = UIO_SYSSPACE;
1409 auio.uio_rw = UIO_READ;
1410 auio.uio_procp = NULL;
1411 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1412 if (error)
1413 goto done;
1414
1415 if (type == KERN_PROC_ARGV || type == KERN_PROC_NARGV)
1416 memcpy(&nargv, (char *)&pss + p->p_psnargv, sizeof(nargv));
1417 else
1418 memcpy(&nargv, (char *)&pss + p->p_psnenv, sizeof(nargv));
1419 if (type == KERN_PROC_NARGV || type == KERN_PROC_NENV) {
1420 error = copyout(&nargv, where, sizeof(nargv));
1421 *sizep = sizeof(nargv);
1422 goto done;
1423 }
1424 /*
1425 * Now read the address of the argument vector.
1426 */
1427 switch (type) {
1428 case KERN_PROC_ARGV:
1429 /* XXX compat32 stuff here */
1430 memcpy(&auio.uio_offset, (char *)&pss + p->p_psargv,
1431 sizeof(auio.uio_offset));
1432 break;
1433 case KERN_PROC_ENV:
1434 memcpy(&auio.uio_offset, (char *)&pss + p->p_psenv,
1435 sizeof(auio.uio_offset));
1436 break;
1437 }
1438 aiov.iov_base = &argv;
1439 aiov.iov_len = sizeof(argv);
1440 auio.uio_iov = &aiov;
1441 auio.uio_iovcnt = 1;
1442 auio.uio_resid = sizeof(argv);
1443 auio.uio_segflg = UIO_SYSSPACE;
1444 auio.uio_rw = UIO_READ;
1445 auio.uio_procp = NULL;
1446 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1447 if (error)
1448 goto done;
1449
1450 /*
1451 * Now copy in the actual argument vector, one page at a time,
1452 * since we don't know how long the vector is (though, we do
1453 * know how many NUL-terminated strings are in the vector).
1454 */
1455 len = 0;
1456 upper_bound = *sizep;
1457 for (; nargv != 0 && len < upper_bound; len += xlen) {
1458 aiov.iov_base = arg;
1459 aiov.iov_len = PAGE_SIZE;
1460 auio.uio_iov = &aiov;
1461 auio.uio_iovcnt = 1;
1462 auio.uio_offset = argv + len;
1463 xlen = PAGE_SIZE - ((argv + len) & PAGE_MASK);
1464 auio.uio_resid = xlen;
1465 auio.uio_segflg = UIO_SYSSPACE;
1466 auio.uio_rw = UIO_READ;
1467 auio.uio_procp = NULL;
1468 error = uvm_io(&p->p_vmspace->vm_map, &auio);
1469 if (error)
1470 goto done;
1471
1472 for (i = 0; i < xlen && nargv != 0; i++) {
1473 if (arg[i] == '\0')
1474 nargv--; /* one full string */
1475 }
1476
1477 /* make sure we don't copyout past the end of the user's buffer */
1478 if (len + i > upper_bound)
1479 i = upper_bound - len;
1480
1481 error = copyout(arg, (char *)where + len, i);
1482 if (error)
1483 break;
1484
1485 if (nargv == 0) {
1486 len += i;
1487 break;
1488 }
1489 }
1490 *sizep = len;
1491
1492 done:
1493 PRELE(p);
1494 uvmspace_free(p->p_vmspace);
1495
1496 free(arg, M_TEMP);
1497 return (error);
1498 }
1499