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