kern_resource.c revision 1.77 1 1.77 pk /* $NetBSD: kern_resource.c,v 1.77 2004/04/04 18:22:44 pk Exp $ */
2 1.20 cgd
3 1.17 cgd /*-
4 1.19 cgd * Copyright (c) 1982, 1986, 1991, 1993
5 1.19 cgd * The Regents of the University of California. All rights reserved.
6 1.17 cgd * (c) UNIX System Laboratories, Inc.
7 1.17 cgd * All or some portions of this file are derived from material licensed
8 1.17 cgd * to the University of California by American Telephone and Telegraph
9 1.17 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.17 cgd * the permission of UNIX System Laboratories, Inc.
11 1.17 cgd *
12 1.17 cgd * Redistribution and use in source and binary forms, with or without
13 1.17 cgd * modification, are permitted provided that the following conditions
14 1.17 cgd * are met:
15 1.17 cgd * 1. Redistributions of source code must retain the above copyright
16 1.17 cgd * notice, this list of conditions and the following disclaimer.
17 1.17 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.17 cgd * notice, this list of conditions and the following disclaimer in the
19 1.17 cgd * documentation and/or other materials provided with the distribution.
20 1.72 agc * 3. Neither the name of the University nor the names of its contributors
21 1.17 cgd * may be used to endorse or promote products derived from this software
22 1.17 cgd * without specific prior written permission.
23 1.17 cgd *
24 1.17 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.17 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.17 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.17 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.17 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.17 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.17 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.17 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.17 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.17 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.17 cgd * SUCH DAMAGE.
35 1.17 cgd *
36 1.45 fvdl * @(#)kern_resource.c 8.8 (Berkeley) 2/14/95
37 1.17 cgd */
38 1.61 lukem
39 1.61 lukem #include <sys/cdefs.h>
40 1.77 pk __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.77 2004/04/04 18:22:44 pk Exp $");
41 1.44 mrg
42 1.17 cgd #include <sys/param.h>
43 1.22 cgd #include <sys/systm.h>
44 1.17 cgd #include <sys/kernel.h>
45 1.19 cgd #include <sys/file.h>
46 1.17 cgd #include <sys/resourcevar.h>
47 1.17 cgd #include <sys/malloc.h>
48 1.49 thorpej #include <sys/pool.h>
49 1.17 cgd #include <sys/proc.h>
50 1.74 atatat #include <sys/sysctl.h>
51 1.17 cgd
52 1.22 cgd #include <sys/mount.h>
53 1.68 thorpej #include <sys/sa.h>
54 1.22 cgd #include <sys/syscallargs.h>
55 1.17 cgd
56 1.43 mrg #include <uvm/uvm_extern.h>
57 1.43 mrg
58 1.17 cgd /*
59 1.60 eeh * Maximum process data and stack limits.
60 1.60 eeh * They are variables so they are patchable.
61 1.60 eeh */
62 1.60 eeh rlim_t maxdmap = MAXDSIZ;
63 1.60 eeh rlim_t maxsmap = MAXSSIZ;
64 1.60 eeh
65 1.60 eeh /*
66 1.17 cgd * Resource controls and accounting.
67 1.17 cgd */
68 1.17 cgd
69 1.25 cgd int
70 1.68 thorpej sys_getpriority(l, v, retval)
71 1.68 thorpej struct lwp *l;
72 1.30 thorpej void *v;
73 1.30 thorpej register_t *retval;
74 1.30 thorpej {
75 1.54 augustss struct sys_getpriority_args /* {
76 1.22 cgd syscallarg(int) which;
77 1.22 cgd syscallarg(int) who;
78 1.30 thorpej } */ *uap = v;
79 1.68 thorpej struct proc *curp = l->l_proc, *p;
80 1.54 augustss int low = NZERO + PRIO_MAX + 1;
81 1.17 cgd
82 1.22 cgd switch (SCARG(uap, which)) {
83 1.17 cgd
84 1.17 cgd case PRIO_PROCESS:
85 1.22 cgd if (SCARG(uap, who) == 0)
86 1.17 cgd p = curp;
87 1.17 cgd else
88 1.22 cgd p = pfind(SCARG(uap, who));
89 1.17 cgd if (p == 0)
90 1.17 cgd break;
91 1.17 cgd low = p->p_nice;
92 1.17 cgd break;
93 1.17 cgd
94 1.17 cgd case PRIO_PGRP: {
95 1.54 augustss struct pgrp *pg;
96 1.17 cgd
97 1.22 cgd if (SCARG(uap, who) == 0)
98 1.17 cgd pg = curp->p_pgrp;
99 1.22 cgd else if ((pg = pgfind(SCARG(uap, who))) == NULL)
100 1.17 cgd break;
101 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
102 1.17 cgd if (p->p_nice < low)
103 1.17 cgd low = p->p_nice;
104 1.17 cgd }
105 1.17 cgd break;
106 1.17 cgd }
107 1.17 cgd
108 1.17 cgd case PRIO_USER:
109 1.22 cgd if (SCARG(uap, who) == 0)
110 1.22 cgd SCARG(uap, who) = curp->p_ucred->cr_uid;
111 1.52 thorpej proclist_lock_read();
112 1.64 matt LIST_FOREACH(p, &allproc, p_list) {
113 1.63 thorpej if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who) &&
114 1.22 cgd p->p_nice < low)
115 1.17 cgd low = p->p_nice;
116 1.64 matt }
117 1.51 thorpej proclist_unlock_read();
118 1.17 cgd break;
119 1.17 cgd
120 1.17 cgd default:
121 1.17 cgd return (EINVAL);
122 1.17 cgd }
123 1.37 ws if (low == NZERO + PRIO_MAX + 1)
124 1.17 cgd return (ESRCH);
125 1.37 ws *retval = low - NZERO;
126 1.17 cgd return (0);
127 1.17 cgd }
128 1.17 cgd
129 1.17 cgd /* ARGSUSED */
130 1.25 cgd int
131 1.68 thorpej sys_setpriority(l, v, retval)
132 1.68 thorpej struct lwp *l;
133 1.30 thorpej void *v;
134 1.30 thorpej register_t *retval;
135 1.30 thorpej {
136 1.54 augustss struct sys_setpriority_args /* {
137 1.22 cgd syscallarg(int) which;
138 1.22 cgd syscallarg(int) who;
139 1.22 cgd syscallarg(int) prio;
140 1.30 thorpej } */ *uap = v;
141 1.68 thorpej struct proc *curp = l->l_proc, *p;
142 1.17 cgd int found = 0, error = 0;
143 1.17 cgd
144 1.22 cgd switch (SCARG(uap, which)) {
145 1.17 cgd
146 1.17 cgd case PRIO_PROCESS:
147 1.22 cgd if (SCARG(uap, who) == 0)
148 1.17 cgd p = curp;
149 1.17 cgd else
150 1.22 cgd p = pfind(SCARG(uap, who));
151 1.17 cgd if (p == 0)
152 1.17 cgd break;
153 1.22 cgd error = donice(curp, p, SCARG(uap, prio));
154 1.17 cgd found++;
155 1.17 cgd break;
156 1.17 cgd
157 1.17 cgd case PRIO_PGRP: {
158 1.54 augustss struct pgrp *pg;
159 1.17 cgd
160 1.22 cgd if (SCARG(uap, who) == 0)
161 1.17 cgd pg = curp->p_pgrp;
162 1.22 cgd else if ((pg = pgfind(SCARG(uap, who))) == NULL)
163 1.17 cgd break;
164 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
165 1.22 cgd error = donice(curp, p, SCARG(uap, prio));
166 1.17 cgd found++;
167 1.17 cgd }
168 1.17 cgd break;
169 1.17 cgd }
170 1.17 cgd
171 1.17 cgd case PRIO_USER:
172 1.22 cgd if (SCARG(uap, who) == 0)
173 1.22 cgd SCARG(uap, who) = curp->p_ucred->cr_uid;
174 1.52 thorpej proclist_lock_read();
175 1.64 matt LIST_FOREACH(p, &allproc, p_list) {
176 1.63 thorpej if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who)) {
177 1.22 cgd error = donice(curp, p, SCARG(uap, prio));
178 1.17 cgd found++;
179 1.17 cgd }
180 1.64 matt }
181 1.51 thorpej proclist_unlock_read();
182 1.17 cgd break;
183 1.17 cgd
184 1.17 cgd default:
185 1.17 cgd return (EINVAL);
186 1.17 cgd }
187 1.17 cgd if (found == 0)
188 1.17 cgd return (ESRCH);
189 1.17 cgd return (error);
190 1.17 cgd }
191 1.17 cgd
192 1.25 cgd int
193 1.17 cgd donice(curp, chgp, n)
194 1.54 augustss struct proc *curp, *chgp;
195 1.54 augustss int n;
196 1.17 cgd {
197 1.54 augustss struct pcred *pcred = curp->p_cred;
198 1.59 thorpej int s;
199 1.17 cgd
200 1.17 cgd if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
201 1.17 cgd pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
202 1.17 cgd pcred->p_ruid != chgp->p_ucred->cr_uid)
203 1.17 cgd return (EPERM);
204 1.17 cgd if (n > PRIO_MAX)
205 1.17 cgd n = PRIO_MAX;
206 1.17 cgd if (n < PRIO_MIN)
207 1.17 cgd n = PRIO_MIN;
208 1.37 ws n += NZERO;
209 1.17 cgd if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
210 1.17 cgd return (EACCES);
211 1.17 cgd chgp->p_nice = n;
212 1.59 thorpej SCHED_LOCK(s);
213 1.68 thorpej (void)resetprocpriority(chgp);
214 1.59 thorpej SCHED_UNLOCK(s);
215 1.17 cgd return (0);
216 1.17 cgd }
217 1.17 cgd
218 1.17 cgd /* ARGSUSED */
219 1.25 cgd int
220 1.68 thorpej sys_setrlimit(l, v, retval)
221 1.68 thorpej struct lwp *l;
222 1.30 thorpej void *v;
223 1.30 thorpej register_t *retval;
224 1.30 thorpej {
225 1.54 augustss struct sys_setrlimit_args /* {
226 1.42 mycroft syscallarg(int) which;
227 1.39 cgd syscallarg(const struct rlimit *) rlp;
228 1.30 thorpej } */ *uap = v;
229 1.68 thorpej struct proc *p = l->l_proc;
230 1.42 mycroft int which = SCARG(uap, which);
231 1.19 cgd struct rlimit alim;
232 1.17 cgd int error;
233 1.17 cgd
234 1.46 perry error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
235 1.33 christos if (error)
236 1.17 cgd return (error);
237 1.53 bouyer return (dosetrlimit(p, p->p_cred, which, &alim));
238 1.17 cgd }
239 1.17 cgd
240 1.17 cgd int
241 1.53 bouyer dosetrlimit(p, cred, which, limp)
242 1.17 cgd struct proc *p;
243 1.53 bouyer struct pcred *cred;
244 1.42 mycroft int which;
245 1.17 cgd struct rlimit *limp;
246 1.17 cgd {
247 1.54 augustss struct rlimit *alimp;
248 1.53 bouyer struct plimit *newplim;
249 1.17 cgd int error;
250 1.17 cgd
251 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
252 1.17 cgd return (EINVAL);
253 1.38 matthias
254 1.38 matthias if (limp->rlim_cur < 0 || limp->rlim_max < 0)
255 1.38 matthias return (EINVAL);
256 1.38 matthias
257 1.17 cgd alimp = &p->p_rlimit[which];
258 1.53 bouyer /* if we don't change the value, no need to limcopy() */
259 1.53 bouyer if (limp->rlim_cur == alimp->rlim_cur &&
260 1.53 bouyer limp->rlim_max == alimp->rlim_max)
261 1.53 bouyer return 0;
262 1.53 bouyer
263 1.62 jdolecek if (limp->rlim_cur > limp->rlim_max) {
264 1.62 jdolecek /*
265 1.62 jdolecek * This is programming error. According to SUSv2, we should
266 1.62 jdolecek * return error in this case.
267 1.62 jdolecek */
268 1.62 jdolecek return (EINVAL);
269 1.62 jdolecek }
270 1.62 jdolecek if (limp->rlim_max > alimp->rlim_max
271 1.62 jdolecek && (error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
272 1.17 cgd return (error);
273 1.62 jdolecek
274 1.17 cgd if (p->p_limit->p_refcnt > 1 &&
275 1.17 cgd (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
276 1.53 bouyer newplim = limcopy(p->p_limit);
277 1.53 bouyer limfree(p->p_limit);
278 1.53 bouyer p->p_limit = newplim;
279 1.17 cgd alimp = &p->p_rlimit[which];
280 1.17 cgd }
281 1.17 cgd
282 1.17 cgd switch (which) {
283 1.17 cgd
284 1.17 cgd case RLIMIT_DATA:
285 1.19 cgd if (limp->rlim_cur > maxdmap)
286 1.19 cgd limp->rlim_cur = maxdmap;
287 1.19 cgd if (limp->rlim_max > maxdmap)
288 1.19 cgd limp->rlim_max = maxdmap;
289 1.17 cgd break;
290 1.17 cgd
291 1.17 cgd case RLIMIT_STACK:
292 1.19 cgd if (limp->rlim_cur > maxsmap)
293 1.19 cgd limp->rlim_cur = maxsmap;
294 1.19 cgd if (limp->rlim_max > maxsmap)
295 1.19 cgd limp->rlim_max = maxsmap;
296 1.62 jdolecek
297 1.62 jdolecek /*
298 1.62 jdolecek * Return EINVAL if the new stack size limit is lower than
299 1.62 jdolecek * current usage. Otherwise, the process would get SIGSEGV the
300 1.62 jdolecek * moment it would try to access anything on it's current stack.
301 1.62 jdolecek * This conforms to SUSv2.
302 1.62 jdolecek */
303 1.62 jdolecek if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
304 1.62 jdolecek || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE)
305 1.62 jdolecek return (EINVAL);
306 1.40 enami
307 1.17 cgd /*
308 1.40 enami * Stack is allocated to the max at exec time with
309 1.40 enami * only "rlim_cur" bytes accessible (In other words,
310 1.40 enami * allocates stack dividing two contiguous regions at
311 1.40 enami * "rlim_cur" bytes boundary).
312 1.40 enami *
313 1.40 enami * Since allocation is done in terms of page, roundup
314 1.40 enami * "rlim_cur" (otherwise, contiguous regions
315 1.40 enami * overlap). If stack limit is going up make more
316 1.40 enami * accessible, if going down make inaccessible.
317 1.17 cgd */
318 1.40 enami limp->rlim_cur = round_page(limp->rlim_cur);
319 1.17 cgd if (limp->rlim_cur != alimp->rlim_cur) {
320 1.48 eeh vaddr_t addr;
321 1.48 eeh vsize_t size;
322 1.17 cgd vm_prot_t prot;
323 1.17 cgd
324 1.17 cgd if (limp->rlim_cur > alimp->rlim_cur) {
325 1.73 chs prot = VM_PROT_READ | VM_PROT_WRITE;
326 1.17 cgd size = limp->rlim_cur - alimp->rlim_cur;
327 1.17 cgd addr = USRSTACK - limp->rlim_cur;
328 1.17 cgd } else {
329 1.17 cgd prot = VM_PROT_NONE;
330 1.17 cgd size = alimp->rlim_cur - limp->rlim_cur;
331 1.17 cgd addr = USRSTACK - alimp->rlim_cur;
332 1.17 cgd }
333 1.43 mrg (void) uvm_map_protect(&p->p_vmspace->vm_map,
334 1.43 mrg addr, addr+size, prot, FALSE);
335 1.17 cgd }
336 1.17 cgd break;
337 1.19 cgd
338 1.19 cgd case RLIMIT_NOFILE:
339 1.19 cgd if (limp->rlim_cur > maxfiles)
340 1.19 cgd limp->rlim_cur = maxfiles;
341 1.19 cgd if (limp->rlim_max > maxfiles)
342 1.19 cgd limp->rlim_max = maxfiles;
343 1.19 cgd break;
344 1.19 cgd
345 1.19 cgd case RLIMIT_NPROC:
346 1.19 cgd if (limp->rlim_cur > maxproc)
347 1.19 cgd limp->rlim_cur = maxproc;
348 1.19 cgd if (limp->rlim_max > maxproc)
349 1.19 cgd limp->rlim_max = maxproc;
350 1.19 cgd break;
351 1.17 cgd }
352 1.17 cgd *alimp = *limp;
353 1.17 cgd return (0);
354 1.17 cgd }
355 1.17 cgd
356 1.17 cgd /* ARGSUSED */
357 1.25 cgd int
358 1.68 thorpej sys_getrlimit(l, v, retval)
359 1.68 thorpej struct lwp *l;
360 1.30 thorpej void *v;
361 1.30 thorpej register_t *retval;
362 1.30 thorpej {
363 1.54 augustss struct sys_getrlimit_args /* {
364 1.42 mycroft syscallarg(int) which;
365 1.22 cgd syscallarg(struct rlimit *) rlp;
366 1.30 thorpej } */ *uap = v;
367 1.68 thorpej struct proc *p = l->l_proc;
368 1.42 mycroft int which = SCARG(uap, which);
369 1.17 cgd
370 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
371 1.17 cgd return (EINVAL);
372 1.42 mycroft return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
373 1.46 perry sizeof(struct rlimit)));
374 1.17 cgd }
375 1.17 cgd
376 1.17 cgd /*
377 1.17 cgd * Transform the running time and tick information in proc p into user,
378 1.17 cgd * system, and interrupt time usage.
379 1.17 cgd */
380 1.25 cgd void
381 1.17 cgd calcru(p, up, sp, ip)
382 1.54 augustss struct proc *p;
383 1.54 augustss struct timeval *up;
384 1.54 augustss struct timeval *sp;
385 1.54 augustss struct timeval *ip;
386 1.17 cgd {
387 1.54 augustss u_quad_t u, st, ut, it, tot;
388 1.70 dsl unsigned long sec;
389 1.70 dsl long usec;
390 1.54 augustss int s;
391 1.17 cgd struct timeval tv;
392 1.68 thorpej struct lwp *l;
393 1.17 cgd
394 1.17 cgd s = splstatclock();
395 1.17 cgd st = p->p_sticks;
396 1.17 cgd ut = p->p_uticks;
397 1.17 cgd it = p->p_iticks;
398 1.17 cgd splx(s);
399 1.17 cgd
400 1.17 cgd sec = p->p_rtime.tv_sec;
401 1.17 cgd usec = p->p_rtime.tv_usec;
402 1.70 dsl LIST_FOREACH(l, &p->p_lwps, l_sibling) {
403 1.68 thorpej if (l->l_stat == LSONPROC) {
404 1.68 thorpej struct schedstate_percpu *spc;
405 1.68 thorpej
406 1.68 thorpej KDASSERT(l->l_cpu != NULL);
407 1.68 thorpej spc = &l->l_cpu->ci_schedstate;
408 1.68 thorpej
409 1.68 thorpej /*
410 1.68 thorpej * Adjust for the current time slice. This is
411 1.68 thorpej * actually fairly important since the error
412 1.68 thorpej * here is on the order of a time quantum,
413 1.68 thorpej * which is much greater than the sampling
414 1.68 thorpej * error.
415 1.68 thorpej */
416 1.68 thorpej microtime(&tv);
417 1.68 thorpej sec += tv.tv_sec - spc->spc_runtime.tv_sec;
418 1.68 thorpej usec += tv.tv_usec - spc->spc_runtime.tv_usec;
419 1.68 thorpej }
420 1.17 cgd }
421 1.69 dsl
422 1.69 dsl tot = st + ut + it;
423 1.70 dsl u = sec * 1000000ull + usec;
424 1.70 dsl
425 1.69 dsl if (tot == 0) {
426 1.69 dsl /* No ticks, so can't use to share time out, split 50-50 */
427 1.70 dsl st = ut = u / 2;
428 1.70 dsl } else {
429 1.70 dsl st = (u * st) / tot;
430 1.70 dsl ut = (u * ut) / tot;
431 1.69 dsl }
432 1.17 cgd sp->tv_sec = st / 1000000;
433 1.17 cgd sp->tv_usec = st % 1000000;
434 1.17 cgd up->tv_sec = ut / 1000000;
435 1.17 cgd up->tv_usec = ut % 1000000;
436 1.17 cgd if (ip != NULL) {
437 1.70 dsl if (it != 0)
438 1.70 dsl it = (u * it) / tot;
439 1.17 cgd ip->tv_sec = it / 1000000;
440 1.17 cgd ip->tv_usec = it % 1000000;
441 1.17 cgd }
442 1.17 cgd }
443 1.17 cgd
444 1.17 cgd /* ARGSUSED */
445 1.25 cgd int
446 1.68 thorpej sys_getrusage(l, v, retval)
447 1.68 thorpej struct lwp *l;
448 1.30 thorpej void *v;
449 1.30 thorpej register_t *retval;
450 1.30 thorpej {
451 1.54 augustss struct sys_getrusage_args /* {
452 1.22 cgd syscallarg(int) who;
453 1.22 cgd syscallarg(struct rusage *) rusage;
454 1.30 thorpej } */ *uap = v;
455 1.54 augustss struct rusage *rup;
456 1.68 thorpej struct proc *p = l->l_proc;
457 1.17 cgd
458 1.22 cgd switch (SCARG(uap, who)) {
459 1.17 cgd
460 1.19 cgd case RUSAGE_SELF:
461 1.17 cgd rup = &p->p_stats->p_ru;
462 1.17 cgd calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
463 1.17 cgd break;
464 1.17 cgd
465 1.17 cgd case RUSAGE_CHILDREN:
466 1.17 cgd rup = &p->p_stats->p_cru;
467 1.17 cgd break;
468 1.17 cgd
469 1.17 cgd default:
470 1.17 cgd return (EINVAL);
471 1.17 cgd }
472 1.46 perry return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
473 1.17 cgd }
474 1.17 cgd
475 1.25 cgd void
476 1.17 cgd ruadd(ru, ru2)
477 1.54 augustss struct rusage *ru, *ru2;
478 1.17 cgd {
479 1.54 augustss long *ip, *ip2;
480 1.54 augustss int i;
481 1.17 cgd
482 1.27 mycroft timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
483 1.27 mycroft timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
484 1.17 cgd if (ru->ru_maxrss < ru2->ru_maxrss)
485 1.17 cgd ru->ru_maxrss = ru2->ru_maxrss;
486 1.17 cgd ip = &ru->ru_first; ip2 = &ru2->ru_first;
487 1.17 cgd for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
488 1.17 cgd *ip++ += *ip2++;
489 1.17 cgd }
490 1.17 cgd
491 1.17 cgd /*
492 1.17 cgd * Make a copy of the plimit structure.
493 1.17 cgd * We share these structures copy-on-write after fork,
494 1.17 cgd * and copy when a limit is changed.
495 1.17 cgd */
496 1.17 cgd struct plimit *
497 1.17 cgd limcopy(lim)
498 1.17 cgd struct plimit *lim;
499 1.17 cgd {
500 1.54 augustss struct plimit *newlim;
501 1.71 itojun size_t l;
502 1.17 cgd
503 1.49 thorpej newlim = pool_get(&plimit_pool, PR_WAITOK);
504 1.47 perry memcpy(newlim->pl_rlimit, lim->pl_rlimit,
505 1.17 cgd sizeof(struct rlimit) * RLIM_NLIMITS);
506 1.53 bouyer if (lim->pl_corename == defcorename) {
507 1.53 bouyer newlim->pl_corename = defcorename;
508 1.53 bouyer } else {
509 1.71 itojun l = strlen(lim->pl_corename) + 1;
510 1.71 itojun newlim->pl_corename = malloc(l, M_TEMP, M_WAITOK);
511 1.71 itojun strlcpy(newlim->pl_corename, lim->pl_corename, l);
512 1.53 bouyer }
513 1.32 mycroft newlim->p_lflags = 0;
514 1.32 mycroft newlim->p_refcnt = 1;
515 1.32 mycroft return (newlim);
516 1.32 mycroft }
517 1.32 mycroft
518 1.32 mycroft void
519 1.32 mycroft limfree(lim)
520 1.32 mycroft struct plimit *lim;
521 1.32 mycroft {
522 1.32 mycroft
523 1.32 mycroft if (--lim->p_refcnt > 0)
524 1.32 mycroft return;
525 1.53 bouyer #ifdef DIAGNOSTIC
526 1.53 bouyer if (lim->p_refcnt < 0)
527 1.53 bouyer panic("limfree");
528 1.53 bouyer #endif
529 1.53 bouyer if (lim->pl_corename != defcorename)
530 1.53 bouyer free(lim->pl_corename, M_TEMP);
531 1.49 thorpej pool_put(&plimit_pool, lim);
532 1.68 thorpej }
533 1.68 thorpej
534 1.68 thorpej struct pstats *
535 1.68 thorpej pstatscopy(ps)
536 1.68 thorpej struct pstats *ps;
537 1.68 thorpej {
538 1.68 thorpej
539 1.68 thorpej struct pstats *newps;
540 1.68 thorpej
541 1.68 thorpej newps = pool_get(&pstats_pool, PR_WAITOK);
542 1.68 thorpej
543 1.68 thorpej memset(&newps->pstat_startzero, 0,
544 1.68 thorpej (unsigned) ((caddr_t)&newps->pstat_endzero -
545 1.68 thorpej (caddr_t)&newps->pstat_startzero));
546 1.68 thorpej memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
547 1.68 thorpej ((caddr_t)&newps->pstat_endcopy -
548 1.68 thorpej (caddr_t)&newps->pstat_startcopy));
549 1.68 thorpej
550 1.68 thorpej return (newps);
551 1.68 thorpej
552 1.68 thorpej }
553 1.68 thorpej
554 1.68 thorpej void
555 1.68 thorpej pstatsfree(ps)
556 1.68 thorpej struct pstats *ps;
557 1.68 thorpej {
558 1.68 thorpej
559 1.68 thorpej pool_put(&pstats_pool, ps);
560 1.74 atatat }
561 1.74 atatat
562 1.74 atatat /*
563 1.74 atatat * sysctl interface in five parts
564 1.74 atatat */
565 1.74 atatat
566 1.74 atatat /*
567 1.74 atatat * a routine for sysctl proc subtree helpers that need to pick a valid
568 1.74 atatat * process by pid.
569 1.74 atatat */
570 1.74 atatat static int
571 1.74 atatat sysctl_proc_findproc(struct proc *p, struct proc **p2, pid_t pid)
572 1.74 atatat {
573 1.74 atatat struct proc *ptmp;
574 1.74 atatat int i, error = 0;
575 1.74 atatat
576 1.74 atatat if (pid == PROC_CURPROC)
577 1.74 atatat ptmp = p;
578 1.74 atatat else if ((ptmp = pfind(pid)) == NULL)
579 1.74 atatat error = ESRCH;
580 1.74 atatat else {
581 1.74 atatat /*
582 1.74 atatat * suid proc of ours or proc not ours
583 1.74 atatat */
584 1.74 atatat if (p->p_cred->p_ruid != ptmp->p_cred->p_ruid ||
585 1.74 atatat p->p_cred->p_ruid != ptmp->p_cred->p_svuid)
586 1.74 atatat error = suser(p->p_ucred, &p->p_acflag);
587 1.74 atatat
588 1.74 atatat /*
589 1.74 atatat * sgid proc has sgid back to us temporarily
590 1.74 atatat */
591 1.74 atatat else if (ptmp->p_cred->p_rgid != ptmp->p_cred->p_svgid)
592 1.74 atatat error = suser(p->p_ucred, &p->p_acflag);
593 1.74 atatat
594 1.74 atatat /*
595 1.74 atatat * our rgid must be in target's group list (ie,
596 1.74 atatat * sub-processes started by a sgid process)
597 1.74 atatat */
598 1.74 atatat else {
599 1.74 atatat for (i = 0; i < p->p_ucred->cr_ngroups; i++) {
600 1.74 atatat if (p->p_ucred->cr_groups[i] ==
601 1.74 atatat ptmp->p_cred->p_rgid)
602 1.74 atatat break;
603 1.74 atatat }
604 1.74 atatat if (i == p->p_ucred->cr_ngroups)
605 1.74 atatat error = suser(p->p_ucred, &p->p_acflag);
606 1.74 atatat }
607 1.74 atatat }
608 1.74 atatat
609 1.74 atatat *p2 = ptmp;
610 1.74 atatat return (error);
611 1.74 atatat }
612 1.74 atatat
613 1.74 atatat /*
614 1.74 atatat * sysctl helper routine for setting a process's specific corefile
615 1.74 atatat * name. picks the process based on the given pid and checks the
616 1.74 atatat * correctness of the new value.
617 1.74 atatat */
618 1.74 atatat static int
619 1.74 atatat sysctl_proc_corename(SYSCTLFN_ARGS)
620 1.74 atatat {
621 1.74 atatat struct proc *ptmp, *p;
622 1.74 atatat struct plimit *newplim;
623 1.74 atatat int error = 0, len;
624 1.74 atatat char cname[MAXPATHLEN], *tmp;
625 1.74 atatat struct sysctlnode node;
626 1.74 atatat
627 1.74 atatat /*
628 1.74 atatat * is this all correct?
629 1.74 atatat */
630 1.74 atatat if (namelen != 0)
631 1.74 atatat return (EINVAL);
632 1.74 atatat if (name[-1] != PROC_PID_CORENAME)
633 1.74 atatat return (EINVAL);
634 1.74 atatat
635 1.74 atatat /*
636 1.74 atatat * whom are we tweaking?
637 1.74 atatat */
638 1.74 atatat p = l->l_proc;
639 1.74 atatat error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
640 1.74 atatat if (error)
641 1.74 atatat return (error);
642 1.74 atatat
643 1.74 atatat /*
644 1.74 atatat * let them modify a temporary copy of the core name
645 1.74 atatat */
646 1.74 atatat node = *rnode;
647 1.74 atatat strlcpy(cname, ptmp->p_limit->pl_corename, sizeof(cname));
648 1.74 atatat node.sysctl_data = cname;
649 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
650 1.74 atatat
651 1.74 atatat /*
652 1.74 atatat * if that failed, or they have nothing new to say, or we've
653 1.74 atatat * heard it before...
654 1.74 atatat */
655 1.74 atatat if (error || newp == NULL ||
656 1.74 atatat strcmp(cname, ptmp->p_limit->pl_corename) == 0)
657 1.74 atatat return (error);
658 1.74 atatat
659 1.74 atatat /*
660 1.74 atatat * no error yet and cname now has the new core name in it.
661 1.74 atatat * let's see if it looks acceptable. it must be either "core"
662 1.74 atatat * or end in ".core" or "/core".
663 1.74 atatat */
664 1.74 atatat len = strlen(cname);
665 1.74 atatat if (len < 4)
666 1.74 atatat return (EINVAL);
667 1.74 atatat if (strcmp(cname + len - 4, "core") != 0)
668 1.74 atatat return (EINVAL);
669 1.74 atatat if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.')
670 1.74 atatat return (EINVAL);
671 1.74 atatat
672 1.74 atatat /*
673 1.74 atatat * hmm...looks good. now...where do we put it?
674 1.74 atatat */
675 1.74 atatat tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
676 1.74 atatat if (tmp == NULL)
677 1.74 atatat return (ENOMEM);
678 1.74 atatat strlcpy(tmp, cname, len + 1);
679 1.74 atatat
680 1.74 atatat if (ptmp->p_limit->p_refcnt > 1 &&
681 1.74 atatat (ptmp->p_limit->p_lflags & PL_SHAREMOD) == 0) {
682 1.74 atatat newplim = limcopy(ptmp->p_limit);
683 1.74 atatat limfree(ptmp->p_limit);
684 1.74 atatat ptmp->p_limit = newplim;
685 1.74 atatat }
686 1.74 atatat if (ptmp->p_limit->pl_corename != defcorename)
687 1.74 atatat FREE(ptmp->p_limit->pl_corename, M_SYSCTLDATA);
688 1.74 atatat ptmp->p_limit->pl_corename = tmp;
689 1.74 atatat
690 1.74 atatat return (error);
691 1.74 atatat }
692 1.74 atatat
693 1.74 atatat /*
694 1.74 atatat * sysctl helper routine for checking/setting a process's stop flags,
695 1.74 atatat * one for fork and one for exec.
696 1.74 atatat */
697 1.74 atatat static int
698 1.74 atatat sysctl_proc_stop(SYSCTLFN_ARGS)
699 1.74 atatat {
700 1.74 atatat struct proc *p, *ptmp;
701 1.74 atatat int i, f, error = 0;
702 1.74 atatat struct sysctlnode node;
703 1.74 atatat
704 1.74 atatat if (namelen != 0)
705 1.74 atatat return (EINVAL);
706 1.74 atatat
707 1.74 atatat p = l->l_proc;
708 1.74 atatat error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-2]);
709 1.74 atatat if (error)
710 1.74 atatat return (error);
711 1.74 atatat
712 1.74 atatat switch (rnode->sysctl_num) {
713 1.74 atatat case PROC_PID_STOPFORK:
714 1.74 atatat f = P_STOPFORK;
715 1.74 atatat break;
716 1.74 atatat case PROC_PID_STOPEXEC:
717 1.74 atatat f = P_STOPEXEC;
718 1.74 atatat break;
719 1.74 atatat case PROC_PID_STOPEXIT:
720 1.74 atatat f = P_STOPEXIT;
721 1.74 atatat break;
722 1.74 atatat default:
723 1.74 atatat return (EINVAL);
724 1.74 atatat }
725 1.74 atatat
726 1.74 atatat i = (ptmp->p_flag & f) ? 1 : 0;
727 1.74 atatat node = *rnode;
728 1.74 atatat node.sysctl_data = &i;
729 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
730 1.74 atatat if (error || newp == NULL)
731 1.74 atatat return (error);
732 1.74 atatat
733 1.74 atatat if (i)
734 1.74 atatat ptmp->p_flag |= f;
735 1.74 atatat else
736 1.74 atatat ptmp->p_flag &= ~f;
737 1.74 atatat
738 1.74 atatat return (0);
739 1.74 atatat }
740 1.74 atatat
741 1.74 atatat /*
742 1.74 atatat * sysctl helper routine for a process's rlimits as exposed by sysctl.
743 1.74 atatat */
744 1.74 atatat static int
745 1.74 atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
746 1.74 atatat {
747 1.74 atatat struct proc *ptmp, *p;
748 1.74 atatat u_int limitno;
749 1.74 atatat int which, error = 0;
750 1.74 atatat struct rlimit alim;
751 1.74 atatat struct sysctlnode node;
752 1.74 atatat
753 1.74 atatat if (namelen != 0)
754 1.74 atatat return (EINVAL);
755 1.74 atatat
756 1.74 atatat which = name[-1];
757 1.74 atatat if (which != PROC_PID_LIMIT_TYPE_SOFT &&
758 1.74 atatat which != PROC_PID_LIMIT_TYPE_HARD)
759 1.74 atatat return (EINVAL);
760 1.74 atatat
761 1.74 atatat limitno = name[-2] - 1;
762 1.74 atatat if (limitno >= RLIM_NLIMITS)
763 1.74 atatat return (EINVAL);
764 1.74 atatat
765 1.74 atatat if (name[-3] != PROC_PID_LIMIT)
766 1.74 atatat return (EINVAL);
767 1.74 atatat
768 1.74 atatat p = l->l_proc;
769 1.74 atatat error = sysctl_proc_findproc(p, &ptmp, (pid_t)name[-4]);
770 1.74 atatat if (error)
771 1.74 atatat return (error);
772 1.74 atatat
773 1.74 atatat node = *rnode;
774 1.74 atatat memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
775 1.74 atatat if (which == PROC_PID_LIMIT_TYPE_HARD)
776 1.74 atatat node.sysctl_data = &alim.rlim_max;
777 1.74 atatat else
778 1.74 atatat node.sysctl_data = &alim.rlim_cur;
779 1.74 atatat
780 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
781 1.74 atatat if (error || newp == NULL)
782 1.74 atatat return (error);
783 1.74 atatat
784 1.74 atatat return (dosetrlimit(ptmp, p->p_cred, limitno, &alim));
785 1.74 atatat }
786 1.74 atatat
787 1.74 atatat /*
788 1.74 atatat * and finally, the actually glue that sticks it to the tree
789 1.74 atatat */
790 1.74 atatat SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
791 1.74 atatat {
792 1.74 atatat
793 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
794 1.76 atatat CTLFLAG_PERMANENT,
795 1.74 atatat CTLTYPE_NODE, "proc", NULL,
796 1.74 atatat NULL, 0, NULL, 0,
797 1.74 atatat CTL_PROC, CTL_EOL);
798 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
799 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
800 1.74 atatat CTLTYPE_NODE, "curproc", NULL,
801 1.74 atatat NULL, 0, NULL, 0,
802 1.74 atatat CTL_PROC, PROC_CURPROC, CTL_EOL);
803 1.74 atatat
804 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
805 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READONLY2|CTLFLAG_ANYWRITE,
806 1.74 atatat CTLTYPE_STRING, "corename", NULL,
807 1.74 atatat sysctl_proc_corename, 0, NULL, MAXPATHLEN,
808 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
809 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
810 1.76 atatat CTLFLAG_PERMANENT,
811 1.74 atatat CTLTYPE_NODE, "rlimit", NULL,
812 1.74 atatat NULL, 0, NULL, 0,
813 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
814 1.74 atatat
815 1.74 atatat #define create_proc_plimit(s, n) do { \
816 1.76 atatat sysctl_createv(clog, 0, NULL, NULL, \
817 1.76 atatat CTLFLAG_PERMANENT, \
818 1.74 atatat CTLTYPE_NODE, s, NULL, \
819 1.74 atatat NULL, 0, NULL, 0, \
820 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
821 1.74 atatat CTL_EOL); \
822 1.76 atatat sysctl_createv(clog, 0, NULL, NULL, \
823 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
824 1.74 atatat CTLTYPE_QUAD, "soft", NULL, \
825 1.74 atatat sysctl_proc_plimit, 0, NULL, 0, \
826 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
827 1.74 atatat PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
828 1.76 atatat sysctl_createv(clog, 0, NULL, NULL, \
829 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
830 1.74 atatat CTLTYPE_QUAD, "hard", NULL, \
831 1.74 atatat sysctl_proc_plimit, 0, NULL, 0, \
832 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
833 1.74 atatat PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
834 1.74 atatat } while (0/*CONSTCOND*/)
835 1.74 atatat
836 1.74 atatat create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
837 1.74 atatat create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
838 1.74 atatat create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
839 1.74 atatat create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
840 1.74 atatat create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
841 1.74 atatat create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
842 1.74 atatat create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
843 1.74 atatat create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
844 1.74 atatat create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
845 1.74 atatat
846 1.74 atatat #undef create_proc_plimit
847 1.74 atatat
848 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
849 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
850 1.74 atatat CTLTYPE_INT, "stopfork", NULL,
851 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
852 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
853 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
854 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
855 1.74 atatat CTLTYPE_INT, "stopexec", NULL,
856 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
857 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
858 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
859 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
860 1.74 atatat CTLTYPE_INT, "stopexit", NULL,
861 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
862 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
863 1.17 cgd }
864