]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
3 | * | |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the | |
10 | * License may not be used to create, or enable the creation or | |
11 | * redistribution of, unlawful or unlicensed copies of an Apple operating | |
12 | * system, or to circumvent, violate, or enable the circumvention or | |
13 | * violation of, any terms of an Apple operating system software license | |
14 | * agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
18 | * file. | |
19 | * | |
20 | * The Original Code and all software distributed under the License are | |
21 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
22 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
23 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
24 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
25 | * Please see the License for the specific language governing rights and | |
26 | * limitations under the License. | |
27 | * | |
28 | * @APPLE_LICENSE_OSREFERENCE_HEADER_END@ | |
1c79356b A |
29 | */ |
30 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
31 | /*- | |
32 | * Copyright (c) 1982, 1986, 1991, 1993 | |
33 | * The Regents of the University of California. All rights reserved. | |
34 | * (c) UNIX System Laboratories, Inc. | |
35 | * All or some portions of this file are derived from material licensed | |
36 | * to the University of California by American Telephone and Telegraph | |
37 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
38 | * the permission of UNIX System Laboratories, Inc. | |
39 | * | |
40 | * Redistribution and use in source and binary forms, with or without | |
41 | * modification, are permitted provided that the following conditions | |
42 | * are met: | |
43 | * 1. Redistributions of source code must retain the above copyright | |
44 | * notice, this list of conditions and the following disclaimer. | |
45 | * 2. Redistributions in binary form must reproduce the above copyright | |
46 | * notice, this list of conditions and the following disclaimer in the | |
47 | * documentation and/or other materials provided with the distribution. | |
48 | * 3. All advertising materials mentioning features or use of this software | |
49 | * must display the following acknowledgement: | |
50 | * This product includes software developed by the University of | |
51 | * California, Berkeley and its contributors. | |
52 | * 4. Neither the name of the University nor the names of its contributors | |
53 | * may be used to endorse or promote products derived from this software | |
54 | * without specific prior written permission. | |
55 | * | |
56 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
57 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
58 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
59 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
60 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
61 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
62 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
63 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
64 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
65 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
66 | * SUCH DAMAGE. | |
67 | * | |
68 | * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 | |
69 | */ | |
70 | ||
71 | #include <sys/param.h> | |
72 | #include <sys/systm.h> | |
55e303ae | 73 | #include <sys/sysctl.h> |
1c79356b | 74 | #include <sys/kernel.h> |
91447636 | 75 | #include <sys/file_internal.h> |
1c79356b A |
76 | #include <sys/resourcevar.h> |
77 | #include <sys/malloc.h> | |
91447636 A |
78 | #include <sys/proc_internal.h> |
79 | #include <sys/kauth.h> | |
80 | #include <machine/spl.h> | |
81 | ||
82 | #include <sys/mount_internal.h> | |
83 | #include <sys/sysproto.h> | |
1c79356b | 84 | |
e5568f75 A |
85 | #include <bsm/audit_kernel.h> |
86 | ||
1c79356b A |
87 | #include <machine/vmparam.h> |
88 | ||
89 | #include <mach/mach_types.h> | |
90 | #include <mach/time_value.h> | |
91 | #include <mach/task_info.h> | |
91447636 | 92 | #include <mach/vm_map.h> |
1c79356b A |
93 | |
94 | #include <vm/vm_map.h> | |
95 | ||
91447636 A |
96 | int donice(struct proc *curp, struct proc *chgp, int n); |
97 | int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp); | |
1c79356b A |
98 | |
99 | rlim_t maxdmap = MAXDSIZ; /* XXX */ | |
100 | rlim_t maxsmap = MAXSSIZ; /* XXX */ | |
101 | ||
55e303ae A |
102 | /* |
103 | * Limits on the number of open files per process, and the number | |
104 | * of child processes per process. | |
105 | * | |
106 | * Note: would be in kern/subr_param.c in FreeBSD. | |
107 | */ | |
5d5c5d0d | 108 | extern int maxprocperuid; /* max # of procs per user */ |
55e303ae A |
109 | int maxfilesperproc = OPEN_MAX; /* per-proc open files limit */ |
110 | ||
111 | SYSCTL_INT( _kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW, | |
112 | &maxprocperuid, 0, "Maximum processes allowed per userid" ); | |
113 | ||
114 | SYSCTL_INT( _kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW, | |
115 | &maxfilesperproc, 0, "Maximum files allowed open per process" ); | |
116 | ||
117 | ||
1c79356b A |
118 | /* |
119 | * Resource controls and accounting. | |
120 | */ | |
1c79356b | 121 | int |
91447636 | 122 | getpriority(struct proc *curp, struct getpriority_args *uap, register_t *retval) |
1c79356b A |
123 | { |
124 | register struct proc *p; | |
125 | register int low = PRIO_MAX + 1; | |
126 | ||
91447636 A |
127 | if (uap->who < 0) |
128 | return (EINVAL); | |
129 | ||
1c79356b A |
130 | switch (uap->which) { |
131 | ||
132 | case PRIO_PROCESS: | |
133 | if (uap->who == 0) | |
134 | p = curp; | |
135 | else | |
136 | p = pfind(uap->who); | |
137 | if (p == 0) | |
138 | break; | |
139 | low = p->p_nice; | |
140 | break; | |
141 | ||
142 | case PRIO_PGRP: { | |
143 | register struct pgrp *pg; | |
144 | ||
145 | if (uap->who == 0) | |
146 | pg = curp->p_pgrp; | |
147 | else if ((pg = pgfind(uap->who)) == NULL) | |
148 | break; | |
149 | for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) { | |
150 | if (p->p_nice < low) | |
151 | low = p->p_nice; | |
152 | } | |
153 | break; | |
154 | } | |
155 | ||
156 | case PRIO_USER: | |
157 | if (uap->who == 0) | |
91447636 | 158 | uap->who = kauth_cred_getuid(kauth_cred_get()); |
1c79356b | 159 | for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) |
91447636 | 160 | if (kauth_cred_getuid(p->p_ucred) == uap->who && |
1c79356b A |
161 | p->p_nice < low) |
162 | low = p->p_nice; | |
163 | break; | |
164 | ||
165 | default: | |
166 | return (EINVAL); | |
167 | } | |
168 | if (low == PRIO_MAX + 1) | |
169 | return (ESRCH); | |
170 | *retval = low; | |
171 | return (0); | |
172 | } | |
173 | ||
1c79356b A |
174 | /* ARGSUSED */ |
175 | int | |
91447636 | 176 | setpriority(struct proc *curp, struct setpriority_args *uap, __unused register_t *retval) |
1c79356b A |
177 | { |
178 | register struct proc *p; | |
179 | int found = 0, error = 0; | |
180 | ||
e5568f75 A |
181 | AUDIT_ARG(cmd, uap->which); |
182 | AUDIT_ARG(owner, uap->who, 0); | |
183 | AUDIT_ARG(value, uap->prio); | |
184 | ||
91447636 A |
185 | if (uap->who < 0) |
186 | return (EINVAL); | |
187 | ||
1c79356b A |
188 | switch (uap->which) { |
189 | ||
190 | case PRIO_PROCESS: | |
191 | if (uap->who == 0) | |
192 | p = curp; | |
193 | else | |
194 | p = pfind(uap->who); | |
195 | if (p == 0) | |
196 | break; | |
197 | error = donice(curp, p, uap->prio); | |
198 | found++; | |
199 | break; | |
200 | ||
201 | case PRIO_PGRP: { | |
202 | register struct pgrp *pg; | |
203 | ||
204 | if (uap->who == 0) | |
205 | pg = curp->p_pgrp; | |
206 | else if ((pg = pgfind(uap->who)) == NULL) | |
207 | break; | |
208 | for (p = pg->pg_members.lh_first; p != 0; | |
209 | p = p->p_pglist.le_next) { | |
210 | error = donice(curp, p, uap->prio); | |
211 | found++; | |
212 | } | |
213 | break; | |
214 | } | |
215 | ||
216 | case PRIO_USER: | |
217 | if (uap->who == 0) | |
91447636 | 218 | uap->who = kauth_cred_getuid(kauth_cred_get()); |
1c79356b | 219 | for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) |
91447636 | 220 | if (kauth_cred_getuid(p->p_ucred) == uap->who) { |
1c79356b A |
221 | error = donice(curp, p, uap->prio); |
222 | found++; | |
223 | } | |
224 | break; | |
225 | ||
226 | default: | |
227 | return (EINVAL); | |
228 | } | |
229 | if (found == 0) | |
230 | return (ESRCH); | |
231 | return (error); | |
232 | } | |
233 | ||
234 | int | |
235 | donice(curp, chgp, n) | |
236 | register struct proc *curp, *chgp; | |
237 | register int n; | |
238 | { | |
91447636 | 239 | kauth_cred_t ucred = curp->p_ucred; |
1c79356b | 240 | |
91447636 A |
241 | if (suser(ucred, NULL) && ucred->cr_ruid && |
242 | kauth_cred_getuid(ucred) != kauth_cred_getuid(chgp->p_ucred) && | |
243 | ucred->cr_ruid != kauth_cred_getuid(chgp->p_ucred)) | |
1c79356b A |
244 | return (EPERM); |
245 | if (n > PRIO_MAX) | |
246 | n = PRIO_MAX; | |
247 | if (n < PRIO_MIN) | |
248 | n = PRIO_MIN; | |
91447636 | 249 | if (n < chgp->p_nice && suser(ucred, &curp->p_acflag)) |
1c79356b A |
250 | return (EACCES); |
251 | chgp->p_nice = n; | |
252 | (void)resetpriority(chgp); | |
253 | return (0); | |
254 | } | |
255 | ||
1c79356b | 256 | |
1c79356b A |
257 | /* ARGSUSED */ |
258 | int | |
91447636 | 259 | setrlimit(struct proc *p, register struct setrlimit_args *uap, __unused register_t *retval) |
1c79356b A |
260 | { |
261 | struct rlimit alim; | |
262 | int error; | |
263 | ||
91447636 A |
264 | if ((error = copyin(uap->rlp, (caddr_t)&alim, |
265 | sizeof (struct rlimit)))) | |
1c79356b A |
266 | return (error); |
267 | return (dosetrlimit(p, uap->which, &alim)); | |
268 | } | |
269 | ||
270 | int | |
271 | dosetrlimit(p, which, limp) | |
272 | struct proc *p; | |
273 | u_int which; | |
274 | struct rlimit *limp; | |
275 | { | |
276 | register struct rlimit *alimp; | |
1c79356b A |
277 | int error; |
278 | ||
279 | if (which >= RLIM_NLIMITS) | |
280 | return (EINVAL); | |
281 | alimp = &p->p_rlimit[which]; | |
282 | if (limp->rlim_cur > alimp->rlim_max || | |
283 | limp->rlim_max > alimp->rlim_max) | |
91447636 | 284 | if ((error = suser(kauth_cred_get(), &p->p_acflag))) |
1c79356b A |
285 | return (error); |
286 | if (limp->rlim_cur > limp->rlim_max) | |
287 | limp->rlim_cur = limp->rlim_max; | |
288 | if (p->p_limit->p_refcnt > 1 && | |
289 | (p->p_limit->p_lflags & PL_SHAREMOD) == 0) { | |
290 | p->p_limit->p_refcnt--; | |
291 | p->p_limit = limcopy(p->p_limit); | |
292 | alimp = &p->p_rlimit[which]; | |
293 | } | |
294 | ||
295 | switch (which) { | |
296 | ||
297 | case RLIMIT_DATA: | |
298 | if (limp->rlim_cur > maxdmap) | |
299 | limp->rlim_cur = maxdmap; | |
300 | if (limp->rlim_max > maxdmap) | |
301 | limp->rlim_max = maxdmap; | |
302 | break; | |
303 | ||
304 | case RLIMIT_STACK: | |
305 | if (limp->rlim_cur > maxsmap) | |
306 | limp->rlim_cur = maxsmap; | |
307 | if (limp->rlim_max > maxsmap) | |
308 | limp->rlim_max = maxsmap; | |
309 | /* | |
310 | * Stack is allocated to the max at exec time with only | |
311 | * "rlim_cur" bytes accessible. If stack limit is going | |
312 | * up make more accessible, if going down make inaccessible. | |
313 | */ | |
314 | if (limp->rlim_cur != alimp->rlim_cur) { | |
91447636 A |
315 | user_addr_t addr; |
316 | user_size_t size; | |
1c79356b A |
317 | |
318 | if (limp->rlim_cur > alimp->rlim_cur) { | |
319 | /* grow stack */ | |
55e303ae A |
320 | size = round_page_64(limp->rlim_cur); |
321 | size -= round_page_64(alimp->rlim_cur); | |
1c79356b A |
322 | |
323 | #if STACK_GROWTH_UP | |
324 | /* go to top of current stack */ | |
91447636 | 325 | addr = p->user_stack + alimp->rlim_cur; |
1c79356b | 326 | #else STACK_GROWTH_UP |
91447636 | 327 | addr = p->user_stack - alimp->rlim_cur; |
1c79356b A |
328 | addr -= size; |
329 | #endif /* STACK_GROWTH_UP */ | |
91447636 A |
330 | if (mach_vm_allocate(current_map(), |
331 | &addr, size, | |
332 | VM_FLAGS_FIXED) != KERN_SUCCESS) | |
1c79356b A |
333 | return(EINVAL); |
334 | } else { | |
335 | /* shrink stack */ | |
336 | } | |
337 | } | |
338 | break; | |
339 | ||
340 | case RLIMIT_NOFILE: | |
55e303ae A |
341 | /* |
342 | * Only root can set the maxfiles limits, as it is systemwide resource | |
343 | */ | |
344 | if ( is_suser() ) { | |
1c79356b A |
345 | if (limp->rlim_cur > maxfiles) |
346 | limp->rlim_cur = maxfiles; | |
347 | if (limp->rlim_max > maxfiles) | |
348 | limp->rlim_max = maxfiles; | |
55e303ae A |
349 | } |
350 | else { | |
351 | if (limp->rlim_cur > maxfilesperproc) | |
352 | limp->rlim_cur = maxfilesperproc; | |
353 | if (limp->rlim_max > maxfilesperproc) | |
354 | limp->rlim_max = maxfilesperproc; | |
1c79356b A |
355 | } |
356 | break; | |
357 | ||
358 | case RLIMIT_NPROC: | |
55e303ae A |
359 | /* |
360 | * Only root can set to the maxproc limits, as it is | |
361 | * systemwide resource; all others are limited to | |
362 | * maxprocperuid (presumably less than maxproc). | |
363 | */ | |
364 | if ( is_suser() ) { | |
1c79356b A |
365 | if (limp->rlim_cur > maxproc) |
366 | limp->rlim_cur = maxproc; | |
367 | if (limp->rlim_max > maxproc) | |
368 | limp->rlim_max = maxproc; | |
55e303ae A |
369 | } |
370 | else { | |
371 | if (limp->rlim_cur > maxprocperuid) | |
372 | limp->rlim_cur = maxprocperuid; | |
373 | if (limp->rlim_max > maxprocperuid) | |
374 | limp->rlim_max = maxprocperuid; | |
1c79356b A |
375 | } |
376 | break; | |
55e303ae A |
377 | |
378 | } /* switch... */ | |
1c79356b A |
379 | *alimp = *limp; |
380 | return (0); | |
381 | } | |
382 | ||
1c79356b A |
383 | /* ARGSUSED */ |
384 | int | |
91447636 | 385 | getrlimit(struct proc *p, register struct getrlimit_args *uap, __unused register_t *retval) |
1c79356b | 386 | { |
1c79356b A |
387 | if (uap->which >= RLIM_NLIMITS) |
388 | return (EINVAL); | |
389 | return (copyout((caddr_t)&p->p_rlimit[uap->which], | |
91447636 | 390 | uap->rlp, sizeof (struct rlimit))); |
1c79356b A |
391 | } |
392 | ||
393 | /* | |
394 | * Transform the running time and tick information in proc p into user, | |
395 | * system, and interrupt time usage. | |
396 | */ | |
397 | void | |
398 | calcru(p, up, sp, ip) | |
399 | register struct proc *p; | |
400 | register struct timeval *up; | |
401 | register struct timeval *sp; | |
402 | register struct timeval *ip; | |
403 | { | |
404 | task_t task; | |
405 | ||
406 | timerclear(up); | |
407 | timerclear(sp); | |
408 | if (ip != NULL) | |
409 | timerclear(ip); | |
410 | ||
411 | task = p->task; | |
412 | if (task) { | |
413 | task_basic_info_data_t tinfo; | |
414 | task_thread_times_info_data_t ttimesinfo; | |
415 | int task_info_stuff, task_ttimes_stuff; | |
416 | struct timeval ut,st; | |
417 | ||
418 | task_info_stuff = TASK_BASIC_INFO_COUNT; | |
419 | task_info(task, TASK_BASIC_INFO, | |
420 | &tinfo, &task_info_stuff); | |
421 | ut.tv_sec = tinfo.user_time.seconds; | |
422 | ut.tv_usec = tinfo.user_time.microseconds; | |
423 | st.tv_sec = tinfo.system_time.seconds; | |
424 | st.tv_usec = tinfo.system_time.microseconds; | |
55e303ae A |
425 | timeradd(&ut, up, up); |
426 | timeradd(&st, sp, sp); | |
1c79356b A |
427 | |
428 | task_ttimes_stuff = TASK_THREAD_TIMES_INFO_COUNT; | |
429 | task_info(task, TASK_THREAD_TIMES_INFO, | |
430 | &ttimesinfo, &task_ttimes_stuff); | |
431 | ||
432 | ut.tv_sec = ttimesinfo.user_time.seconds; | |
433 | ut.tv_usec = ttimesinfo.user_time.microseconds; | |
434 | st.tv_sec = ttimesinfo.system_time.seconds; | |
435 | st.tv_usec = ttimesinfo.system_time.microseconds; | |
55e303ae A |
436 | timeradd(&ut, up, up); |
437 | timeradd(&st, sp, sp); | |
1c79356b A |
438 | } |
439 | } | |
440 | ||
91447636 A |
441 | __private_extern__ void munge_rusage(struct rusage *a_rusage_p, struct user_rusage *a_user_rusage_p); |
442 | ||
1c79356b A |
443 | /* ARGSUSED */ |
444 | int | |
91447636 | 445 | getrusage(register struct proc *p, register struct getrusage_args *uap, __unused register_t *retval) |
1c79356b A |
446 | { |
447 | struct rusage *rup, rubuf; | |
91447636 A |
448 | struct user_rusage rubuf64; |
449 | size_t retsize = sizeof(rubuf); /* default: 32 bits */ | |
450 | caddr_t retbuf = (caddr_t)&rubuf; /* default: 32 bits */ | |
1c79356b A |
451 | |
452 | switch (uap->who) { | |
1c79356b A |
453 | case RUSAGE_SELF: |
454 | rup = &p->p_stats->p_ru; | |
455 | calcru(p, &rup->ru_utime, &rup->ru_stime, NULL); | |
91447636 | 456 | // LP64todo: proc struct should have 64 bit version of struct |
1c79356b A |
457 | rubuf = *rup; |
458 | break; | |
459 | ||
460 | case RUSAGE_CHILDREN: | |
461 | rup = &p->p_stats->p_cru; | |
462 | rubuf = *rup; | |
463 | break; | |
464 | ||
465 | default: | |
466 | return (EINVAL); | |
467 | } | |
91447636 A |
468 | if (IS_64BIT_PROCESS(p)) { |
469 | retsize = sizeof(rubuf64); | |
470 | retbuf = (caddr_t)&rubuf64; | |
471 | munge_rusage(&rubuf, &rubuf64); | |
472 | } | |
473 | return (copyout(retbuf, uap->rusage, retsize)); | |
1c79356b A |
474 | } |
475 | ||
476 | void | |
477 | ruadd(ru, ru2) | |
478 | register struct rusage *ru, *ru2; | |
479 | { | |
480 | register long *ip, *ip2; | |
481 | register int i; | |
482 | ||
483 | timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime); | |
484 | timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime); | |
485 | if (ru->ru_maxrss < ru2->ru_maxrss) | |
486 | ru->ru_maxrss = ru2->ru_maxrss; | |
487 | ip = &ru->ru_first; ip2 = &ru2->ru_first; | |
488 | for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--) | |
489 | *ip++ += *ip2++; | |
490 | } | |
491 | ||
492 | /* | |
493 | * Make a copy of the plimit structure. | |
494 | * We share these structures copy-on-write after fork, | |
495 | * and copy when a limit is changed. | |
496 | */ | |
497 | struct plimit * | |
498 | limcopy(lim) | |
499 | struct plimit *lim; | |
500 | { | |
501 | register struct plimit *copy; | |
502 | ||
503 | MALLOC_ZONE(copy, struct plimit *, | |
504 | sizeof(struct plimit), M_SUBPROC, M_WAITOK); | |
91447636 A |
505 | if (copy == NULL) |
506 | panic("limcopy"); | |
1c79356b A |
507 | bcopy(lim->pl_rlimit, copy->pl_rlimit, |
508 | sizeof(struct rlimit) * RLIM_NLIMITS); | |
509 | copy->p_lflags = 0; | |
510 | copy->p_refcnt = 1; | |
511 | return (copy); | |
512 | } |