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