kern_resource.c revision 1.72 1 1.72 agc /* $NetBSD: kern_resource.c,v 1.72 2003/08/07 16:31:48 agc 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.72 agc __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.72 2003/08/07 16:31:48 agc 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.17 cgd
51 1.22 cgd #include <sys/mount.h>
52 1.68 thorpej #include <sys/sa.h>
53 1.22 cgd #include <sys/syscallargs.h>
54 1.17 cgd
55 1.43 mrg #include <uvm/uvm_extern.h>
56 1.43 mrg
57 1.17 cgd /*
58 1.60 eeh * Maximum process data and stack limits.
59 1.60 eeh * They are variables so they are patchable.
60 1.60 eeh *
61 1.60 eeh * XXXX Do we really need them to be patchable?
62 1.60 eeh */
63 1.60 eeh rlim_t maxdmap = MAXDSIZ;
64 1.60 eeh rlim_t maxsmap = MAXSSIZ;
65 1.60 eeh
66 1.60 eeh /*
67 1.17 cgd * Resource controls and accounting.
68 1.17 cgd */
69 1.17 cgd
70 1.25 cgd int
71 1.68 thorpej sys_getpriority(l, v, retval)
72 1.68 thorpej struct lwp *l;
73 1.30 thorpej void *v;
74 1.30 thorpej register_t *retval;
75 1.30 thorpej {
76 1.54 augustss struct sys_getpriority_args /* {
77 1.22 cgd syscallarg(int) which;
78 1.22 cgd syscallarg(int) who;
79 1.30 thorpej } */ *uap = v;
80 1.68 thorpej struct proc *curp = l->l_proc, *p;
81 1.54 augustss int low = NZERO + PRIO_MAX + 1;
82 1.17 cgd
83 1.22 cgd switch (SCARG(uap, which)) {
84 1.17 cgd
85 1.17 cgd case PRIO_PROCESS:
86 1.22 cgd if (SCARG(uap, who) == 0)
87 1.17 cgd p = curp;
88 1.17 cgd else
89 1.22 cgd p = pfind(SCARG(uap, who));
90 1.17 cgd if (p == 0)
91 1.17 cgd break;
92 1.17 cgd low = p->p_nice;
93 1.17 cgd break;
94 1.17 cgd
95 1.17 cgd case PRIO_PGRP: {
96 1.54 augustss struct pgrp *pg;
97 1.17 cgd
98 1.22 cgd if (SCARG(uap, who) == 0)
99 1.17 cgd pg = curp->p_pgrp;
100 1.22 cgd else if ((pg = pgfind(SCARG(uap, who))) == NULL)
101 1.17 cgd break;
102 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
103 1.17 cgd if (p->p_nice < low)
104 1.17 cgd low = p->p_nice;
105 1.17 cgd }
106 1.17 cgd break;
107 1.17 cgd }
108 1.17 cgd
109 1.17 cgd case PRIO_USER:
110 1.22 cgd if (SCARG(uap, who) == 0)
111 1.22 cgd SCARG(uap, who) = curp->p_ucred->cr_uid;
112 1.52 thorpej proclist_lock_read();
113 1.64 matt LIST_FOREACH(p, &allproc, p_list) {
114 1.63 thorpej if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who) &&
115 1.22 cgd p->p_nice < low)
116 1.17 cgd low = p->p_nice;
117 1.64 matt }
118 1.51 thorpej proclist_unlock_read();
119 1.17 cgd break;
120 1.17 cgd
121 1.17 cgd default:
122 1.17 cgd return (EINVAL);
123 1.17 cgd }
124 1.37 ws if (low == NZERO + PRIO_MAX + 1)
125 1.17 cgd return (ESRCH);
126 1.37 ws *retval = low - NZERO;
127 1.17 cgd return (0);
128 1.17 cgd }
129 1.17 cgd
130 1.17 cgd /* ARGSUSED */
131 1.25 cgd int
132 1.68 thorpej sys_setpriority(l, v, retval)
133 1.68 thorpej struct lwp *l;
134 1.30 thorpej void *v;
135 1.30 thorpej register_t *retval;
136 1.30 thorpej {
137 1.54 augustss struct sys_setpriority_args /* {
138 1.22 cgd syscallarg(int) which;
139 1.22 cgd syscallarg(int) who;
140 1.22 cgd syscallarg(int) prio;
141 1.30 thorpej } */ *uap = v;
142 1.68 thorpej struct proc *curp = l->l_proc, *p;
143 1.17 cgd int found = 0, error = 0;
144 1.17 cgd
145 1.22 cgd switch (SCARG(uap, which)) {
146 1.17 cgd
147 1.17 cgd case PRIO_PROCESS:
148 1.22 cgd if (SCARG(uap, who) == 0)
149 1.17 cgd p = curp;
150 1.17 cgd else
151 1.22 cgd p = pfind(SCARG(uap, who));
152 1.17 cgd if (p == 0)
153 1.17 cgd break;
154 1.22 cgd error = donice(curp, p, SCARG(uap, prio));
155 1.17 cgd found++;
156 1.17 cgd break;
157 1.17 cgd
158 1.17 cgd case PRIO_PGRP: {
159 1.54 augustss struct pgrp *pg;
160 1.17 cgd
161 1.22 cgd if (SCARG(uap, who) == 0)
162 1.17 cgd pg = curp->p_pgrp;
163 1.22 cgd else if ((pg = pgfind(SCARG(uap, who))) == NULL)
164 1.17 cgd break;
165 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
166 1.22 cgd error = donice(curp, p, SCARG(uap, prio));
167 1.17 cgd found++;
168 1.17 cgd }
169 1.17 cgd break;
170 1.17 cgd }
171 1.17 cgd
172 1.17 cgd case PRIO_USER:
173 1.22 cgd if (SCARG(uap, who) == 0)
174 1.22 cgd SCARG(uap, who) = curp->p_ucred->cr_uid;
175 1.52 thorpej proclist_lock_read();
176 1.64 matt LIST_FOREACH(p, &allproc, p_list) {
177 1.63 thorpej if (p->p_ucred->cr_uid == (uid_t) SCARG(uap, who)) {
178 1.22 cgd error = donice(curp, p, SCARG(uap, prio));
179 1.17 cgd found++;
180 1.17 cgd }
181 1.64 matt }
182 1.51 thorpej proclist_unlock_read();
183 1.17 cgd break;
184 1.17 cgd
185 1.17 cgd default:
186 1.17 cgd return (EINVAL);
187 1.17 cgd }
188 1.17 cgd if (found == 0)
189 1.17 cgd return (ESRCH);
190 1.17 cgd return (error);
191 1.17 cgd }
192 1.17 cgd
193 1.25 cgd int
194 1.17 cgd donice(curp, chgp, n)
195 1.54 augustss struct proc *curp, *chgp;
196 1.54 augustss int n;
197 1.17 cgd {
198 1.54 augustss struct pcred *pcred = curp->p_cred;
199 1.59 thorpej int s;
200 1.17 cgd
201 1.17 cgd if (pcred->pc_ucred->cr_uid && pcred->p_ruid &&
202 1.17 cgd pcred->pc_ucred->cr_uid != chgp->p_ucred->cr_uid &&
203 1.17 cgd pcred->p_ruid != chgp->p_ucred->cr_uid)
204 1.17 cgd return (EPERM);
205 1.17 cgd if (n > PRIO_MAX)
206 1.17 cgd n = PRIO_MAX;
207 1.17 cgd if (n < PRIO_MIN)
208 1.17 cgd n = PRIO_MIN;
209 1.37 ws n += NZERO;
210 1.17 cgd if (n < chgp->p_nice && suser(pcred->pc_ucred, &curp->p_acflag))
211 1.17 cgd return (EACCES);
212 1.17 cgd chgp->p_nice = n;
213 1.59 thorpej SCHED_LOCK(s);
214 1.68 thorpej (void)resetprocpriority(chgp);
215 1.59 thorpej SCHED_UNLOCK(s);
216 1.17 cgd return (0);
217 1.17 cgd }
218 1.17 cgd
219 1.17 cgd /* ARGSUSED */
220 1.25 cgd int
221 1.68 thorpej sys_setrlimit(l, v, retval)
222 1.68 thorpej struct lwp *l;
223 1.30 thorpej void *v;
224 1.30 thorpej register_t *retval;
225 1.30 thorpej {
226 1.54 augustss struct sys_setrlimit_args /* {
227 1.42 mycroft syscallarg(int) which;
228 1.39 cgd syscallarg(const struct rlimit *) rlp;
229 1.30 thorpej } */ *uap = v;
230 1.68 thorpej struct proc *p = l->l_proc;
231 1.42 mycroft int which = SCARG(uap, which);
232 1.19 cgd struct rlimit alim;
233 1.17 cgd int error;
234 1.17 cgd
235 1.46 perry error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
236 1.33 christos if (error)
237 1.17 cgd return (error);
238 1.53 bouyer return (dosetrlimit(p, p->p_cred, which, &alim));
239 1.17 cgd }
240 1.17 cgd
241 1.17 cgd int
242 1.53 bouyer dosetrlimit(p, cred, which, limp)
243 1.17 cgd struct proc *p;
244 1.53 bouyer struct pcred *cred;
245 1.42 mycroft int which;
246 1.17 cgd struct rlimit *limp;
247 1.17 cgd {
248 1.54 augustss struct rlimit *alimp;
249 1.53 bouyer struct plimit *newplim;
250 1.17 cgd int error;
251 1.17 cgd
252 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
253 1.17 cgd return (EINVAL);
254 1.38 matthias
255 1.38 matthias if (limp->rlim_cur < 0 || limp->rlim_max < 0)
256 1.38 matthias return (EINVAL);
257 1.38 matthias
258 1.17 cgd alimp = &p->p_rlimit[which];
259 1.53 bouyer /* if we don't change the value, no need to limcopy() */
260 1.53 bouyer if (limp->rlim_cur == alimp->rlim_cur &&
261 1.53 bouyer limp->rlim_max == alimp->rlim_max)
262 1.53 bouyer return 0;
263 1.53 bouyer
264 1.62 jdolecek if (limp->rlim_cur > limp->rlim_max) {
265 1.62 jdolecek /*
266 1.62 jdolecek * This is programming error. According to SUSv2, we should
267 1.62 jdolecek * return error in this case.
268 1.62 jdolecek */
269 1.62 jdolecek return (EINVAL);
270 1.62 jdolecek }
271 1.62 jdolecek if (limp->rlim_max > alimp->rlim_max
272 1.62 jdolecek && (error = suser(cred->pc_ucred, &p->p_acflag)) != 0)
273 1.17 cgd return (error);
274 1.62 jdolecek
275 1.17 cgd if (p->p_limit->p_refcnt > 1 &&
276 1.17 cgd (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
277 1.53 bouyer newplim = limcopy(p->p_limit);
278 1.53 bouyer limfree(p->p_limit);
279 1.53 bouyer p->p_limit = newplim;
280 1.17 cgd alimp = &p->p_rlimit[which];
281 1.17 cgd }
282 1.17 cgd
283 1.17 cgd switch (which) {
284 1.17 cgd
285 1.17 cgd case RLIMIT_DATA:
286 1.19 cgd if (limp->rlim_cur > maxdmap)
287 1.19 cgd limp->rlim_cur = maxdmap;
288 1.19 cgd if (limp->rlim_max > maxdmap)
289 1.19 cgd limp->rlim_max = maxdmap;
290 1.17 cgd break;
291 1.17 cgd
292 1.17 cgd case RLIMIT_STACK:
293 1.19 cgd if (limp->rlim_cur > maxsmap)
294 1.19 cgd limp->rlim_cur = maxsmap;
295 1.19 cgd if (limp->rlim_max > maxsmap)
296 1.19 cgd limp->rlim_max = maxsmap;
297 1.62 jdolecek
298 1.62 jdolecek /*
299 1.62 jdolecek * Return EINVAL if the new stack size limit is lower than
300 1.62 jdolecek * current usage. Otherwise, the process would get SIGSEGV the
301 1.62 jdolecek * moment it would try to access anything on it's current stack.
302 1.62 jdolecek * This conforms to SUSv2.
303 1.62 jdolecek */
304 1.62 jdolecek if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
305 1.62 jdolecek || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE)
306 1.62 jdolecek return (EINVAL);
307 1.40 enami
308 1.17 cgd /*
309 1.40 enami * Stack is allocated to the max at exec time with
310 1.40 enami * only "rlim_cur" bytes accessible (In other words,
311 1.40 enami * allocates stack dividing two contiguous regions at
312 1.40 enami * "rlim_cur" bytes boundary).
313 1.40 enami *
314 1.40 enami * Since allocation is done in terms of page, roundup
315 1.40 enami * "rlim_cur" (otherwise, contiguous regions
316 1.40 enami * overlap). If stack limit is going up make more
317 1.40 enami * accessible, if going down make inaccessible.
318 1.17 cgd */
319 1.40 enami limp->rlim_cur = round_page(limp->rlim_cur);
320 1.17 cgd if (limp->rlim_cur != alimp->rlim_cur) {
321 1.48 eeh vaddr_t addr;
322 1.48 eeh vsize_t size;
323 1.17 cgd vm_prot_t prot;
324 1.17 cgd
325 1.17 cgd if (limp->rlim_cur > alimp->rlim_cur) {
326 1.17 cgd prot = VM_PROT_ALL;
327 1.17 cgd size = limp->rlim_cur - alimp->rlim_cur;
328 1.17 cgd addr = USRSTACK - limp->rlim_cur;
329 1.17 cgd } else {
330 1.17 cgd prot = VM_PROT_NONE;
331 1.17 cgd size = alimp->rlim_cur - limp->rlim_cur;
332 1.17 cgd addr = USRSTACK - alimp->rlim_cur;
333 1.17 cgd }
334 1.43 mrg (void) uvm_map_protect(&p->p_vmspace->vm_map,
335 1.43 mrg addr, addr+size, prot, FALSE);
336 1.17 cgd }
337 1.17 cgd break;
338 1.19 cgd
339 1.19 cgd case RLIMIT_NOFILE:
340 1.19 cgd if (limp->rlim_cur > maxfiles)
341 1.19 cgd limp->rlim_cur = maxfiles;
342 1.19 cgd if (limp->rlim_max > maxfiles)
343 1.19 cgd limp->rlim_max = maxfiles;
344 1.19 cgd break;
345 1.19 cgd
346 1.19 cgd case RLIMIT_NPROC:
347 1.19 cgd if (limp->rlim_cur > maxproc)
348 1.19 cgd limp->rlim_cur = maxproc;
349 1.19 cgd if (limp->rlim_max > maxproc)
350 1.19 cgd limp->rlim_max = maxproc;
351 1.19 cgd break;
352 1.17 cgd }
353 1.17 cgd *alimp = *limp;
354 1.17 cgd return (0);
355 1.17 cgd }
356 1.17 cgd
357 1.17 cgd /* ARGSUSED */
358 1.25 cgd int
359 1.68 thorpej sys_getrlimit(l, v, retval)
360 1.68 thorpej struct lwp *l;
361 1.30 thorpej void *v;
362 1.30 thorpej register_t *retval;
363 1.30 thorpej {
364 1.54 augustss struct sys_getrlimit_args /* {
365 1.42 mycroft syscallarg(int) which;
366 1.22 cgd syscallarg(struct rlimit *) rlp;
367 1.30 thorpej } */ *uap = v;
368 1.68 thorpej struct proc *p = l->l_proc;
369 1.42 mycroft int which = SCARG(uap, which);
370 1.17 cgd
371 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
372 1.17 cgd return (EINVAL);
373 1.42 mycroft return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
374 1.46 perry sizeof(struct rlimit)));
375 1.17 cgd }
376 1.17 cgd
377 1.17 cgd /*
378 1.17 cgd * Transform the running time and tick information in proc p into user,
379 1.17 cgd * system, and interrupt time usage.
380 1.17 cgd */
381 1.25 cgd void
382 1.17 cgd calcru(p, up, sp, ip)
383 1.54 augustss struct proc *p;
384 1.54 augustss struct timeval *up;
385 1.54 augustss struct timeval *sp;
386 1.54 augustss struct timeval *ip;
387 1.17 cgd {
388 1.54 augustss u_quad_t u, st, ut, it, tot;
389 1.70 dsl unsigned long sec;
390 1.70 dsl long usec;
391 1.54 augustss int s;
392 1.17 cgd struct timeval tv;
393 1.68 thorpej struct lwp *l;
394 1.17 cgd
395 1.17 cgd s = splstatclock();
396 1.17 cgd st = p->p_sticks;
397 1.17 cgd ut = p->p_uticks;
398 1.17 cgd it = p->p_iticks;
399 1.17 cgd splx(s);
400 1.17 cgd
401 1.17 cgd sec = p->p_rtime.tv_sec;
402 1.17 cgd usec = p->p_rtime.tv_usec;
403 1.70 dsl LIST_FOREACH(l, &p->p_lwps, l_sibling) {
404 1.68 thorpej if (l->l_stat == LSONPROC) {
405 1.68 thorpej struct schedstate_percpu *spc;
406 1.68 thorpej
407 1.68 thorpej KDASSERT(l->l_cpu != NULL);
408 1.68 thorpej spc = &l->l_cpu->ci_schedstate;
409 1.68 thorpej
410 1.68 thorpej /*
411 1.68 thorpej * Adjust for the current time slice. This is
412 1.68 thorpej * actually fairly important since the error
413 1.68 thorpej * here is on the order of a time quantum,
414 1.68 thorpej * which is much greater than the sampling
415 1.68 thorpej * error.
416 1.68 thorpej */
417 1.68 thorpej microtime(&tv);
418 1.68 thorpej sec += tv.tv_sec - spc->spc_runtime.tv_sec;
419 1.68 thorpej usec += tv.tv_usec - spc->spc_runtime.tv_usec;
420 1.68 thorpej }
421 1.17 cgd }
422 1.69 dsl
423 1.69 dsl tot = st + ut + it;
424 1.70 dsl u = sec * 1000000ull + usec;
425 1.70 dsl
426 1.69 dsl if (tot == 0) {
427 1.69 dsl /* No ticks, so can't use to share time out, split 50-50 */
428 1.70 dsl st = ut = u / 2;
429 1.70 dsl } else {
430 1.70 dsl st = (u * st) / tot;
431 1.70 dsl ut = (u * ut) / tot;
432 1.69 dsl }
433 1.17 cgd sp->tv_sec = st / 1000000;
434 1.17 cgd sp->tv_usec = st % 1000000;
435 1.17 cgd up->tv_sec = ut / 1000000;
436 1.17 cgd up->tv_usec = ut % 1000000;
437 1.17 cgd if (ip != NULL) {
438 1.70 dsl if (it != 0)
439 1.70 dsl it = (u * it) / tot;
440 1.17 cgd ip->tv_sec = it / 1000000;
441 1.17 cgd ip->tv_usec = it % 1000000;
442 1.17 cgd }
443 1.17 cgd }
444 1.17 cgd
445 1.17 cgd /* ARGSUSED */
446 1.25 cgd int
447 1.68 thorpej sys_getrusage(l, v, retval)
448 1.68 thorpej struct lwp *l;
449 1.30 thorpej void *v;
450 1.30 thorpej register_t *retval;
451 1.30 thorpej {
452 1.54 augustss struct sys_getrusage_args /* {
453 1.22 cgd syscallarg(int) who;
454 1.22 cgd syscallarg(struct rusage *) rusage;
455 1.30 thorpej } */ *uap = v;
456 1.54 augustss struct rusage *rup;
457 1.68 thorpej struct proc *p = l->l_proc;
458 1.17 cgd
459 1.22 cgd switch (SCARG(uap, who)) {
460 1.17 cgd
461 1.19 cgd case RUSAGE_SELF:
462 1.17 cgd rup = &p->p_stats->p_ru;
463 1.17 cgd calcru(p, &rup->ru_utime, &rup->ru_stime, NULL);
464 1.17 cgd break;
465 1.17 cgd
466 1.17 cgd case RUSAGE_CHILDREN:
467 1.17 cgd rup = &p->p_stats->p_cru;
468 1.17 cgd break;
469 1.17 cgd
470 1.17 cgd default:
471 1.17 cgd return (EINVAL);
472 1.17 cgd }
473 1.46 perry return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
474 1.17 cgd }
475 1.17 cgd
476 1.25 cgd void
477 1.17 cgd ruadd(ru, ru2)
478 1.54 augustss struct rusage *ru, *ru2;
479 1.17 cgd {
480 1.54 augustss long *ip, *ip2;
481 1.54 augustss int i;
482 1.17 cgd
483 1.27 mycroft timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
484 1.27 mycroft timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
485 1.17 cgd if (ru->ru_maxrss < ru2->ru_maxrss)
486 1.17 cgd ru->ru_maxrss = ru2->ru_maxrss;
487 1.17 cgd ip = &ru->ru_first; ip2 = &ru2->ru_first;
488 1.17 cgd for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
489 1.17 cgd *ip++ += *ip2++;
490 1.17 cgd }
491 1.17 cgd
492 1.17 cgd /*
493 1.17 cgd * Make a copy of the plimit structure.
494 1.17 cgd * We share these structures copy-on-write after fork,
495 1.17 cgd * and copy when a limit is changed.
496 1.17 cgd */
497 1.17 cgd struct plimit *
498 1.17 cgd limcopy(lim)
499 1.17 cgd struct plimit *lim;
500 1.17 cgd {
501 1.54 augustss struct plimit *newlim;
502 1.71 itojun size_t l;
503 1.17 cgd
504 1.49 thorpej newlim = pool_get(&plimit_pool, PR_WAITOK);
505 1.47 perry memcpy(newlim->pl_rlimit, lim->pl_rlimit,
506 1.17 cgd sizeof(struct rlimit) * RLIM_NLIMITS);
507 1.53 bouyer if (lim->pl_corename == defcorename) {
508 1.53 bouyer newlim->pl_corename = defcorename;
509 1.53 bouyer } else {
510 1.71 itojun l = strlen(lim->pl_corename) + 1;
511 1.71 itojun newlim->pl_corename = malloc(l, M_TEMP, M_WAITOK);
512 1.71 itojun strlcpy(newlim->pl_corename, lim->pl_corename, l);
513 1.53 bouyer }
514 1.32 mycroft newlim->p_lflags = 0;
515 1.32 mycroft newlim->p_refcnt = 1;
516 1.32 mycroft return (newlim);
517 1.32 mycroft }
518 1.32 mycroft
519 1.32 mycroft void
520 1.32 mycroft limfree(lim)
521 1.32 mycroft struct plimit *lim;
522 1.32 mycroft {
523 1.32 mycroft
524 1.32 mycroft if (--lim->p_refcnt > 0)
525 1.32 mycroft return;
526 1.53 bouyer #ifdef DIAGNOSTIC
527 1.53 bouyer if (lim->p_refcnt < 0)
528 1.53 bouyer panic("limfree");
529 1.53 bouyer #endif
530 1.53 bouyer if (lim->pl_corename != defcorename)
531 1.53 bouyer free(lim->pl_corename, M_TEMP);
532 1.49 thorpej pool_put(&plimit_pool, lim);
533 1.68 thorpej }
534 1.68 thorpej
535 1.68 thorpej struct pstats *
536 1.68 thorpej pstatscopy(ps)
537 1.68 thorpej struct pstats *ps;
538 1.68 thorpej {
539 1.68 thorpej
540 1.68 thorpej struct pstats *newps;
541 1.68 thorpej
542 1.68 thorpej newps = pool_get(&pstats_pool, PR_WAITOK);
543 1.68 thorpej
544 1.68 thorpej memset(&newps->pstat_startzero, 0,
545 1.68 thorpej (unsigned) ((caddr_t)&newps->pstat_endzero -
546 1.68 thorpej (caddr_t)&newps->pstat_startzero));
547 1.68 thorpej memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
548 1.68 thorpej ((caddr_t)&newps->pstat_endcopy -
549 1.68 thorpej (caddr_t)&newps->pstat_startcopy));
550 1.68 thorpej
551 1.68 thorpej return (newps);
552 1.68 thorpej
553 1.68 thorpej }
554 1.68 thorpej
555 1.68 thorpej void
556 1.68 thorpej pstatsfree(ps)
557 1.68 thorpej struct pstats *ps;
558 1.68 thorpej {
559 1.68 thorpej
560 1.68 thorpej pool_put(&pstats_pool, ps);
561 1.17 cgd }
562