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