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