kern_sysctl.c revision 1.55 1 1.55 is /* $NetBSD: kern_sysctl.c,v 1.55 1999/11/17 23:24:54 is 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.52 bouyer #include "opt_defcorename.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.52 bouyer #include <sys/pool.h>
55 1.1 cgd #include <sys/proc.h>
56 1.1 cgd #include <sys/file.h>
57 1.1 cgd #include <sys/vnode.h>
58 1.1 cgd #include <sys/unistd.h>
59 1.1 cgd #include <sys/buf.h>
60 1.1 cgd #include <sys/ioctl.h>
61 1.1 cgd #include <sys/tty.h>
62 1.7 cgd #include <sys/disklabel.h>
63 1.23 thorpej #include <sys/device.h>
64 1.1 cgd #include <vm/vm.h>
65 1.1 cgd #include <sys/sysctl.h>
66 1.28 leo #include <sys/msgbuf.h>
67 1.1 cgd
68 1.31 mrg #include <uvm/uvm_extern.h>
69 1.31 mrg
70 1.5 cgd #include <sys/mount.h>
71 1.5 cgd #include <sys/syscallargs.h>
72 1.52 bouyer #include <sys/resource.h>
73 1.52 bouyer #include <sys/resourcevar.h>
74 1.1 cgd
75 1.38 jonathan
76 1.38 jonathan #if defined(DDB)
77 1.38 jonathan #include <ddb/ddbvar.h>
78 1.31 mrg #endif
79 1.31 mrg
80 1.1 cgd /*
81 1.1 cgd * Locking and stats
82 1.1 cgd */
83 1.1 cgd static struct sysctl_lock {
84 1.1 cgd int sl_lock;
85 1.1 cgd int sl_want;
86 1.1 cgd int sl_locked;
87 1.1 cgd } memlock;
88 1.1 cgd
89 1.1 cgd int
90 1.12 mycroft sys___sysctl(p, v, retval)
91 1.1 cgd struct proc *p;
92 1.11 thorpej void *v;
93 1.11 thorpej register_t *retval;
94 1.11 thorpej {
95 1.12 mycroft register struct sys___sysctl_args /* {
96 1.5 cgd syscallarg(int *) name;
97 1.5 cgd syscallarg(u_int) namelen;
98 1.5 cgd syscallarg(void *) old;
99 1.5 cgd syscallarg(size_t *) oldlenp;
100 1.5 cgd syscallarg(void *) new;
101 1.5 cgd syscallarg(size_t) newlen;
102 1.11 thorpej } */ *uap = v;
103 1.1 cgd int error, dolock = 1;
104 1.13 christos size_t savelen = 0, oldlen = 0;
105 1.1 cgd sysctlfn *fn;
106 1.1 cgd int name[CTL_MAXNAME];
107 1.55 is size_t *oldlenp;
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * all top-level sysctl names are non-terminal
111 1.1 cgd */
112 1.5 cgd if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
113 1.1 cgd return (EINVAL);
114 1.13 christos error = copyin(SCARG(uap, name), &name,
115 1.13 christos SCARG(uap, namelen) * sizeof(int));
116 1.13 christos if (error)
117 1.1 cgd return (error);
118 1.1 cgd
119 1.52 bouyer /*
120 1.52 bouyer * For all but CTL_PROC, must be root to change a value.
121 1.52 bouyer * For CTL_PROC, must be root, or owner of the proc (and not suid),
122 1.52 bouyer * this is checked in proc_sysctl() (once we know the targer proc).
123 1.52 bouyer */
124 1.52 bouyer if (SCARG(uap, new) != NULL && name[0] != CTL_PROC &&
125 1.52 bouyer (error = suser(p->p_ucred, &p->p_acflag)))
126 1.52 bouyer return error;
127 1.52 bouyer
128 1.1 cgd switch (name[0]) {
129 1.1 cgd case CTL_KERN:
130 1.1 cgd fn = kern_sysctl;
131 1.1 cgd if (name[2] != KERN_VNODE) /* XXX */
132 1.1 cgd dolock = 0;
133 1.1 cgd break;
134 1.1 cgd case CTL_HW:
135 1.1 cgd fn = hw_sysctl;
136 1.1 cgd break;
137 1.1 cgd case CTL_VM:
138 1.31 mrg fn = uvm_sysctl;
139 1.1 cgd break;
140 1.1 cgd case CTL_NET:
141 1.1 cgd fn = net_sysctl;
142 1.1 cgd break;
143 1.34 fvdl case CTL_VFS:
144 1.34 fvdl fn = vfs_sysctl;
145 1.1 cgd break;
146 1.1 cgd case CTL_MACHDEP:
147 1.1 cgd fn = cpu_sysctl;
148 1.1 cgd break;
149 1.1 cgd #ifdef DEBUG
150 1.1 cgd case CTL_DEBUG:
151 1.1 cgd fn = debug_sysctl;
152 1.20 thorpej break;
153 1.20 thorpej #endif
154 1.20 thorpej #ifdef DDB
155 1.20 thorpej case CTL_DDB:
156 1.20 thorpej fn = ddb_sysctl;
157 1.1 cgd break;
158 1.1 cgd #endif
159 1.52 bouyer case CTL_PROC:
160 1.52 bouyer fn = proc_sysctl;
161 1.52 bouyer break;
162 1.1 cgd default:
163 1.1 cgd return (EOPNOTSUPP);
164 1.1 cgd }
165 1.1 cgd
166 1.55 is oldlenp = SCARG(uap, oldlenp);
167 1.55 is if (oldlenp) {
168 1.55 is if ((error = copyin(oldlenp, &oldlen, sizeof(oldlen))))
169 1.55 is return (error);
170 1.55 is oldlenp = &oldlen;
171 1.55 is }
172 1.5 cgd if (SCARG(uap, old) != NULL) {
173 1.31 mrg if (!uvm_useracc(SCARG(uap, old), oldlen, B_WRITE))
174 1.1 cgd return (EFAULT);
175 1.1 cgd while (memlock.sl_lock) {
176 1.1 cgd memlock.sl_want = 1;
177 1.1 cgd sleep((caddr_t)&memlock, PRIBIO+1);
178 1.1 cgd memlock.sl_locked++;
179 1.1 cgd }
180 1.1 cgd memlock.sl_lock = 1;
181 1.45 thorpej if (dolock) {
182 1.45 thorpej /*
183 1.45 thorpej * XXX Um, this is kind of evil. What should we
184 1.45 thorpej * XXX be passing here?
185 1.45 thorpej */
186 1.46 thorpej if (uvm_vslock(p, SCARG(uap, old), oldlen,
187 1.46 thorpej VM_PROT_NONE) != KERN_SUCCESS) {
188 1.46 thorpej memlock.sl_lock = 0;
189 1.46 thorpej if (memlock.sl_want) {
190 1.46 thorpej memlock.sl_want = 0;
191 1.46 thorpej wakeup((caddr_t)&memlock);
192 1.46 thorpej return (EFAULT);
193 1.46 thorpej }
194 1.46 thorpej }
195 1.45 thorpej }
196 1.1 cgd savelen = oldlen;
197 1.1 cgd }
198 1.5 cgd error = (*fn)(name + 1, SCARG(uap, namelen) - 1, SCARG(uap, old),
199 1.55 is oldlenp, SCARG(uap, new), SCARG(uap, newlen), p);
200 1.5 cgd if (SCARG(uap, old) != NULL) {
201 1.1 cgd if (dolock)
202 1.35 thorpej uvm_vsunlock(p, SCARG(uap, old), savelen);
203 1.1 cgd memlock.sl_lock = 0;
204 1.1 cgd if (memlock.sl_want) {
205 1.1 cgd memlock.sl_want = 0;
206 1.1 cgd wakeup((caddr_t)&memlock);
207 1.1 cgd }
208 1.1 cgd }
209 1.1 cgd if (error)
210 1.1 cgd return (error);
211 1.5 cgd if (SCARG(uap, oldlenp))
212 1.5 cgd error = copyout(&oldlen, SCARG(uap, oldlenp), sizeof(oldlen));
213 1.16 thorpej return (error);
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Attributes stored in the kernel.
218 1.1 cgd */
219 1.1 cgd char hostname[MAXHOSTNAMELEN];
220 1.1 cgd int hostnamelen;
221 1.1 cgd char domainname[MAXHOSTNAMELEN];
222 1.1 cgd int domainnamelen;
223 1.1 cgd long hostid;
224 1.8 cgd #ifdef INSECURE
225 1.8 cgd int securelevel = -1;
226 1.8 cgd #else
227 1.17 mrg int securelevel = 0;
228 1.8 cgd #endif
229 1.52 bouyer #ifdef DEFCORENAME
230 1.52 bouyer char defcorename[MAXPATHLEN] = DEFCORENAME;
231 1.52 bouyer int defcorenamelen = sizeof(DEFCORENAME);
232 1.37 nathanw #else
233 1.52 bouyer char defcorename[MAXPATHLEN] = "%n.core";
234 1.52 bouyer int defcorenamelen = sizeof("%n.core");
235 1.37 nathanw #endif
236 1.1 cgd
237 1.1 cgd /*
238 1.1 cgd * kernel related system variables.
239 1.1 cgd */
240 1.13 christos int
241 1.1 cgd kern_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
242 1.1 cgd int *name;
243 1.1 cgd u_int namelen;
244 1.1 cgd void *oldp;
245 1.1 cgd size_t *oldlenp;
246 1.1 cgd void *newp;
247 1.1 cgd size_t newlen;
248 1.1 cgd struct proc *p;
249 1.1 cgd {
250 1.1 cgd int error, level, inthostid;
251 1.18 explorer int old_autonicetime;
252 1.22 tls int old_vnodes;
253 1.1 cgd extern char ostype[], osrelease[], version[];
254 1.1 cgd
255 1.44 thorpej /* All sysctl names at this level, except for a few, are terminal. */
256 1.44 thorpej switch (name[0]) {
257 1.44 thorpej case KERN_PROC:
258 1.44 thorpej case KERN_PROF:
259 1.44 thorpej case KERN_MBUF:
260 1.44 thorpej /* Not terminal. */
261 1.44 thorpej break;
262 1.44 thorpej default:
263 1.44 thorpej if (namelen != 1)
264 1.44 thorpej return (ENOTDIR); /* overloaded */
265 1.44 thorpej }
266 1.1 cgd
267 1.1 cgd switch (name[0]) {
268 1.1 cgd case KERN_OSTYPE:
269 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, ostype));
270 1.1 cgd case KERN_OSRELEASE:
271 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, osrelease));
272 1.1 cgd case KERN_OSREV:
273 1.25 mikel return (sysctl_rdint(oldp, oldlenp, newp, NetBSD));
274 1.1 cgd case KERN_VERSION:
275 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, version));
276 1.1 cgd case KERN_MAXVNODES:
277 1.22 tls old_vnodes = desiredvnodes;
278 1.29 sommerfe error = sysctl_int(oldp, oldlenp, newp, newlen, &desiredvnodes);
279 1.29 sommerfe if (old_vnodes > desiredvnodes) {
280 1.29 sommerfe desiredvnodes = old_vnodes;
281 1.22 tls return (EINVAL);
282 1.29 sommerfe }
283 1.22 tls return (error);
284 1.1 cgd case KERN_MAXPROC:
285 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &maxproc));
286 1.1 cgd case KERN_MAXFILES:
287 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, &maxfiles));
288 1.1 cgd case KERN_ARGMAX:
289 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, ARG_MAX));
290 1.1 cgd case KERN_SECURELVL:
291 1.1 cgd level = securelevel;
292 1.1 cgd if ((error = sysctl_int(oldp, oldlenp, newp, newlen, &level)) ||
293 1.1 cgd newp == NULL)
294 1.1 cgd return (error);
295 1.1 cgd if (level < securelevel && p->p_pid != 1)
296 1.1 cgd return (EPERM);
297 1.1 cgd securelevel = level;
298 1.1 cgd return (0);
299 1.1 cgd case KERN_HOSTNAME:
300 1.1 cgd error = sysctl_string(oldp, oldlenp, newp, newlen,
301 1.1 cgd hostname, sizeof(hostname));
302 1.1 cgd if (newp && !error)
303 1.1 cgd hostnamelen = newlen;
304 1.1 cgd return (error);
305 1.1 cgd case KERN_DOMAINNAME:
306 1.1 cgd error = sysctl_string(oldp, oldlenp, newp, newlen,
307 1.1 cgd domainname, sizeof(domainname));
308 1.1 cgd if (newp && !error)
309 1.1 cgd domainnamelen = newlen;
310 1.1 cgd return (error);
311 1.1 cgd case KERN_HOSTID:
312 1.1 cgd inthostid = hostid; /* XXX assumes sizeof long <= sizeof int */
313 1.1 cgd error = sysctl_int(oldp, oldlenp, newp, newlen, &inthostid);
314 1.1 cgd hostid = inthostid;
315 1.1 cgd return (error);
316 1.1 cgd case KERN_CLOCKRATE:
317 1.1 cgd return (sysctl_clockrate(oldp, oldlenp));
318 1.1 cgd case KERN_BOOTTIME:
319 1.1 cgd return (sysctl_rdstruct(oldp, oldlenp, newp, &boottime,
320 1.1 cgd sizeof(struct timeval)));
321 1.1 cgd case KERN_VNODE:
322 1.34 fvdl return (sysctl_vnode(oldp, oldlenp, p));
323 1.1 cgd case KERN_PROC:
324 1.52 bouyer return (sysctl_doeproc(name + 1, namelen - 1, oldp, oldlenp));
325 1.1 cgd case KERN_FILE:
326 1.1 cgd return (sysctl_file(oldp, oldlenp));
327 1.1 cgd #ifdef GPROF
328 1.1 cgd case KERN_PROF:
329 1.1 cgd return (sysctl_doprof(name + 1, namelen - 1, oldp, oldlenp,
330 1.1 cgd newp, newlen));
331 1.1 cgd #endif
332 1.1 cgd case KERN_POSIX1:
333 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, _POSIX_VERSION));
334 1.1 cgd case KERN_NGROUPS:
335 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, NGROUPS_MAX));
336 1.1 cgd case KERN_JOB_CONTROL:
337 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1));
338 1.1 cgd case KERN_SAVED_IDS:
339 1.1 cgd #ifdef _POSIX_SAVED_IDS
340 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1));
341 1.1 cgd #else
342 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 0));
343 1.1 cgd #endif
344 1.7 cgd case KERN_MAXPARTITIONS:
345 1.7 cgd return (sysctl_rdint(oldp, oldlenp, newp, MAXPARTITIONS));
346 1.10 thorpej case KERN_RAWPARTITION:
347 1.10 thorpej return (sysctl_rdint(oldp, oldlenp, newp, RAW_PART));
348 1.19 thorpej #ifdef NTP
349 1.15 jonathan case KERN_NTPTIME:
350 1.15 jonathan return (sysctl_ntptime(oldp, oldlenp));
351 1.19 thorpej #endif
352 1.18 explorer case KERN_AUTONICETIME:
353 1.18 explorer old_autonicetime = autonicetime;
354 1.18 explorer error = sysctl_int(oldp, oldlenp, newp, newlen, &autonicetime);
355 1.18 explorer if (autonicetime < 0)
356 1.18 explorer autonicetime = old_autonicetime;
357 1.18 explorer return (error);
358 1.18 explorer case KERN_AUTONICEVAL:
359 1.18 explorer error = sysctl_int(oldp, oldlenp, newp, newlen, &autoniceval);
360 1.18 explorer if (autoniceval < PRIO_MIN)
361 1.18 explorer autoniceval = PRIO_MIN;
362 1.18 explorer if (autoniceval > PRIO_MAX)
363 1.18 explorer autoniceval = PRIO_MAX;
364 1.18 explorer return (error);
365 1.21 perry case KERN_RTC_OFFSET:
366 1.21 perry return (sysctl_rdint(oldp, oldlenp, newp, rtc_offset));
367 1.23 thorpej case KERN_ROOT_DEVICE:
368 1.23 thorpej return (sysctl_rdstring(oldp, oldlenp, newp,
369 1.23 thorpej root_device->dv_xname));
370 1.28 leo case KERN_MSGBUFSIZE:
371 1.28 leo /*
372 1.28 leo * deal with cases where the message buffer has
373 1.28 leo * become corrupted.
374 1.28 leo */
375 1.28 leo if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
376 1.28 leo msgbufenabled = 0;
377 1.28 leo return (ENXIO);
378 1.28 leo }
379 1.28 leo return (sysctl_rdint(oldp, oldlenp, newp, msgbufp->msg_bufs));
380 1.36 kleink case KERN_FSYNC:
381 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
382 1.36 kleink case KERN_SYSVMSG:
383 1.36 kleink #ifdef SYSVMSG
384 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
385 1.36 kleink #else
386 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
387 1.36 kleink #endif
388 1.36 kleink case KERN_SYSVSEM:
389 1.36 kleink #ifdef SYSVSEM
390 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
391 1.36 kleink #else
392 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
393 1.36 kleink #endif
394 1.36 kleink case KERN_SYSVSHM:
395 1.36 kleink #ifdef SYSVSHM
396 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
397 1.36 kleink #else
398 1.36 kleink return (sysctl_rdint(oldp, oldlenp, newp, 0));
399 1.36 kleink #endif
400 1.52 bouyer case KERN_DEFCORENAME:
401 1.52 bouyer if (newp && newlen < 1)
402 1.52 bouyer return (EINVAL);
403 1.52 bouyer error = sysctl_string(oldp, oldlenp, newp, newlen,
404 1.52 bouyer defcorename, sizeof(defcorename));
405 1.52 bouyer if (newp && !error)
406 1.52 bouyer defcorenamelen = newlen;
407 1.52 bouyer return (error);
408 1.40 kleink case KERN_SYNCHRONIZED_IO:
409 1.40 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
410 1.40 kleink case KERN_IOV_MAX:
411 1.40 kleink return (sysctl_rdint(oldp, oldlenp, newp, IOV_MAX));
412 1.44 thorpej case KERN_MBUF:
413 1.44 thorpej return (sysctl_dombuf(name + 1, namelen - 1, oldp, oldlenp,
414 1.44 thorpej newp, newlen));
415 1.47 kleink case KERN_MAPPED_FILES:
416 1.47 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
417 1.47 kleink case KERN_MEMLOCK:
418 1.47 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
419 1.47 kleink case KERN_MEMLOCK_RANGE:
420 1.47 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
421 1.47 kleink case KERN_MEMORY_PROTECTION:
422 1.47 kleink return (sysctl_rdint(oldp, oldlenp, newp, 1));
423 1.51 kleink case KERN_LOGIN_NAME_MAX:
424 1.51 kleink return (sysctl_rdint(oldp, oldlenp, newp, LOGIN_NAME_MAX));
425 1.1 cgd default:
426 1.1 cgd return (EOPNOTSUPP);
427 1.1 cgd }
428 1.1 cgd /* NOTREACHED */
429 1.1 cgd }
430 1.1 cgd
431 1.1 cgd /*
432 1.1 cgd * hardware related system variables.
433 1.1 cgd */
434 1.13 christos int
435 1.1 cgd hw_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
436 1.1 cgd int *name;
437 1.1 cgd u_int namelen;
438 1.1 cgd void *oldp;
439 1.1 cgd size_t *oldlenp;
440 1.1 cgd void *newp;
441 1.1 cgd size_t newlen;
442 1.1 cgd struct proc *p;
443 1.1 cgd {
444 1.27 veego extern char machine[], machine_arch[], cpu_model[];
445 1.1 cgd
446 1.1 cgd /* all sysctl names at this level are terminal */
447 1.1 cgd if (namelen != 1)
448 1.1 cgd return (ENOTDIR); /* overloaded */
449 1.1 cgd
450 1.1 cgd switch (name[0]) {
451 1.1 cgd case HW_MACHINE:
452 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, machine));
453 1.27 veego case HW_MACHINE_ARCH:
454 1.27 veego return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch));
455 1.1 cgd case HW_MODEL:
456 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, cpu_model));
457 1.1 cgd case HW_NCPU:
458 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, 1)); /* XXX */
459 1.1 cgd case HW_BYTEORDER:
460 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, BYTE_ORDER));
461 1.1 cgd case HW_PHYSMEM:
462 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, ctob(physmem)));
463 1.1 cgd case HW_USERMEM:
464 1.31 mrg return (sysctl_rdint(oldp, oldlenp, newp,
465 1.31 mrg ctob(physmem - uvmexp.wired)));
466 1.1 cgd case HW_PAGESIZE:
467 1.1 cgd return (sysctl_rdint(oldp, oldlenp, newp, PAGE_SIZE));
468 1.1 cgd default:
469 1.1 cgd return (EOPNOTSUPP);
470 1.1 cgd }
471 1.1 cgd /* NOTREACHED */
472 1.1 cgd }
473 1.1 cgd
474 1.1 cgd #ifdef DEBUG
475 1.1 cgd /*
476 1.1 cgd * Debugging related system variables.
477 1.1 cgd */
478 1.1 cgd struct ctldebug debug0, debug1, debug2, debug3, debug4;
479 1.1 cgd struct ctldebug debug5, debug6, debug7, debug8, debug9;
480 1.1 cgd struct ctldebug debug10, debug11, debug12, debug13, debug14;
481 1.1 cgd struct ctldebug debug15, debug16, debug17, debug18, debug19;
482 1.1 cgd static struct ctldebug *debugvars[CTL_DEBUG_MAXID] = {
483 1.1 cgd &debug0, &debug1, &debug2, &debug3, &debug4,
484 1.1 cgd &debug5, &debug6, &debug7, &debug8, &debug9,
485 1.1 cgd &debug10, &debug11, &debug12, &debug13, &debug14,
486 1.1 cgd &debug15, &debug16, &debug17, &debug18, &debug19,
487 1.1 cgd };
488 1.1 cgd int
489 1.1 cgd debug_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
490 1.1 cgd int *name;
491 1.1 cgd u_int namelen;
492 1.1 cgd void *oldp;
493 1.1 cgd size_t *oldlenp;
494 1.1 cgd void *newp;
495 1.1 cgd size_t newlen;
496 1.1 cgd struct proc *p;
497 1.1 cgd {
498 1.1 cgd struct ctldebug *cdp;
499 1.1 cgd
500 1.1 cgd /* all sysctl names at this level are name and field */
501 1.1 cgd if (namelen != 2)
502 1.1 cgd return (ENOTDIR); /* overloaded */
503 1.1 cgd cdp = debugvars[name[0]];
504 1.34 fvdl if (name[0] >= CTL_DEBUG_MAXID || cdp->debugname == 0)
505 1.1 cgd return (EOPNOTSUPP);
506 1.1 cgd switch (name[1]) {
507 1.1 cgd case CTL_DEBUG_NAME:
508 1.1 cgd return (sysctl_rdstring(oldp, oldlenp, newp, cdp->debugname));
509 1.1 cgd case CTL_DEBUG_VALUE:
510 1.1 cgd return (sysctl_int(oldp, oldlenp, newp, newlen, cdp->debugvar));
511 1.1 cgd default:
512 1.1 cgd return (EOPNOTSUPP);
513 1.1 cgd }
514 1.1 cgd /* NOTREACHED */
515 1.1 cgd }
516 1.1 cgd #endif /* DEBUG */
517 1.1 cgd
518 1.52 bouyer int
519 1.52 bouyer proc_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
520 1.52 bouyer int *name;
521 1.52 bouyer u_int namelen;
522 1.52 bouyer void *oldp;
523 1.52 bouyer size_t *oldlenp;
524 1.52 bouyer void *newp;
525 1.52 bouyer size_t newlen;
526 1.52 bouyer struct proc *p;
527 1.52 bouyer {
528 1.53 jdolecek struct proc *ptmp=NULL;
529 1.52 bouyer const struct proclist_desc *pd;
530 1.52 bouyer int error = 0;
531 1.52 bouyer struct rlimit alim;
532 1.52 bouyer struct plimit *newplim;
533 1.52 bouyer char *tmps = NULL;
534 1.52 bouyer int i, curlen, len;
535 1.52 bouyer
536 1.52 bouyer if (namelen < 2)
537 1.52 bouyer return EINVAL;
538 1.52 bouyer
539 1.52 bouyer if (name[0] == PROC_CURPROC) {
540 1.52 bouyer ptmp = p;
541 1.52 bouyer } else {
542 1.52 bouyer proclist_lock_read();
543 1.52 bouyer for (pd = proclists; pd->pd_list != NULL; pd++) {
544 1.52 bouyer for (ptmp = LIST_FIRST(pd->pd_list); ptmp != NULL;
545 1.52 bouyer ptmp = LIST_NEXT(ptmp, p_list)) {
546 1.52 bouyer /* Skip embryonic processes. */
547 1.52 bouyer if (ptmp->p_stat == SIDL)
548 1.52 bouyer continue;
549 1.52 bouyer if (ptmp->p_pid == (pid_t)name[0])
550 1.52 bouyer break;
551 1.52 bouyer }
552 1.52 bouyer if (ptmp != NULL)
553 1.52 bouyer break;
554 1.52 bouyer }
555 1.52 bouyer proclist_unlock_read();
556 1.52 bouyer if (ptmp == NULL)
557 1.52 bouyer return(ESRCH);
558 1.52 bouyer if (p->p_ucred->cr_uid != 0) {
559 1.52 bouyer if(p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
560 1.52 bouyer p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
561 1.52 bouyer return EPERM;
562 1.52 bouyer if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
563 1.52 bouyer return EPERM; /* sgid proc */
564 1.52 bouyer for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
565 1.52 bouyer if (p->p_ucred->cr_groups[i] ==
566 1.52 bouyer ptmp->p_cred->p_rgid)
567 1.52 bouyer break;
568 1.52 bouyer }
569 1.52 bouyer if (i == p->p_ucred->cr_ngroups)
570 1.52 bouyer return EPERM;
571 1.52 bouyer }
572 1.52 bouyer }
573 1.52 bouyer if (name[1] == PROC_PID_CORENAME) {
574 1.52 bouyer if (namelen != 2)
575 1.52 bouyer return EINVAL;
576 1.52 bouyer /*
577 1.52 bouyer * Can't use sysctl_string() here because we may malloc a new
578 1.52 bouyer * area during the process, so we have to do it by hand.
579 1.52 bouyer */
580 1.52 bouyer curlen = strlen(ptmp->p_limit->pl_corename) + 1;
581 1.55 is if (oldlenp && *oldlenp < curlen) {
582 1.55 is if (!oldp)
583 1.55 is *oldlenp = curlen;
584 1.52 bouyer return (ENOMEM);
585 1.55 is }
586 1.52 bouyer if (newp) {
587 1.52 bouyer if (securelevel > 2)
588 1.52 bouyer return EPERM;
589 1.52 bouyer if (newlen > MAXPATHLEN)
590 1.52 bouyer return ENAMETOOLONG;
591 1.52 bouyer tmps = malloc(newlen + 1, M_TEMP, M_WAITOK);
592 1.52 bouyer if (tmps == NULL)
593 1.52 bouyer return ENOMEM;
594 1.52 bouyer error = copyin(newp, tmps, newlen + 1);
595 1.52 bouyer tmps[newlen] = '\0';
596 1.52 bouyer if (error)
597 1.52 bouyer goto cleanup;
598 1.52 bouyer /* Enforce to be either 'core' for end with '.core' */
599 1.52 bouyer if (newlen < 4) { /* c.o.r.e */
600 1.52 bouyer error = EINVAL;
601 1.52 bouyer goto cleanup;
602 1.52 bouyer }
603 1.52 bouyer len = newlen - 4;
604 1.52 bouyer if (len > 0) {
605 1.52 bouyer if (tmps[len - 1] != '.' &&
606 1.52 bouyer tmps[len - 1] != '/') {
607 1.52 bouyer error = EINVAL;
608 1.52 bouyer goto cleanup;
609 1.52 bouyer }
610 1.52 bouyer }
611 1.52 bouyer if (strcmp(&tmps[len], "core") != 0) {
612 1.52 bouyer error = EINVAL;
613 1.52 bouyer goto cleanup;
614 1.52 bouyer }
615 1.52 bouyer }
616 1.55 is if (oldp && oldlenp) {
617 1.52 bouyer *oldlenp = curlen;
618 1.52 bouyer error = copyout(ptmp->p_limit->pl_corename, oldp,
619 1.52 bouyer curlen);
620 1.52 bouyer }
621 1.52 bouyer if (newp && error == 0) {
622 1.52 bouyer /* if the 2 strings are identical, don't limcopy() */
623 1.52 bouyer if (strcmp(tmps, ptmp->p_limit->pl_corename) == 0) {
624 1.52 bouyer error = 0;
625 1.52 bouyer goto cleanup;
626 1.52 bouyer }
627 1.52 bouyer if (ptmp->p_limit->p_refcnt > 1 &&
628 1.52 bouyer (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
629 1.52 bouyer newplim = limcopy(ptmp->p_limit);
630 1.52 bouyer limfree(ptmp->p_limit);
631 1.52 bouyer ptmp->p_limit = newplim;
632 1.52 bouyer } else if (ptmp->p_limit->pl_corename != defcorename) {
633 1.52 bouyer free(ptmp->p_limit->pl_corename, M_TEMP);
634 1.52 bouyer }
635 1.52 bouyer ptmp->p_limit->pl_corename = tmps;
636 1.52 bouyer return (0);
637 1.52 bouyer }
638 1.52 bouyer cleanup:
639 1.52 bouyer if (tmps)
640 1.52 bouyer free(tmps, M_TEMP);
641 1.52 bouyer return (error);
642 1.52 bouyer }
643 1.52 bouyer if (name[1] == PROC_PID_LIMIT) {
644 1.52 bouyer if (namelen != 4 || name[2] >= PROC_PID_LIMIT_MAXID)
645 1.52 bouyer return EINVAL;
646 1.52 bouyer memcpy(&alim, &ptmp->p_rlimit[name[2] - 1], sizeof(alim));
647 1.52 bouyer if (name[3] == PROC_PID_LIMIT_TYPE_HARD)
648 1.52 bouyer error = sysctl_quad(oldp, oldlenp, newp, newlen,
649 1.52 bouyer &alim.rlim_max);
650 1.52 bouyer else if (name[3] == PROC_PID_LIMIT_TYPE_SOFT)
651 1.52 bouyer error = sysctl_quad(oldp, oldlenp, newp, newlen,
652 1.52 bouyer &alim.rlim_cur);
653 1.52 bouyer else
654 1.52 bouyer error = EINVAL;
655 1.52 bouyer
656 1.52 bouyer if (error)
657 1.52 bouyer return error;
658 1.52 bouyer
659 1.52 bouyer if (newp)
660 1.52 bouyer error = dosetrlimit(ptmp, p->p_cred,
661 1.52 bouyer name[2] - 1, &alim);
662 1.52 bouyer return error;
663 1.52 bouyer }
664 1.52 bouyer return (EINVAL);
665 1.52 bouyer }
666 1.52 bouyer
667 1.1 cgd /*
668 1.55 is * Convenience macros.
669 1.55 is */
670 1.55 is
671 1.55 is #define SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, len) \
672 1.55 is if (oldlenp) { \
673 1.55 is if (!oldp) \
674 1.55 is *oldlenp = len; \
675 1.55 is else { \
676 1.55 is if (*oldlenp < len) \
677 1.55 is return(ENOMEM); \
678 1.55 is *oldlenp = len; \
679 1.55 is error = copyout((caddr_t)valp, oldp, len); \
680 1.55 is } \
681 1.55 is }
682 1.55 is
683 1.55 is #define SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, typ) \
684 1.55 is SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, valp, sizeof(typ))
685 1.55 is
686 1.55 is #define SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len) \
687 1.55 is if (newp && newlen != len) \
688 1.55 is return (EINVAL);
689 1.55 is
690 1.55 is #define SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, typ) \
691 1.55 is SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, sizeof(typ))
692 1.55 is
693 1.55 is #define SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, len) \
694 1.55 is if (error == 0 && newp) \
695 1.55 is error = copyin(newp, valp, len);
696 1.55 is
697 1.55 is #define SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, typ) \
698 1.55 is SYSCTL_SCALAR_NEWPCOP_LEN(newp, valp, sizeof(typ))
699 1.55 is
700 1.55 is #define SYSCTL_STRING_CORE(oldp, oldlenp, str) \
701 1.55 is if (oldlenp) { \
702 1.55 is len = strlen(str) + 1; \
703 1.55 is if (!oldp) \
704 1.55 is *oldlenp = len; \
705 1.55 is else { \
706 1.55 is if (*oldlenp < len) { \
707 1.55 is err2 = ENOMEM; \
708 1.55 is len = *oldlenp; \
709 1.55 is } else \
710 1.55 is *oldlenp = len; \
711 1.55 is error = copyout(str, oldp, len);\
712 1.55 is if (error == 0) \
713 1.55 is error = err2; \
714 1.55 is } \
715 1.55 is }
716 1.55 is
717 1.55 is /*
718 1.1 cgd * Validate parameters and get old / set new parameters
719 1.1 cgd * for an integer-valued sysctl function.
720 1.1 cgd */
721 1.13 christos int
722 1.1 cgd sysctl_int(oldp, oldlenp, newp, newlen, valp)
723 1.1 cgd void *oldp;
724 1.1 cgd size_t *oldlenp;
725 1.1 cgd void *newp;
726 1.1 cgd size_t newlen;
727 1.1 cgd int *valp;
728 1.1 cgd {
729 1.1 cgd int error = 0;
730 1.1 cgd
731 1.55 is SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, int)
732 1.55 is SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, int)
733 1.55 is SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, int)
734 1.55 is
735 1.1 cgd return (error);
736 1.1 cgd }
737 1.1 cgd
738 1.55 is
739 1.1 cgd /*
740 1.1 cgd * As above, but read-only.
741 1.1 cgd */
742 1.13 christos int
743 1.1 cgd sysctl_rdint(oldp, oldlenp, newp, val)
744 1.1 cgd void *oldp;
745 1.1 cgd size_t *oldlenp;
746 1.1 cgd void *newp;
747 1.1 cgd int val;
748 1.1 cgd {
749 1.1 cgd int error = 0;
750 1.1 cgd
751 1.1 cgd if (newp)
752 1.1 cgd return (EPERM);
753 1.55 is
754 1.55 is SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, int)
755 1.55 is
756 1.1 cgd return (error);
757 1.1 cgd }
758 1.1 cgd
759 1.1 cgd /*
760 1.1 cgd * Validate parameters and get old / set new parameters
761 1.52 bouyer * for an quad-valued sysctl function.
762 1.52 bouyer */
763 1.52 bouyer int
764 1.52 bouyer sysctl_quad(oldp, oldlenp, newp, newlen, valp)
765 1.52 bouyer void *oldp;
766 1.52 bouyer size_t *oldlenp;
767 1.52 bouyer void *newp;
768 1.52 bouyer size_t newlen;
769 1.52 bouyer quad_t *valp;
770 1.52 bouyer {
771 1.52 bouyer int error = 0;
772 1.52 bouyer
773 1.55 is SYSCTL_SCALAR_NEWPCHECK_TYP(newp, newlen, quad_t)
774 1.55 is SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, valp, quad_t)
775 1.55 is SYSCTL_SCALAR_NEWPCOP_TYP(newp, valp, quad_t)
776 1.55 is
777 1.52 bouyer return (error);
778 1.52 bouyer }
779 1.52 bouyer
780 1.52 bouyer /*
781 1.52 bouyer * As above, but read-only.
782 1.52 bouyer */
783 1.52 bouyer int
784 1.52 bouyer sysctl_rdquad(oldp, oldlenp, newp, val)
785 1.52 bouyer void *oldp;
786 1.52 bouyer size_t *oldlenp;
787 1.52 bouyer void *newp;
788 1.52 bouyer quad_t val;
789 1.52 bouyer {
790 1.52 bouyer int error = 0;
791 1.52 bouyer
792 1.52 bouyer if (newp)
793 1.52 bouyer return (EPERM);
794 1.55 is
795 1.55 is SYSCTL_SCALAR_CORE_TYP(oldp, oldlenp, &val, quad_t)
796 1.55 is
797 1.52 bouyer return (error);
798 1.52 bouyer }
799 1.52 bouyer
800 1.52 bouyer /*
801 1.52 bouyer * Validate parameters and get old / set new parameters
802 1.1 cgd * for a string-valued sysctl function.
803 1.1 cgd */
804 1.13 christos int
805 1.1 cgd sysctl_string(oldp, oldlenp, newp, newlen, str, maxlen)
806 1.1 cgd void *oldp;
807 1.1 cgd size_t *oldlenp;
808 1.1 cgd void *newp;
809 1.1 cgd size_t newlen;
810 1.1 cgd char *str;
811 1.1 cgd int maxlen;
812 1.1 cgd {
813 1.55 is int len, error = 0, err2 = 0;
814 1.1 cgd
815 1.1 cgd if (newp && newlen >= maxlen)
816 1.1 cgd return (EINVAL);
817 1.55 is
818 1.55 is SYSCTL_STRING_CORE(oldp, oldlenp, str);
819 1.55 is
820 1.1 cgd if (error == 0 && newp) {
821 1.1 cgd error = copyin(newp, str, newlen);
822 1.1 cgd str[newlen] = 0;
823 1.1 cgd }
824 1.1 cgd return (error);
825 1.1 cgd }
826 1.1 cgd
827 1.1 cgd /*
828 1.1 cgd * As above, but read-only.
829 1.1 cgd */
830 1.13 christos int
831 1.1 cgd sysctl_rdstring(oldp, oldlenp, newp, str)
832 1.1 cgd void *oldp;
833 1.1 cgd size_t *oldlenp;
834 1.1 cgd void *newp;
835 1.1 cgd char *str;
836 1.1 cgd {
837 1.55 is int len, error = 0, err2 = 0;
838 1.1 cgd
839 1.1 cgd if (newp)
840 1.1 cgd return (EPERM);
841 1.55 is
842 1.55 is SYSCTL_STRING_CORE(oldp, oldlenp, str);
843 1.55 is
844 1.1 cgd return (error);
845 1.1 cgd }
846 1.1 cgd
847 1.1 cgd /*
848 1.1 cgd * Validate parameters and get old / set new parameters
849 1.1 cgd * for a structure oriented sysctl function.
850 1.1 cgd */
851 1.13 christos int
852 1.1 cgd sysctl_struct(oldp, oldlenp, newp, newlen, sp, len)
853 1.1 cgd void *oldp;
854 1.1 cgd size_t *oldlenp;
855 1.1 cgd void *newp;
856 1.1 cgd size_t newlen;
857 1.1 cgd void *sp;
858 1.1 cgd int len;
859 1.1 cgd {
860 1.1 cgd int error = 0;
861 1.1 cgd
862 1.55 is SYSCTL_SCALAR_NEWPCHECK_LEN(newp, newlen, len)
863 1.55 is SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
864 1.55 is SYSCTL_SCALAR_NEWPCOP_LEN(newp, sp, len)
865 1.55 is
866 1.1 cgd return (error);
867 1.1 cgd }
868 1.1 cgd
869 1.1 cgd /*
870 1.1 cgd * Validate parameters and get old parameters
871 1.1 cgd * for a structure oriented sysctl function.
872 1.1 cgd */
873 1.13 christos int
874 1.1 cgd sysctl_rdstruct(oldp, oldlenp, newp, sp, len)
875 1.1 cgd void *oldp;
876 1.1 cgd size_t *oldlenp;
877 1.1 cgd void *newp, *sp;
878 1.1 cgd int len;
879 1.1 cgd {
880 1.1 cgd int error = 0;
881 1.1 cgd
882 1.1 cgd if (newp)
883 1.1 cgd return (EPERM);
884 1.55 is
885 1.55 is SYSCTL_SCALAR_CORE_LEN(oldp, oldlenp, sp, len)
886 1.55 is
887 1.1 cgd return (error);
888 1.1 cgd }
889 1.1 cgd
890 1.1 cgd /*
891 1.1 cgd * Get file structures.
892 1.1 cgd */
893 1.13 christos int
894 1.1 cgd sysctl_file(where, sizep)
895 1.1 cgd char *where;
896 1.1 cgd size_t *sizep;
897 1.1 cgd {
898 1.1 cgd int buflen, error;
899 1.1 cgd struct file *fp;
900 1.1 cgd char *start = where;
901 1.1 cgd
902 1.1 cgd buflen = *sizep;
903 1.1 cgd if (where == NULL) {
904 1.1 cgd /*
905 1.1 cgd * overestimate by 10 files
906 1.1 cgd */
907 1.1 cgd *sizep = sizeof(filehead) + (nfiles + 10) * sizeof(struct file);
908 1.1 cgd return (0);
909 1.1 cgd }
910 1.1 cgd
911 1.1 cgd /*
912 1.1 cgd * first copyout filehead
913 1.1 cgd */
914 1.1 cgd if (buflen < sizeof(filehead)) {
915 1.1 cgd *sizep = 0;
916 1.1 cgd return (0);
917 1.1 cgd }
918 1.13 christos error = copyout((caddr_t)&filehead, where, sizeof(filehead));
919 1.13 christos if (error)
920 1.1 cgd return (error);
921 1.1 cgd buflen -= sizeof(filehead);
922 1.1 cgd where += sizeof(filehead);
923 1.1 cgd
924 1.1 cgd /*
925 1.1 cgd * followed by an array of file structures
926 1.1 cgd */
927 1.3 mycroft for (fp = filehead.lh_first; fp != 0; fp = fp->f_list.le_next) {
928 1.1 cgd if (buflen < sizeof(struct file)) {
929 1.1 cgd *sizep = where - start;
930 1.1 cgd return (ENOMEM);
931 1.1 cgd }
932 1.39 perry error = copyout((caddr_t)fp, where, sizeof(struct file));
933 1.13 christos if (error)
934 1.1 cgd return (error);
935 1.1 cgd buflen -= sizeof(struct file);
936 1.1 cgd where += sizeof(struct file);
937 1.1 cgd }
938 1.1 cgd *sizep = where - start;
939 1.1 cgd return (0);
940 1.1 cgd }
941 1.1 cgd
942 1.1 cgd /*
943 1.1 cgd * try over estimating by 5 procs
944 1.1 cgd */
945 1.39 perry #define KERN_PROCSLOP (5 * sizeof(struct kinfo_proc))
946 1.1 cgd
947 1.13 christos int
948 1.52 bouyer sysctl_doeproc(name, namelen, where, sizep)
949 1.1 cgd int *name;
950 1.1 cgd u_int namelen;
951 1.1 cgd char *where;
952 1.1 cgd size_t *sizep;
953 1.1 cgd {
954 1.1 cgd register struct proc *p;
955 1.1 cgd register struct kinfo_proc *dp = (struct kinfo_proc *)where;
956 1.1 cgd register int needed = 0;
957 1.1 cgd int buflen = where != NULL ? *sizep : 0;
958 1.41 thorpej const struct proclist_desc *pd;
959 1.1 cgd struct eproc eproc;
960 1.1 cgd int error = 0;
961 1.1 cgd
962 1.1 cgd if (namelen != 2 && !(namelen == 1 && name[0] == KERN_PROC_ALL))
963 1.1 cgd return (EINVAL);
964 1.41 thorpej
965 1.50 thorpej proclist_lock_read();
966 1.49 thorpej
967 1.41 thorpej pd = proclists;
968 1.1 cgd again:
969 1.41 thorpej for (p = LIST_FIRST(pd->pd_list); p != NULL;
970 1.41 thorpej p = LIST_NEXT(p, p_list)) {
971 1.1 cgd /*
972 1.1 cgd * Skip embryonic processes.
973 1.1 cgd */
974 1.1 cgd if (p->p_stat == SIDL)
975 1.1 cgd continue;
976 1.1 cgd /*
977 1.1 cgd * TODO - make more efficient (see notes below).
978 1.1 cgd * do by session.
979 1.1 cgd */
980 1.1 cgd switch (name[0]) {
981 1.1 cgd
982 1.1 cgd case KERN_PROC_PID:
983 1.1 cgd /* could do this with just a lookup */
984 1.1 cgd if (p->p_pid != (pid_t)name[1])
985 1.1 cgd continue;
986 1.1 cgd break;
987 1.1 cgd
988 1.1 cgd case KERN_PROC_PGRP:
989 1.1 cgd /* could do this by traversing pgrp */
990 1.1 cgd if (p->p_pgrp->pg_id != (pid_t)name[1])
991 1.1 cgd continue;
992 1.1 cgd break;
993 1.1 cgd
994 1.1 cgd case KERN_PROC_TTY:
995 1.1 cgd if ((p->p_flag & P_CONTROLT) == 0 ||
996 1.1 cgd p->p_session->s_ttyp == NULL ||
997 1.1 cgd p->p_session->s_ttyp->t_dev != (dev_t)name[1])
998 1.1 cgd continue;
999 1.1 cgd break;
1000 1.1 cgd
1001 1.1 cgd case KERN_PROC_UID:
1002 1.1 cgd if (p->p_ucred->cr_uid != (uid_t)name[1])
1003 1.1 cgd continue;
1004 1.1 cgd break;
1005 1.1 cgd
1006 1.1 cgd case KERN_PROC_RUID:
1007 1.1 cgd if (p->p_cred->p_ruid != (uid_t)name[1])
1008 1.1 cgd continue;
1009 1.1 cgd break;
1010 1.1 cgd }
1011 1.1 cgd if (buflen >= sizeof(struct kinfo_proc)) {
1012 1.1 cgd fill_eproc(p, &eproc);
1013 1.13 christos error = copyout((caddr_t)p, &dp->kp_proc,
1014 1.13 christos sizeof(struct proc));
1015 1.13 christos if (error)
1016 1.1 cgd return (error);
1017 1.13 christos error = copyout((caddr_t)&eproc, &dp->kp_eproc,
1018 1.13 christos sizeof(eproc));
1019 1.13 christos if (error)
1020 1.1 cgd return (error);
1021 1.1 cgd dp++;
1022 1.1 cgd buflen -= sizeof(struct kinfo_proc);
1023 1.1 cgd }
1024 1.1 cgd needed += sizeof(struct kinfo_proc);
1025 1.1 cgd }
1026 1.41 thorpej pd++;
1027 1.41 thorpej if (pd->pd_list != NULL)
1028 1.1 cgd goto again;
1029 1.49 thorpej proclist_unlock_read();
1030 1.41 thorpej
1031 1.1 cgd if (where != NULL) {
1032 1.1 cgd *sizep = (caddr_t)dp - where;
1033 1.1 cgd if (needed > *sizep)
1034 1.1 cgd return (ENOMEM);
1035 1.1 cgd } else {
1036 1.1 cgd needed += KERN_PROCSLOP;
1037 1.1 cgd *sizep = needed;
1038 1.1 cgd }
1039 1.1 cgd return (0);
1040 1.1 cgd }
1041 1.1 cgd
1042 1.1 cgd /*
1043 1.1 cgd * Fill in an eproc structure for the specified process.
1044 1.1 cgd */
1045 1.1 cgd void
1046 1.1 cgd fill_eproc(p, ep)
1047 1.1 cgd register struct proc *p;
1048 1.1 cgd register struct eproc *ep;
1049 1.1 cgd {
1050 1.1 cgd register struct tty *tp;
1051 1.1 cgd
1052 1.1 cgd ep->e_paddr = p;
1053 1.1 cgd ep->e_sess = p->p_pgrp->pg_session;
1054 1.1 cgd ep->e_pcred = *p->p_cred;
1055 1.1 cgd ep->e_ucred = *p->p_ucred;
1056 1.48 thorpej if (p->p_stat == SIDL || P_ZOMBIE(p)) {
1057 1.1 cgd ep->e_vm.vm_rssize = 0;
1058 1.1 cgd ep->e_vm.vm_tsize = 0;
1059 1.1 cgd ep->e_vm.vm_dsize = 0;
1060 1.1 cgd ep->e_vm.vm_ssize = 0;
1061 1.1 cgd /* ep->e_vm.vm_pmap = XXX; */
1062 1.1 cgd } else {
1063 1.1 cgd register struct vmspace *vm = p->p_vmspace;
1064 1.1 cgd
1065 1.26 gwr ep->e_vm.vm_rssize = vm_resident_count(vm);
1066 1.1 cgd ep->e_vm.vm_tsize = vm->vm_tsize;
1067 1.1 cgd ep->e_vm.vm_dsize = vm->vm_dsize;
1068 1.1 cgd ep->e_vm.vm_ssize = vm->vm_ssize;
1069 1.1 cgd }
1070 1.1 cgd if (p->p_pptr)
1071 1.1 cgd ep->e_ppid = p->p_pptr->p_pid;
1072 1.1 cgd else
1073 1.1 cgd ep->e_ppid = 0;
1074 1.1 cgd ep->e_pgid = p->p_pgrp->pg_id;
1075 1.33 thorpej ep->e_sid = ep->e_sess->s_sid;
1076 1.1 cgd ep->e_jobc = p->p_pgrp->pg_jobc;
1077 1.1 cgd if ((p->p_flag & P_CONTROLT) &&
1078 1.1 cgd (tp = ep->e_sess->s_ttyp)) {
1079 1.1 cgd ep->e_tdev = tp->t_dev;
1080 1.1 cgd ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
1081 1.1 cgd ep->e_tsess = tp->t_session;
1082 1.1 cgd } else
1083 1.1 cgd ep->e_tdev = NODEV;
1084 1.1 cgd if (p->p_wmesg)
1085 1.1 cgd strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
1086 1.1 cgd ep->e_xsize = ep->e_xrssize = 0;
1087 1.1 cgd ep->e_xccount = ep->e_xswrss = 0;
1088 1.24 mycroft ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
1089 1.24 mycroft if (SESS_LEADER(p))
1090 1.24 mycroft ep->e_flag |= EPROC_SLEADER;
1091 1.24 mycroft strncpy(ep->e_login, ep->e_sess->s_login, MAXLOGNAME);
1092 1.1 cgd }
1093