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