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