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