kern_resource.c revision 1.103.4.7 1 1.103.4.7 ad /* $NetBSD: kern_resource.c,v 1.103.4.7 2007/01/12 01:04:06 ad 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.103.4.7 ad __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.103.4.7 2007/01/12 01:04:06 ad 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.100 yamt #include <sys/namei.h>
49 1.49 thorpej #include <sys/pool.h>
50 1.17 cgd #include <sys/proc.h>
51 1.74 atatat #include <sys/sysctl.h>
52 1.101 elad #include <sys/kauth.h>
53 1.17 cgd
54 1.22 cgd #include <sys/mount.h>
55 1.68 thorpej #include <sys/sa.h>
56 1.22 cgd #include <sys/syscallargs.h>
57 1.17 cgd
58 1.43 mrg #include <uvm/uvm_extern.h>
59 1.43 mrg
60 1.17 cgd /*
61 1.60 eeh * Maximum process data and stack limits.
62 1.60 eeh * They are variables so they are patchable.
63 1.60 eeh */
64 1.60 eeh rlim_t maxdmap = MAXDSIZ;
65 1.60 eeh rlim_t maxsmap = MAXSSIZ;
66 1.60 eeh
67 1.82 matt struct uihashhead *uihashtbl;
68 1.82 matt u_long uihash; /* size of hash table - 1 */
69 1.88 christos struct simplelock uihashtbl_slock = SIMPLELOCK_INITIALIZER;
70 1.82 matt
71 1.79 christos
72 1.60 eeh /*
73 1.17 cgd * Resource controls and accounting.
74 1.17 cgd */
75 1.17 cgd
76 1.25 cgd int
77 1.98 thorpej sys_getpriority(struct lwp *l, void *v, register_t *retval)
78 1.30 thorpej {
79 1.54 augustss struct sys_getpriority_args /* {
80 1.22 cgd syscallarg(int) which;
81 1.81 kleink syscallarg(id_t) who;
82 1.30 thorpej } */ *uap = v;
83 1.68 thorpej struct proc *curp = l->l_proc, *p;
84 1.54 augustss int low = NZERO + PRIO_MAX + 1;
85 1.103.4.1 ad int who = SCARG(uap, who);
86 1.17 cgd
87 1.22 cgd switch (SCARG(uap, which)) {
88 1.17 cgd
89 1.17 cgd case PRIO_PROCESS:
90 1.103.4.1 ad if (who == 0)
91 1.17 cgd p = curp;
92 1.17 cgd else
93 1.103.4.1 ad p = p_find(who, 0);
94 1.103.4.1 ad if (p != NULL)
95 1.103.4.1 ad low = p->p_nice;
96 1.103.4.1 ad if (who != 0)
97 1.103.4.1 ad rw_exit(&proclist_lock);
98 1.17 cgd break;
99 1.17 cgd
100 1.17 cgd case PRIO_PGRP: {
101 1.54 augustss struct pgrp *pg;
102 1.17 cgd
103 1.103.4.1 ad rw_enter(&proclist_lock, RW_READER);
104 1.103.4.1 ad if (who == 0)
105 1.17 cgd pg = curp->p_pgrp;
106 1.103.4.1 ad else if ((pg = pg_find(who, PFIND_LOCKED | PFIND_UNLOCK_FAIL))
107 1.103.4.1 ad == NULL)
108 1.17 cgd break;
109 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
110 1.17 cgd if (p->p_nice < low)
111 1.17 cgd low = p->p_nice;
112 1.17 cgd }
113 1.103.4.1 ad rw_exit(&proclist_lock);
114 1.17 cgd break;
115 1.17 cgd }
116 1.17 cgd
117 1.17 cgd case PRIO_USER:
118 1.103.4.1 ad if (who == 0)
119 1.103.4.1 ad who = (int)kauth_cred_geteuid(l->l_cred);
120 1.103.4.1 ad rw_enter(&proclist_lock, RW_READER);
121 1.86 yamt PROCLIST_FOREACH(p, &allproc) {
122 1.103.4.4 ad mutex_enter(&p->p_mutex);
123 1.102 ad if (kauth_cred_geteuid(p->p_cred) ==
124 1.103.4.1 ad (uid_t)who && p->p_nice < low)
125 1.17 cgd low = p->p_nice;
126 1.103.4.4 ad mutex_exit(&p->p_mutex);
127 1.64 matt }
128 1.103.4.1 ad rw_exit(&proclist_lock);
129 1.17 cgd break;
130 1.17 cgd
131 1.17 cgd default:
132 1.17 cgd return (EINVAL);
133 1.17 cgd }
134 1.37 ws if (low == NZERO + PRIO_MAX + 1)
135 1.17 cgd return (ESRCH);
136 1.37 ws *retval = low - NZERO;
137 1.17 cgd return (0);
138 1.17 cgd }
139 1.17 cgd
140 1.17 cgd /* ARGSUSED */
141 1.25 cgd int
142 1.98 thorpej sys_setpriority(struct lwp *l, void *v, register_t *retval)
143 1.30 thorpej {
144 1.54 augustss struct sys_setpriority_args /* {
145 1.22 cgd syscallarg(int) which;
146 1.81 kleink syscallarg(id_t) who;
147 1.22 cgd syscallarg(int) prio;
148 1.30 thorpej } */ *uap = v;
149 1.68 thorpej struct proc *curp = l->l_proc, *p;
150 1.17 cgd int found = 0, error = 0;
151 1.103.4.1 ad int who = SCARG(uap, who);
152 1.17 cgd
153 1.22 cgd switch (SCARG(uap, which)) {
154 1.17 cgd
155 1.17 cgd case PRIO_PROCESS:
156 1.103.4.1 ad if (who == 0)
157 1.17 cgd p = curp;
158 1.17 cgd else
159 1.103.4.1 ad p = p_find(who, 0);
160 1.103.4.1 ad if (p != 0) {
161 1.103.4.4 ad mutex_enter(&p->p_mutex);
162 1.103.4.1 ad error = donice(l, p, SCARG(uap, prio));
163 1.103.4.4 ad mutex_exit(&p->p_mutex);
164 1.103.4.1 ad }
165 1.103.4.1 ad if (who != 0)
166 1.103.4.1 ad rw_exit(&proclist_lock);
167 1.17 cgd found++;
168 1.17 cgd break;
169 1.17 cgd
170 1.17 cgd case PRIO_PGRP: {
171 1.54 augustss struct pgrp *pg;
172 1.87 perry
173 1.103.4.1 ad rw_enter(&proclist_lock, RW_READER);
174 1.103.4.1 ad if (who == 0)
175 1.17 cgd pg = curp->p_pgrp;
176 1.103.4.1 ad else if ((pg = pg_find(who, PFIND_LOCKED | PFIND_UNLOCK_FAIL)) == NULL)
177 1.17 cgd break;
178 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
179 1.103.4.4 ad mutex_enter(&p->p_mutex);
180 1.102 ad error = donice(l, p, SCARG(uap, prio));
181 1.103.4.4 ad mutex_exit(&p->p_mutex);
182 1.17 cgd found++;
183 1.17 cgd }
184 1.103.4.1 ad rw_exit(&proclist_lock);
185 1.17 cgd break;
186 1.17 cgd }
187 1.17 cgd
188 1.17 cgd case PRIO_USER:
189 1.103.4.1 ad if (who == 0)
190 1.103.4.1 ad who = (int)kauth_cred_geteuid(l->l_cred);
191 1.103.4.1 ad rw_enter(&proclist_lock, RW_READER);
192 1.86 yamt PROCLIST_FOREACH(p, &allproc) {
193 1.103.4.4 ad mutex_enter(&p->p_mutex);
194 1.102 ad if (kauth_cred_geteuid(p->p_cred) ==
195 1.102 ad (uid_t)SCARG(uap, who)) {
196 1.102 ad error = donice(l, p, SCARG(uap, prio));
197 1.17 cgd found++;
198 1.17 cgd }
199 1.103.4.4 ad mutex_exit(&p->p_mutex);
200 1.64 matt }
201 1.103.4.1 ad rw_exit(&proclist_lock);
202 1.17 cgd break;
203 1.17 cgd
204 1.17 cgd default:
205 1.17 cgd return (EINVAL);
206 1.17 cgd }
207 1.17 cgd if (found == 0)
208 1.17 cgd return (ESRCH);
209 1.17 cgd return (error);
210 1.17 cgd }
211 1.17 cgd
212 1.103.4.1 ad /*
213 1.103.4.1 ad * Renice a process.
214 1.103.4.1 ad *
215 1.103.4.1 ad * Call with the target process' credentials locked.
216 1.103.4.1 ad */
217 1.25 cgd int
218 1.102 ad donice(struct lwp *l, struct proc *chgp, int n)
219 1.17 cgd {
220 1.102 ad kauth_cred_t cred = l->l_cred;
221 1.103.4.2 ad int onice;
222 1.17 cgd
223 1.103.4.4 ad LOCK_ASSERT(mutex_owned(&chgp->p_mutex));
224 1.103.4.1 ad
225 1.17 cgd if (n > PRIO_MAX)
226 1.17 cgd n = PRIO_MAX;
227 1.17 cgd if (n < PRIO_MIN)
228 1.17 cgd n = PRIO_MIN;
229 1.37 ws n += NZERO;
230 1.103.4.7 ad onice = chgp->p_nice; /* XXXSMP not covered by p_mutex */
231 1.103.4.2 ad
232 1.103.4.2 ad again:
233 1.103.4.7 ad if (kauth_authorize_process(cred, KAUTH_PROCESS_RESOURCE, chgp,
234 1.103.4.7 ad (void *)KAUTH_REQ_PROCESS_RESOURCE_NICE, KAUTH_ARG(n), NULL))
235 1.17 cgd return (EACCES);
236 1.103.4.2 ad mutex_enter(&chgp->p_smutex);
237 1.103.4.2 ad if (onice != chgp->p_nice) {
238 1.103.4.2 ad mutex_exit(&chgp->p_smutex);
239 1.103.4.2 ad goto again;
240 1.103.4.2 ad }
241 1.17 cgd chgp->p_nice = n;
242 1.68 thorpej (void)resetprocpriority(chgp);
243 1.103.4.2 ad mutex_exit(&chgp->p_smutex);
244 1.17 cgd return (0);
245 1.17 cgd }
246 1.17 cgd
247 1.17 cgd /* ARGSUSED */
248 1.25 cgd int
249 1.98 thorpej sys_setrlimit(struct lwp *l, void *v, register_t *retval)
250 1.30 thorpej {
251 1.54 augustss struct sys_setrlimit_args /* {
252 1.42 mycroft syscallarg(int) which;
253 1.39 cgd syscallarg(const struct rlimit *) rlp;
254 1.30 thorpej } */ *uap = v;
255 1.42 mycroft int which = SCARG(uap, which);
256 1.19 cgd struct rlimit alim;
257 1.17 cgd int error;
258 1.17 cgd
259 1.46 perry error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
260 1.33 christos if (error)
261 1.17 cgd return (error);
262 1.102 ad return (dosetrlimit(l, l->l_proc, which, &alim));
263 1.17 cgd }
264 1.17 cgd
265 1.17 cgd int
266 1.102 ad dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp)
267 1.17 cgd {
268 1.54 augustss struct rlimit *alimp;
269 1.83 pk struct plimit *oldplim;
270 1.17 cgd int error;
271 1.17 cgd
272 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
273 1.17 cgd return (EINVAL);
274 1.38 matthias
275 1.38 matthias if (limp->rlim_cur < 0 || limp->rlim_max < 0)
276 1.38 matthias return (EINVAL);
277 1.38 matthias
278 1.17 cgd alimp = &p->p_rlimit[which];
279 1.53 bouyer /* if we don't change the value, no need to limcopy() */
280 1.53 bouyer if (limp->rlim_cur == alimp->rlim_cur &&
281 1.53 bouyer limp->rlim_max == alimp->rlim_max)
282 1.53 bouyer return 0;
283 1.53 bouyer
284 1.62 jdolecek if (limp->rlim_cur > limp->rlim_max) {
285 1.62 jdolecek /*
286 1.62 jdolecek * This is programming error. According to SUSv2, we should
287 1.62 jdolecek * return error in this case.
288 1.62 jdolecek */
289 1.62 jdolecek return (EINVAL);
290 1.62 jdolecek }
291 1.103.4.7 ad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RESOURCE,
292 1.103.4.7 ad p, KAUTH_ARG(KAUTH_REQ_PROCESS_RESOURCE_RLIMIT), limp,
293 1.103.4.7 ad KAUTH_ARG(which));
294 1.103.4.7 ad if (error)
295 1.17 cgd return (error);
296 1.62 jdolecek
297 1.17 cgd if (p->p_limit->p_refcnt > 1 &&
298 1.17 cgd (p->p_limit->p_lflags & PL_SHAREMOD) == 0) {
299 1.83 pk p->p_limit = limcopy(oldplim = p->p_limit);
300 1.83 pk limfree(oldplim);
301 1.17 cgd alimp = &p->p_rlimit[which];
302 1.17 cgd }
303 1.17 cgd
304 1.17 cgd switch (which) {
305 1.17 cgd
306 1.17 cgd case RLIMIT_DATA:
307 1.19 cgd if (limp->rlim_cur > maxdmap)
308 1.19 cgd limp->rlim_cur = maxdmap;
309 1.19 cgd if (limp->rlim_max > maxdmap)
310 1.19 cgd limp->rlim_max = maxdmap;
311 1.17 cgd break;
312 1.17 cgd
313 1.17 cgd case RLIMIT_STACK:
314 1.19 cgd if (limp->rlim_cur > maxsmap)
315 1.19 cgd limp->rlim_cur = maxsmap;
316 1.19 cgd if (limp->rlim_max > maxsmap)
317 1.19 cgd limp->rlim_max = maxsmap;
318 1.62 jdolecek
319 1.62 jdolecek /*
320 1.62 jdolecek * Return EINVAL if the new stack size limit is lower than
321 1.62 jdolecek * current usage. Otherwise, the process would get SIGSEGV the
322 1.62 jdolecek * moment it would try to access anything on it's current stack.
323 1.62 jdolecek * This conforms to SUSv2.
324 1.62 jdolecek */
325 1.62 jdolecek if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
326 1.62 jdolecek || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE)
327 1.62 jdolecek return (EINVAL);
328 1.40 enami
329 1.17 cgd /*
330 1.40 enami * Stack is allocated to the max at exec time with
331 1.40 enami * only "rlim_cur" bytes accessible (In other words,
332 1.40 enami * allocates stack dividing two contiguous regions at
333 1.40 enami * "rlim_cur" bytes boundary).
334 1.40 enami *
335 1.40 enami * Since allocation is done in terms of page, roundup
336 1.40 enami * "rlim_cur" (otherwise, contiguous regions
337 1.40 enami * overlap). If stack limit is going up make more
338 1.40 enami * accessible, if going down make inaccessible.
339 1.17 cgd */
340 1.40 enami limp->rlim_cur = round_page(limp->rlim_cur);
341 1.17 cgd if (limp->rlim_cur != alimp->rlim_cur) {
342 1.48 eeh vaddr_t addr;
343 1.48 eeh vsize_t size;
344 1.17 cgd vm_prot_t prot;
345 1.17 cgd
346 1.17 cgd if (limp->rlim_cur > alimp->rlim_cur) {
347 1.73 chs prot = VM_PROT_READ | VM_PROT_WRITE;
348 1.17 cgd size = limp->rlim_cur - alimp->rlim_cur;
349 1.91 fvdl addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
350 1.91 fvdl limp->rlim_cur;
351 1.17 cgd } else {
352 1.17 cgd prot = VM_PROT_NONE;
353 1.17 cgd size = alimp->rlim_cur - limp->rlim_cur;
354 1.91 fvdl addr = (vaddr_t)p->p_vmspace->vm_minsaddr -
355 1.91 fvdl alimp->rlim_cur;
356 1.17 cgd }
357 1.43 mrg (void) uvm_map_protect(&p->p_vmspace->vm_map,
358 1.102 ad addr, addr+size, prot, FALSE);
359 1.17 cgd }
360 1.17 cgd break;
361 1.19 cgd
362 1.19 cgd case RLIMIT_NOFILE:
363 1.19 cgd if (limp->rlim_cur > maxfiles)
364 1.19 cgd limp->rlim_cur = maxfiles;
365 1.19 cgd if (limp->rlim_max > maxfiles)
366 1.19 cgd limp->rlim_max = maxfiles;
367 1.19 cgd break;
368 1.19 cgd
369 1.19 cgd case RLIMIT_NPROC:
370 1.19 cgd if (limp->rlim_cur > maxproc)
371 1.19 cgd limp->rlim_cur = maxproc;
372 1.19 cgd if (limp->rlim_max > maxproc)
373 1.19 cgd limp->rlim_max = maxproc;
374 1.19 cgd break;
375 1.17 cgd }
376 1.17 cgd *alimp = *limp;
377 1.17 cgd return (0);
378 1.17 cgd }
379 1.17 cgd
380 1.17 cgd /* ARGSUSED */
381 1.25 cgd int
382 1.98 thorpej sys_getrlimit(struct lwp *l, void *v, register_t *retval)
383 1.30 thorpej {
384 1.54 augustss struct sys_getrlimit_args /* {
385 1.42 mycroft syscallarg(int) which;
386 1.22 cgd syscallarg(struct rlimit *) rlp;
387 1.30 thorpej } */ *uap = v;
388 1.68 thorpej struct proc *p = l->l_proc;
389 1.42 mycroft int which = SCARG(uap, which);
390 1.17 cgd
391 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
392 1.17 cgd return (EINVAL);
393 1.42 mycroft return (copyout(&p->p_rlimit[which], SCARG(uap, rlp),
394 1.46 perry sizeof(struct rlimit)));
395 1.17 cgd }
396 1.17 cgd
397 1.17 cgd /*
398 1.17 cgd * Transform the running time and tick information in proc p into user,
399 1.17 cgd * system, and interrupt time usage.
400 1.103.4.6 ad *
401 1.103.4.6 ad * Should be called with p->p_smutex held unless called from exit1().
402 1.17 cgd */
403 1.25 cgd void
404 1.98 thorpej calcru(struct proc *p, struct timeval *up, struct timeval *sp,
405 1.103.4.2 ad struct timeval *ip, struct timeval *rp)
406 1.17 cgd {
407 1.54 augustss u_quad_t u, st, ut, it, tot;
408 1.70 dsl unsigned long sec;
409 1.70 dsl long usec;
410 1.103.4.6 ad struct timeval tv;
411 1.68 thorpej struct lwp *l;
412 1.17 cgd
413 1.103.4.6 ad mutex_enter(&p->p_stmutex);
414 1.17 cgd st = p->p_sticks;
415 1.17 cgd ut = p->p_uticks;
416 1.17 cgd it = p->p_iticks;
417 1.103.4.6 ad mutex_exit(&p->p_stmutex);
418 1.103.4.6 ad
419 1.103.4.6 ad sec = p->p_rtime.tv_sec;
420 1.103.4.6 ad usec = p->p_rtime.tv_usec;
421 1.17 cgd
422 1.70 dsl LIST_FOREACH(l, &p->p_lwps, l_sibling) {
423 1.103.4.2 ad lwp_lock(l);
424 1.103.4.2 ad sec += l->l_rtime.tv_sec;
425 1.103.4.6 ad if ((usec += l->l_rtime.tv_usec) >= 1000000) {
426 1.103.4.6 ad sec++;
427 1.103.4.6 ad usec -= 1000000;
428 1.103.4.6 ad }
429 1.103.4.6 ad if (l->l_cpu == curcpu()) {
430 1.68 thorpej struct schedstate_percpu *spc;
431 1.87 perry
432 1.68 thorpej KDASSERT(l->l_cpu != NULL);
433 1.68 thorpej spc = &l->l_cpu->ci_schedstate;
434 1.87 perry
435 1.68 thorpej /*
436 1.68 thorpej * Adjust for the current time slice. This is
437 1.68 thorpej * actually fairly important since the error
438 1.68 thorpej * here is on the order of a time quantum,
439 1.68 thorpej * which is much greater than the sampling
440 1.87 perry * error.
441 1.68 thorpej */
442 1.68 thorpej microtime(&tv);
443 1.68 thorpej sec += tv.tv_sec - spc->spc_runtime.tv_sec;
444 1.68 thorpej usec += tv.tv_usec - spc->spc_runtime.tv_usec;
445 1.103.4.6 ad if (usec >= 1000000) {
446 1.103.4.6 ad sec++;
447 1.103.4.6 ad usec -= 1000000;
448 1.103.4.6 ad }
449 1.68 thorpej }
450 1.103.4.2 ad lwp_unlock(l);
451 1.17 cgd }
452 1.69 dsl
453 1.69 dsl tot = st + ut + it;
454 1.70 dsl u = sec * 1000000ull + usec;
455 1.70 dsl
456 1.69 dsl if (tot == 0) {
457 1.69 dsl /* No ticks, so can't use to share time out, split 50-50 */
458 1.70 dsl st = ut = u / 2;
459 1.70 dsl } else {
460 1.70 dsl st = (u * st) / tot;
461 1.70 dsl ut = (u * ut) / tot;
462 1.69 dsl }
463 1.103.4.2 ad if (sp != NULL) {
464 1.103.4.2 ad sp->tv_sec = st / 1000000;
465 1.103.4.2 ad sp->tv_usec = st % 1000000;
466 1.103.4.2 ad }
467 1.103.4.2 ad if (up != NULL) {
468 1.103.4.2 ad up->tv_sec = ut / 1000000;
469 1.103.4.2 ad up->tv_usec = ut % 1000000;
470 1.103.4.2 ad }
471 1.17 cgd if (ip != NULL) {
472 1.70 dsl if (it != 0)
473 1.70 dsl it = (u * it) / tot;
474 1.17 cgd ip->tv_sec = it / 1000000;
475 1.17 cgd ip->tv_usec = it % 1000000;
476 1.17 cgd }
477 1.103.4.2 ad if (rp != NULL) {
478 1.103.4.2 ad rp->tv_sec = sec;
479 1.103.4.2 ad rp->tv_usec = usec;
480 1.103.4.2 ad }
481 1.17 cgd }
482 1.17 cgd
483 1.17 cgd /* ARGSUSED */
484 1.25 cgd int
485 1.98 thorpej sys_getrusage(struct lwp *l, void *v, register_t *retval)
486 1.30 thorpej {
487 1.54 augustss struct sys_getrusage_args /* {
488 1.22 cgd syscallarg(int) who;
489 1.22 cgd syscallarg(struct rusage *) rusage;
490 1.30 thorpej } */ *uap = v;
491 1.54 augustss struct rusage *rup;
492 1.68 thorpej struct proc *p = l->l_proc;
493 1.17 cgd
494 1.22 cgd switch (SCARG(uap, who)) {
495 1.17 cgd
496 1.19 cgd case RUSAGE_SELF:
497 1.17 cgd rup = &p->p_stats->p_ru;
498 1.103.4.3 ad mutex_enter(&p->p_smutex);
499 1.103.4.2 ad calcru(p, &rup->ru_utime, &rup->ru_stime, NULL, NULL);
500 1.103.4.3 ad mutex_exit(&p->p_smutex);
501 1.17 cgd break;
502 1.17 cgd
503 1.17 cgd case RUSAGE_CHILDREN:
504 1.17 cgd rup = &p->p_stats->p_cru;
505 1.17 cgd break;
506 1.17 cgd
507 1.17 cgd default:
508 1.17 cgd return (EINVAL);
509 1.17 cgd }
510 1.46 perry return (copyout(rup, SCARG(uap, rusage), sizeof(struct rusage)));
511 1.17 cgd }
512 1.17 cgd
513 1.25 cgd void
514 1.98 thorpej ruadd(struct rusage *ru, struct rusage *ru2)
515 1.17 cgd {
516 1.54 augustss long *ip, *ip2;
517 1.54 augustss int i;
518 1.17 cgd
519 1.27 mycroft timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
520 1.27 mycroft timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
521 1.17 cgd if (ru->ru_maxrss < ru2->ru_maxrss)
522 1.17 cgd ru->ru_maxrss = ru2->ru_maxrss;
523 1.17 cgd ip = &ru->ru_first; ip2 = &ru2->ru_first;
524 1.17 cgd for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
525 1.17 cgd *ip++ += *ip2++;
526 1.17 cgd }
527 1.17 cgd
528 1.17 cgd /*
529 1.17 cgd * Make a copy of the plimit structure.
530 1.17 cgd * We share these structures copy-on-write after fork,
531 1.17 cgd * and copy when a limit is changed.
532 1.17 cgd */
533 1.17 cgd struct plimit *
534 1.98 thorpej limcopy(struct plimit *lim)
535 1.17 cgd {
536 1.54 augustss struct plimit *newlim;
537 1.83 pk size_t l = 0;
538 1.83 pk
539 1.83 pk simple_lock(&lim->p_slock);
540 1.83 pk if (lim->pl_corename != defcorename)
541 1.83 pk l = strlen(lim->pl_corename) + 1;
542 1.83 pk simple_unlock(&lim->p_slock);
543 1.17 cgd
544 1.49 thorpej newlim = pool_get(&plimit_pool, PR_WAITOK);
545 1.83 pk simple_lock_init(&newlim->p_slock);
546 1.83 pk newlim->p_lflags = 0;
547 1.83 pk newlim->p_refcnt = 1;
548 1.83 pk newlim->pl_corename = (l != 0)
549 1.83 pk ? malloc(l, M_TEMP, M_WAITOK)
550 1.83 pk : defcorename;
551 1.83 pk
552 1.83 pk simple_lock(&lim->p_slock);
553 1.47 perry memcpy(newlim->pl_rlimit, lim->pl_rlimit,
554 1.17 cgd sizeof(struct rlimit) * RLIM_NLIMITS);
555 1.83 pk
556 1.83 pk if (l != 0)
557 1.71 itojun strlcpy(newlim->pl_corename, lim->pl_corename, l);
558 1.83 pk simple_unlock(&lim->p_slock);
559 1.83 pk
560 1.32 mycroft return (newlim);
561 1.32 mycroft }
562 1.32 mycroft
563 1.32 mycroft void
564 1.98 thorpej limfree(struct plimit *lim)
565 1.32 mycroft {
566 1.84 christos int n;
567 1.85 kleink
568 1.83 pk simple_lock(&lim->p_slock);
569 1.84 christos n = --lim->p_refcnt;
570 1.83 pk simple_unlock(&lim->p_slock);
571 1.83 pk if (n > 0)
572 1.32 mycroft return;
573 1.53 bouyer #ifdef DIAGNOSTIC
574 1.83 pk if (n < 0)
575 1.53 bouyer panic("limfree");
576 1.53 bouyer #endif
577 1.53 bouyer if (lim->pl_corename != defcorename)
578 1.53 bouyer free(lim->pl_corename, M_TEMP);
579 1.49 thorpej pool_put(&plimit_pool, lim);
580 1.68 thorpej }
581 1.68 thorpej
582 1.68 thorpej struct pstats *
583 1.98 thorpej pstatscopy(struct pstats *ps)
584 1.68 thorpej {
585 1.87 perry
586 1.68 thorpej struct pstats *newps;
587 1.68 thorpej
588 1.68 thorpej newps = pool_get(&pstats_pool, PR_WAITOK);
589 1.68 thorpej
590 1.68 thorpej memset(&newps->pstat_startzero, 0,
591 1.68 thorpej (unsigned) ((caddr_t)&newps->pstat_endzero -
592 1.68 thorpej (caddr_t)&newps->pstat_startzero));
593 1.68 thorpej memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
594 1.68 thorpej ((caddr_t)&newps->pstat_endcopy -
595 1.68 thorpej (caddr_t)&newps->pstat_startcopy));
596 1.68 thorpej
597 1.68 thorpej return (newps);
598 1.68 thorpej
599 1.68 thorpej }
600 1.68 thorpej
601 1.68 thorpej void
602 1.98 thorpej pstatsfree(struct pstats *ps)
603 1.68 thorpej {
604 1.68 thorpej
605 1.68 thorpej pool_put(&pstats_pool, ps);
606 1.74 atatat }
607 1.74 atatat
608 1.74 atatat /*
609 1.74 atatat * sysctl interface in five parts
610 1.74 atatat */
611 1.74 atatat
612 1.74 atatat /*
613 1.74 atatat * a routine for sysctl proc subtree helpers that need to pick a valid
614 1.74 atatat * process by pid.
615 1.74 atatat */
616 1.74 atatat static int
617 1.102 ad sysctl_proc_findproc(struct lwp *l, struct proc **p2, pid_t pid)
618 1.74 atatat {
619 1.74 atatat struct proc *ptmp;
620 1.101 elad int error = 0;
621 1.74 atatat
622 1.74 atatat if (pid == PROC_CURPROC)
623 1.102 ad ptmp = l->l_proc;
624 1.74 atatat else if ((ptmp = pfind(pid)) == NULL)
625 1.74 atatat error = ESRCH;
626 1.74 atatat
627 1.74 atatat *p2 = ptmp;
628 1.74 atatat return (error);
629 1.74 atatat }
630 1.74 atatat
631 1.74 atatat /*
632 1.74 atatat * sysctl helper routine for setting a process's specific corefile
633 1.74 atatat * name. picks the process based on the given pid and checks the
634 1.74 atatat * correctness of the new value.
635 1.74 atatat */
636 1.74 atatat static int
637 1.74 atatat sysctl_proc_corename(SYSCTLFN_ARGS)
638 1.74 atatat {
639 1.102 ad struct proc *ptmp;
640 1.83 pk struct plimit *lim;
641 1.74 atatat int error = 0, len;
642 1.100 yamt char *cname;
643 1.100 yamt char *tmp;
644 1.74 atatat struct sysctlnode node;
645 1.74 atatat
646 1.74 atatat /*
647 1.74 atatat * is this all correct?
648 1.74 atatat */
649 1.74 atatat if (namelen != 0)
650 1.74 atatat return (EINVAL);
651 1.74 atatat if (name[-1] != PROC_PID_CORENAME)
652 1.74 atatat return (EINVAL);
653 1.74 atatat
654 1.74 atatat /*
655 1.74 atatat * whom are we tweaking?
656 1.74 atatat */
657 1.102 ad error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]);
658 1.74 atatat if (error)
659 1.74 atatat return (error);
660 1.74 atatat
661 1.103.4.7 ad /* XXX this should be in p_find() */
662 1.103.4.7 ad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
663 1.103.4.7 ad ptmp, NULL, NULL, NULL);
664 1.103.4.7 ad if (error)
665 1.103.4.7 ad return (error);
666 1.103.4.7 ad
667 1.100 yamt cname = PNBUF_GET();
668 1.74 atatat /*
669 1.74 atatat * let them modify a temporary copy of the core name
670 1.74 atatat */
671 1.74 atatat node = *rnode;
672 1.100 yamt strlcpy(cname, ptmp->p_limit->pl_corename, MAXPATHLEN);
673 1.74 atatat node.sysctl_data = cname;
674 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
675 1.74 atatat
676 1.74 atatat /*
677 1.74 atatat * if that failed, or they have nothing new to say, or we've
678 1.74 atatat * heard it before...
679 1.74 atatat */
680 1.74 atatat if (error || newp == NULL ||
681 1.100 yamt strcmp(cname, ptmp->p_limit->pl_corename) == 0) {
682 1.100 yamt goto done;
683 1.100 yamt }
684 1.74 atatat
685 1.103.4.7 ad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CORENAME,
686 1.103.4.7 ad ptmp, cname, NULL, NULL);
687 1.103.4.7 ad if (error)
688 1.103.4.7 ad return (error);
689 1.103 elad
690 1.74 atatat /*
691 1.74 atatat * no error yet and cname now has the new core name in it.
692 1.74 atatat * let's see if it looks acceptable. it must be either "core"
693 1.74 atatat * or end in ".core" or "/core".
694 1.74 atatat */
695 1.74 atatat len = strlen(cname);
696 1.100 yamt if (len < 4) {
697 1.100 yamt error = EINVAL;
698 1.100 yamt } else if (strcmp(cname + len - 4, "core") != 0) {
699 1.100 yamt error = EINVAL;
700 1.100 yamt } else if (len > 4 && cname[len - 5] != '/' && cname[len - 5] != '.') {
701 1.100 yamt error = EINVAL;
702 1.100 yamt }
703 1.100 yamt if (error != 0) {
704 1.100 yamt goto done;
705 1.100 yamt }
706 1.74 atatat
707 1.74 atatat /*
708 1.74 atatat * hmm...looks good. now...where do we put it?
709 1.74 atatat */
710 1.74 atatat tmp = malloc(len + 1, M_TEMP, M_WAITOK|M_CANFAIL);
711 1.100 yamt if (tmp == NULL) {
712 1.100 yamt error = ENOMEM;
713 1.100 yamt goto done;
714 1.100 yamt }
715 1.74 atatat strlcpy(tmp, cname, len + 1);
716 1.74 atatat
717 1.83 pk lim = ptmp->p_limit;
718 1.83 pk if (lim->p_refcnt > 1 && (lim->p_lflags & PL_SHAREMOD) == 0) {
719 1.83 pk ptmp->p_limit = limcopy(lim);
720 1.83 pk limfree(lim);
721 1.83 pk lim = ptmp->p_limit;
722 1.83 pk }
723 1.83 pk if (lim->pl_corename != defcorename)
724 1.83 pk free(lim->pl_corename, M_TEMP);
725 1.83 pk lim->pl_corename = tmp;
726 1.100 yamt done:
727 1.100 yamt PNBUF_PUT(cname);
728 1.100 yamt return error;
729 1.74 atatat }
730 1.74 atatat
731 1.74 atatat /*
732 1.74 atatat * sysctl helper routine for checking/setting a process's stop flags,
733 1.74 atatat * one for fork and one for exec.
734 1.74 atatat */
735 1.74 atatat static int
736 1.74 atatat sysctl_proc_stop(SYSCTLFN_ARGS)
737 1.74 atatat {
738 1.102 ad struct proc *ptmp;
739 1.74 atatat int i, f, error = 0;
740 1.74 atatat struct sysctlnode node;
741 1.74 atatat
742 1.74 atatat if (namelen != 0)
743 1.74 atatat return (EINVAL);
744 1.74 atatat
745 1.102 ad error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-2]);
746 1.74 atatat if (error)
747 1.74 atatat return (error);
748 1.74 atatat
749 1.103.4.7 ad /* XXX this should be in p_find() */
750 1.103.4.7 ad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
751 1.103.4.7 ad ptmp, NULL, NULL, NULL);
752 1.103.4.7 ad if (error)
753 1.103.4.7 ad return (error);
754 1.103.4.7 ad
755 1.74 atatat switch (rnode->sysctl_num) {
756 1.74 atatat case PROC_PID_STOPFORK:
757 1.103.4.4 ad f = PS_STOPFORK;
758 1.74 atatat break;
759 1.74 atatat case PROC_PID_STOPEXEC:
760 1.103.4.4 ad f = PS_STOPEXEC;
761 1.74 atatat break;
762 1.74 atatat case PROC_PID_STOPEXIT:
763 1.103.4.4 ad f = PS_STOPEXIT;
764 1.74 atatat break;
765 1.74 atatat default:
766 1.74 atatat return (EINVAL);
767 1.74 atatat }
768 1.74 atatat
769 1.74 atatat i = (ptmp->p_flag & f) ? 1 : 0;
770 1.74 atatat node = *rnode;
771 1.74 atatat node.sysctl_data = &i;
772 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
773 1.74 atatat if (error || newp == NULL)
774 1.74 atatat return (error);
775 1.74 atatat
776 1.103.4.4 ad mutex_enter(&ptmp->p_smutex);
777 1.103.4.7 ad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
778 1.103.4.7 ad ptmp, KAUTH_ARG(f), NULL, NULL);
779 1.103.4.7 ad if (error)
780 1.103.4.7 ad return (error);
781 1.74 atatat if (i)
782 1.103.4.4 ad ptmp->p_sflag |= f;
783 1.74 atatat else
784 1.103.4.4 ad ptmp->p_sflag &= ~f;
785 1.103.4.4 ad mutex_exit(&ptmp->p_smutex);
786 1.74 atatat
787 1.74 atatat return (0);
788 1.74 atatat }
789 1.74 atatat
790 1.74 atatat /*
791 1.74 atatat * sysctl helper routine for a process's rlimits as exposed by sysctl.
792 1.74 atatat */
793 1.74 atatat static int
794 1.74 atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
795 1.74 atatat {
796 1.102 ad struct proc *ptmp;
797 1.74 atatat u_int limitno;
798 1.74 atatat int which, error = 0;
799 1.74 atatat struct rlimit alim;
800 1.74 atatat struct sysctlnode node;
801 1.74 atatat
802 1.74 atatat if (namelen != 0)
803 1.74 atatat return (EINVAL);
804 1.74 atatat
805 1.74 atatat which = name[-1];
806 1.74 atatat if (which != PROC_PID_LIMIT_TYPE_SOFT &&
807 1.74 atatat which != PROC_PID_LIMIT_TYPE_HARD)
808 1.74 atatat return (EINVAL);
809 1.74 atatat
810 1.74 atatat limitno = name[-2] - 1;
811 1.74 atatat if (limitno >= RLIM_NLIMITS)
812 1.74 atatat return (EINVAL);
813 1.74 atatat
814 1.74 atatat if (name[-3] != PROC_PID_LIMIT)
815 1.74 atatat return (EINVAL);
816 1.74 atatat
817 1.102 ad error = sysctl_proc_findproc(l, &ptmp, (pid_t)name[-4]);
818 1.74 atatat if (error)
819 1.74 atatat return (error);
820 1.74 atatat
821 1.103.4.7 ad /* XXX this should be in p_find() */
822 1.103.4.7 ad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE,
823 1.103.4.7 ad ptmp, NULL, NULL, NULL);
824 1.103.4.7 ad if (error)
825 1.103.4.7 ad return (error);
826 1.103.4.7 ad
827 1.74 atatat node = *rnode;
828 1.74 atatat memcpy(&alim, &ptmp->p_rlimit[limitno], sizeof(alim));
829 1.74 atatat if (which == PROC_PID_LIMIT_TYPE_HARD)
830 1.74 atatat node.sysctl_data = &alim.rlim_max;
831 1.74 atatat else
832 1.74 atatat node.sysctl_data = &alim.rlim_cur;
833 1.74 atatat
834 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
835 1.74 atatat if (error || newp == NULL)
836 1.74 atatat return (error);
837 1.74 atatat
838 1.102 ad return (dosetrlimit(l, ptmp, limitno, &alim));
839 1.74 atatat }
840 1.74 atatat
841 1.74 atatat /*
842 1.74 atatat * and finally, the actually glue that sticks it to the tree
843 1.74 atatat */
844 1.74 atatat SYSCTL_SETUP(sysctl_proc_setup, "sysctl proc subtree setup")
845 1.74 atatat {
846 1.74 atatat
847 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
848 1.76 atatat CTLFLAG_PERMANENT,
849 1.74 atatat CTLTYPE_NODE, "proc", NULL,
850 1.74 atatat NULL, 0, NULL, 0,
851 1.74 atatat CTL_PROC, CTL_EOL);
852 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
853 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
854 1.78 atatat CTLTYPE_NODE, "curproc",
855 1.78 atatat SYSCTL_DESCR("Per-process settings"),
856 1.74 atatat NULL, 0, NULL, 0,
857 1.74 atatat CTL_PROC, PROC_CURPROC, CTL_EOL);
858 1.74 atatat
859 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
860 1.103 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
861 1.78 atatat CTLTYPE_STRING, "corename",
862 1.78 atatat SYSCTL_DESCR("Core file name"),
863 1.74 atatat sysctl_proc_corename, 0, NULL, MAXPATHLEN,
864 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
865 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
866 1.76 atatat CTLFLAG_PERMANENT,
867 1.78 atatat CTLTYPE_NODE, "rlimit",
868 1.78 atatat SYSCTL_DESCR("Process limits"),
869 1.74 atatat NULL, 0, NULL, 0,
870 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
871 1.74 atatat
872 1.74 atatat #define create_proc_plimit(s, n) do { \
873 1.76 atatat sysctl_createv(clog, 0, NULL, NULL, \
874 1.76 atatat CTLFLAG_PERMANENT, \
875 1.78 atatat CTLTYPE_NODE, s, \
876 1.78 atatat SYSCTL_DESCR("Process " s " limits"), \
877 1.74 atatat NULL, 0, NULL, 0, \
878 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
879 1.74 atatat CTL_EOL); \
880 1.76 atatat sysctl_createv(clog, 0, NULL, NULL, \
881 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
882 1.78 atatat CTLTYPE_QUAD, "soft", \
883 1.78 atatat SYSCTL_DESCR("Process soft " s " limit"), \
884 1.74 atatat sysctl_proc_plimit, 0, NULL, 0, \
885 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
886 1.74 atatat PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
887 1.76 atatat sysctl_createv(clog, 0, NULL, NULL, \
888 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
889 1.78 atatat CTLTYPE_QUAD, "hard", \
890 1.78 atatat SYSCTL_DESCR("Process hard " s " limit"), \
891 1.74 atatat sysctl_proc_plimit, 0, NULL, 0, \
892 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
893 1.74 atatat PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
894 1.74 atatat } while (0/*CONSTCOND*/)
895 1.74 atatat
896 1.74 atatat create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
897 1.74 atatat create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
898 1.74 atatat create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
899 1.74 atatat create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
900 1.74 atatat create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
901 1.74 atatat create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
902 1.74 atatat create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
903 1.74 atatat create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
904 1.74 atatat create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
905 1.79 christos create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
906 1.74 atatat
907 1.74 atatat #undef create_proc_plimit
908 1.74 atatat
909 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
910 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
911 1.78 atatat CTLTYPE_INT, "stopfork",
912 1.78 atatat SYSCTL_DESCR("Stop process at fork(2)"),
913 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
914 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
915 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
916 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
917 1.78 atatat CTLTYPE_INT, "stopexec",
918 1.78 atatat SYSCTL_DESCR("Stop process at execve(2)"),
919 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
920 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
921 1.76 atatat sysctl_createv(clog, 0, NULL, NULL,
922 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
923 1.78 atatat CTLTYPE_INT, "stopexit",
924 1.78 atatat SYSCTL_DESCR("Stop process before completing exit"),
925 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
926 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
927 1.17 cgd }
928 1.79 christos
929 1.88 christos struct uidinfo *
930 1.88 christos uid_find(uid_t uid)
931 1.79 christos {
932 1.79 christos struct uidinfo *uip;
933 1.90 christos struct uidinfo *newuip = NULL;
934 1.79 christos struct uihashhead *uipp;
935 1.79 christos
936 1.79 christos uipp = UIHASH(uid);
937 1.79 christos
938 1.90 christos again:
939 1.89 christos simple_lock(&uihashtbl_slock);
940 1.79 christos LIST_FOREACH(uip, uipp, ui_hash)
941 1.88 christos if (uip->ui_uid == uid) {
942 1.88 christos simple_unlock(&uihashtbl_slock);
943 1.90 christos if (newuip)
944 1.90 christos free(newuip, M_PROC);
945 1.79 christos return uip;
946 1.88 christos }
947 1.79 christos
948 1.90 christos if (newuip == NULL) {
949 1.90 christos simple_unlock(&uihashtbl_slock);
950 1.90 christos newuip = malloc(sizeof(*uip), M_PROC, M_WAITOK | M_ZERO);
951 1.90 christos goto again;
952 1.90 christos }
953 1.90 christos uip = newuip;
954 1.89 christos
955 1.79 christos LIST_INSERT_HEAD(uipp, uip, ui_hash);
956 1.79 christos uip->ui_uid = uid;
957 1.94 christos simple_lock_init(&uip->ui_slock);
958 1.88 christos simple_unlock(&uihashtbl_slock);
959 1.89 christos
960 1.79 christos return uip;
961 1.79 christos }
962 1.79 christos
963 1.79 christos /*
964 1.79 christos * Change the count associated with number of processes
965 1.79 christos * a given user is using.
966 1.79 christos */
967 1.79 christos int
968 1.79 christos chgproccnt(uid_t uid, int diff)
969 1.79 christos {
970 1.79 christos struct uidinfo *uip;
971 1.96 christos int s;
972 1.79 christos
973 1.79 christos if (diff == 0)
974 1.79 christos return 0;
975 1.79 christos
976 1.88 christos uip = uid_find(uid);
977 1.96 christos UILOCK(uip, s);
978 1.88 christos uip->ui_proccnt += diff;
979 1.88 christos KASSERT(uip->ui_proccnt >= 0);
980 1.96 christos UIUNLOCK(uip, s);
981 1.88 christos return uip->ui_proccnt;
982 1.79 christos }
983 1.79 christos
984 1.79 christos int
985 1.97 christos chgsbsize(struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t xmax)
986 1.79 christos {
987 1.79 christos rlim_t nsb;
988 1.96 christos int s;
989 1.79 christos
990 1.96 christos UILOCK(uip, s);
991 1.80 yamt nsb = uip->ui_sbsize + to - *hiwat;
992 1.97 christos if (to > *hiwat && nsb > xmax) {
993 1.96 christos UIUNLOCK(uip, s);
994 1.95 christos splx(s);
995 1.88 christos return 0;
996 1.94 christos }
997 1.79 christos *hiwat = to;
998 1.79 christos uip->ui_sbsize = nsb;
999 1.79 christos KASSERT(uip->ui_sbsize >= 0);
1000 1.96 christos UIUNLOCK(uip, s);
1001 1.88 christos return 1;
1002 1.79 christos }
1003