kern_sysctl.c revision 1.46 1 1.46 thorpej /* $NetBSD: kern_sysctl.c,v 1.46 1999/06/17 15:47:23 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.46 thorpej if (uvm_vslock(p, SCARG(uap, old), oldlen,
171 1.46 thorpej VM_PROT_NONE) != KERN_SUCCESS) {
172 1.46 thorpej memlock.sl_lock = 0;
173 1.46 thorpej if (memlock.sl_want) {
174 1.46 thorpej memlock.sl_want = 0;
175 1.46 thorpej wakeup((caddr_t)&memlock);
176 1.46 thorpej return (EFAULT);
177 1.46 thorpej }
178 1.46 thorpej }
179 1.45 thorpej }
180 1.1 cgd savelen = oldlen;
181 1.1 cgd }
182 1.5 cgd error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
183 1.5 cgd &oldlen, SCARG(uap, new), SCARG(uap, newlen), p);
184 1.5 cgd if (SCARG(uap, old) != NULL) {
185 1.1 cgd if (dolock)
186 1.35 thorpej uvm_vsunlock(p, SCARG(uap, old), savelen);
187 1.1 cgd memlock.sl_lock = 0;
188 1.1 cgd if (memlock.sl_want) {
189 1.1 cgd memlock.sl_want = 0;
190 1.1 cgd wakeup((caddr_t)&memlock);
191 1.1 cgd }
192 1.1 cgd }
193 1.1 cgd if (error)
194 1.1 cgd return (error);
195 1.5 cgd if (SCARG(uap, oldlenp))
196 1.5 cgd error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
197 1.16 thorpej return (error);
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd /*
201 1.1 cgd * Attributes stored in the kernel.
202 1.1 cgd */
203 1.1 cgd char hostname[MAXHOSTNAMELEN];
204 1.1 cgd int hostnamelen;
205 1.1 cgd char domainname[MAXHOSTNAMELEN];
206 1.1 cgd int domainnamelen;
207 1.1 cgd long hostid;
208 1.8 cgd #ifdef INSECURE
209 1.8 cgd int securelevel = -1;
210 1.8 cgd #else
211 1.17 mrg int securelevel = 0;
212 1.8 cgd #endif
213 1.37 nathanw #ifdef SHORTCORENAME
214 1.37 nathanw int shortcorename = 1;
215 1.37 nathanw #else
216 1.37 nathanw int shortcorename = 0;
217 1.37 nathanw #endif
218 1.1 cgd
219 1.1 cgd /*
220 1.1 cgd * kernel related system variables.
221 1.1 cgd */
222 1.13 christos int
223 1.1 cgd kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
224 1.1 cgd int *name;
225 1.1 cgd u_int namelen;
226 1.1 cgd void *oldp;
227 1.1 cgd size_t *oldlenp;
228 1.1 cgd void *newp;
229 1.1 cgd size_t newlen;
230 1.1 cgd struct proc *p;
231 1.1 cgd {
232 1.1 cgd int error, level, inthostid;
233 1.18 explorer int old_autonicetime;
234 1.22 tls int old_vnodes;
235 1.37 nathanw int old_shortcorename;
236 1.1 cgd extern char ostype[], osrelease[], version[];
237 1.1 cgd
238 1.44 thorpej /* All sysctl names at this level, except for a few, are terminal. */
239 1.44 thorpej switch (name[0]) {
240 1.44 thorpej case KERN_PROC:
241 1.44 thorpej case KERN_PROF:
242 1.44 thorpej case KERN_MBUF:
243 1.44 thorpej /* Not terminal. */
244 1.44 thorpej break;
245 1.44 thorpej default:
246 1.44 thorpej if (namelen != 1)
247 1.44 thorpej return (ENOTDIR); /* overloaded */
248 1.44 thorpej }
249 1.1 cgd
250 1.1 cgd switch (name[0]) {
251 1.1 cgd case KERN_OSTYPE:
252 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
253 1.1 cgd case KERN_OSRELEASE:
254 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
255 1.1 cgd case KERN_OSREV:
256 1.25 mikel return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
257 1.1 cgd case KERN_VERSION:
258 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, version));
259 1.1 cgd case KERN_MAXVNODES:
260 1.22 tls old_vnodes = desiredvnodes;
261 1.29 sommerfe error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
262 1.29 sommerfe if (old_vnodes > desiredvnodes) {
263 1.29 sommerfe desiredvnodes = old_vnodes;
264 1.22 tls return (EINVAL);
265 1.29 sommerfe }
266 1.22 tls return (error);
267 1.1 cgd case KERN_MAXPROC:
268 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
269 1.1 cgd case KERN_MAXFILES:
270 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
271 1.1 cgd case KERN_ARGMAX:
272 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
273 1.1 cgd case KERN_SECURELVL:
274 1.1 cgd level = securelevel;
275 1.1 cgd if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
276 1.1 cgd newp == NULL)
277 1.1 cgd return (error);
278 1.1 cgd if (level < securelevel && p->p_pid != 1)
279 1.1 cgd return (EPERM);
280 1.1 cgd securelevel = level;
281 1.1 cgd return (0);
282 1.1 cgd case KERN_HOSTNAME:
283 1.1 cgd error = sysctl_string(oldp, oldlenp, newp, newlen,
284 1.1 cgd hostname, sizeof(hostname));
285 1.1 cgd if (newp && !error)
286 1.1 cgd hostnamelen = newlen;
287 1.1 cgd return (error);
288 1.1 cgd case KERN_DOMAINNAME:
289 1.1 cgd error = sysctl_string(oldp, oldlenp, newp, newlen,
290 1.1 cgd domainname, sizeof(domainname));
291 1.1 cgd if (newp && !error)
292 1.1 cgd domainnamelen = newlen;
293 1.1 cgd return (error);
294 1.1 cgd case KERN_HOSTID:
295 1.1 cgd inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
296 1.1 cgd error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
297 1.1 cgd hostid = inthostid;
298 1.1 cgd return (error);
299 1.1 cgd case KERN_CLOCKRATE:
300 1.1 cgd return (sysctl_clockrate(oldp, oldlenp));
301 1.1 cgd case KERN_BOOTTIME:
302 1.1 cgd return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
303 1.1 cgd sizeof(struct timeval)));
304 1.1 cgd case KERN_VNODE:
305 1.34 fvdl return (sysctl_vnode(oldp, oldlenp, p));
306 1.1 cgd case KERN_PROC:
307 1.1 cgd return (sysctl_doproc(name + 1, namelen - 1, oldp, oldlenp));
308 1.1 cgd case KERN_FILE:
309 1.1 cgd return (sysctl_file(oldp, oldlenp));
310 1.1 cgd #ifdef GPROF
311 1.1 cgd case KERN_PROF:
312 1.1 cgd return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
313 1.1 cgd newp, newlen));
314 1.1 cgd #endif
315 1.1 cgd case KERN_POSIX1:
316 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
317 1.1 cgd case KERN_NGROUPS:
318 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
319 1.1 cgd case KERN_JOB_CONTROL:
320 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1));
321 1.1 cgd case KERN_SAVED_IDS:
322 1.1 cgd #ifdef _POSIX_SAVED_IDS
323 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1));
324 1.1 cgd #else
325 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 0));
326 1.1 cgd #endif
327 1.7 cgd case KERN_MAXPARTITIONS:
328 1.7 cgd return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
329 1.10 thorpej case KERN_RAWPARTITION:
330 1.10 thorpej return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
331 1.19 thorpej #ifdef NTP
332 1.15 jonathan case KERN_NTPTIME:
333 1.15 jonathan return (sysctl_ntptime(oldp, oldlenp));
334 1.19 thorpej #endif
335 1.18 explorer case KERN_AUTONICETIME:
336 1.18 explorer old_autonicetime = autonicetime;
337 1.18 explorer error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
338 1.18 explorer if (autonicetime < 0)
339 1.18 explorer autonicetime = old_autonicetime;
340 1.18 explorer return (error);
341 1.18 explorer case KERN_AUTONICEVAL:
342 1.18 explorer error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
343 1.18 explorer if (autoniceval < PRIO_MIN)
344 1.18 explorer autoniceval = PRIO_MIN;
345 1.18 explorer if (autoniceval > PRIO_MAX)
346 1.18 explorer autoniceval = PRIO_MAX;
347 1.18 explorer return (error);
348 1.21 perry case KERN_RTC_OFFSET:
349 1.21 perry return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
350 1.23 thorpej case KERN_ROOT_DEVICE:
351 1.23 thorpej return (sysctl_rdstring(oldp, oldlenp, newp,
352 1.23 thorpej root_device->dv_xname));
353 1.28 leo case KERN_MSGBUFSIZE:
354 1.28 leo /*
355 1.28 leo * deal with cases where the message buffer has
356 1.28 leo * become corrupted.
357 1.28 leo */
358 1.28 leo if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
359 1.28 leo msgbufenabled = 0;
360 1.28 leo return (ENXIO);
361 1.28 leo }
362 1.28 leo return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
363 1.36 kleink case KERN_FSYNC:
364 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
365 1.36 kleink case KERN_SYSVMSG:
366 1.36 kleink #ifdef SYSVMSG
367 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
368 1.36 kleink #else
369 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
370 1.36 kleink #endif
371 1.36 kleink case KERN_SYSVSEM:
372 1.36 kleink #ifdef SYSVSEM
373 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
374 1.36 kleink #else
375 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
376 1.36 kleink #endif
377 1.36 kleink case KERN_SYSVSHM:
378 1.36 kleink #ifdef SYSVSHM
379 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
380 1.36 kleink #else
381 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
382 1.36 kleink #endif
383 1.37 nathanw case KERN_SHORTCORENAME:
384 1.37 nathanw /* Only allow values of zero or one. */
385 1.37 nathanw old_shortcorename = shortcorename;
386 1.37 nathanw error = sysctl_int(oldp, oldlenp, newp, newlen,
387 1.37 nathanw &shortcorename);
388 1.37 nathanw if (shortcorename != 0 && shortcorename != 1) {
389 1.37 nathanw shortcorename = old_shortcorename;
390 1.37 nathanw return (EINVAL);
391 1.37 nathanw }
392 1.37 nathanw return (error);
393 1.40 kleink case KERN_SYNCHRONIZED_IO:
394 1.40 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
395 1.40 kleink case KERN_IOV_MAX:
396 1.40 kleink return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
397 1.44 thorpej case KERN_MBUF:
398 1.44 thorpej return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
399 1.44 thorpej newp, newlen));
400 1.1 cgd default:
401 1.1 cgd return (EOPNOTSUPP);
402 1.1 cgd }
403 1.1 cgd /* NOTREACHED */
404 1.1 cgd }
405 1.1 cgd
406 1.1 cgd /*
407 1.1 cgd * hardware related system variables.
408 1.1 cgd */
409 1.13 christos int
410 1.1 cgd hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
411 1.1 cgd int *name;
412 1.1 cgd u_int namelen;
413 1.1 cgd void *oldp;
414 1.1 cgd size_t *oldlenp;
415 1.1 cgd void *newp;
416 1.1 cgd size_t newlen;
417 1.1 cgd struct proc *p;
418 1.1 cgd {
419 1.27 veego extern char machine[], machine_arch[], cpu_model[];
420 1.1 cgd
421 1.1 cgd /* all sysctl names at this level are terminal */
422 1.1 cgd if (namelen != 1)
423 1.1 cgd return (ENOTDIR); /* overloaded */
424 1.1 cgd
425 1.1 cgd switch (name[0]) {
426 1.1 cgd case HW_MACHINE:
427 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, machine));
428 1.27 veego case HW_MACHINE_ARCH:
429 1.27 veego return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
430 1.1 cgd case HW_MODEL:
431 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
432 1.1 cgd case HW_NCPU:
433 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */
434 1.1 cgd case HW_BYTEORDER:
435 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
436 1.1 cgd case HW_PHYSMEM:
437 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
438 1.1 cgd case HW_USERMEM:
439 1.31 mrg return (sysctl_rdint(oldp, oldlenp, newp,
440 1.31 mrg ctob(physmem - uvmexp.wired)));
441 1.1 cgd case HW_PAGESIZE:
442 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
443 1.1 cgd default:
444 1.1 cgd return (EOPNOTSUPP);
445 1.1 cgd }
446 1.1 cgd /* NOTREACHED */
447 1.1 cgd }
448 1.1 cgd
449 1.1 cgd #ifdef DEBUG
450 1.1 cgd /*
451 1.1 cgd * Debugging related system variables.
452 1.1 cgd */
453 1.1 cgd struct ctldebug debug0, debug1, debug2, debug3, debug4;
454 1.1 cgd struct ctldebug debug5, debug6, debug7, debug8, debug9;
455 1.1 cgd struct ctldebug debug10, debug11, debug12, debug13, debug14;
456 1.1 cgd struct ctldebug debug15, debug16, debug17, debug18, debug19;
457 1.1 cgd static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
458 1.1 cgd &debug0, &debug1, &debug2, &debug3, &debug4,
459 1.1 cgd &debug5, &debug6, &debug7, &debug8, &debug9,
460 1.1 cgd &debug10, &debug11, &debug12, &debug13, &debug14,
461 1.1 cgd &debug15, &debug16, &debug17, &debug18, &debug19,
462 1.1 cgd };
463 1.1 cgd int
464 1.1 cgd debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
465 1.1 cgd int *name;
466 1.1 cgd u_int namelen;
467 1.1 cgd void *oldp;
468 1.1 cgd size_t *oldlenp;
469 1.1 cgd void *newp;
470 1.1 cgd size_t newlen;
471 1.1 cgd struct proc *p;
472 1.1 cgd {
473 1.1 cgd struct ctldebug *cdp;
474 1.1 cgd
475 1.1 cgd /* all sysctl names at this level are name and field */
476 1.1 cgd if (namelen != 2)
477 1.1 cgd return (ENOTDIR); /* overloaded */
478 1.1 cgd cdp = debugvars[name[0]];
479 1.34 fvdl if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
480 1.1 cgd return (EOPNOTSUPP);
481 1.1 cgd switch (name[1]) {
482 1.1 cgd case CTL_DEBUG_NAME:
483 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
484 1.1 cgd case CTL_DEBUG_VALUE:
485 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
486 1.1 cgd default:
487 1.1 cgd return (EOPNOTSUPP);
488 1.1 cgd }
489 1.1 cgd /* NOTREACHED */
490 1.1 cgd }
491 1.1 cgd #endif /* DEBUG */
492 1.1 cgd
493 1.1 cgd /*
494 1.1 cgd * Validate parameters and get old / set new parameters
495 1.1 cgd * for an integer-valued sysctl function.
496 1.1 cgd */
497 1.13 christos int
498 1.1 cgd sysctl_int(oldp, oldlenp, newp, newlen, valp)
499 1.1 cgd void *oldp;
500 1.1 cgd size_t *oldlenp;
501 1.1 cgd void *newp;
502 1.1 cgd size_t newlen;
503 1.1 cgd int *valp;
504 1.1 cgd {
505 1.1 cgd int error = 0;
506 1.1 cgd
507 1.1 cgd if (oldp && *oldlenp < sizeof(int))
508 1.1 cgd return (ENOMEM);
509 1.1 cgd if (newp && newlen != sizeof(int))
510 1.1 cgd return (EINVAL);
511 1.1 cgd *oldlenp = sizeof(int);
512 1.1 cgd if (oldp)
513 1.1 cgd error = copyout(valp, oldp, sizeof(int));
514 1.1 cgd if (error == 0 && newp)
515 1.1 cgd error = copyin(newp, valp, sizeof(int));
516 1.1 cgd return (error);
517 1.1 cgd }
518 1.1 cgd
519 1.1 cgd /*
520 1.1 cgd * As above, but read-only.
521 1.1 cgd */
522 1.13 christos int
523 1.1 cgd sysctl_rdint(oldp, oldlenp, newp, val)
524 1.1 cgd void *oldp;
525 1.1 cgd size_t *oldlenp;
526 1.1 cgd void *newp;
527 1.1 cgd int val;
528 1.1 cgd {
529 1.1 cgd int error = 0;
530 1.1 cgd
531 1.1 cgd if (oldp && *oldlenp < sizeof(int))
532 1.1 cgd return (ENOMEM);
533 1.1 cgd if (newp)
534 1.1 cgd return (EPERM);
535 1.1 cgd *oldlenp = sizeof(int);
536 1.1 cgd if (oldp)
537 1.1 cgd error = copyout((caddr_t)&val, oldp, sizeof(int));
538 1.1 cgd return (error);
539 1.1 cgd }
540 1.1 cgd
541 1.1 cgd /*
542 1.1 cgd * Validate parameters and get old / set new parameters
543 1.1 cgd * for a string-valued sysctl function.
544 1.1 cgd */
545 1.13 christos int
546 1.1 cgd sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
547 1.1 cgd void *oldp;
548 1.1 cgd size_t *oldlenp;
549 1.1 cgd void *newp;
550 1.1 cgd size_t newlen;
551 1.1 cgd char *str;
552 1.1 cgd int maxlen;
553 1.1 cgd {
554 1.1 cgd int len, error = 0;
555 1.1 cgd
556 1.1 cgd len = strlen(str) + 1;
557 1.1 cgd if (oldp && *oldlenp < len)
558 1.1 cgd return (ENOMEM);
559 1.1 cgd if (newp && newlen >= maxlen)
560 1.1 cgd return (EINVAL);
561 1.1 cgd if (oldp) {
562 1.1 cgd *oldlenp = len;
563 1.1 cgd error = copyout(str, oldp, len);
564 1.1 cgd }
565 1.1 cgd if (error == 0 && newp) {
566 1.1 cgd error = copyin(newp, str, newlen);
567 1.1 cgd str[newlen] = 0;
568 1.1 cgd }
569 1.1 cgd return (error);
570 1.1 cgd }
571 1.1 cgd
572 1.1 cgd /*
573 1.1 cgd * As above, but read-only.
574 1.1 cgd */
575 1.13 christos int
576 1.1 cgd sysctl_rdstring(oldp, oldlenp, newp, str)
577 1.1 cgd void *oldp;
578 1.1 cgd size_t *oldlenp;
579 1.1 cgd void *newp;
580 1.1 cgd char *str;
581 1.1 cgd {
582 1.1 cgd int len, error = 0;
583 1.1 cgd
584 1.1 cgd len = strlen(str) + 1;
585 1.1 cgd if (oldp && *oldlenp < len)
586 1.1 cgd return (ENOMEM);
587 1.1 cgd if (newp)
588 1.1 cgd return (EPERM);
589 1.1 cgd *oldlenp = len;
590 1.1 cgd if (oldp)
591 1.1 cgd error = copyout(str, oldp, len);
592 1.1 cgd return (error);
593 1.1 cgd }
594 1.1 cgd
595 1.1 cgd /*
596 1.1 cgd * Validate parameters and get old / set new parameters
597 1.1 cgd * for a structure oriented sysctl function.
598 1.1 cgd */
599 1.13 christos int
600 1.1 cgd sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
601 1.1 cgd void *oldp;
602 1.1 cgd size_t *oldlenp;
603 1.1 cgd void *newp;
604 1.1 cgd size_t newlen;
605 1.1 cgd void *sp;
606 1.1 cgd int len;
607 1.1 cgd {
608 1.1 cgd int error = 0;
609 1.1 cgd
610 1.1 cgd if (oldp && *oldlenp < len)
611 1.1 cgd return (ENOMEM);
612 1.1 cgd if (newp && newlen > len)
613 1.1 cgd return (EINVAL);
614 1.1 cgd if (oldp) {
615 1.1 cgd *oldlenp = len;
616 1.1 cgd error = copyout(sp, oldp, len);
617 1.1 cgd }
618 1.1 cgd if (error == 0 && newp)
619 1.1 cgd error = copyin(newp, sp, len);
620 1.1 cgd return (error);
621 1.1 cgd }
622 1.1 cgd
623 1.1 cgd /*
624 1.1 cgd * Validate parameters and get old parameters
625 1.1 cgd * for a structure oriented sysctl function.
626 1.1 cgd */
627 1.13 christos int
628 1.1 cgd sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
629 1.1 cgd void *oldp;
630 1.1 cgd size_t *oldlenp;
631 1.1 cgd void *newp, *sp;
632 1.1 cgd int len;
633 1.1 cgd {
634 1.1 cgd int error = 0;
635 1.1 cgd
636 1.1 cgd if (oldp && *oldlenp < len)
637 1.1 cgd return (ENOMEM);
638 1.1 cgd if (newp)
639 1.1 cgd return (EPERM);
640 1.1 cgd *oldlenp = len;
641 1.1 cgd if (oldp)
642 1.1 cgd error = copyout(sp, oldp, len);
643 1.1 cgd return (error);
644 1.1 cgd }
645 1.1 cgd
646 1.1 cgd /*
647 1.1 cgd * Get file structures.
648 1.1 cgd */
649 1.13 christos int
650 1.1 cgd sysctl_file(where, sizep)
651 1.1 cgd char *where;
652 1.1 cgd size_t *sizep;
653 1.1 cgd {
654 1.1 cgd int buflen, error;
655 1.1 cgd struct file *fp;
656 1.1 cgd char *start = where;
657 1.1 cgd
658 1.1 cgd buflen = *sizep;
659 1.1 cgd if (where == NULL) {
660 1.1 cgd /*
661 1.1 cgd * overestimate by 10 files
662 1.1 cgd */
663 1.1 cgd *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
664 1.1 cgd return (0);
665 1.1 cgd }
666 1.1 cgd
667 1.1 cgd /*
668 1.1 cgd * first copyout filehead
669 1.1 cgd */
670 1.1 cgd if (buflen < sizeof(filehead)) {
671 1.1 cgd *sizep = 0;
672 1.1 cgd return (0);
673 1.1 cgd }
674 1.13 christos error = copyout((caddr_t)&filehead, where, sizeof(filehead));
675 1.13 christos if (error)
676 1.1 cgd return (error);
677 1.1 cgd buflen -= sizeof(filehead);
678 1.1 cgd where += sizeof(filehead);
679 1.1 cgd
680 1.1 cgd /*
681 1.1 cgd * followed by an array of file structures
682 1.1 cgd */
683 1.3 mycroft for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
684 1.1 cgd if (buflen < sizeof(struct file)) {
685 1.1 cgd *sizep = where - start;
686 1.1 cgd return (ENOMEM);
687 1.1 cgd }
688 1.39 perry error = copyout((caddr_t)fp, where, sizeof(struct file));
689 1.13 christos if (error)
690 1.1 cgd return (error);
691 1.1 cgd buflen -= sizeof(struct file);
692 1.1 cgd where += sizeof(struct file);
693 1.1 cgd }
694 1.1 cgd *sizep = where - start;
695 1.1 cgd return (0);
696 1.1 cgd }
697 1.1 cgd
698 1.1 cgd /*
699 1.1 cgd * try over estimating by 5 procs
700 1.1 cgd */
701 1.39 perry #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
702 1.1 cgd
703 1.13 christos int
704 1.1 cgd sysctl_doproc(name, namelen, where, sizep)
705 1.1 cgd int *name;
706 1.1 cgd u_int namelen;
707 1.1 cgd char *where;
708 1.1 cgd size_t *sizep;
709 1.1 cgd {
710 1.1 cgd register struct proc *p;
711 1.1 cgd register struct kinfo_proc *dp = (struct kinfo_proc *)where;
712 1.1 cgd register int needed = 0;
713 1.1 cgd int buflen = where != NULL ? *sizep : 0;
714 1.41 thorpej const struct proclist_desc *pd;
715 1.1 cgd struct eproc eproc;
716 1.1 cgd int error = 0;
717 1.1 cgd
718 1.1 cgd if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
719 1.1 cgd return (EINVAL);
720 1.41 thorpej
721 1.41 thorpej pd = proclists;
722 1.1 cgd again:
723 1.41 thorpej for (p = LIST_FIRST(pd->pd_list); p != NULL;
724 1.41 thorpej p = LIST_NEXT(p, p_list)) {
725 1.1 cgd /*
726 1.1 cgd * Skip embryonic processes.
727 1.1 cgd */
728 1.1 cgd if (p->p_stat == SIDL)
729 1.1 cgd continue;
730 1.1 cgd /*
731 1.1 cgd * TODO - make more efficient (see notes below).
732 1.1 cgd * do by session.
733 1.1 cgd */
734 1.1 cgd switch (name[0]) {
735 1.1 cgd
736 1.1 cgd case KERN_PROC_PID:
737 1.1 cgd /* could do this with just a lookup */
738 1.1 cgd if (p->p_pid != (pid_t)name[1])
739 1.1 cgd continue;
740 1.1 cgd break;
741 1.1 cgd
742 1.1 cgd case KERN_PROC_PGRP:
743 1.1 cgd /* could do this by traversing pgrp */
744 1.1 cgd if (p->p_pgrp->pg_id != (pid_t)name[1])
745 1.1 cgd continue;
746 1.1 cgd break;
747 1.1 cgd
748 1.1 cgd case KERN_PROC_TTY:
749 1.1 cgd if ((p->p_flag & P_CONTROLT) == 0 ||
750 1.1 cgd p->p_session->s_ttyp == NULL ||
751 1.1 cgd p->p_session->s_ttyp->t_dev != (dev_t)name[1])
752 1.1 cgd continue;
753 1.1 cgd break;
754 1.1 cgd
755 1.1 cgd case KERN_PROC_UID:
756 1.1 cgd if (p->p_ucred->cr_uid != (uid_t)name[1])
757 1.1 cgd continue;
758 1.1 cgd break;
759 1.1 cgd
760 1.1 cgd case KERN_PROC_RUID:
761 1.1 cgd if (p->p_cred->p_ruid != (uid_t)name[1])
762 1.1 cgd continue;
763 1.1 cgd break;
764 1.1 cgd }
765 1.1 cgd if (buflen >= sizeof(struct kinfo_proc)) {
766 1.1 cgd fill_eproc(p, &eproc);
767 1.13 christos error = copyout((caddr_t)p, &dp->kp_proc,
768 1.13 christos sizeof(struct proc));
769 1.13 christos if (error)
770 1.1 cgd return (error);
771 1.13 christos error = copyout((caddr_t)&eproc, &dp->kp_eproc,
772 1.13 christos sizeof(eproc));
773 1.13 christos if (error)
774 1.1 cgd return (error);
775 1.1 cgd dp++;
776 1.1 cgd buflen -= sizeof(struct kinfo_proc);
777 1.1 cgd }
778 1.1 cgd needed += sizeof(struct kinfo_proc);
779 1.1 cgd }
780 1.41 thorpej pd++;
781 1.41 thorpej if (pd->pd_list != NULL)
782 1.1 cgd goto again;
783 1.41 thorpej
784 1.1 cgd if (where != NULL) {
785 1.1 cgd *sizep = (caddr_t)dp - where;
786 1.1 cgd if (needed > *sizep)
787 1.1 cgd return (ENOMEM);
788 1.1 cgd } else {
789 1.1 cgd needed += KERN_PROCSLOP;
790 1.1 cgd *sizep = needed;
791 1.1 cgd }
792 1.1 cgd return (0);
793 1.1 cgd }
794 1.1 cgd
795 1.1 cgd /*
796 1.1 cgd * Fill in an eproc structure for the specified process.
797 1.1 cgd */
798 1.1 cgd void
799 1.1 cgd fill_eproc(p, ep)
800 1.1 cgd register struct proc *p;
801 1.1 cgd register struct eproc *ep;
802 1.1 cgd {
803 1.1 cgd register struct tty *tp;
804 1.1 cgd
805 1.1 cgd ep->e_paddr = p;
806 1.1 cgd ep->e_sess = p->p_pgrp->pg_session;
807 1.1 cgd ep->e_pcred = *p->p_cred;
808 1.1 cgd ep->e_ucred = *p->p_ucred;
809 1.1 cgd if (p->p_stat == SIDL || p->p_stat == SZOMB) {
810 1.1 cgd ep->e_vm.vm_rssize = 0;
811 1.1 cgd ep->e_vm.vm_tsize = 0;
812 1.1 cgd ep->e_vm.vm_dsize = 0;
813 1.1 cgd ep->e_vm.vm_ssize = 0;
814 1.1 cgd /* ep->e_vm.vm_pmap = XXX; */
815 1.1 cgd } else {
816 1.1 cgd register struct vmspace *vm = p->p_vmspace;
817 1.1 cgd
818 1.26 gwr ep->e_vm.vm_rssize = vm_resident_count(vm);
819 1.1 cgd ep->e_vm.vm_tsize = vm->vm_tsize;
820 1.1 cgd ep->e_vm.vm_dsize = vm->vm_dsize;
821 1.1 cgd ep->e_vm.vm_ssize = vm->vm_ssize;
822 1.1 cgd }
823 1.1 cgd if (p->p_pptr)
824 1.1 cgd ep->e_ppid = p->p_pptr->p_pid;
825 1.1 cgd else
826 1.1 cgd ep->e_ppid = 0;
827 1.1 cgd ep->e_pgid = p->p_pgrp->pg_id;
828 1.33 thorpej ep->e_sid = ep->e_sess->s_sid;
829 1.1 cgd ep->e_jobc = p->p_pgrp->pg_jobc;
830 1.1 cgd if ((p->p_flag & P_CONTROLT) &&
831 1.1 cgd (tp = ep->e_sess->s_ttyp)) {
832 1.1 cgd ep->e_tdev = tp->t_dev;
833 1.1 cgd ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
834 1.1 cgd ep->e_tsess = tp->t_session;
835 1.1 cgd } else
836 1.1 cgd ep->e_tdev = NODEV;
837 1.1 cgd if (p->p_wmesg)
838 1.1 cgd strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
839 1.1 cgd ep->e_xsize = ep->e_xrssize = 0;
840 1.1 cgd ep->e_xccount = ep->e_xswrss = 0;
841 1.24 mycroft ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
842 1.24 mycroft if (SESS_LEADER(p))
843 1.24 mycroft ep->e_flag |= EPROC_SLEADER;
844 1.24 mycroft strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
845 1.1 cgd }
846