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