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