kern_sysctl.c revision 1.45 1 1.45 thorpej /* $NetBSD: kern_sysctl.c,v 1.45 1999/05/26 01:07:06 thorpej Exp $ */
2 1.2 cgd
3 1.1 cgd /*-
4 1.1 cgd * Copyright (c) 1982, 1986, 1989, 1993
5 1.1 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Mike Karels at Berkeley Software Design, Inc.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd *
38 1.34 fvdl * @(#)kern_sysctl.c 8.9 (Berkeley) 5/20/95
39 1.1 cgd */
40 1.1 cgd
41 1.1 cgd /*
42 1.1 cgd * sysctl system call.
43 1.1 cgd */
44 1.30 thorpej
45 1.38 jonathan #include "opt_ddb.h"
46 1.30 thorpej #include "opt_insecure.h"
47 1.37 nathanw #include "opt_shortcorename.h"
48 1.42 tron #include "opt_sysv.h"
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/systm.h>
52 1.1 cgd #include <sys/kernel.h>
53 1.1 cgd #include <sys/malloc.h>
54 1.1 cgd #include <sys/proc.h>
55 1.1 cgd #include <sys/file.h>
56 1.1 cgd #include <sys/vnode.h>
57 1.1 cgd #include <sys/unistd.h>
58 1.1 cgd #include <sys/buf.h>
59 1.1 cgd #include <sys/ioctl.h>
60 1.1 cgd #include <sys/tty.h>
61 1.7 cgd #include <sys/disklabel.h>
62 1.23 thorpej #include <sys/device.h>
63 1.1 cgd #include <vm/vm.h>
64 1.1 cgd #include <sys/sysctl.h>
65 1.28 leo #include <sys/msgbuf.h>
66 1.1 cgd
67 1.31 mrg #include <uvm/uvm_extern.h>
68 1.31 mrg
69 1.5 cgd #include <sys/mount.h>
70 1.5 cgd #include <sys/syscallargs.h>
71 1.1 cgd
72 1.38 jonathan
73 1.38 jonathan #if defined(DDB)
74 1.38 jonathan #include <ddb/ddbvar.h>
75 1.31 mrg #endif
76 1.31 mrg
77 1.1 cgd /*
78 1.1 cgd * Locking and stats
79 1.1 cgd */
80 1.1 cgd static struct sysctl_lock {
81 1.1 cgd int sl_lock;
82 1.1 cgd int sl_want;
83 1.1 cgd int sl_locked;
84 1.1 cgd } memlock;
85 1.1 cgd
86 1.1 cgd int
87 1.12 mycroft sys___sysctl(p, v, retval)
88 1.1 cgd struct proc *p;
89 1.11 thorpej void *v;
90 1.11 thorpej register_t *retval;
91 1.11 thorpej {
92 1.12 mycroft register struct sys___sysctl_args /* {
93 1.5 cgd syscallarg(int *) name;
94 1.5 cgd syscallarg(u_int) namelen;
95 1.5 cgd syscallarg(void *) old;
96 1.5 cgd syscallarg(size_t *) oldlenp;
97 1.5 cgd syscallarg(void *) new;
98 1.5 cgd syscallarg(size_t) newlen;
99 1.11 thorpej } */ *uap = v;
100 1.1 cgd int error, dolock = 1;
101 1.13 christos size_t savelen = 0, oldlen = 0;
102 1.1 cgd sysctlfn *fn;
103 1.1 cgd int name[CTL_MAXNAME];
104 1.1 cgd
105 1.5 cgd if (SCARG(uap, new) != NULL &&
106 1.5 cgd (error = suser(p->p_ucred, &p->p_acflag)))
107 1.1 cgd return (error);
108 1.1 cgd /*
109 1.1 cgd * all top-level sysctl names are non-terminal
110 1.1 cgd */
111 1.5 cgd if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
112 1.1 cgd return (EINVAL);
113 1.13 christos error = copyin(SCARG(uap, name), &name,
114 1.13 christos SCARG(uap, namelen) * sizeof(int));
115 1.13 christos if (error)
116 1.1 cgd return (error);
117 1.1 cgd
118 1.1 cgd switch (name[0]) {
119 1.1 cgd case CTL_KERN:
120 1.1 cgd fn = kern_sysctl;
121 1.1 cgd if (name[2] != KERN_VNODE) /* XXX */
122 1.1 cgd dolock = 0;
123 1.1 cgd break;
124 1.1 cgd case CTL_HW:
125 1.1 cgd fn = hw_sysctl;
126 1.1 cgd break;
127 1.1 cgd case CTL_VM:
128 1.31 mrg fn = uvm_sysctl;
129 1.1 cgd break;
130 1.1 cgd case CTL_NET:
131 1.1 cgd fn = net_sysctl;
132 1.1 cgd break;
133 1.34 fvdl case CTL_VFS:
134 1.34 fvdl fn = vfs_sysctl;
135 1.1 cgd break;
136 1.1 cgd case CTL_MACHDEP:
137 1.1 cgd fn = cpu_sysctl;
138 1.1 cgd break;
139 1.1 cgd #ifdef DEBUG
140 1.1 cgd case CTL_DEBUG:
141 1.1 cgd fn = debug_sysctl;
142 1.20 thorpej break;
143 1.20 thorpej #endif
144 1.20 thorpej #ifdef DDB
145 1.20 thorpej case CTL_DDB:
146 1.20 thorpej fn = ddb_sysctl;
147 1.1 cgd break;
148 1.1 cgd #endif
149 1.1 cgd default:
150 1.1 cgd return (EOPNOTSUPP);
151 1.1 cgd }
152 1.1 cgd
153 1.5 cgd if (SCARG(uap, oldlenp) &&
154 1.5 cgd (error = copyin(SCARG(uap, oldlenp), &oldlen, sizeof(oldlen))))
155 1.1 cgd return (error);
156 1.5 cgd if (SCARG(uap, old) != NULL) {
157 1.31 mrg if (!uvm_useracc(SCARG(uap, old), oldlen, B_WRITE))
158 1.1 cgd return (EFAULT);
159 1.1 cgd while (memlock.sl_lock) {
160 1.1 cgd memlock.sl_want = 1;
161 1.1 cgd sleep((caddr_t)&memlock, PRIBIO+1);
162 1.1 cgd memlock.sl_locked++;
163 1.1 cgd }
164 1.1 cgd memlock.sl_lock = 1;
165 1.45 thorpej if (dolock) {
166 1.45 thorpej /*
167 1.45 thorpej * XXX Um, this is kind of evil. What should we
168 1.45 thorpej * XXX be passing here?
169 1.45 thorpej */
170 1.45 thorpej uvm_vslock(p, SCARG(uap, old), oldlen, VM_PROT_NONE);
171 1.45 thorpej }
172 1.1 cgd savelen = oldlen;
173 1.1 cgd }
174 1.5 cgd error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
175 1.5 cgd &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
176 1.5 cgd if (SCARG(uap, old) != NULL) {
177 1.1 cgd if (dolock)
178 1.35 thorpej uvm_vsunlock(p, SCARG(uap, old), savelen);
179 1.1 cgd memlock.sl_lock = 0;
180 1.1 cgd if (memlock.sl_want) {
181 1.1 cgd memlock.sl_want = 0;
182 1.1 cgd wakeup((caddr_t)&memlock);
183 1.1 cgd }
184 1.1 cgd }
185 1.1 cgd if (error)
186 1.1 cgd return (error);
187 1.5 cgd if (SCARG(uap, oldlenp))
188 1.5 cgd error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
189 1.16 thorpej return (error);
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd /*
193 1.1 cgd * Attributes stored in the kernel.
194 1.1 cgd */
195 1.1 cgd char hostname[MAXHOSTNAMELEN];
196 1.1 cgd int hostnamelen;
197 1.1 cgd char domainname[MAXHOSTNAMELEN];
198 1.1 cgd int domainnamelen;
199 1.1 cgd long hostid;
200 1.8 cgd #ifdef INSECURE
201 1.8 cgd int securelevel = -1;
202 1.8 cgd #else
203 1.17 mrg int securelevel = 0;
204 1.8 cgd #endif
205 1.37 nathanw #ifdef SHORTCORENAME
206 1.37 nathanw int shortcorename = 1;
207 1.37 nathanw #else
208 1.37 nathanw int shortcorename = 0;
209 1.37 nathanw #endif
210 1.1 cgd
211 1.1 cgd /*
212 1.1 cgd * kernel related system variables.
213 1.1 cgd */
214 1.13 christos int
215 1.1 cgd kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
216 1.1 cgd int *name;
217 1.1 cgd u_int namelen;
218 1.1 cgd void *oldp;
219 1.1 cgd size_t *oldlenp;
220 1.1 cgd void *newp;
221 1.1 cgd size_t newlen;
222 1.1 cgd struct proc *p;
223 1.1 cgd {
224 1.1 cgd int error, level, inthostid;
225 1.18 explorer int old_autonicetime;
226 1.22 tls int old_vnodes;
227 1.37 nathanw int old_shortcorename;
228 1.1 cgd extern char ostype[], osrelease[], version[];
229 1.1 cgd
230 1.44 thorpej /* All sysctl names at this level, except for a few, are terminal. */
231 1.44 thorpej switch (name[0]) {
232 1.44 thorpej case KERN_PROC:
233 1.44 thorpej case KERN_PROF:
234 1.44 thorpej case KERN_MBUF:
235 1.44 thorpej /* Not terminal. */
236 1.44 thorpej break;
237 1.44 thorpej default:
238 1.44 thorpej if (namelen != 1)
239 1.44 thorpej return (ENOTDIR); /* overloaded */
240 1.44 thorpej }
241 1.1 cgd
242 1.1 cgd switch (name[0]) {
243 1.1 cgd case KERN_OSTYPE:
244 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
245 1.1 cgd case KERN_OSRELEASE:
246 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
247 1.1 cgd case KERN_OSREV:
248 1.25 mikel return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
249 1.1 cgd case KERN_VERSION:
250 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, version));
251 1.1 cgd case KERN_MAXVNODES:
252 1.22 tls old_vnodes = desiredvnodes;
253 1.29 sommerfe error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
254 1.29 sommerfe if (old_vnodes > desiredvnodes) {
255 1.29 sommerfe desiredvnodes = old_vnodes;
256 1.22 tls return (EINVAL);
257 1.29 sommerfe }
258 1.22 tls return (error);
259 1.1 cgd case KERN_MAXPROC:
260 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
261 1.1 cgd case KERN_MAXFILES:
262 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
263 1.1 cgd case KERN_ARGMAX:
264 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
265 1.1 cgd case KERN_SECURELVL:
266 1.1 cgd level = securelevel;
267 1.1 cgd if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
268 1.1 cgd newp == NULL)
269 1.1 cgd return (error);
270 1.1 cgd if (level < securelevel && p->p_pid != 1)
271 1.1 cgd return (EPERM);
272 1.1 cgd securelevel = level;
273 1.1 cgd return (0);
274 1.1 cgd case KERN_HOSTNAME:
275 1.1 cgd error = sysctl_string(oldp, oldlenp, newp, newlen,
276 1.1 cgd hostname, sizeof(hostname));
277 1.1 cgd if (newp && !error)
278 1.1 cgd hostnamelen = newlen;
279 1.1 cgd return (error);
280 1.1 cgd case KERN_DOMAINNAME:
281 1.1 cgd error = sysctl_string(oldp, oldlenp, newp, newlen,
282 1.1 cgd domainname, sizeof(domainname));
283 1.1 cgd if (newp && !error)
284 1.1 cgd domainnamelen = newlen;
285 1.1 cgd return (error);
286 1.1 cgd case KERN_HOSTID:
287 1.1 cgd inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
288 1.1 cgd error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
289 1.1 cgd hostid = inthostid;
290 1.1 cgd return (error);
291 1.1 cgd case KERN_CLOCKRATE:
292 1.1 cgd return (sysctl_clockrate(oldp, oldlenp));
293 1.1 cgd case KERN_BOOTTIME:
294 1.1 cgd return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
295 1.1 cgd sizeof(struct timeval)));
296 1.1 cgd case KERN_VNODE:
297 1.34 fvdl return (sysctl_vnode(oldp, oldlenp, p));
298 1.1 cgd case KERN_PROC:
299 1.1 cgd return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
300 1.1 cgd case KERN_FILE:
301 1.1 cgd return (sysctl_file(oldp, oldlenp));
302 1.1 cgd #ifdef GPROF
303 1.1 cgd case KERN_PROF:
304 1.1 cgd return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
305 1.1 cgd newp, newlen));
306 1.1 cgd #endif
307 1.1 cgd case KERN_POSIX1:
308 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
309 1.1 cgd case KERN_NGROUPS:
310 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
311 1.1 cgd case KERN_JOB_CONTROL:
312 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1));
313 1.1 cgd case KERN_SAVED_IDS:
314 1.1 cgd #ifdef _POSIX_SAVED_IDS
315 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1));
316 1.1 cgd #else
317 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 0));
318 1.1 cgd #endif
319 1.7 cgd case KERN_MAXPARTITIONS:
320 1.7 cgd return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
321 1.10 thorpej case KERN_RAWPARTITION:
322 1.10 thorpej return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
323 1.19 thorpej #ifdef NTP
324 1.15 jonathan case KERN_NTPTIME:
325 1.15 jonathan return (sysctl_ntptime(oldp, oldlenp));
326 1.19 thorpej #endif
327 1.18 explorer case KERN_AUTONICETIME:
328 1.18 explorer old_autonicetime = autonicetime;
329 1.18 explorer error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
330 1.18 explorer if (autonicetime < 0)
331 1.18 explorer autonicetime = old_autonicetime;
332 1.18 explorer return (error);
333 1.18 explorer case KERN_AUTONICEVAL:
334 1.18 explorer error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
335 1.18 explorer if (autoniceval < PRIO_MIN)
336 1.18 explorer autoniceval = PRIO_MIN;
337 1.18 explorer if (autoniceval > PRIO_MAX)
338 1.18 explorer autoniceval = PRIO_MAX;
339 1.18 explorer return (error);
340 1.21 perry case KERN_RTC_OFFSET:
341 1.21 perry return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
342 1.23 thorpej case KERN_ROOT_DEVICE:
343 1.23 thorpej return (sysctl_rdstring(oldp, oldlenp, newp,
344 1.23 thorpej root_device->dv_xname));
345 1.28 leo case KERN_MSGBUFSIZE:
346 1.28 leo /*
347 1.28 leo * deal with cases where the message buffer has
348 1.28 leo * become corrupted.
349 1.28 leo */
350 1.28 leo if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
351 1.28 leo msgbufenabled = 0;
352 1.28 leo return (ENXIO);
353 1.28 leo }
354 1.28 leo return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
355 1.36 kleink case KERN_FSYNC:
356 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
357 1.36 kleink case KERN_SYSVMSG:
358 1.36 kleink #ifdef SYSVMSG
359 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
360 1.36 kleink #else
361 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
362 1.36 kleink #endif
363 1.36 kleink case KERN_SYSVSEM:
364 1.36 kleink #ifdef SYSVSEM
365 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
366 1.36 kleink #else
367 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
368 1.36 kleink #endif
369 1.36 kleink case KERN_SYSVSHM:
370 1.36 kleink #ifdef SYSVSHM
371 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
372 1.36 kleink #else
373 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
374 1.36 kleink #endif
375 1.37 nathanw case KERN_SHORTCORENAME:
376 1.37 nathanw /* Only allow values of zero or one. */
377 1.37 nathanw old_shortcorename = shortcorename;
378 1.37 nathanw error = sysctl_int(oldp, oldlenp, newp, newlen,
379 1.37 nathanw &shortcorename);
380 1.37 nathanw if (shortcorename != 0 && shortcorename != 1) {
381 1.37 nathanw shortcorename = old_shortcorename;
382 1.37 nathanw return (EINVAL);
383 1.37 nathanw }
384 1.37 nathanw return (error);
385 1.40 kleink case KERN_SYNCHRONIZED_IO:
386 1.40 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
387 1.40 kleink case KERN_IOV_MAX:
388 1.40 kleink return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
389 1.44 thorpej case KERN_MBUF:
390 1.44 thorpej return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
391 1.44 thorpej newp, newlen));
392 1.1 cgd default:
393 1.1 cgd return (EOPNOTSUPP);
394 1.1 cgd }
395 1.1 cgd /* NOTREACHED */
396 1.1 cgd }
397 1.1 cgd
398 1.1 cgd /*
399 1.1 cgd * hardware related system variables.
400 1.1 cgd */
401 1.13 christos int
402 1.1 cgd hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
403 1.1 cgd int *name;
404 1.1 cgd u_int namelen;
405 1.1 cgd void *oldp;
406 1.1 cgd size_t *oldlenp;
407 1.1 cgd void *newp;
408 1.1 cgd size_t newlen;
409 1.1 cgd struct proc *p;
410 1.1 cgd {
411 1.27 veego extern char machine[], machine_arch[], cpu_model[];
412 1.1 cgd
413 1.1 cgd /* all sysctl names at this level are terminal */
414 1.1 cgd if (namelen != 1)
415 1.1 cgd return (ENOTDIR); /* overloaded */
416 1.1 cgd
417 1.1 cgd switch (name[0]) {
418 1.1 cgd case HW_MACHINE:
419 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, machine));
420 1.27 veego case HW_MACHINE_ARCH:
421 1.27 veego return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
422 1.1 cgd case HW_MODEL:
423 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
424 1.1 cgd case HW_NCPU:
425 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */
426 1.1 cgd case HW_BYTEORDER:
427 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
428 1.1 cgd case HW_PHYSMEM:
429 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
430 1.1 cgd case HW_USERMEM:
431 1.31 mrg return (sysctl_rdint(oldp, oldlenp, newp,
432 1.31 mrg ctob(physmem - uvmexp.wired)));
433 1.1 cgd case HW_PAGESIZE:
434 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
435 1.1 cgd default:
436 1.1 cgd return (EOPNOTSUPP);
437 1.1 cgd }
438 1.1 cgd /* NOTREACHED */
439 1.1 cgd }
440 1.1 cgd
441 1.1 cgd #ifdef DEBUG
442 1.1 cgd /*
443 1.1 cgd * Debugging related system variables.
444 1.1 cgd */
445 1.1 cgd struct ctldebug debug0, debug1, debug2, debug3, debug4;
446 1.1 cgd struct ctldebug debug5, debug6, debug7, debug8, debug9;
447 1.1 cgd struct ctldebug debug10, debug11, debug12, debug13, debug14;
448 1.1 cgd struct ctldebug debug15, debug16, debug17, debug18, debug19;
449 1.1 cgd static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
450 1.1 cgd &debug0, &debug1, &debug2, &debug3, &debug4,
451 1.1 cgd &debug5, &debug6, &debug7, &debug8, &debug9,
452 1.1 cgd &debug10, &debug11, &debug12, &debug13, &debug14,
453 1.1 cgd &debug15, &debug16, &debug17, &debug18, &debug19,
454 1.1 cgd };
455 1.1 cgd int
456 1.1 cgd debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
457 1.1 cgd int *name;
458 1.1 cgd u_int namelen;
459 1.1 cgd void *oldp;
460 1.1 cgd size_t *oldlenp;
461 1.1 cgd void *newp;
462 1.1 cgd size_t newlen;
463 1.1 cgd struct proc *p;
464 1.1 cgd {
465 1.1 cgd struct ctldebug *cdp;
466 1.1 cgd
467 1.1 cgd /* all sysctl names at this level are name and field */
468 1.1 cgd if (namelen != 2)
469 1.1 cgd return (ENOTDIR); /* overloaded */
470 1.1 cgd cdp = debugvars[name[0]];
471 1.34 fvdl if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
472 1.1 cgd return (EOPNOTSUPP);
473 1.1 cgd switch (name[1]) {
474 1.1 cgd case CTL_DEBUG_NAME:
475 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
476 1.1 cgd case CTL_DEBUG_VALUE:
477 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
478 1.1 cgd default:
479 1.1 cgd return (EOPNOTSUPP);
480 1.1 cgd }
481 1.1 cgd /* NOTREACHED */
482 1.1 cgd }
483 1.1 cgd #endif /* DEBUG */
484 1.1 cgd
485 1.1 cgd /*
486 1.1 cgd * Validate parameters and get old / set new parameters
487 1.1 cgd * for an integer-valued sysctl function.
488 1.1 cgd */
489 1.13 christos int
490 1.1 cgd sysctl_int(oldp, oldlenp, newp, newlen, valp)
491 1.1 cgd void *oldp;
492 1.1 cgd size_t *oldlenp;
493 1.1 cgd void *newp;
494 1.1 cgd size_t newlen;
495 1.1 cgd int *valp;
496 1.1 cgd {
497 1.1 cgd int error = 0;
498 1.1 cgd
499 1.1 cgd if (oldp && *oldlenp < sizeof(int))
500 1.1 cgd return (ENOMEM);
501 1.1 cgd if (newp && newlen != sizeof(int))
502 1.1 cgd return (EINVAL);
503 1.1 cgd *oldlenp = sizeof(int);
504 1.1 cgd if (oldp)
505 1.1 cgd error = copyout(valp, oldp, sizeof(int));
506 1.1 cgd if (error == 0 && newp)
507 1.1 cgd error = copyin(newp, valp, sizeof(int));
508 1.1 cgd return (error);
509 1.1 cgd }
510 1.1 cgd
511 1.1 cgd /*
512 1.1 cgd * As above, but read-only.
513 1.1 cgd */
514 1.13 christos int
515 1.1 cgd sysctl_rdint(oldp, oldlenp, newp, val)
516 1.1 cgd void *oldp;
517 1.1 cgd size_t *oldlenp;
518 1.1 cgd void *newp;
519 1.1 cgd int val;
520 1.1 cgd {
521 1.1 cgd int error = 0;
522 1.1 cgd
523 1.1 cgd if (oldp && *oldlenp < sizeof(int))
524 1.1 cgd return (ENOMEM);
525 1.1 cgd if (newp)
526 1.1 cgd return (EPERM);
527 1.1 cgd *oldlenp = sizeof(int);
528 1.1 cgd if (oldp)
529 1.1 cgd error = copyout((caddr_t)&val, oldp, sizeof(int));
530 1.1 cgd return (error);
531 1.1 cgd }
532 1.1 cgd
533 1.1 cgd /*
534 1.1 cgd * Validate parameters and get old / set new parameters
535 1.1 cgd * for a string-valued sysctl function.
536 1.1 cgd */
537 1.13 christos int
538 1.1 cgd sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
539 1.1 cgd void *oldp;
540 1.1 cgd size_t *oldlenp;
541 1.1 cgd void *newp;
542 1.1 cgd size_t newlen;
543 1.1 cgd char *str;
544 1.1 cgd int maxlen;
545 1.1 cgd {
546 1.1 cgd int len, error = 0;
547 1.1 cgd
548 1.1 cgd len = strlen(str) + 1;
549 1.1 cgd if (oldp && *oldlenp < len)
550 1.1 cgd return (ENOMEM);
551 1.1 cgd if (newp && newlen >= maxlen)
552 1.1 cgd return (EINVAL);
553 1.1 cgd if (oldp) {
554 1.1 cgd *oldlenp = len;
555 1.1 cgd error = copyout(str, oldp, len);
556 1.1 cgd }
557 1.1 cgd if (error == 0 && newp) {
558 1.1 cgd error = copyin(newp, str, newlen);
559 1.1 cgd str[newlen] = 0;
560 1.1 cgd }
561 1.1 cgd return (error);
562 1.1 cgd }
563 1.1 cgd
564 1.1 cgd /*
565 1.1 cgd * As above, but read-only.
566 1.1 cgd */
567 1.13 christos int
568 1.1 cgd sysctl_rdstring(oldp, oldlenp, newp, str)
569 1.1 cgd void *oldp;
570 1.1 cgd size_t *oldlenp;
571 1.1 cgd void *newp;
572 1.1 cgd char *str;
573 1.1 cgd {
574 1.1 cgd int len, error = 0;
575 1.1 cgd
576 1.1 cgd len = strlen(str) + 1;
577 1.1 cgd if (oldp && *oldlenp < len)
578 1.1 cgd return (ENOMEM);
579 1.1 cgd if (newp)
580 1.1 cgd return (EPERM);
581 1.1 cgd *oldlenp = len;
582 1.1 cgd if (oldp)
583 1.1 cgd error = copyout(str, oldp, len);
584 1.1 cgd return (error);
585 1.1 cgd }
586 1.1 cgd
587 1.1 cgd /*
588 1.1 cgd * Validate parameters and get old / set new parameters
589 1.1 cgd * for a structure oriented sysctl function.
590 1.1 cgd */
591 1.13 christos int
592 1.1 cgd sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
593 1.1 cgd void *oldp;
594 1.1 cgd size_t *oldlenp;
595 1.1 cgd void *newp;
596 1.1 cgd size_t newlen;
597 1.1 cgd void *sp;
598 1.1 cgd int len;
599 1.1 cgd {
600 1.1 cgd int error = 0;
601 1.1 cgd
602 1.1 cgd if (oldp && *oldlenp < len)
603 1.1 cgd return (ENOMEM);
604 1.1 cgd if (newp && newlen > len)
605 1.1 cgd return (EINVAL);
606 1.1 cgd if (oldp) {
607 1.1 cgd *oldlenp = len;
608 1.1 cgd error = copyout(sp, oldp, len);
609 1.1 cgd }
610 1.1 cgd if (error == 0 && newp)
611 1.1 cgd error = copyin(newp, sp, len);
612 1.1 cgd return (error);
613 1.1 cgd }
614 1.1 cgd
615 1.1 cgd /*
616 1.1 cgd * Validate parameters and get old parameters
617 1.1 cgd * for a structure oriented sysctl function.
618 1.1 cgd */
619 1.13 christos int
620 1.1 cgd sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
621 1.1 cgd void *oldp;
622 1.1 cgd size_t *oldlenp;
623 1.1 cgd void *newp, *sp;
624 1.1 cgd int len;
625 1.1 cgd {
626 1.1 cgd int error = 0;
627 1.1 cgd
628 1.1 cgd if (oldp && *oldlenp < len)
629 1.1 cgd return (ENOMEM);
630 1.1 cgd if (newp)
631 1.1 cgd return (EPERM);
632 1.1 cgd *oldlenp = len;
633 1.1 cgd if (oldp)
634 1.1 cgd error = copyout(sp, oldp, len);
635 1.1 cgd return (error);
636 1.1 cgd }
637 1.1 cgd
638 1.1 cgd /*
639 1.1 cgd * Get file structures.
640 1.1 cgd */
641 1.13 christos int
642 1.1 cgd sysctl_file(where, sizep)
643 1.1 cgd char *where;
644 1.1 cgd size_t *sizep;
645 1.1 cgd {
646 1.1 cgd int buflen, error;
647 1.1 cgd struct file *fp;
648 1.1 cgd char *start = where;
649 1.1 cgd
650 1.1 cgd buflen = *sizep;
651 1.1 cgd if (where == NULL) {
652 1.1 cgd /*
653 1.1 cgd * overestimate by 10 files
654 1.1 cgd */
655 1.1 cgd *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
656 1.1 cgd return (0);
657 1.1 cgd }
658 1.1 cgd
659 1.1 cgd /*
660 1.1 cgd * first copyout filehead
661 1.1 cgd */
662 1.1 cgd if (buflen < sizeof(filehead)) {
663 1.1 cgd *sizep = 0;
664 1.1 cgd return (0);
665 1.1 cgd }
666 1.13 christos error = copyout((caddr_t)&filehead, where, sizeof(filehead));
667 1.13 christos if (error)
668 1.1 cgd return (error);
669 1.1 cgd buflen -= sizeof(filehead);
670 1.1 cgd where += sizeof(filehead);
671 1.1 cgd
672 1.1 cgd /*
673 1.1 cgd * followed by an array of file structures
674 1.1 cgd */
675 1.3 mycroft for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
676 1.1 cgd if (buflen < sizeof(struct file)) {
677 1.1 cgd *sizep = where - start;
678 1.1 cgd return (ENOMEM);
679 1.1 cgd }
680 1.39 perry error = copyout((caddr_t)fp, where, sizeof(struct file));
681 1.13 christos if (error)
682 1.1 cgd return (error);
683 1.1 cgd buflen -= sizeof(struct file);
684 1.1 cgd where += sizeof(struct file);
685 1.1 cgd }
686 1.1 cgd *sizep = where - start;
687 1.1 cgd return (0);
688 1.1 cgd }
689 1.1 cgd
690 1.1 cgd /*
691 1.1 cgd * try over estimating by 5 procs
692 1.1 cgd */
693 1.39 perry #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
694 1.1 cgd
695 1.13 christos int
696 1.1 cgd sysctl_doproc(name, namelen, where, sizep)
697 1.1 cgd int *name;
698 1.1 cgd u_int namelen;
699 1.1 cgd char *where;
700 1.1 cgd size_t *sizep;
701 1.1 cgd {
702 1.1 cgd register struct proc *p;
703 1.1 cgd register struct kinfo_proc *dp = (struct kinfo_proc *)where;
704 1.1 cgd register int needed = 0;
705 1.1 cgd int buflen = where != NULL ? *sizep : 0;
706 1.41 thorpej const struct proclist_desc *pd;
707 1.1 cgd struct eproc eproc;
708 1.1 cgd int error = 0;
709 1.1 cgd
710 1.1 cgd if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
711 1.1 cgd return (EINVAL);
712 1.41 thorpej
713 1.41 thorpej pd = proclists;
714 1.1 cgd again:
715 1.41 thorpej for (p = LIST_FIRST(pd->pd_list); p != NULL;
716 1.41 thorpej p = LIST_NEXT(p, p_list)) {
717 1.1 cgd /*
718 1.1 cgd * Skip embryonic processes.
719 1.1 cgd */
720 1.1 cgd if (p->p_stat == SIDL)
721 1.1 cgd continue;
722 1.1 cgd /*
723 1.1 cgd * TODO - make more efficient (see notes below).
724 1.1 cgd * do by session.
725 1.1 cgd */
726 1.1 cgd switch (name[0]) {
727 1.1 cgd
728 1.1 cgd case KERN_PROC_PID:
729 1.1 cgd /* could do this with just a lookup */
730 1.1 cgd if (p->p_pid != (pid_t)name[1])
731 1.1 cgd continue;
732 1.1 cgd break;
733 1.1 cgd
734 1.1 cgd case KERN_PROC_PGRP:
735 1.1 cgd /* could do this by traversing pgrp */
736 1.1 cgd if (p->p_pgrp->pg_id != (pid_t)name[1])
737 1.1 cgd continue;
738 1.1 cgd break;
739 1.1 cgd
740 1.1 cgd case KERN_PROC_TTY:
741 1.1 cgd if ((p->p_flag & P_CONTROLT) == 0 ||
742 1.1 cgd p->p_session->s_ttyp == NULL ||
743 1.1 cgd p->p_session->s_ttyp->t_dev != (dev_t)name[1])
744 1.1 cgd continue;
745 1.1 cgd break;
746 1.1 cgd
747 1.1 cgd case KERN_PROC_UID:
748 1.1 cgd if (p->p_ucred->cr_uid != (uid_t)name[1])
749 1.1 cgd continue;
750 1.1 cgd break;
751 1.1 cgd
752 1.1 cgd case KERN_PROC_RUID:
753 1.1 cgd if (p->p_cred->p_ruid != (uid_t)name[1])
754 1.1 cgd continue;
755 1.1 cgd break;
756 1.1 cgd }
757 1.1 cgd if (buflen >= sizeof(struct kinfo_proc)) {
758 1.1 cgd fill_eproc(p, &eproc);
759 1.13 christos error = copyout((caddr_t)p, &dp->kp_proc,
760 1.13 christos sizeof(struct proc));
761 1.13 christos if (error)
762 1.1 cgd return (error);
763 1.13 christos error = copyout((caddr_t)&eproc, &dp->kp_eproc,
764 1.13 christos sizeof(eproc));
765 1.13 christos if (error)
766 1.1 cgd return (error);
767 1.1 cgd dp++;
768 1.1 cgd buflen -= sizeof(struct kinfo_proc);
769 1.1 cgd }
770 1.1 cgd needed += sizeof(struct kinfo_proc);
771 1.1 cgd }
772 1.41 thorpej pd++;
773 1.41 thorpej if (pd->pd_list != NULL)
774 1.1 cgd goto again;
775 1.41 thorpej
776 1.1 cgd if (where != NULL) {
777 1.1 cgd *sizep = (caddr_t)dp - where;
778 1.1 cgd if (needed > *sizep)
779 1.1 cgd return (ENOMEM);
780 1.1 cgd } else {
781 1.1 cgd needed += KERN_PROCSLOP;
782 1.1 cgd *sizep = needed;
783 1.1 cgd }
784 1.1 cgd return (0);
785 1.1 cgd }
786 1.1 cgd
787 1.1 cgd /*
788 1.1 cgd * Fill in an eproc structure for the specified process.
789 1.1 cgd */
790 1.1 cgd void
791 1.1 cgd fill_eproc(p, ep)
792 1.1 cgd register struct proc *p;
793 1.1 cgd register struct eproc *ep;
794 1.1 cgd {
795 1.1 cgd register struct tty *tp;
796 1.1 cgd
797 1.1 cgd ep->e_paddr = p;
798 1.1 cgd ep->e_sess = p->p_pgrp->pg_session;
799 1.1 cgd ep->e_pcred = *p->p_cred;
800 1.1 cgd ep->e_ucred = *p->p_ucred;
801 1.1 cgd if (p->p_stat == SIDL || p->p_stat == SZOMB) {
802 1.1 cgd ep->e_vm.vm_rssize = 0;
803 1.1 cgd ep->e_vm.vm_tsize = 0;
804 1.1 cgd ep->e_vm.vm_dsize = 0;
805 1.1 cgd ep->e_vm.vm_ssize = 0;
806 1.1 cgd /* ep->e_vm.vm_pmap = XXX; */
807 1.1 cgd } else {
808 1.1 cgd register struct vmspace *vm = p->p_vmspace;
809 1.1 cgd
810 1.26 gwr ep->e_vm.vm_rssize = vm_resident_count(vm);
811 1.1 cgd ep->e_vm.vm_tsize = vm->vm_tsize;
812 1.1 cgd ep->e_vm.vm_dsize = vm->vm_dsize;
813 1.1 cgd ep->e_vm.vm_ssize = vm->vm_ssize;
814 1.1 cgd }
815 1.1 cgd if (p->p_pptr)
816 1.1 cgd ep->e_ppid = p->p_pptr->p_pid;
817 1.1 cgd else
818 1.1 cgd ep->e_ppid = 0;
819 1.1 cgd ep->e_pgid = p->p_pgrp->pg_id;
820 1.33 thorpej ep->e_sid = ep->e_sess->s_sid;
821 1.1 cgd ep->e_jobc = p->p_pgrp->pg_jobc;
822 1.1 cgd if ((p->p_flag & P_CONTROLT) &&
823 1.1 cgd (tp = ep->e_sess->s_ttyp)) {
824 1.1 cgd ep->e_tdev = tp->t_dev;
825 1.1 cgd ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
826 1.1 cgd ep->e_tsess = tp->t_session;
827 1.1 cgd } else
828 1.1 cgd ep->e_tdev = NODEV;
829 1.1 cgd if (p->p_wmesg)
830 1.1 cgd strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
831 1.1 cgd ep->e_xsize = ep->e_xrssize = 0;
832 1.1 cgd ep->e_xccount = ep->e_xswrss = 0;
833 1.24 mycroft ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
834 1.24 mycroft if (SESS_LEADER(p))
835 1.24 mycroft ep->e_flag |= EPROC_SLEADER;
836 1.24 mycroft strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
837 1.1 cgd }
838