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