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