kern_resource.c revision 1.172.2.1 1 1.172.2.1 rmind /* $NetBSD: kern_resource.c,v 1.172.2.1 2014/05/18 17:46:07 rmind 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.172.2.1 rmind __KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.172.2.1 2014/05/18 17:46:07 rmind 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.132 yamt #include <sys/kmem.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.129 yamt #include <sys/timevar.h>
53 1.101 elad #include <sys/kauth.h>
54 1.125 ad #include <sys/atomic.h>
55 1.22 cgd #include <sys/mount.h>
56 1.22 cgd #include <sys/syscallargs.h>
57 1.136 ad #include <sys/atomic.h>
58 1.17 cgd
59 1.43 mrg #include <uvm/uvm_extern.h>
60 1.43 mrg
61 1.17 cgd /*
62 1.60 eeh * Maximum process data and stack limits.
63 1.60 eeh * They are variables so they are patchable.
64 1.60 eeh */
65 1.167 rmind rlim_t maxdmap = MAXDSIZ;
66 1.167 rmind rlim_t maxsmap = MAXSSIZ;
67 1.60 eeh
68 1.164 rmind static pool_cache_t plimit_cache __read_mostly;
69 1.164 rmind static pool_cache_t pstats_cache __read_mostly;
70 1.130 ad
71 1.154 elad static kauth_listener_t resource_listener;
72 1.164 rmind static struct sysctllog *proc_sysctllog;
73 1.153 elad
74 1.164 rmind static int donice(struct lwp *, struct proc *, int);
75 1.164 rmind static void sysctl_proc_setup(void);
76 1.156 pooka
77 1.153 elad static int
78 1.154 elad resource_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
79 1.153 elad void *arg0, void *arg1, void *arg2, void *arg3)
80 1.153 elad {
81 1.153 elad struct proc *p;
82 1.153 elad int result;
83 1.153 elad
84 1.153 elad result = KAUTH_RESULT_DEFER;
85 1.153 elad p = arg0;
86 1.153 elad
87 1.154 elad switch (action) {
88 1.154 elad case KAUTH_PROCESS_NICE:
89 1.154 elad if (kauth_cred_geteuid(cred) != kauth_cred_geteuid(p->p_cred) &&
90 1.164 rmind kauth_cred_getuid(cred) != kauth_cred_geteuid(p->p_cred)) {
91 1.164 rmind break;
92 1.164 rmind }
93 1.153 elad
94 1.164 rmind if ((u_long)arg1 >= p->p_nice)
95 1.164 rmind result = KAUTH_RESULT_ALLOW;
96 1.153 elad
97 1.154 elad break;
98 1.154 elad
99 1.154 elad case KAUTH_PROCESS_RLIMIT: {
100 1.154 elad enum kauth_process_req req;
101 1.153 elad
102 1.154 elad req = (enum kauth_process_req)(unsigned long)arg1;
103 1.153 elad
104 1.154 elad switch (req) {
105 1.154 elad case KAUTH_REQ_PROCESS_RLIMIT_GET:
106 1.153 elad result = KAUTH_RESULT_ALLOW;
107 1.154 elad break;
108 1.154 elad
109 1.154 elad case KAUTH_REQ_PROCESS_RLIMIT_SET: {
110 1.154 elad struct rlimit *new_rlimit;
111 1.154 elad u_long which;
112 1.154 elad
113 1.154 elad if ((p != curlwp->l_proc) &&
114 1.154 elad (proc_uidmatch(cred, p->p_cred) != 0))
115 1.154 elad break;
116 1.154 elad
117 1.154 elad new_rlimit = arg2;
118 1.154 elad which = (u_long)arg3;
119 1.154 elad
120 1.154 elad if (new_rlimit->rlim_max <= p->p_rlimit[which].rlim_max)
121 1.154 elad result = KAUTH_RESULT_ALLOW;
122 1.154 elad
123 1.154 elad break;
124 1.154 elad }
125 1.154 elad
126 1.154 elad default:
127 1.154 elad break;
128 1.154 elad }
129 1.154 elad
130 1.154 elad break;
131 1.154 elad }
132 1.154 elad
133 1.154 elad default:
134 1.154 elad break;
135 1.153 elad }
136 1.153 elad
137 1.153 elad return result;
138 1.153 elad }
139 1.153 elad
140 1.130 ad void
141 1.130 ad resource_init(void)
142 1.130 ad {
143 1.130 ad
144 1.130 ad plimit_cache = pool_cache_init(sizeof(struct plimit), 0, 0, 0,
145 1.130 ad "plimitpl", NULL, IPL_NONE, NULL, NULL, NULL);
146 1.130 ad pstats_cache = pool_cache_init(sizeof(struct pstats), 0, 0, 0,
147 1.130 ad "pstatspl", NULL, IPL_NONE, NULL, NULL, NULL);
148 1.153 elad
149 1.154 elad resource_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
150 1.154 elad resource_listener_cb, NULL);
151 1.156 pooka
152 1.156 pooka sysctl_proc_setup();
153 1.130 ad }
154 1.130 ad
155 1.60 eeh /*
156 1.17 cgd * Resource controls and accounting.
157 1.17 cgd */
158 1.17 cgd
159 1.25 cgd int
160 1.134 rmind sys_getpriority(struct lwp *l, const struct sys_getpriority_args *uap,
161 1.134 rmind register_t *retval)
162 1.30 thorpej {
163 1.128 dsl /* {
164 1.22 cgd syscallarg(int) which;
165 1.81 kleink syscallarg(id_t) who;
166 1.128 dsl } */
167 1.68 thorpej struct proc *curp = l->l_proc, *p;
168 1.164 rmind id_t who = SCARG(uap, who);
169 1.54 augustss int low = NZERO + PRIO_MAX + 1;
170 1.17 cgd
171 1.138 ad mutex_enter(proc_lock);
172 1.22 cgd switch (SCARG(uap, which)) {
173 1.17 cgd case PRIO_PROCESS:
174 1.171 njoly p = who ? proc_find(who) : curp;
175 1.113 ad if (p != NULL)
176 1.113 ad low = p->p_nice;
177 1.17 cgd break;
178 1.17 cgd
179 1.17 cgd case PRIO_PGRP: {
180 1.54 augustss struct pgrp *pg;
181 1.17 cgd
182 1.113 ad if (who == 0)
183 1.17 cgd pg = curp->p_pgrp;
184 1.157 rmind else if ((pg = pgrp_find(who)) == NULL)
185 1.17 cgd break;
186 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
187 1.17 cgd if (p->p_nice < low)
188 1.17 cgd low = p->p_nice;
189 1.17 cgd }
190 1.17 cgd break;
191 1.17 cgd }
192 1.17 cgd
193 1.17 cgd case PRIO_USER:
194 1.113 ad if (who == 0)
195 1.113 ad who = (int)kauth_cred_geteuid(l->l_cred);
196 1.86 yamt PROCLIST_FOREACH(p, &allproc) {
197 1.139 ad mutex_enter(p->p_lock);
198 1.102 ad if (kauth_cred_geteuid(p->p_cred) ==
199 1.113 ad (uid_t)who && p->p_nice < low)
200 1.17 cgd low = p->p_nice;
201 1.139 ad mutex_exit(p->p_lock);
202 1.64 matt }
203 1.17 cgd break;
204 1.17 cgd
205 1.17 cgd default:
206 1.138 ad mutex_exit(proc_lock);
207 1.164 rmind return EINVAL;
208 1.17 cgd }
209 1.138 ad mutex_exit(proc_lock);
210 1.113 ad
211 1.164 rmind if (low == NZERO + PRIO_MAX + 1) {
212 1.164 rmind return ESRCH;
213 1.164 rmind }
214 1.37 ws *retval = low - NZERO;
215 1.164 rmind return 0;
216 1.17 cgd }
217 1.17 cgd
218 1.25 cgd int
219 1.134 rmind sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap,
220 1.134 rmind register_t *retval)
221 1.30 thorpej {
222 1.128 dsl /* {
223 1.22 cgd syscallarg(int) which;
224 1.81 kleink syscallarg(id_t) who;
225 1.22 cgd syscallarg(int) prio;
226 1.128 dsl } */
227 1.68 thorpej struct proc *curp = l->l_proc, *p;
228 1.164 rmind id_t who = SCARG(uap, who);
229 1.17 cgd int found = 0, error = 0;
230 1.17 cgd
231 1.138 ad mutex_enter(proc_lock);
232 1.22 cgd switch (SCARG(uap, which)) {
233 1.17 cgd case PRIO_PROCESS:
234 1.157 rmind p = who ? proc_find(who) : curp;
235 1.157 rmind if (p != NULL) {
236 1.139 ad mutex_enter(p->p_lock);
237 1.162 christos found++;
238 1.113 ad error = donice(l, p, SCARG(uap, prio));
239 1.139 ad mutex_exit(p->p_lock);
240 1.113 ad }
241 1.17 cgd break;
242 1.17 cgd
243 1.17 cgd case PRIO_PGRP: {
244 1.54 augustss struct pgrp *pg;
245 1.87 perry
246 1.113 ad if (who == 0)
247 1.17 cgd pg = curp->p_pgrp;
248 1.157 rmind else if ((pg = pgrp_find(who)) == NULL)
249 1.17 cgd break;
250 1.64 matt LIST_FOREACH(p, &pg->pg_members, p_pglist) {
251 1.139 ad mutex_enter(p->p_lock);
252 1.162 christos found++;
253 1.102 ad error = donice(l, p, SCARG(uap, prio));
254 1.139 ad mutex_exit(p->p_lock);
255 1.162 christos if (error)
256 1.162 christos break;
257 1.17 cgd }
258 1.17 cgd break;
259 1.17 cgd }
260 1.17 cgd
261 1.17 cgd case PRIO_USER:
262 1.113 ad if (who == 0)
263 1.113 ad who = (int)kauth_cred_geteuid(l->l_cred);
264 1.86 yamt PROCLIST_FOREACH(p, &allproc) {
265 1.139 ad mutex_enter(p->p_lock);
266 1.102 ad if (kauth_cred_geteuid(p->p_cred) ==
267 1.102 ad (uid_t)SCARG(uap, who)) {
268 1.162 christos found++;
269 1.102 ad error = donice(l, p, SCARG(uap, prio));
270 1.17 cgd }
271 1.139 ad mutex_exit(p->p_lock);
272 1.162 christos if (error)
273 1.162 christos break;
274 1.64 matt }
275 1.17 cgd break;
276 1.17 cgd
277 1.17 cgd default:
278 1.144 njoly mutex_exit(proc_lock);
279 1.144 njoly return EINVAL;
280 1.17 cgd }
281 1.138 ad mutex_exit(proc_lock);
282 1.164 rmind
283 1.164 rmind return (found == 0) ? ESRCH : error;
284 1.17 cgd }
285 1.17 cgd
286 1.113 ad /*
287 1.113 ad * Renice a process.
288 1.113 ad *
289 1.113 ad * Call with the target process' credentials locked.
290 1.113 ad */
291 1.164 rmind static int
292 1.102 ad donice(struct lwp *l, struct proc *chgp, int n)
293 1.17 cgd {
294 1.102 ad kauth_cred_t cred = l->l_cred;
295 1.113 ad
296 1.139 ad KASSERT(mutex_owned(chgp->p_lock));
297 1.17 cgd
298 1.152 elad if (kauth_cred_geteuid(cred) && kauth_cred_getuid(cred) &&
299 1.152 elad kauth_cred_geteuid(cred) != kauth_cred_geteuid(chgp->p_cred) &&
300 1.152 elad kauth_cred_getuid(cred) != kauth_cred_geteuid(chgp->p_cred))
301 1.164 rmind return EPERM;
302 1.152 elad
303 1.164 rmind if (n > PRIO_MAX) {
304 1.17 cgd n = PRIO_MAX;
305 1.164 rmind }
306 1.164 rmind if (n < PRIO_MIN) {
307 1.17 cgd n = PRIO_MIN;
308 1.164 rmind }
309 1.37 ws n += NZERO;
310 1.164 rmind
311 1.112 elad if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp,
312 1.164 rmind KAUTH_ARG(n), NULL, NULL)) {
313 1.164 rmind return EACCES;
314 1.164 rmind }
315 1.164 rmind
316 1.117 yamt sched_nice(chgp, n);
317 1.164 rmind return 0;
318 1.17 cgd }
319 1.17 cgd
320 1.25 cgd int
321 1.134 rmind sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap,
322 1.134 rmind register_t *retval)
323 1.30 thorpej {
324 1.128 dsl /* {
325 1.42 mycroft syscallarg(int) which;
326 1.39 cgd syscallarg(const struct rlimit *) rlp;
327 1.128 dsl } */
328 1.164 rmind int error, which = SCARG(uap, which);
329 1.19 cgd struct rlimit alim;
330 1.17 cgd
331 1.46 perry error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
332 1.164 rmind if (error) {
333 1.164 rmind return error;
334 1.164 rmind }
335 1.164 rmind return dosetrlimit(l, l->l_proc, which, &alim);
336 1.17 cgd }
337 1.17 cgd
338 1.17 cgd int
339 1.102 ad dosetrlimit(struct lwp *l, struct proc *p, int which, struct rlimit *limp)
340 1.17 cgd {
341 1.54 augustss struct rlimit *alimp;
342 1.17 cgd int error;
343 1.17 cgd
344 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
345 1.164 rmind return EINVAL;
346 1.38 matthias
347 1.62 jdolecek if (limp->rlim_cur > limp->rlim_max) {
348 1.62 jdolecek /*
349 1.62 jdolecek * This is programming error. According to SUSv2, we should
350 1.62 jdolecek * return error in this case.
351 1.62 jdolecek */
352 1.164 rmind return EINVAL;
353 1.62 jdolecek }
354 1.122 dsl
355 1.122 dsl alimp = &p->p_rlimit[which];
356 1.122 dsl /* if we don't change the value, no need to limcopy() */
357 1.122 dsl if (limp->rlim_cur == alimp->rlim_cur &&
358 1.122 dsl limp->rlim_max == alimp->rlim_max)
359 1.122 dsl return 0;
360 1.122 dsl
361 1.112 elad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
362 1.131 elad p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which));
363 1.111 elad if (error)
364 1.164 rmind return error;
365 1.62 jdolecek
366 1.161 rmind lim_privatise(p);
367 1.122 dsl /* p->p_limit is now unchangeable */
368 1.122 dsl alimp = &p->p_rlimit[which];
369 1.17 cgd
370 1.17 cgd switch (which) {
371 1.17 cgd
372 1.17 cgd case RLIMIT_DATA:
373 1.19 cgd if (limp->rlim_cur > maxdmap)
374 1.19 cgd limp->rlim_cur = maxdmap;
375 1.19 cgd if (limp->rlim_max > maxdmap)
376 1.19 cgd limp->rlim_max = maxdmap;
377 1.17 cgd break;
378 1.17 cgd
379 1.17 cgd case RLIMIT_STACK:
380 1.19 cgd if (limp->rlim_cur > maxsmap)
381 1.19 cgd limp->rlim_cur = maxsmap;
382 1.19 cgd if (limp->rlim_max > maxsmap)
383 1.19 cgd limp->rlim_max = maxsmap;
384 1.62 jdolecek
385 1.62 jdolecek /*
386 1.62 jdolecek * Return EINVAL if the new stack size limit is lower than
387 1.62 jdolecek * current usage. Otherwise, the process would get SIGSEGV the
388 1.62 jdolecek * moment it would try to access anything on it's current stack.
389 1.62 jdolecek * This conforms to SUSv2.
390 1.62 jdolecek */
391 1.164 rmind if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE ||
392 1.164 rmind limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
393 1.164 rmind return EINVAL;
394 1.113 ad }
395 1.40 enami
396 1.17 cgd /*
397 1.40 enami * Stack is allocated to the max at exec time with
398 1.40 enami * only "rlim_cur" bytes accessible (In other words,
399 1.40 enami * allocates stack dividing two contiguous regions at
400 1.40 enami * "rlim_cur" bytes boundary).
401 1.40 enami *
402 1.40 enami * Since allocation is done in terms of page, roundup
403 1.40 enami * "rlim_cur" (otherwise, contiguous regions
404 1.40 enami * overlap). If stack limit is going up make more
405 1.40 enami * accessible, if going down make inaccessible.
406 1.17 cgd */
407 1.40 enami limp->rlim_cur = round_page(limp->rlim_cur);
408 1.17 cgd if (limp->rlim_cur != alimp->rlim_cur) {
409 1.48 eeh vaddr_t addr;
410 1.48 eeh vsize_t size;
411 1.17 cgd vm_prot_t prot;
412 1.172 chs char *base, *tmp;
413 1.17 cgd
414 1.172 chs base = p->p_vmspace->vm_minsaddr;
415 1.17 cgd if (limp->rlim_cur > alimp->rlim_cur) {
416 1.73 chs prot = VM_PROT_READ | VM_PROT_WRITE;
417 1.17 cgd size = limp->rlim_cur - alimp->rlim_cur;
418 1.172 chs tmp = STACK_GROW(base, alimp->rlim_cur);
419 1.17 cgd } else {
420 1.17 cgd prot = VM_PROT_NONE;
421 1.17 cgd size = alimp->rlim_cur - limp->rlim_cur;
422 1.172 chs tmp = STACK_GROW(base, limp->rlim_cur);
423 1.17 cgd }
424 1.172 chs addr = (vaddr_t)STACK_ALLOC(tmp, size);
425 1.43 mrg (void) uvm_map_protect(&p->p_vmspace->vm_map,
426 1.172 chs addr, addr + size, prot, false);
427 1.17 cgd }
428 1.17 cgd break;
429 1.19 cgd
430 1.19 cgd case RLIMIT_NOFILE:
431 1.19 cgd if (limp->rlim_cur > maxfiles)
432 1.19 cgd limp->rlim_cur = maxfiles;
433 1.19 cgd if (limp->rlim_max > maxfiles)
434 1.19 cgd limp->rlim_max = maxfiles;
435 1.19 cgd break;
436 1.19 cgd
437 1.19 cgd case RLIMIT_NPROC:
438 1.19 cgd if (limp->rlim_cur > maxproc)
439 1.19 cgd limp->rlim_cur = maxproc;
440 1.19 cgd if (limp->rlim_max > maxproc)
441 1.19 cgd limp->rlim_max = maxproc;
442 1.19 cgd break;
443 1.169 christos
444 1.169 christos case RLIMIT_NTHR:
445 1.169 christos if (limp->rlim_cur > maxlwp)
446 1.169 christos limp->rlim_cur = maxlwp;
447 1.169 christos if (limp->rlim_max > maxlwp)
448 1.169 christos limp->rlim_max = maxlwp;
449 1.169 christos break;
450 1.17 cgd }
451 1.122 dsl
452 1.122 dsl mutex_enter(&p->p_limit->pl_lock);
453 1.17 cgd *alimp = *limp;
454 1.122 dsl mutex_exit(&p->p_limit->pl_lock);
455 1.164 rmind return 0;
456 1.17 cgd }
457 1.17 cgd
458 1.25 cgd int
459 1.134 rmind sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap,
460 1.134 rmind register_t *retval)
461 1.30 thorpej {
462 1.128 dsl /* {
463 1.42 mycroft syscallarg(int) which;
464 1.22 cgd syscallarg(struct rlimit *) rlp;
465 1.128 dsl } */
466 1.68 thorpej struct proc *p = l->l_proc;
467 1.42 mycroft int which = SCARG(uap, which);
468 1.119 ad struct rlimit rl;
469 1.17 cgd
470 1.67 itojun if ((u_int)which >= RLIM_NLIMITS)
471 1.164 rmind return EINVAL;
472 1.119 ad
473 1.139 ad mutex_enter(p->p_lock);
474 1.119 ad memcpy(&rl, &p->p_rlimit[which], sizeof(rl));
475 1.139 ad mutex_exit(p->p_lock);
476 1.119 ad
477 1.119 ad return copyout(&rl, SCARG(uap, rlp), sizeof(rl));
478 1.17 cgd }
479 1.17 cgd
480 1.17 cgd /*
481 1.17 cgd * Transform the running time and tick information in proc p into user,
482 1.17 cgd * system, and interrupt time usage.
483 1.113 ad *
484 1.139 ad * Should be called with p->p_lock held unless called from exit1().
485 1.17 cgd */
486 1.25 cgd void
487 1.98 thorpej calcru(struct proc *p, struct timeval *up, struct timeval *sp,
488 1.113 ad struct timeval *ip, struct timeval *rp)
489 1.17 cgd {
490 1.129 yamt uint64_t u, st, ut, it, tot;
491 1.68 thorpej struct lwp *l;
492 1.129 yamt struct bintime tm;
493 1.129 yamt struct timeval tv;
494 1.17 cgd
495 1.168 yamt KASSERT(p->p_stat == SDEAD || mutex_owned(p->p_lock));
496 1.168 yamt
497 1.113 ad mutex_spin_enter(&p->p_stmutex);
498 1.17 cgd st = p->p_sticks;
499 1.17 cgd ut = p->p_uticks;
500 1.17 cgd it = p->p_iticks;
501 1.113 ad mutex_spin_exit(&p->p_stmutex);
502 1.17 cgd
503 1.129 yamt tm = p->p_rtime;
504 1.113 ad
505 1.70 dsl LIST_FOREACH(l, &p->p_lwps, l_sibling) {
506 1.113 ad lwp_lock(l);
507 1.129 yamt bintime_add(&tm, &l->l_rtime);
508 1.142 ad if ((l->l_pflag & LP_RUNNING) != 0) {
509 1.129 yamt struct bintime diff;
510 1.68 thorpej /*
511 1.68 thorpej * Adjust for the current time slice. This is
512 1.68 thorpej * actually fairly important since the error
513 1.68 thorpej * here is on the order of a time quantum,
514 1.68 thorpej * which is much greater than the sampling
515 1.87 perry * error.
516 1.68 thorpej */
517 1.129 yamt binuptime(&diff);
518 1.129 yamt bintime_sub(&diff, &l->l_stime);
519 1.129 yamt bintime_add(&tm, &diff);
520 1.68 thorpej }
521 1.113 ad lwp_unlock(l);
522 1.17 cgd }
523 1.69 dsl
524 1.69 dsl tot = st + ut + it;
525 1.129 yamt bintime2timeval(&tm, &tv);
526 1.129 yamt u = (uint64_t)tv.tv_sec * 1000000ul + tv.tv_usec;
527 1.70 dsl
528 1.69 dsl if (tot == 0) {
529 1.69 dsl /* No ticks, so can't use to share time out, split 50-50 */
530 1.70 dsl st = ut = u / 2;
531 1.70 dsl } else {
532 1.70 dsl st = (u * st) / tot;
533 1.70 dsl ut = (u * ut) / tot;
534 1.69 dsl }
535 1.113 ad if (sp != NULL) {
536 1.113 ad sp->tv_sec = st / 1000000;
537 1.113 ad sp->tv_usec = st % 1000000;
538 1.113 ad }
539 1.113 ad if (up != NULL) {
540 1.113 ad up->tv_sec = ut / 1000000;
541 1.113 ad up->tv_usec = ut % 1000000;
542 1.113 ad }
543 1.17 cgd if (ip != NULL) {
544 1.70 dsl if (it != 0)
545 1.70 dsl it = (u * it) / tot;
546 1.17 cgd ip->tv_sec = it / 1000000;
547 1.17 cgd ip->tv_usec = it % 1000000;
548 1.17 cgd }
549 1.113 ad if (rp != NULL) {
550 1.129 yamt *rp = tv;
551 1.113 ad }
552 1.17 cgd }
553 1.17 cgd
554 1.25 cgd int
555 1.148 christos sys___getrusage50(struct lwp *l, const struct sys___getrusage50_args *uap,
556 1.134 rmind register_t *retval)
557 1.30 thorpej {
558 1.128 dsl /* {
559 1.22 cgd syscallarg(int) who;
560 1.22 cgd syscallarg(struct rusage *) rusage;
561 1.128 dsl } */
562 1.170 njoly int error;
563 1.119 ad struct rusage ru;
564 1.68 thorpej struct proc *p = l->l_proc;
565 1.17 cgd
566 1.170 njoly error = getrusage1(p, SCARG(uap, who), &ru);
567 1.170 njoly if (error != 0)
568 1.170 njoly return error;
569 1.170 njoly
570 1.170 njoly return copyout(&ru, SCARG(uap, rusage), sizeof(ru));
571 1.170 njoly }
572 1.170 njoly
573 1.170 njoly int
574 1.170 njoly getrusage1(struct proc *p, int who, struct rusage *ru) {
575 1.170 njoly
576 1.170 njoly switch (who) {
577 1.19 cgd case RUSAGE_SELF:
578 1.139 ad mutex_enter(p->p_lock);
579 1.170 njoly memcpy(ru, &p->p_stats->p_ru, sizeof(*ru));
580 1.170 njoly calcru(p, &ru->ru_utime, &ru->ru_stime, NULL, NULL);
581 1.170 njoly rulwps(p, ru);
582 1.139 ad mutex_exit(p->p_lock);
583 1.17 cgd break;
584 1.17 cgd case RUSAGE_CHILDREN:
585 1.139 ad mutex_enter(p->p_lock);
586 1.170 njoly memcpy(ru, &p->p_stats->p_cru, sizeof(*ru));
587 1.139 ad mutex_exit(p->p_lock);
588 1.17 cgd break;
589 1.17 cgd default:
590 1.119 ad return EINVAL;
591 1.17 cgd }
592 1.119 ad
593 1.170 njoly return 0;
594 1.17 cgd }
595 1.17 cgd
596 1.25 cgd void
597 1.98 thorpej ruadd(struct rusage *ru, struct rusage *ru2)
598 1.17 cgd {
599 1.54 augustss long *ip, *ip2;
600 1.54 augustss int i;
601 1.17 cgd
602 1.27 mycroft timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
603 1.27 mycroft timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
604 1.17 cgd if (ru->ru_maxrss < ru2->ru_maxrss)
605 1.17 cgd ru->ru_maxrss = ru2->ru_maxrss;
606 1.17 cgd ip = &ru->ru_first; ip2 = &ru2->ru_first;
607 1.17 cgd for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
608 1.17 cgd *ip++ += *ip2++;
609 1.17 cgd }
610 1.17 cgd
611 1.137 ad void
612 1.137 ad rulwps(proc_t *p, struct rusage *ru)
613 1.137 ad {
614 1.137 ad lwp_t *l;
615 1.137 ad
616 1.139 ad KASSERT(mutex_owned(p->p_lock));
617 1.137 ad
618 1.137 ad LIST_FOREACH(l, &p->p_lwps, l_sibling) {
619 1.137 ad ruadd(ru, &l->l_ru);
620 1.137 ad ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
621 1.137 ad ru->ru_nivcsw += l->l_nivcsw;
622 1.137 ad }
623 1.137 ad }
624 1.137 ad
625 1.17 cgd /*
626 1.161 rmind * lim_copy: make a copy of the plimit structure.
627 1.113 ad *
628 1.161 rmind * We use copy-on-write after fork, and copy when a limit is changed.
629 1.17 cgd */
630 1.17 cgd struct plimit *
631 1.122 dsl lim_copy(struct plimit *lim)
632 1.17 cgd {
633 1.122 dsl struct plimit *newlim;
634 1.113 ad char *corename;
635 1.122 dsl size_t alen, len;
636 1.17 cgd
637 1.130 ad newlim = pool_cache_get(plimit_cache, PR_WAITOK);
638 1.121 dsl mutex_init(&newlim->pl_lock, MUTEX_DEFAULT, IPL_NONE);
639 1.161 rmind newlim->pl_writeable = false;
640 1.121 dsl newlim->pl_refcnt = 1;
641 1.122 dsl newlim->pl_sv_limit = NULL;
642 1.122 dsl
643 1.122 dsl mutex_enter(&lim->pl_lock);
644 1.122 dsl memcpy(newlim->pl_rlimit, lim->pl_rlimit,
645 1.122 dsl sizeof(struct rlimit) * RLIM_NLIMITS);
646 1.83 pk
647 1.161 rmind /*
648 1.161 rmind * Note: the common case is a use of default core name.
649 1.161 rmind */
650 1.122 dsl alen = 0;
651 1.122 dsl corename = NULL;
652 1.113 ad for (;;) {
653 1.122 dsl if (lim->pl_corename == defcorename) {
654 1.122 dsl newlim->pl_corename = defcorename;
655 1.161 rmind newlim->pl_cnlen = 0;
656 1.122 dsl break;
657 1.122 dsl }
658 1.161 rmind len = lim->pl_cnlen;
659 1.161 rmind if (len == alen) {
660 1.122 dsl newlim->pl_corename = corename;
661 1.161 rmind newlim->pl_cnlen = len;
662 1.122 dsl memcpy(corename, lim->pl_corename, len);
663 1.122 dsl corename = NULL;
664 1.122 dsl break;
665 1.122 dsl }
666 1.122 dsl mutex_exit(&lim->pl_lock);
667 1.161 rmind if (corename) {
668 1.161 rmind kmem_free(corename, alen);
669 1.161 rmind }
670 1.122 dsl alen = len;
671 1.161 rmind corename = kmem_alloc(alen, KM_SLEEP);
672 1.121 dsl mutex_enter(&lim->pl_lock);
673 1.122 dsl }
674 1.122 dsl mutex_exit(&lim->pl_lock);
675 1.161 rmind
676 1.161 rmind if (corename) {
677 1.161 rmind kmem_free(corename, alen);
678 1.161 rmind }
679 1.122 dsl return newlim;
680 1.122 dsl }
681 1.122 dsl
682 1.122 dsl void
683 1.122 dsl lim_addref(struct plimit *lim)
684 1.122 dsl {
685 1.125 ad atomic_inc_uint(&lim->pl_refcnt);
686 1.122 dsl }
687 1.113 ad
688 1.122 dsl /*
689 1.161 rmind * lim_privatise: give a process its own private plimit structure.
690 1.122 dsl */
691 1.122 dsl void
692 1.161 rmind lim_privatise(proc_t *p)
693 1.122 dsl {
694 1.161 rmind struct plimit *lim = p->p_limit, *newlim;
695 1.122 dsl
696 1.161 rmind if (lim->pl_writeable) {
697 1.122 dsl return;
698 1.122 dsl }
699 1.122 dsl
700 1.122 dsl newlim = lim_copy(lim);
701 1.113 ad
702 1.139 ad mutex_enter(p->p_lock);
703 1.161 rmind if (p->p_limit->pl_writeable) {
704 1.161 rmind /* Other thread won the race. */
705 1.139 ad mutex_exit(p->p_lock);
706 1.159 rmind lim_free(newlim);
707 1.122 dsl return;
708 1.113 ad }
709 1.83 pk
710 1.122 dsl /*
711 1.161 rmind * Since p->p_limit can be accessed without locked held,
712 1.161 rmind * old limit structure must not be deleted yet.
713 1.122 dsl */
714 1.122 dsl newlim->pl_sv_limit = p->p_limit;
715 1.161 rmind newlim->pl_writeable = true;
716 1.122 dsl p->p_limit = newlim;
717 1.139 ad mutex_exit(p->p_lock);
718 1.32 mycroft }
719 1.32 mycroft
720 1.32 mycroft void
721 1.160 rmind lim_setcorename(proc_t *p, char *name, size_t len)
722 1.160 rmind {
723 1.160 rmind struct plimit *lim;
724 1.160 rmind char *oname;
725 1.161 rmind size_t olen;
726 1.160 rmind
727 1.161 rmind lim_privatise(p);
728 1.160 rmind lim = p->p_limit;
729 1.160 rmind
730 1.160 rmind mutex_enter(&lim->pl_lock);
731 1.160 rmind oname = lim->pl_corename;
732 1.161 rmind olen = lim->pl_cnlen;
733 1.160 rmind lim->pl_corename = name;
734 1.161 rmind lim->pl_cnlen = len;
735 1.160 rmind mutex_exit(&lim->pl_lock);
736 1.160 rmind
737 1.160 rmind if (oname != defcorename) {
738 1.161 rmind kmem_free(oname, olen);
739 1.160 rmind }
740 1.160 rmind }
741 1.160 rmind
742 1.160 rmind void
743 1.159 rmind lim_free(struct plimit *lim)
744 1.32 mycroft {
745 1.122 dsl struct plimit *sv_lim;
746 1.85 kleink
747 1.122 dsl do {
748 1.159 rmind if (atomic_dec_uint_nv(&lim->pl_refcnt) > 0) {
749 1.122 dsl return;
750 1.159 rmind }
751 1.159 rmind if (lim->pl_corename != defcorename) {
752 1.161 rmind kmem_free(lim->pl_corename, lim->pl_cnlen);
753 1.159 rmind }
754 1.122 dsl sv_lim = lim->pl_sv_limit;
755 1.122 dsl mutex_destroy(&lim->pl_lock);
756 1.130 ad pool_cache_put(plimit_cache, lim);
757 1.122 dsl } while ((lim = sv_lim) != NULL);
758 1.68 thorpej }
759 1.68 thorpej
760 1.68 thorpej struct pstats *
761 1.98 thorpej pstatscopy(struct pstats *ps)
762 1.68 thorpej {
763 1.164 rmind struct pstats *nps;
764 1.164 rmind size_t len;
765 1.87 perry
766 1.164 rmind nps = pool_cache_get(pstats_cache, PR_WAITOK);
767 1.68 thorpej
768 1.164 rmind len = (char *)&nps->pstat_endzero - (char *)&nps->pstat_startzero;
769 1.164 rmind memset(&nps->pstat_startzero, 0, len);
770 1.68 thorpej
771 1.164 rmind len = (char *)&nps->pstat_endcopy - (char *)&nps->pstat_startcopy;
772 1.164 rmind memcpy(&nps->pstat_startcopy, &ps->pstat_startcopy, len);
773 1.68 thorpej
774 1.164 rmind return nps;
775 1.68 thorpej }
776 1.68 thorpej
777 1.68 thorpej void
778 1.98 thorpej pstatsfree(struct pstats *ps)
779 1.68 thorpej {
780 1.68 thorpej
781 1.130 ad pool_cache_put(pstats_cache, ps);
782 1.74 atatat }
783 1.74 atatat
784 1.74 atatat /*
785 1.157 rmind * sysctl_proc_findproc: a routine for sysctl proc subtree helpers that
786 1.157 rmind * need to pick a valid process by PID.
787 1.157 rmind *
788 1.157 rmind * => Hold a reference on the process, on success.
789 1.74 atatat */
790 1.74 atatat static int
791 1.157 rmind sysctl_proc_findproc(lwp_t *l, pid_t pid, proc_t **p2)
792 1.74 atatat {
793 1.157 rmind proc_t *p;
794 1.157 rmind int error;
795 1.74 atatat
796 1.157 rmind if (pid == PROC_CURPROC) {
797 1.157 rmind p = l->l_proc;
798 1.157 rmind } else {
799 1.157 rmind mutex_enter(proc_lock);
800 1.157 rmind p = proc_find(pid);
801 1.157 rmind if (p == NULL) {
802 1.157 rmind mutex_exit(proc_lock);
803 1.157 rmind return ESRCH;
804 1.157 rmind }
805 1.157 rmind }
806 1.157 rmind error = rw_tryenter(&p->p_reflock, RW_READER) ? 0 : EBUSY;
807 1.157 rmind if (pid != PROC_CURPROC) {
808 1.157 rmind mutex_exit(proc_lock);
809 1.157 rmind }
810 1.157 rmind *p2 = p;
811 1.157 rmind return error;
812 1.74 atatat }
813 1.74 atatat
814 1.74 atatat /*
815 1.158 rmind * sysctl_proc_corename: helper routine to get or set the core file name
816 1.158 rmind * for a process specified by PID.
817 1.74 atatat */
818 1.74 atatat static int
819 1.74 atatat sysctl_proc_corename(SYSCTLFN_ARGS)
820 1.74 atatat {
821 1.158 rmind struct proc *p;
822 1.83 pk struct plimit *lim;
823 1.158 rmind char *cnbuf, *cname;
824 1.157 rmind struct sysctlnode node;
825 1.158 rmind size_t len;
826 1.158 rmind int error;
827 1.74 atatat
828 1.158 rmind /* First, validate the request. */
829 1.158 rmind if (namelen != 0 || name[-1] != PROC_PID_CORENAME)
830 1.158 rmind return EINVAL;
831 1.74 atatat
832 1.157 rmind /* Find the process. Hold a reference (p_reflock), if found. */
833 1.158 rmind error = sysctl_proc_findproc(l, (pid_t)name[-2], &p);
834 1.74 atatat if (error)
835 1.157 rmind return error;
836 1.74 atatat
837 1.131 elad /* XXX-elad */
838 1.158 rmind error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, p,
839 1.131 elad KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
840 1.157 rmind if (error) {
841 1.158 rmind rw_exit(&p->p_reflock);
842 1.157 rmind return error;
843 1.157 rmind }
844 1.111 elad
845 1.158 rmind cnbuf = PNBUF_GET();
846 1.158 rmind
847 1.165 mrg if (oldp) {
848 1.158 rmind /* Get case: copy the core name into the buffer. */
849 1.131 elad error = kauth_authorize_process(l->l_cred,
850 1.158 rmind KAUTH_PROCESS_CORENAME, p,
851 1.131 elad KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_GET), NULL, NULL);
852 1.157 rmind if (error) {
853 1.158 rmind goto done;
854 1.158 rmind }
855 1.158 rmind lim = p->p_limit;
856 1.158 rmind mutex_enter(&lim->pl_lock);
857 1.158 rmind strlcpy(cnbuf, lim->pl_corename, MAXPATHLEN);
858 1.158 rmind mutex_exit(&lim->pl_lock);
859 1.165 mrg }
860 1.131 elad
861 1.74 atatat node = *rnode;
862 1.158 rmind node.sysctl_data = cnbuf;
863 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
864 1.74 atatat
865 1.163 rmind /* Return if error, or if caller is only getting the core name. */
866 1.158 rmind if (error || newp == NULL) {
867 1.100 yamt goto done;
868 1.157 rmind }
869 1.103 elad
870 1.74 atatat /*
871 1.166 rmind * Set case. Check permission and then validate new core name.
872 1.166 rmind * It must be either "core", "/core", or end in ".core".
873 1.74 atatat */
874 1.166 rmind error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CORENAME,
875 1.166 rmind p, KAUTH_ARG(KAUTH_REQ_PROCESS_CORENAME_SET), cnbuf, NULL);
876 1.166 rmind if (error) {
877 1.166 rmind goto done;
878 1.166 rmind }
879 1.158 rmind len = strlen(cnbuf);
880 1.158 rmind if ((len < 4 || strcmp(cnbuf + len - 4, "core") != 0) ||
881 1.158 rmind (len > 4 && cnbuf[len - 5] != '/' && cnbuf[len - 5] != '.')) {
882 1.100 yamt error = EINVAL;
883 1.100 yamt goto done;
884 1.100 yamt }
885 1.74 atatat
886 1.158 rmind /* Allocate, copy and set the new core name for plimit structure. */
887 1.161 rmind cname = kmem_alloc(++len, KM_NOSLEEP);
888 1.158 rmind if (cname == NULL) {
889 1.100 yamt error = ENOMEM;
890 1.100 yamt goto done;
891 1.100 yamt }
892 1.158 rmind memcpy(cname, cnbuf, len);
893 1.160 rmind lim_setcorename(p, cname, len);
894 1.100 yamt done:
895 1.158 rmind rw_exit(&p->p_reflock);
896 1.158 rmind PNBUF_PUT(cnbuf);
897 1.100 yamt return error;
898 1.74 atatat }
899 1.74 atatat
900 1.74 atatat /*
901 1.163 rmind * sysctl_proc_stop: helper routine for checking/setting the stop flags.
902 1.74 atatat */
903 1.74 atatat static int
904 1.74 atatat sysctl_proc_stop(SYSCTLFN_ARGS)
905 1.74 atatat {
906 1.163 rmind struct proc *p;
907 1.163 rmind int isset, flag, error = 0;
908 1.74 atatat struct sysctlnode node;
909 1.74 atatat
910 1.74 atatat if (namelen != 0)
911 1.163 rmind return EINVAL;
912 1.74 atatat
913 1.157 rmind /* Find the process. Hold a reference (p_reflock), if found. */
914 1.163 rmind error = sysctl_proc_findproc(l, (pid_t)name[-2], &p);
915 1.74 atatat if (error)
916 1.157 rmind return error;
917 1.74 atatat
918 1.131 elad /* XXX-elad */
919 1.163 rmind error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, p,
920 1.131 elad KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
921 1.163 rmind if (error) {
922 1.157 rmind goto out;
923 1.163 rmind }
924 1.111 elad
925 1.163 rmind /* Determine the flag. */
926 1.74 atatat switch (rnode->sysctl_num) {
927 1.74 atatat case PROC_PID_STOPFORK:
928 1.163 rmind flag = PS_STOPFORK;
929 1.74 atatat break;
930 1.74 atatat case PROC_PID_STOPEXEC:
931 1.163 rmind flag = PS_STOPEXEC;
932 1.74 atatat break;
933 1.74 atatat case PROC_PID_STOPEXIT:
934 1.163 rmind flag = PS_STOPEXIT;
935 1.74 atatat break;
936 1.74 atatat default:
937 1.157 rmind error = EINVAL;
938 1.157 rmind goto out;
939 1.74 atatat }
940 1.163 rmind isset = (p->p_flag & flag) ? 1 : 0;
941 1.74 atatat node = *rnode;
942 1.163 rmind node.sysctl_data = &isset;
943 1.74 atatat error = sysctl_lookup(SYSCTLFN_CALL(&node));
944 1.163 rmind
945 1.163 rmind /* Return if error, or if callers is only getting the flag. */
946 1.163 rmind if (error || newp == NULL) {
947 1.157 rmind goto out;
948 1.163 rmind }
949 1.74 atatat
950 1.163 rmind /* Check if caller can set the flags. */
951 1.111 elad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_STOPFLAG,
952 1.163 rmind p, KAUTH_ARG(flag), NULL, NULL);
953 1.163 rmind if (error) {
954 1.163 rmind goto out;
955 1.163 rmind }
956 1.163 rmind mutex_enter(p->p_lock);
957 1.163 rmind if (isset) {
958 1.163 rmind p->p_sflag |= flag;
959 1.163 rmind } else {
960 1.163 rmind p->p_sflag &= ~flag;
961 1.143 rmind }
962 1.163 rmind mutex_exit(p->p_lock);
963 1.157 rmind out:
964 1.163 rmind rw_exit(&p->p_reflock);
965 1.143 rmind return error;
966 1.74 atatat }
967 1.74 atatat
968 1.74 atatat /*
969 1.163 rmind * sysctl_proc_plimit: helper routine to get/set rlimits of a process.
970 1.74 atatat */
971 1.74 atatat static int
972 1.74 atatat sysctl_proc_plimit(SYSCTLFN_ARGS)
973 1.74 atatat {
974 1.163 rmind struct proc *p;
975 1.74 atatat u_int limitno;
976 1.74 atatat int which, error = 0;
977 1.74 atatat struct rlimit alim;
978 1.74 atatat struct sysctlnode node;
979 1.74 atatat
980 1.74 atatat if (namelen != 0)
981 1.163 rmind return EINVAL;
982 1.74 atatat
983 1.74 atatat which = name[-1];
984 1.74 atatat if (which != PROC_PID_LIMIT_TYPE_SOFT &&
985 1.74 atatat which != PROC_PID_LIMIT_TYPE_HARD)
986 1.163 rmind return EINVAL;
987 1.74 atatat
988 1.74 atatat limitno = name[-2] - 1;
989 1.74 atatat if (limitno >= RLIM_NLIMITS)
990 1.163 rmind return EINVAL;
991 1.74 atatat
992 1.74 atatat if (name[-3] != PROC_PID_LIMIT)
993 1.163 rmind return EINVAL;
994 1.74 atatat
995 1.157 rmind /* Find the process. Hold a reference (p_reflock), if found. */
996 1.163 rmind error = sysctl_proc_findproc(l, (pid_t)name[-4], &p);
997 1.74 atatat if (error)
998 1.157 rmind return error;
999 1.74 atatat
1000 1.131 elad /* XXX-elad */
1001 1.163 rmind error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_CANSEE, p,
1002 1.131 elad KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_ENTRY), NULL, NULL);
1003 1.111 elad if (error)
1004 1.157 rmind goto out;
1005 1.111 elad
1006 1.163 rmind /* Check if caller can retrieve the limits. */
1007 1.131 elad if (newp == NULL) {
1008 1.131 elad error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
1009 1.163 rmind p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_GET), &alim,
1010 1.131 elad KAUTH_ARG(which));
1011 1.131 elad if (error)
1012 1.157 rmind goto out;
1013 1.131 elad }
1014 1.131 elad
1015 1.163 rmind /* Retrieve the limits. */
1016 1.74 atatat node = *rnode;
1017 1.163 rmind memcpy(&alim, &p->p_rlimit[limitno], sizeof(alim));
1018 1.163 rmind if (which == PROC_PID_LIMIT_TYPE_HARD) {
1019 1.74 atatat node.sysctl_data = &alim.rlim_max;
1020 1.163 rmind } else {
1021 1.74 atatat node.sysctl_data = &alim.rlim_cur;
1022 1.163 rmind }
1023 1.163 rmind error = sysctl_lookup(SYSCTLFN_CALL(&node));
1024 1.74 atatat
1025 1.163 rmind /* Return if error, or if we are only retrieving the limits. */
1026 1.157 rmind if (error || newp == NULL) {
1027 1.157 rmind goto out;
1028 1.157 rmind }
1029 1.163 rmind error = dosetrlimit(l, p, limitno, &alim);
1030 1.157 rmind out:
1031 1.163 rmind rw_exit(&p->p_reflock);
1032 1.157 rmind return error;
1033 1.74 atatat }
1034 1.74 atatat
1035 1.74 atatat /*
1036 1.164 rmind * Setup sysctl nodes.
1037 1.74 atatat */
1038 1.156 pooka static void
1039 1.164 rmind sysctl_proc_setup(void)
1040 1.74 atatat {
1041 1.74 atatat
1042 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1043 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_ANYNUMBER,
1044 1.78 atatat CTLTYPE_NODE, "curproc",
1045 1.78 atatat SYSCTL_DESCR("Per-process settings"),
1046 1.74 atatat NULL, 0, NULL, 0,
1047 1.74 atatat CTL_PROC, PROC_CURPROC, CTL_EOL);
1048 1.74 atatat
1049 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1050 1.103 elad CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1051 1.78 atatat CTLTYPE_STRING, "corename",
1052 1.78 atatat SYSCTL_DESCR("Core file name"),
1053 1.74 atatat sysctl_proc_corename, 0, NULL, MAXPATHLEN,
1054 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_CORENAME, CTL_EOL);
1055 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1056 1.76 atatat CTLFLAG_PERMANENT,
1057 1.78 atatat CTLTYPE_NODE, "rlimit",
1058 1.78 atatat SYSCTL_DESCR("Process limits"),
1059 1.74 atatat NULL, 0, NULL, 0,
1060 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, CTL_EOL);
1061 1.74 atatat
1062 1.74 atatat #define create_proc_plimit(s, n) do { \
1063 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1064 1.76 atatat CTLFLAG_PERMANENT, \
1065 1.78 atatat CTLTYPE_NODE, s, \
1066 1.78 atatat SYSCTL_DESCR("Process " s " limits"), \
1067 1.74 atatat NULL, 0, NULL, 0, \
1068 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1069 1.74 atatat CTL_EOL); \
1070 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1071 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1072 1.78 atatat CTLTYPE_QUAD, "soft", \
1073 1.78 atatat SYSCTL_DESCR("Process soft " s " limit"), \
1074 1.74 atatat sysctl_proc_plimit, 0, NULL, 0, \
1075 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1076 1.74 atatat PROC_PID_LIMIT_TYPE_SOFT, CTL_EOL); \
1077 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL, \
1078 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE, \
1079 1.78 atatat CTLTYPE_QUAD, "hard", \
1080 1.78 atatat SYSCTL_DESCR("Process hard " s " limit"), \
1081 1.74 atatat sysctl_proc_plimit, 0, NULL, 0, \
1082 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_LIMIT, n, \
1083 1.74 atatat PROC_PID_LIMIT_TYPE_HARD, CTL_EOL); \
1084 1.74 atatat } while (0/*CONSTCOND*/)
1085 1.74 atatat
1086 1.74 atatat create_proc_plimit("cputime", PROC_PID_LIMIT_CPU);
1087 1.74 atatat create_proc_plimit("filesize", PROC_PID_LIMIT_FSIZE);
1088 1.74 atatat create_proc_plimit("datasize", PROC_PID_LIMIT_DATA);
1089 1.74 atatat create_proc_plimit("stacksize", PROC_PID_LIMIT_STACK);
1090 1.74 atatat create_proc_plimit("coredumpsize", PROC_PID_LIMIT_CORE);
1091 1.74 atatat create_proc_plimit("memoryuse", PROC_PID_LIMIT_RSS);
1092 1.74 atatat create_proc_plimit("memorylocked", PROC_PID_LIMIT_MEMLOCK);
1093 1.74 atatat create_proc_plimit("maxproc", PROC_PID_LIMIT_NPROC);
1094 1.74 atatat create_proc_plimit("descriptors", PROC_PID_LIMIT_NOFILE);
1095 1.79 christos create_proc_plimit("sbsize", PROC_PID_LIMIT_SBSIZE);
1096 1.151 mrg create_proc_plimit("vmemoryuse", PROC_PID_LIMIT_AS);
1097 1.169 christos create_proc_plimit("maxlwp", PROC_PID_LIMIT_NTHR);
1098 1.74 atatat
1099 1.74 atatat #undef create_proc_plimit
1100 1.74 atatat
1101 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1102 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1103 1.78 atatat CTLTYPE_INT, "stopfork",
1104 1.78 atatat SYSCTL_DESCR("Stop process at fork(2)"),
1105 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
1106 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPFORK, CTL_EOL);
1107 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1108 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1109 1.78 atatat CTLTYPE_INT, "stopexec",
1110 1.78 atatat SYSCTL_DESCR("Stop process at execve(2)"),
1111 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
1112 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXEC, CTL_EOL);
1113 1.156 pooka sysctl_createv(&proc_sysctllog, 0, NULL, NULL,
1114 1.76 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE|CTLFLAG_ANYWRITE,
1115 1.78 atatat CTLTYPE_INT, "stopexit",
1116 1.78 atatat SYSCTL_DESCR("Stop process before completing exit"),
1117 1.74 atatat sysctl_proc_stop, 0, NULL, 0,
1118 1.74 atatat CTL_PROC, PROC_CURPROC, PROC_PID_STOPEXIT, CTL_EOL);
1119 1.17 cgd }
1120