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