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