]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_resource.c
xnu-3247.1.106.tar.gz
[apple/xnu.git] / bsd / kern / kern_resource.c
1 /*
2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
72 * Version 2.0.
73 */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sysctl.h>
78 #include <sys/kernel.h>
79 #include <sys/file_internal.h>
80 #include <sys/resourcevar.h>
81 #include <sys/malloc.h>
82 #include <sys/proc_internal.h>
83 #include <sys/kauth.h>
84 #include <machine/spl.h>
85
86 #include <sys/mount_internal.h>
87 #include <sys/sysproto.h>
88
89 #include <security/audit/audit.h>
90
91 #include <machine/vmparam.h>
92
93 #include <mach/mach_types.h>
94 #include <mach/time_value.h>
95 #include <mach/task.h>
96 #include <mach/task_info.h>
97 #include <mach/vm_map.h>
98 #include <mach/mach_vm.h>
99 #include <mach/thread_act.h> /* for thread_policy_set( ) */
100 #include <kern/thread.h>
101
102 #include <kern/task.h>
103 #include <kern/clock.h> /* for absolutetime_to_microtime() */
104 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
105 #include <sys/socketvar.h> /* for struct socket */
106
107 #include <vm/vm_map.h>
108
109 #include <kern/assert.h>
110 #include <sys/resource.h>
111 #include <IOKit/IOBSD.h>
112
113 int donice(struct proc *curp, struct proc *chgp, int n);
114 int dosetrlimit(struct proc *p, u_int which, struct rlimit *limp);
115 int uthread_get_background_state(uthread_t);
116 static void do_background_socket(struct proc *p, thread_t thread);
117 static int do_background_thread(struct proc *curp, thread_t thread, int priority);
118 static int do_background_proc(struct proc *curp, struct proc *targetp, int priority);
119 static int set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority);
120 static int proc_set_darwin_role(proc_t curp, proc_t targetp, int priority);
121 static int proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority);
122 static int get_background_proc(struct proc *curp, struct proc *targetp, int *priority);
123 void proc_apply_task_networkbg_internal(proc_t, thread_t);
124 void proc_restore_task_networkbg_internal(proc_t, thread_t);
125 int proc_pid_rusage(int pid, int flavor, user_addr_t buf, int32_t *retval);
126 void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
127 int fill_task_rusage(task_t task, rusage_info_current *ri);
128 void fill_task_billed_usage(task_t task, rusage_info_current *ri);
129 int fill_task_io_rusage(task_t task, rusage_info_current *ri);
130 int fill_task_qos_rusage(task_t task, rusage_info_current *ri);
131 static void rusage_info_conversion(rusage_info_t ri_info, rusage_info_current *ri_current, int flavor);
132
133 int proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie);
134
135 rlim_t maxdmap = MAXDSIZ; /* XXX */
136 rlim_t maxsmap = MAXSSIZ - PAGE_MAX_SIZE; /* XXX */
137
138 /*
139 * Limits on the number of open files per process, and the number
140 * of child processes per process.
141 *
142 * Note: would be in kern/subr_param.c in FreeBSD.
143 */
144 __private_extern__ int maxfilesperproc = OPEN_MAX; /* per-proc open files limit */
145
146 SYSCTL_INT(_kern, KERN_MAXPROCPERUID, maxprocperuid, CTLFLAG_RW | CTLFLAG_LOCKED,
147 &maxprocperuid, 0, "Maximum processes allowed per userid" );
148
149 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW | CTLFLAG_LOCKED,
150 &maxfilesperproc, 0, "Maximum files allowed open per process" );
151
152 /* Args and fn for proc_iteration callback used in setpriority */
153 struct puser_nice_args {
154 proc_t curp;
155 int prio;
156 id_t who;
157 int * foundp;
158 int * errorp;
159 };
160 static int puser_donice_callback(proc_t p, void * arg);
161
162
163 /* Args and fn for proc_iteration callback used in setpriority */
164 struct ppgrp_nice_args {
165 proc_t curp;
166 int prio;
167 int * foundp;
168 int * errorp;
169 };
170 static int ppgrp_donice_callback(proc_t p, void * arg);
171
172 /*
173 * Resource controls and accounting.
174 */
175 int
176 getpriority(struct proc *curp, struct getpriority_args *uap, int32_t *retval)
177 {
178 struct proc *p;
179 int low = PRIO_MAX + 1;
180 kauth_cred_t my_cred;
181 int refheld = 0;
182 int error = 0;
183
184 /* would also test (uap->who < 0), but id_t is unsigned */
185 if (uap->who > 0x7fffffff)
186 return (EINVAL);
187
188 switch (uap->which) {
189
190 case PRIO_PROCESS:
191 if (uap->who == 0) {
192 p = curp;
193 low = p->p_nice;
194 } else {
195 p = proc_find(uap->who);
196 if (p == 0)
197 break;
198 low = p->p_nice;
199 proc_rele(p);
200
201 }
202 break;
203
204 case PRIO_PGRP: {
205 struct pgrp *pg = PGRP_NULL;
206
207 if (uap->who == 0) {
208 /* returns the pgrp to ref */
209 pg = proc_pgrp(curp);
210 } else if ((pg = pgfind(uap->who)) == PGRP_NULL) {
211 break;
212 }
213 /* No need for iteration as it is a simple scan */
214 pgrp_lock(pg);
215 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
216 if (p->p_nice < low)
217 low = p->p_nice;
218 }
219 pgrp_unlock(pg);
220 pg_rele(pg);
221 break;
222 }
223
224 case PRIO_USER:
225 if (uap->who == 0)
226 uap->who = kauth_cred_getuid(kauth_cred_get());
227
228 proc_list_lock();
229
230 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
231 my_cred = kauth_cred_proc_ref(p);
232 if (kauth_cred_getuid(my_cred) == uap->who &&
233 p->p_nice < low)
234 low = p->p_nice;
235 kauth_cred_unref(&my_cred);
236 }
237
238 proc_list_unlock();
239
240 break;
241
242 case PRIO_DARWIN_THREAD:
243 /* we currently only support the current thread */
244 if (uap->who != 0)
245 return (EINVAL);
246
247 low = proc_get_task_policy(current_task(), current_thread(), TASK_POLICY_INTERNAL, TASK_POLICY_DARWIN_BG);
248
249 break;
250
251 case PRIO_DARWIN_PROCESS:
252 if (uap->who == 0) {
253 p = curp;
254 } else {
255 p = proc_find(uap->who);
256 if (p == PROC_NULL)
257 break;
258 refheld = 1;
259 }
260
261 error = get_background_proc(curp, p, &low);
262
263 if (refheld)
264 proc_rele(p);
265 if (error)
266 return (error);
267 break;
268
269 case PRIO_DARWIN_ROLE:
270 if (uap->who == 0) {
271 p = curp;
272 } else {
273 p = proc_find(uap->who);
274 if (p == PROC_NULL)
275 break;
276 refheld = 1;
277 }
278
279 error = proc_get_darwin_role(curp, p, &low);
280
281 if (refheld)
282 proc_rele(p);
283 if (error)
284 return (error);
285 break;
286
287 default:
288 return (EINVAL);
289 }
290 if (low == PRIO_MAX + 1)
291 return (ESRCH);
292 *retval = low;
293 return (0);
294 }
295
296 /* call back function used for proc iteration in PRIO_USER */
297 static int
298 puser_donice_callback(proc_t p, void * arg)
299 {
300 int error, n;
301 struct puser_nice_args * pun = (struct puser_nice_args *)arg;
302 kauth_cred_t my_cred;
303
304 my_cred = kauth_cred_proc_ref(p);
305 if (kauth_cred_getuid(my_cred) == pun->who) {
306 error = donice(pun->curp, p, pun->prio);
307 if (pun->errorp != NULL)
308 *pun->errorp = error;
309 if (pun->foundp != NULL) {
310 n = *pun->foundp;
311 *pun->foundp = n+1;
312 }
313 }
314 kauth_cred_unref(&my_cred);
315
316 return(PROC_RETURNED);
317 }
318
319 /* call back function used for proc iteration in PRIO_PGRP */
320 static int
321 ppgrp_donice_callback(proc_t p, void * arg)
322 {
323 int error;
324 struct ppgrp_nice_args * pun = (struct ppgrp_nice_args *)arg;
325 int n;
326
327 error = donice(pun->curp, p, pun->prio);
328 if (pun->errorp != NULL)
329 *pun->errorp = error;
330 if (pun->foundp!= NULL) {
331 n = *pun->foundp;
332 *pun->foundp = n+1;
333 }
334
335 return(PROC_RETURNED);
336 }
337
338 /*
339 * Returns: 0 Success
340 * EINVAL
341 * ESRCH
342 * donice:EPERM
343 * donice:EACCES
344 */
345 /* ARGSUSED */
346 int
347 setpriority(struct proc *curp, struct setpriority_args *uap, int32_t *retval)
348 {
349 struct proc *p;
350 int found = 0, error = 0;
351 int refheld = 0;
352
353 AUDIT_ARG(cmd, uap->which);
354 AUDIT_ARG(owner, uap->who, 0);
355 AUDIT_ARG(value32, uap->prio);
356
357 /* would also test (uap->who < 0), but id_t is unsigned */
358 if (uap->who > 0x7fffffff)
359 return (EINVAL);
360
361 switch (uap->which) {
362
363 case PRIO_PROCESS:
364 if (uap->who == 0)
365 p = curp;
366 else {
367 p = proc_find(uap->who);
368 if (p == 0)
369 break;
370 refheld = 1;
371 }
372 error = donice(curp, p, uap->prio);
373 found++;
374 if (refheld != 0)
375 proc_rele(p);
376 break;
377
378 case PRIO_PGRP: {
379 struct pgrp *pg = PGRP_NULL;
380 struct ppgrp_nice_args ppgrp;
381
382 if (uap->who == 0) {
383 pg = proc_pgrp(curp);
384 } else if ((pg = pgfind(uap->who)) == PGRP_NULL)
385 break;
386
387 ppgrp.curp = curp;
388 ppgrp.prio = uap->prio;
389 ppgrp.foundp = &found;
390 ppgrp.errorp = &error;
391
392 /* PGRP_DROPREF drops the reference on process group */
393 pgrp_iterate(pg, PGRP_DROPREF, ppgrp_donice_callback, (void *)&ppgrp, NULL, NULL);
394
395 break;
396 }
397
398 case PRIO_USER: {
399 struct puser_nice_args punice;
400
401 if (uap->who == 0)
402 uap->who = kauth_cred_getuid(kauth_cred_get());
403
404 punice.curp = curp;
405 punice.prio = uap->prio;
406 punice.who = uap->who;
407 punice.foundp = &found;
408 error = 0;
409 punice.errorp = &error;
410 proc_iterate(PROC_ALLPROCLIST, puser_donice_callback, (void *)&punice, NULL, NULL);
411
412 break;
413 }
414
415 case PRIO_DARWIN_THREAD: {
416 /* we currently only support the current thread */
417 if (uap->who != 0)
418 return (EINVAL);
419
420 error = do_background_thread(curp, current_thread(), uap->prio);
421 found++;
422 break;
423 }
424
425 case PRIO_DARWIN_PROCESS: {
426 if (uap->who == 0)
427 p = curp;
428 else {
429 p = proc_find(uap->who);
430 if (p == 0)
431 break;
432 refheld = 1;
433 }
434
435 error = do_background_proc(curp, p, uap->prio);
436
437 found++;
438 if (refheld != 0)
439 proc_rele(p);
440 break;
441 }
442
443 case PRIO_DARWIN_GPU: {
444 if (uap->who == 0)
445 return (EINVAL);
446
447 p = proc_find(uap->who);
448 if (p == PROC_NULL)
449 break;
450
451 error = set_gpudeny_proc(curp, p, uap->prio);
452
453 found++;
454 proc_rele(p);
455 break;
456 }
457
458 case PRIO_DARWIN_ROLE: {
459 if (uap->who == 0) {
460 p = curp;
461 } else {
462 p = proc_find(uap->who);
463 if (p == PROC_NULL)
464 break;
465 refheld = 1;
466 }
467
468 error = proc_set_darwin_role(curp, p, uap->prio);
469
470 found++;
471 if (refheld != 0)
472 proc_rele(p);
473 break;
474 }
475
476 default:
477 return (EINVAL);
478 }
479 if (found == 0)
480 return (ESRCH);
481 if (error == EIDRM) {
482 *retval = -2;
483 error = 0;
484 }
485 return (error);
486 }
487
488
489 /*
490 * Returns: 0 Success
491 * EPERM
492 * EACCES
493 * mac_check_proc_sched:???
494 */
495 int
496 donice(struct proc *curp, struct proc *chgp, int n)
497 {
498 int error = 0;
499 kauth_cred_t ucred;
500 kauth_cred_t my_cred;
501
502 ucred = kauth_cred_proc_ref(curp);
503 my_cred = kauth_cred_proc_ref(chgp);
504
505 if (suser(ucred, NULL) && kauth_cred_getruid(ucred) &&
506 kauth_cred_getuid(ucred) != kauth_cred_getuid(my_cred) &&
507 kauth_cred_getruid(ucred) != kauth_cred_getuid(my_cred)) {
508 error = EPERM;
509 goto out;
510 }
511 if (n > PRIO_MAX)
512 n = PRIO_MAX;
513 if (n < PRIO_MIN)
514 n = PRIO_MIN;
515 if (n < chgp->p_nice && suser(ucred, &curp->p_acflag)) {
516 error = EACCES;
517 goto out;
518 }
519 #if CONFIG_MACF
520 error = mac_proc_check_sched(curp, chgp);
521 if (error)
522 goto out;
523 #endif
524 proc_lock(chgp);
525 chgp->p_nice = n;
526 proc_unlock(chgp);
527 (void)resetpriority(chgp);
528 out:
529 kauth_cred_unref(&ucred);
530 kauth_cred_unref(&my_cred);
531 return (error);
532 }
533
534 static int
535 set_gpudeny_proc(struct proc *curp, struct proc *targetp, int priority)
536 {
537 int error = 0;
538 kauth_cred_t ucred;
539 kauth_cred_t target_cred;
540
541 ucred = kauth_cred_get();
542 target_cred = kauth_cred_proc_ref(targetp);
543
544 /* TODO: Entitlement instead of uid check */
545
546 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
547 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
548 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
549 error = EPERM;
550 goto out;
551 }
552
553 if (curp == targetp) {
554 error = EPERM;
555 goto out;
556 }
557
558 #if CONFIG_MACF
559 error = mac_proc_check_sched(curp, targetp);
560 if (error)
561 goto out;
562 #endif
563
564 switch (priority) {
565 case PRIO_DARWIN_GPU_DENY:
566 task_set_gpu_denied(proc_task(targetp), TRUE);
567 break;
568 case PRIO_DARWIN_GPU_ALLOW:
569 task_set_gpu_denied(proc_task(targetp), FALSE);
570 break;
571 default:
572 error = EINVAL;
573 goto out;
574 }
575
576 out:
577 kauth_cred_unref(&target_cred);
578 return (error);
579
580 }
581
582 static int
583 proc_set_darwin_role(proc_t curp, proc_t targetp, int priority)
584 {
585 int error = 0;
586 uint32_t flagsp;
587
588 kauth_cred_t ucred, target_cred;
589
590 ucred = kauth_cred_get();
591 target_cred = kauth_cred_proc_ref(targetp);
592
593 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
594 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
595 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
596 error = EPERM;
597 goto out;
598 }
599
600 if (curp != targetp) {
601 #if CONFIG_MACF
602 if ((error = mac_proc_check_sched(curp, targetp)))
603 goto out;
604 #endif
605 }
606
607 proc_get_darwinbgstate(proc_task(targetp), &flagsp);
608 if ((flagsp & PROC_FLAG_APPLICATION) != PROC_FLAG_APPLICATION) {
609 error = ENOTSUP;
610 goto out;
611 }
612
613 integer_t role = 0;
614
615 if ((error = proc_darwin_role_to_task_role(priority, &role)))
616 goto out;
617
618 proc_set_task_policy(proc_task(targetp), THREAD_NULL,
619 TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE, role);
620
621 out:
622 kauth_cred_unref(&target_cred);
623 return (error);
624 }
625
626 static int
627 proc_get_darwin_role(proc_t curp, proc_t targetp, int *priority)
628 {
629 int error = 0;
630 int role = 0;
631
632 kauth_cred_t ucred, target_cred;
633
634 ucred = kauth_cred_get();
635 target_cred = kauth_cred_proc_ref(targetp);
636
637 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
638 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
639 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
640 error = EPERM;
641 goto out;
642 }
643
644 if (curp != targetp) {
645 #if CONFIG_MACF
646 if ((error = mac_proc_check_sched(curp, targetp)))
647 goto out;
648 #endif
649 }
650
651 role = proc_get_task_policy(proc_task(targetp), THREAD_NULL,
652 TASK_POLICY_ATTRIBUTE, TASK_POLICY_ROLE);
653
654 *priority = proc_task_role_to_darwin_role(role);
655
656 out:
657 kauth_cred_unref(&target_cred);
658 return (error);
659 }
660
661
662 static int
663 get_background_proc(struct proc *curp, struct proc *targetp, int *priority)
664 {
665 int external = 0;
666 int error = 0;
667 kauth_cred_t ucred, target_cred;
668
669 ucred = kauth_cred_get();
670 target_cred = kauth_cred_proc_ref(targetp);
671
672 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
673 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
674 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred)) {
675 error = EPERM;
676 goto out;
677 }
678
679 external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
680
681 *priority = proc_get_task_policy(current_task(), THREAD_NULL, external, TASK_POLICY_DARWIN_BG);
682
683 out:
684 kauth_cred_unref(&target_cred);
685 return (error);
686 }
687
688 static int
689 do_background_proc(struct proc *curp, struct proc *targetp, int priority)
690 {
691 #if !CONFIG_MACF
692 #pragma unused(curp)
693 #endif
694 int error = 0;
695 kauth_cred_t ucred;
696 kauth_cred_t target_cred;
697 int external;
698 int enable;
699
700 ucred = kauth_cred_get();
701 target_cred = kauth_cred_proc_ref(targetp);
702
703 if (!kauth_cred_issuser(ucred) && kauth_cred_getruid(ucred) &&
704 kauth_cred_getuid(ucred) != kauth_cred_getuid(target_cred) &&
705 kauth_cred_getruid(ucred) != kauth_cred_getuid(target_cred))
706 {
707 error = EPERM;
708 goto out;
709 }
710
711 #if CONFIG_MACF
712 error = mac_proc_check_sched(curp, targetp);
713 if (error)
714 goto out;
715 #endif
716
717 external = (curp == targetp) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
718
719 switch (priority) {
720 case PRIO_DARWIN_BG:
721 enable = TASK_POLICY_ENABLE;
722 break;
723 case PRIO_DARWIN_NONUI:
724 /* ignored for compatibility */
725 goto out;
726 default:
727 /* TODO: EINVAL if priority != 0 */
728 enable = TASK_POLICY_DISABLE;
729 break;
730 }
731
732 proc_set_task_policy(proc_task(targetp), THREAD_NULL, external, TASK_POLICY_DARWIN_BG, enable);
733
734 out:
735 kauth_cred_unref(&target_cred);
736 return (error);
737 }
738
739 static void
740 do_background_socket(struct proc *p, thread_t thread)
741 {
742 #if SOCKETS
743 struct filedesc *fdp;
744 struct fileproc *fp;
745 int i, background;
746
747 proc_fdlock(p);
748
749 if (thread != THREAD_NULL)
750 background = proc_get_effective_thread_policy(thread, TASK_POLICY_ALL_SOCKETS_BG);
751 else
752 background = proc_get_effective_task_policy(proc_task(p), TASK_POLICY_ALL_SOCKETS_BG);
753
754 if (background) {
755 /*
756 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
757 * the sockets with the background flag. There's nothing
758 * to do here for the PRIO_DARWIN_THREAD case.
759 */
760 if (thread == THREAD_NULL) {
761 fdp = p->p_fd;
762
763 for (i = 0; i < fdp->fd_nfiles; i++) {
764 struct socket *sockp;
765
766 fp = fdp->fd_ofiles[i];
767 if (fp == NULL || (fdp->fd_ofileflags[i] & UF_RESERVED) != 0 ||
768 FILEGLOB_DTYPE(fp->f_fglob) != DTYPE_SOCKET) {
769 continue;
770 }
771 sockp = (struct socket *)fp->f_fglob->fg_data;
772 socket_set_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
773 sockp->so_background_thread = NULL;
774 }
775 }
776 } else {
777 /* disable networking IO throttle.
778 * NOTE - It is a known limitation of the current design that we
779 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
780 * sockets created by other threads within this process.
781 */
782 fdp = p->p_fd;
783 for ( i = 0; i < fdp->fd_nfiles; i++ ) {
784 struct socket *sockp;
785
786 fp = fdp->fd_ofiles[ i ];
787 if ( fp == NULL || (fdp->fd_ofileflags[ i ] & UF_RESERVED) != 0 ||
788 FILEGLOB_DTYPE(fp->f_fglob) != DTYPE_SOCKET ) {
789 continue;
790 }
791 sockp = (struct socket *)fp->f_fglob->fg_data;
792 /* skip if only clearing this thread's sockets */
793 if ((thread) && (sockp->so_background_thread != thread)) {
794 continue;
795 }
796 socket_clear_traffic_mgt_flags(sockp, TRAFFIC_MGT_SO_BACKGROUND);
797 sockp->so_background_thread = NULL;
798 }
799 }
800
801 proc_fdunlock(p);
802 #else
803 #pragma unused(p, thread)
804 #endif
805 }
806
807
808 /*
809 * do_background_thread
810 * Returns: 0 Success
811 * EPERM Tried to background while in vfork
812 * XXX - todo - does this need a MACF hook?
813 */
814 static int
815 do_background_thread(struct proc *curp, thread_t thread, int priority)
816 {
817 struct uthread *ut;
818 int enable, external;
819 int rv = 0;
820
821 ut = get_bsdthread_info(thread);
822
823 /* Backgrounding is unsupported for threads in vfork */
824 if ((ut->uu_flag & UT_VFORK) != 0)
825 return(EPERM);
826
827 if (thread_is_static_param(thread)) {
828 return(EPERM);
829 }
830
831 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
832 if (thread_has_qos_policy(thread)) {
833 thread_remove_qos_policy(thread);
834 rv = EIDRM;
835 }
836
837 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
838 enable = (priority == PRIO_DARWIN_BG) ? TASK_POLICY_ENABLE : TASK_POLICY_DISABLE;
839 external = (current_thread() == thread) ? TASK_POLICY_INTERNAL : TASK_POLICY_EXTERNAL;
840
841 proc_set_task_policy_thread(curp->task, thread_tid(thread), external,
842 TASK_POLICY_DARWIN_BG, enable);
843
844 return rv;
845 }
846
847
848 /*
849 * Returns: 0 Success
850 * copyin:EFAULT
851 * dosetrlimit:
852 */
853 /* ARGSUSED */
854 int
855 setrlimit(struct proc *p, struct setrlimit_args *uap, __unused int32_t *retval)
856 {
857 struct rlimit alim;
858 int error;
859
860 if ((error = copyin(uap->rlp, (caddr_t)&alim,
861 sizeof (struct rlimit))))
862 return (error);
863
864 return (dosetrlimit(p, uap->which, &alim));
865 }
866
867 /*
868 * Returns: 0 Success
869 * EINVAL
870 * ENOMEM Cannot copy limit structure
871 * suser:EPERM
872 *
873 * Notes: EINVAL is returned both for invalid arguments, and in the
874 * case that the current usage (e.g. RLIMIT_STACK) is already
875 * in excess of the requested limit.
876 */
877 int
878 dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
879 {
880 struct rlimit *alimp;
881 int error;
882 kern_return_t kr;
883 int posix = (which & _RLIMIT_POSIX_FLAG) ? 1 : 0;
884
885 /* Mask out POSIX flag, saved above */
886 which &= ~_RLIMIT_POSIX_FLAG;
887
888 if (which >= RLIM_NLIMITS)
889 return (EINVAL);
890
891 alimp = &p->p_rlimit[which];
892 if (limp->rlim_cur > limp->rlim_max)
893 return EINVAL;
894
895 if (limp->rlim_cur > alimp->rlim_max ||
896 limp->rlim_max > alimp->rlim_max)
897 if ((error = suser(kauth_cred_get(), &p->p_acflag))) {
898 return (error);
899 }
900
901 proc_limitblock(p);
902
903 if ((error = proc_limitreplace(p)) != 0) {
904 proc_limitunblock(p);
905 return(error);
906 }
907
908 alimp = &p->p_rlimit[which];
909
910 switch (which) {
911
912 case RLIMIT_CPU:
913 if (limp->rlim_cur == RLIM_INFINITY) {
914 task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
915 timerclear(&p->p_rlim_cpu);
916 }
917 else {
918 task_absolutetime_info_data_t tinfo;
919 mach_msg_type_number_t count;
920 struct timeval ttv, tv;
921 clock_sec_t tv_sec;
922 clock_usec_t tv_usec;
923
924 count = TASK_ABSOLUTETIME_INFO_COUNT;
925 task_info(p->task, TASK_ABSOLUTETIME_INFO,
926 (task_info_t)&tinfo, &count);
927 absolutetime_to_microtime(tinfo.total_user + tinfo.total_system,
928 &tv_sec, &tv_usec);
929 ttv.tv_sec = tv_sec;
930 ttv.tv_usec = tv_usec;
931
932 tv.tv_sec = (limp->rlim_cur > __INT_MAX__ ? __INT_MAX__ : limp->rlim_cur);
933 tv.tv_usec = 0;
934 timersub(&tv, &ttv, &p->p_rlim_cpu);
935
936 timerclear(&tv);
937 if (timercmp(&p->p_rlim_cpu, &tv, >))
938 task_vtimer_set(p->task, TASK_VTIMER_RLIM);
939 else {
940 task_vtimer_clear(p->task, TASK_VTIMER_RLIM);
941
942 timerclear(&p->p_rlim_cpu);
943
944 psignal(p, SIGXCPU);
945 }
946 }
947 break;
948
949 case RLIMIT_DATA:
950 if (limp->rlim_cur > maxdmap)
951 limp->rlim_cur = maxdmap;
952 if (limp->rlim_max > maxdmap)
953 limp->rlim_max = maxdmap;
954 break;
955
956 case RLIMIT_STACK:
957 /* Disallow illegal stack size instead of clipping */
958 if (limp->rlim_cur > maxsmap ||
959 limp->rlim_max > maxsmap) {
960 if (posix) {
961 error = EINVAL;
962 goto out;
963 }
964 else {
965 /*
966 * 4797860 - workaround poorly written installers by
967 * doing previous implementation (< 10.5) when caller
968 * is non-POSIX conforming.
969 */
970 if (limp->rlim_cur > maxsmap)
971 limp->rlim_cur = maxsmap;
972 if (limp->rlim_max > maxsmap)
973 limp->rlim_max = maxsmap;
974 }
975 }
976
977 /*
978 * Stack is allocated to the max at exec time with only
979 * "rlim_cur" bytes accessible. If stack limit is going
980 * up make more accessible, if going down make inaccessible.
981 */
982 if (limp->rlim_cur > alimp->rlim_cur) {
983 user_addr_t addr;
984 user_size_t size;
985
986 /* grow stack */
987 size = round_page_64(limp->rlim_cur);
988 size -= round_page_64(alimp->rlim_cur);
989
990 addr = p->user_stack - round_page_64(limp->rlim_cur);
991 kr = mach_vm_protect(current_map(),
992 addr, size,
993 FALSE, VM_PROT_DEFAULT);
994 if (kr != KERN_SUCCESS) {
995 error = EINVAL;
996 goto out;
997 }
998 } else if (limp->rlim_cur < alimp->rlim_cur) {
999 user_addr_t addr;
1000 user_size_t size;
1001 user_addr_t cur_sp;
1002
1003 /* shrink stack */
1004
1005 /*
1006 * First check if new stack limit would agree
1007 * with current stack usage.
1008 * Get the current thread's stack pointer...
1009 */
1010 cur_sp = thread_adjuserstack(current_thread(),
1011 0);
1012 if (cur_sp <= p->user_stack &&
1013 cur_sp > (p->user_stack -
1014 round_page_64(alimp->rlim_cur))) {
1015 /* stack pointer is in main stack */
1016 if (cur_sp <= (p->user_stack -
1017 round_page_64(limp->rlim_cur))) {
1018 /*
1019 * New limit would cause
1020 * current usage to be invalid:
1021 * reject new limit.
1022 */
1023 error = EINVAL;
1024 goto out;
1025 }
1026 } else {
1027 /* not on the main stack: reject */
1028 error = EINVAL;
1029 goto out;
1030 }
1031
1032 size = round_page_64(alimp->rlim_cur);
1033 size -= round_page_64(limp->rlim_cur);
1034
1035 addr = p->user_stack - round_page_64(alimp->rlim_cur);
1036
1037 kr = mach_vm_protect(current_map(),
1038 addr, size,
1039 FALSE, VM_PROT_NONE);
1040 if (kr != KERN_SUCCESS) {
1041 error = EINVAL;
1042 goto out;
1043 }
1044 } else {
1045 /* no change ... */
1046 }
1047 break;
1048
1049 case RLIMIT_NOFILE:
1050 /*
1051 * Only root can set the maxfiles limits, as it is
1052 * systemwide resource. If we are expecting POSIX behavior,
1053 * instead of clamping the value, return EINVAL. We do this
1054 * because historically, people have been able to attempt to
1055 * set RLIM_INFINITY to get "whatever the maximum is".
1056 */
1057 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1058 if (limp->rlim_cur != alimp->rlim_cur &&
1059 limp->rlim_cur > (rlim_t)maxfiles) {
1060 if (posix) {
1061 error = EINVAL;
1062 goto out;
1063 }
1064 limp->rlim_cur = maxfiles;
1065 }
1066 if (limp->rlim_max != alimp->rlim_max &&
1067 limp->rlim_max > (rlim_t)maxfiles)
1068 limp->rlim_max = maxfiles;
1069 }
1070 else {
1071 if (limp->rlim_cur != alimp->rlim_cur &&
1072 limp->rlim_cur > (rlim_t)maxfilesperproc) {
1073 if (posix) {
1074 error = EINVAL;
1075 goto out;
1076 }
1077 limp->rlim_cur = maxfilesperproc;
1078 }
1079 if (limp->rlim_max != alimp->rlim_max &&
1080 limp->rlim_max > (rlim_t)maxfilesperproc)
1081 limp->rlim_max = maxfilesperproc;
1082 }
1083 break;
1084
1085 case RLIMIT_NPROC:
1086 /*
1087 * Only root can set to the maxproc limits, as it is
1088 * systemwide resource; all others are limited to
1089 * maxprocperuid (presumably less than maxproc).
1090 */
1091 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1092 if (limp->rlim_cur > (rlim_t)maxproc)
1093 limp->rlim_cur = maxproc;
1094 if (limp->rlim_max > (rlim_t)maxproc)
1095 limp->rlim_max = maxproc;
1096 }
1097 else {
1098 if (limp->rlim_cur > (rlim_t)maxprocperuid)
1099 limp->rlim_cur = maxprocperuid;
1100 if (limp->rlim_max > (rlim_t)maxprocperuid)
1101 limp->rlim_max = maxprocperuid;
1102 }
1103 break;
1104
1105 case RLIMIT_MEMLOCK:
1106 /*
1107 * Tell the Mach VM layer about the new limit value.
1108 */
1109
1110 vm_map_set_user_wire_limit(current_map(), limp->rlim_cur);
1111 break;
1112
1113 } /* switch... */
1114 proc_lock(p);
1115 *alimp = *limp;
1116 proc_unlock(p);
1117 error = 0;
1118 out:
1119 proc_limitunblock(p);
1120 return (error);
1121 }
1122
1123 /* ARGSUSED */
1124 int
1125 getrlimit(struct proc *p, struct getrlimit_args *uap, __unused int32_t *retval)
1126 {
1127 struct rlimit lim;
1128
1129 /*
1130 * Take out flag now in case we need to use it to trigger variant
1131 * behaviour later.
1132 */
1133 uap->which &= ~_RLIMIT_POSIX_FLAG;
1134
1135 if (uap->which >= RLIM_NLIMITS)
1136 return (EINVAL);
1137 proc_limitget(p, uap->which, &lim);
1138 return (copyout((caddr_t)&lim,
1139 uap->rlp, sizeof (struct rlimit)));
1140 }
1141
1142 /*
1143 * Transform the running time and tick information in proc p into user,
1144 * system, and interrupt time usage.
1145 */
1146 /* No lock on proc is held for this.. */
1147 void
1148 calcru(struct proc *p, struct timeval *up, struct timeval *sp, struct timeval *ip)
1149 {
1150 task_t task;
1151
1152 timerclear(up);
1153 timerclear(sp);
1154 if (ip != NULL)
1155 timerclear(ip);
1156
1157 task = p->task;
1158 if (task) {
1159 mach_task_basic_info_data_t tinfo;
1160 task_thread_times_info_data_t ttimesinfo;
1161 task_events_info_data_t teventsinfo;
1162 mach_msg_type_number_t task_info_count, task_ttimes_count;
1163 mach_msg_type_number_t task_events_count;
1164 struct timeval ut,st;
1165
1166 task_info_count = MACH_TASK_BASIC_INFO_COUNT;
1167 task_info(task, MACH_TASK_BASIC_INFO,
1168 (task_info_t)&tinfo, &task_info_count);
1169 ut.tv_sec = tinfo.user_time.seconds;
1170 ut.tv_usec = tinfo.user_time.microseconds;
1171 st.tv_sec = tinfo.system_time.seconds;
1172 st.tv_usec = tinfo.system_time.microseconds;
1173 timeradd(&ut, up, up);
1174 timeradd(&st, sp, sp);
1175
1176 task_ttimes_count = TASK_THREAD_TIMES_INFO_COUNT;
1177 task_info(task, TASK_THREAD_TIMES_INFO,
1178 (task_info_t)&ttimesinfo, &task_ttimes_count);
1179
1180 ut.tv_sec = ttimesinfo.user_time.seconds;
1181 ut.tv_usec = ttimesinfo.user_time.microseconds;
1182 st.tv_sec = ttimesinfo.system_time.seconds;
1183 st.tv_usec = ttimesinfo.system_time.microseconds;
1184 timeradd(&ut, up, up);
1185 timeradd(&st, sp, sp);
1186
1187 task_events_count = TASK_EVENTS_INFO_COUNT;
1188 task_info(task, TASK_EVENTS_INFO,
1189 (task_info_t)&teventsinfo, &task_events_count);
1190
1191 /*
1192 * No need to lock "p": this does not need to be
1193 * completely consistent, right ?
1194 */
1195 p->p_stats->p_ru.ru_minflt = (teventsinfo.faults -
1196 teventsinfo.pageins);
1197 p->p_stats->p_ru.ru_majflt = teventsinfo.pageins;
1198 p->p_stats->p_ru.ru_nivcsw = (teventsinfo.csw -
1199 p->p_stats->p_ru.ru_nvcsw);
1200 if (p->p_stats->p_ru.ru_nivcsw < 0)
1201 p->p_stats->p_ru.ru_nivcsw = 0;
1202
1203 p->p_stats->p_ru.ru_maxrss = tinfo.resident_size_max;
1204 }
1205 }
1206
1207 __private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
1208 __private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
1209
1210 /* ARGSUSED */
1211 int
1212 getrusage(struct proc *p, struct getrusage_args *uap, __unused int32_t *retval)
1213 {
1214 struct rusage *rup, rubuf;
1215 struct user64_rusage rubuf64;
1216 struct user32_rusage rubuf32;
1217 size_t retsize = sizeof(rubuf); /* default: 32 bits */
1218 caddr_t retbuf = (caddr_t)&rubuf; /* default: 32 bits */
1219 struct timeval utime;
1220 struct timeval stime;
1221
1222
1223 switch (uap->who) {
1224 case RUSAGE_SELF:
1225 calcru(p, &utime, &stime, NULL);
1226 proc_lock(p);
1227 rup = &p->p_stats->p_ru;
1228 rup->ru_utime = utime;
1229 rup->ru_stime = stime;
1230
1231 rubuf = *rup;
1232 proc_unlock(p);
1233
1234 break;
1235
1236 case RUSAGE_CHILDREN:
1237 proc_lock(p);
1238 rup = &p->p_stats->p_cru;
1239 rubuf = *rup;
1240 proc_unlock(p);
1241 break;
1242
1243 default:
1244 return (EINVAL);
1245 }
1246 if (IS_64BIT_PROCESS(p)) {
1247 retsize = sizeof(rubuf64);
1248 retbuf = (caddr_t)&rubuf64;
1249 munge_user64_rusage(&rubuf, &rubuf64);
1250 } else {
1251 retsize = sizeof(rubuf32);
1252 retbuf = (caddr_t)&rubuf32;
1253 munge_user32_rusage(&rubuf, &rubuf32);
1254 }
1255
1256 return (copyout(retbuf, uap->rusage, retsize));
1257 }
1258
1259 void
1260 ruadd(struct rusage *ru, struct rusage *ru2)
1261 {
1262 long *ip, *ip2;
1263 long i;
1264
1265 timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
1266 timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
1267 if (ru->ru_maxrss < ru2->ru_maxrss)
1268 ru->ru_maxrss = ru2->ru_maxrss;
1269 ip = &ru->ru_first; ip2 = &ru2->ru_first;
1270 for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
1271 *ip++ += *ip2++;
1272 }
1273
1274 /*
1275 * Add the rusage stats of child in parent.
1276 *
1277 * It adds rusage statistics of child process and statistics of all its
1278 * children to its parent.
1279 *
1280 * Note: proc lock of parent should be held while calling this function.
1281 */
1282 void
1283 update_rusage_info_child(struct rusage_info_child *ri, rusage_info_current *ri_current)
1284 {
1285 ri->ri_child_user_time += (ri_current->ri_user_time +
1286 ri_current->ri_child_user_time);
1287 ri->ri_child_system_time += (ri_current->ri_system_time +
1288 ri_current->ri_child_system_time);
1289 ri->ri_child_pkg_idle_wkups += (ri_current->ri_pkg_idle_wkups +
1290 ri_current->ri_child_pkg_idle_wkups);
1291 ri->ri_child_interrupt_wkups += (ri_current->ri_interrupt_wkups +
1292 ri_current->ri_child_interrupt_wkups);
1293 ri->ri_child_pageins += (ri_current->ri_pageins +
1294 ri_current->ri_child_pageins);
1295 ri->ri_child_elapsed_abstime += ((ri_current->ri_proc_exit_abstime -
1296 ri_current->ri_proc_start_abstime) + ri_current->ri_child_elapsed_abstime);
1297 }
1298
1299 void
1300 proc_limitget(proc_t p, int which, struct rlimit * limp)
1301 {
1302 proc_list_lock();
1303 limp->rlim_cur = p->p_rlimit[which].rlim_cur;
1304 limp->rlim_max = p->p_rlimit[which].rlim_max;
1305 proc_list_unlock();
1306 }
1307
1308
1309 void
1310 proc_limitdrop(proc_t p, int exiting)
1311 {
1312 struct plimit * freelim = NULL;
1313 struct plimit * freeoldlim = NULL;
1314
1315 proc_list_lock();
1316
1317 if (--p->p_limit->pl_refcnt == 0) {
1318 freelim = p->p_limit;
1319 p->p_limit = NULL;
1320 }
1321 if ((exiting != 0) && (p->p_olimit != NULL) && (--p->p_olimit->pl_refcnt == 0)) {
1322 freeoldlim = p->p_olimit;
1323 p->p_olimit = NULL;
1324 }
1325
1326 proc_list_unlock();
1327 if (freelim != NULL)
1328 FREE_ZONE(freelim, sizeof *p->p_limit, M_PLIMIT);
1329 if (freeoldlim != NULL)
1330 FREE_ZONE(freeoldlim, sizeof *p->p_olimit, M_PLIMIT);
1331 }
1332
1333
1334 void
1335 proc_limitfork(proc_t parent, proc_t child)
1336 {
1337 proc_list_lock();
1338 child->p_limit = parent->p_limit;
1339 child->p_limit->pl_refcnt++;
1340 child->p_olimit = NULL;
1341 proc_list_unlock();
1342 }
1343
1344 void
1345 proc_limitblock(proc_t p)
1346 {
1347 proc_lock(p);
1348 while (p->p_lflag & P_LLIMCHANGE) {
1349 p->p_lflag |= P_LLIMWAIT;
1350 msleep(&p->p_olimit, &p->p_mlock, 0, "proc_limitblock", NULL);
1351 }
1352 p->p_lflag |= P_LLIMCHANGE;
1353 proc_unlock(p);
1354
1355 }
1356
1357
1358 void
1359 proc_limitunblock(proc_t p)
1360 {
1361 proc_lock(p);
1362 p->p_lflag &= ~P_LLIMCHANGE;
1363 if (p->p_lflag & P_LLIMWAIT) {
1364 p->p_lflag &= ~P_LLIMWAIT;
1365 wakeup(&p->p_olimit);
1366 }
1367 proc_unlock(p);
1368 }
1369
1370 /* This is called behind serialization provided by proc_limitblock/unlbock */
1371 int
1372 proc_limitreplace(proc_t p)
1373 {
1374 struct plimit *copy;
1375
1376
1377 proc_list_lock();
1378
1379 if (p->p_limit->pl_refcnt == 1) {
1380 proc_list_unlock();
1381 return(0);
1382 }
1383
1384 proc_list_unlock();
1385
1386 MALLOC_ZONE(copy, struct plimit *,
1387 sizeof(struct plimit), M_PLIMIT, M_WAITOK);
1388 if (copy == NULL) {
1389 return(ENOMEM);
1390 }
1391
1392 proc_list_lock();
1393 bcopy(p->p_limit->pl_rlimit, copy->pl_rlimit,
1394 sizeof(struct rlimit) * RLIM_NLIMITS);
1395 copy->pl_refcnt = 1;
1396 /* hang on to reference to old till process exits */
1397 p->p_olimit = p->p_limit;
1398 p->p_limit = copy;
1399 proc_list_unlock();
1400
1401 return(0);
1402 }
1403
1404 /*
1405 * iopolicysys
1406 *
1407 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1408 *
1409 * Parameters: cmd Policy command
1410 * arg Pointer to policy arguments
1411 *
1412 * Returns: 0 Success
1413 * EINVAL Invalid command or invalid policy arguments
1414 *
1415 */
1416
1417 static int
1418 iopolicysys_disk(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1419 static int
1420 iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param);
1421
1422 int
1423 iopolicysys(struct proc *p, struct iopolicysys_args *uap, int32_t *retval)
1424 {
1425 int error = 0;
1426 struct _iopol_param_t iop_param;
1427
1428 if ((error = copyin(uap->arg, &iop_param, sizeof(iop_param))) != 0)
1429 goto out;
1430
1431 switch (iop_param.iop_iotype) {
1432 case IOPOL_TYPE_DISK:
1433 error = iopolicysys_disk(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1434 if (error == EIDRM) {
1435 *retval = -2;
1436 error = 0;
1437 }
1438 if (error)
1439 goto out;
1440 break;
1441 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY:
1442 error = iopolicysys_vfs(p, uap->cmd, iop_param.iop_scope, iop_param.iop_policy, &iop_param);
1443 if (error)
1444 goto out;
1445 break;
1446 default:
1447 error = EINVAL;
1448 goto out;
1449 }
1450
1451 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1452 if (uap->cmd == IOPOL_CMD_GET) {
1453 error = copyout((caddr_t)&iop_param, uap->arg, sizeof(iop_param));
1454 if (error)
1455 goto out;
1456 }
1457
1458 out:
1459 return (error);
1460 }
1461
1462 static int
1463 iopolicysys_disk(struct proc *p __unused, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1464 {
1465 int error = 0;
1466 thread_t thread;
1467 int policy_flavor;
1468
1469 /* Validate scope */
1470 switch (scope) {
1471 case IOPOL_SCOPE_PROCESS:
1472 thread = THREAD_NULL;
1473 policy_flavor = TASK_POLICY_IOPOL;
1474 break;
1475
1476 case IOPOL_SCOPE_THREAD:
1477 thread = current_thread();
1478 policy_flavor = TASK_POLICY_IOPOL;
1479
1480 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1481 if (cmd == IOPOL_CMD_SET && thread_has_qos_policy(thread)) {
1482 switch (policy) {
1483 case IOPOL_DEFAULT:
1484 case IOPOL_PASSIVE:
1485 break;
1486 case IOPOL_UTILITY:
1487 case IOPOL_THROTTLE:
1488 case IOPOL_IMPORTANT:
1489 case IOPOL_STANDARD:
1490 if (!thread_is_static_param(thread)) {
1491 thread_remove_qos_policy(thread);
1492 /*
1493 * This is not an error case, this is to return a marker to user-space that
1494 * we stripped the thread of its QoS class.
1495 */
1496 error = EIDRM;
1497 break;
1498 }
1499 /* otherwise, fall through to the error case. */
1500 default:
1501 error = EINVAL;
1502 goto out;
1503 }
1504 }
1505 break;
1506
1507 case IOPOL_SCOPE_DARWIN_BG:
1508 thread = THREAD_NULL;
1509 policy_flavor = TASK_POLICY_DARWIN_BG_IOPOL;
1510 break;
1511
1512 default:
1513 error = EINVAL;
1514 goto out;
1515 }
1516
1517 /* Validate policy */
1518 if (cmd == IOPOL_CMD_SET) {
1519 switch (policy) {
1520 case IOPOL_DEFAULT:
1521 if (scope == IOPOL_SCOPE_DARWIN_BG) {
1522 /* the current default BG throttle level is UTILITY */
1523 policy = IOPOL_UTILITY;
1524 } else {
1525 policy = IOPOL_IMPORTANT;
1526 }
1527 break;
1528 case IOPOL_UTILITY:
1529 /* fall-through */
1530 case IOPOL_THROTTLE:
1531 /* These levels are OK */
1532 break;
1533 case IOPOL_IMPORTANT:
1534 /* fall-through */
1535 case IOPOL_STANDARD:
1536 /* fall-through */
1537 case IOPOL_PASSIVE:
1538 if (scope == IOPOL_SCOPE_DARWIN_BG) {
1539 /* These levels are invalid for BG */
1540 error = EINVAL;
1541 goto out;
1542 } else {
1543 /* OK for other scopes */
1544 }
1545 break;
1546 default:
1547 error = EINVAL;
1548 goto out;
1549 }
1550 }
1551
1552 /* Perform command */
1553 switch(cmd) {
1554 case IOPOL_CMD_SET:
1555 proc_set_task_policy(current_task(), thread,
1556 TASK_POLICY_INTERNAL, policy_flavor,
1557 policy);
1558 break;
1559 case IOPOL_CMD_GET:
1560 policy = proc_get_task_policy(current_task(), thread,
1561 TASK_POLICY_INTERNAL, policy_flavor);
1562
1563 iop_param->iop_policy = policy;
1564 break;
1565 default:
1566 error = EINVAL; /* unknown command */
1567 break;
1568 }
1569
1570 out:
1571 return (error);
1572 }
1573
1574 static int
1575 iopolicysys_vfs(struct proc *p, int cmd, int scope, int policy, struct _iopol_param_t *iop_param)
1576 {
1577 int error = 0;
1578
1579 /* Validate scope */
1580 switch (scope) {
1581 case IOPOL_SCOPE_PROCESS:
1582 /* Only process OK */
1583 break;
1584 default:
1585 error = EINVAL;
1586 goto out;
1587 }
1588
1589 /* Validate policy */
1590 if (cmd == IOPOL_CMD_SET) {
1591 switch (policy) {
1592 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
1593 /* fall-through */
1594 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
1595 /* These policies are OK */
1596 break;
1597 default:
1598 error = EINVAL;
1599 goto out;
1600 }
1601 }
1602
1603 /* Perform command */
1604 switch(cmd) {
1605 case IOPOL_CMD_SET:
1606 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1607 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1608 boolean_t entitled = FALSE;
1609 entitled = IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1610 if (!entitled) {
1611 error = EPERM;
1612 goto out;
1613 }
1614 }
1615
1616 switch (policy) {
1617 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT:
1618 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY), &p->p_vfs_iopolicy);
1619 break;
1620 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE:
1621 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY, &p->p_vfs_iopolicy);
1622 break;
1623 default:
1624 error = EINVAL;
1625 goto out;
1626 }
1627
1628 break;
1629 case IOPOL_CMD_GET:
1630 iop_param->iop_policy = (p->p_vfs_iopolicy & P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY)
1631 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1632 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT;
1633 break;
1634 default:
1635 error = EINVAL; /* unknown command */
1636 break;
1637 }
1638
1639 out:
1640 return (error);
1641 }
1642
1643 /* BSD call back function for task_policy */
1644 void proc_apply_task_networkbg(void * bsd_info, thread_t thread);
1645
1646 void
1647 proc_apply_task_networkbg(void * bsd_info, thread_t thread)
1648 {
1649 assert(bsd_info != PROC_NULL);
1650
1651 pid_t pid = proc_pid((proc_t)bsd_info);
1652
1653 proc_t p = proc_find(pid);
1654
1655 if (p != PROC_NULL) {
1656 assert(p == (proc_t)bsd_info);
1657
1658 do_background_socket(p, thread);
1659 proc_rele(p);
1660 }
1661 }
1662
1663 void
1664 gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor)
1665 {
1666 struct rusage_info_child *ri_child;
1667
1668 assert(p->p_stats != NULL);
1669 switch(flavor) {
1670
1671 case RUSAGE_INFO_V3:
1672 fill_task_qos_rusage(p->task, ru);
1673 fill_task_billed_usage(p->task, ru);
1674 /* fall through */
1675
1676 case RUSAGE_INFO_V2:
1677 fill_task_io_rusage(p->task, ru);
1678 /* fall through */
1679
1680 case RUSAGE_INFO_V1:
1681 /*
1682 * p->p_stats->ri_child statistics are protected under proc lock.
1683 */
1684 proc_lock(p);
1685
1686 ri_child = &(p->p_stats->ri_child);
1687 ru->ri_child_user_time = ri_child->ri_child_user_time;
1688 ru->ri_child_system_time = ri_child->ri_child_system_time;
1689 ru->ri_child_pkg_idle_wkups = ri_child->ri_child_pkg_idle_wkups;
1690 ru->ri_child_interrupt_wkups = ri_child->ri_child_interrupt_wkups;
1691 ru->ri_child_pageins = ri_child->ri_child_pageins;
1692 ru->ri_child_elapsed_abstime = ri_child->ri_child_elapsed_abstime;
1693
1694 proc_unlock(p);
1695 /* fall through */
1696
1697 case RUSAGE_INFO_V0:
1698 proc_getexecutableuuid(p, (unsigned char *)&ru->ri_uuid, sizeof (ru->ri_uuid));
1699 fill_task_rusage(p->task, ru);
1700 ru->ri_proc_start_abstime = p->p_stats->ps_start;
1701 }
1702 }
1703
1704 static void
1705 rusage_info_conversion(rusage_info_t ri_info, rusage_info_current *ri_current, int flavor)
1706 {
1707 struct rusage_info_v0 *ri_v0;
1708 struct rusage_info_v1 *ri_v1;
1709 struct rusage_info_v2 *ri_v2;
1710
1711 switch (flavor) {
1712
1713 case RUSAGE_INFO_V2:
1714 ri_v2 = (struct rusage_info_v2 *)ri_info;
1715 ri_v2->ri_diskio_bytesread = ri_current->ri_diskio_bytesread;
1716 ri_v2->ri_diskio_byteswritten = ri_current->ri_diskio_byteswritten;
1717 /* fall through */
1718
1719 case RUSAGE_INFO_V1:
1720 ri_v1 = (struct rusage_info_v1 *)ri_info;
1721 ri_v1->ri_child_user_time = ri_current->ri_child_user_time;
1722 ri_v1->ri_child_system_time = ri_current->ri_child_system_time;
1723 ri_v1->ri_child_pkg_idle_wkups = ri_current->ri_child_pkg_idle_wkups;
1724 ri_v1->ri_child_interrupt_wkups = ri_current->ri_child_interrupt_wkups;
1725 ri_v1->ri_child_pageins = ri_current->ri_child_pageins;
1726 ri_v1->ri_child_elapsed_abstime = ri_current->ri_child_elapsed_abstime;
1727 /* fall through */
1728
1729 case RUSAGE_INFO_V0:
1730 ri_v0 = (struct rusage_info_v0 *)ri_info;
1731 memcpy(&ri_v0->ri_uuid[0], &ri_current->ri_uuid[0], sizeof(ri_v0->ri_uuid));
1732 ri_v0->ri_user_time = ri_current->ri_user_time;
1733 ri_v0->ri_system_time = ri_current->ri_system_time;
1734 ri_v0->ri_pkg_idle_wkups = ri_current->ri_pkg_idle_wkups;
1735 ri_v0->ri_interrupt_wkups = ri_current->ri_interrupt_wkups;
1736 ri_v0->ri_pageins = ri_current->ri_pageins;
1737 ri_v0->ri_wired_size = ri_current->ri_wired_size;
1738 ri_v0->ri_resident_size = ri_current->ri_resident_size;
1739 ri_v0->ri_phys_footprint = ri_current->ri_phys_footprint;
1740 ri_v0->ri_proc_start_abstime = ri_current->ri_proc_start_abstime;
1741 ri_v0->ri_proc_exit_abstime = ri_current->ri_proc_exit_abstime;
1742
1743 break;
1744
1745 default:
1746 break;
1747 }
1748 }
1749
1750
1751 int
1752 proc_get_rusage(proc_t p, int flavor, user_addr_t buffer, __unused int is_zombie)
1753 {
1754 struct rusage_info_v0 ri_v0;
1755 struct rusage_info_v1 ri_v1;
1756 struct rusage_info_v2 ri_v2;
1757 struct rusage_info_v3 ri_v3;
1758
1759 rusage_info_current ri_current;
1760
1761 int error = 0;
1762
1763 switch (flavor) {
1764 case RUSAGE_INFO_V0:
1765 /*
1766 * If task is still alive, collect info from the live task itself.
1767 * Otherwise, look to the cached info in the zombie proc.
1768 */
1769 if (p->p_ru == NULL) {
1770 gather_rusage_info(p, &ri_current, flavor);
1771 ri_current.ri_proc_exit_abstime = 0;
1772 rusage_info_conversion(&ri_v0, &ri_current, flavor);
1773 } else {
1774 rusage_info_conversion(&ri_v0, &p->p_ru->ri, flavor);
1775 }
1776 error = copyout(&ri_v0, buffer, sizeof (ri_v0));
1777 break;
1778
1779 case RUSAGE_INFO_V1:
1780 /*
1781 * If task is still alive, collect info from the live task itself.
1782 * Otherwise, look to the cached info in the zombie proc.
1783 */
1784 if (p->p_ru == NULL) {
1785 gather_rusage_info(p, &ri_current, flavor);
1786 ri_current.ri_proc_exit_abstime = 0;
1787 rusage_info_conversion(&ri_v1, &ri_current, flavor);
1788 } else {
1789 rusage_info_conversion(&ri_v1, &p->p_ru->ri, flavor);
1790 }
1791 error = copyout(&ri_v1, buffer, sizeof (ri_v1));
1792 break;
1793
1794 case RUSAGE_INFO_V2:
1795 /*
1796 * If task is still alive, collect info from the live task itself.
1797 * Otherwise, look to the cached info in the zombie proc.
1798 */
1799 if (p->p_ru == NULL) {
1800 gather_rusage_info(p, &ri_current, flavor);
1801 ri_current.ri_proc_exit_abstime = 0;
1802 rusage_info_conversion(&ri_v2, &ri_current, flavor);
1803 } else {
1804 rusage_info_conversion(&ri_v2, &p->p_ru->ri, flavor);
1805 }
1806 error = copyout(&ri_v2, buffer, sizeof (ri_v2));
1807 break;
1808
1809 case RUSAGE_INFO_V3:
1810 /*
1811 * If task is still alive, collect info from the live task itself.
1812 * Otherwise, look to the cached info in the zombie proc.
1813 */
1814 if (p->p_ru == NULL) {
1815 gather_rusage_info(p, &ri_v3, flavor);
1816 ri_v3.ri_proc_exit_abstime = 0;
1817 } else {
1818 ri_v3 = p->p_ru->ri;
1819 }
1820 error = copyout(&ri_v3, buffer, sizeof (ri_v3));
1821 break;
1822
1823 default:
1824 error = EINVAL;
1825 break;
1826 }
1827
1828 return (error);
1829 }
1830
1831 static int
1832 mach_to_bsd_rv(int mach_rv)
1833 {
1834 int bsd_rv = 0;
1835
1836 switch (mach_rv) {
1837 case KERN_SUCCESS:
1838 bsd_rv = 0;
1839 break;
1840 case KERN_INVALID_ARGUMENT:
1841 bsd_rv = EINVAL;
1842 break;
1843 default:
1844 panic("unknown error %#x", mach_rv);
1845 }
1846
1847 return bsd_rv;
1848 }
1849
1850 /*
1851 * Resource limit controls
1852 *
1853 * uap->flavor available flavors:
1854 *
1855 * RLIMIT_WAKEUPS_MONITOR
1856 */
1857 int
1858 proc_rlimit_control(__unused struct proc *p, struct proc_rlimit_control_args *uap, __unused int32_t *retval)
1859 {
1860 proc_t targetp;
1861 int error = 0;
1862 struct proc_rlimit_control_wakeupmon wakeupmon_args;
1863 uint32_t cpumon_flags;
1864 uint32_t cpulimits_flags;
1865 kauth_cred_t my_cred, target_cred;
1866
1867 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
1868 if (uap->pid == -1) {
1869 targetp = proc_self();
1870 } else {
1871 targetp = proc_find(uap->pid);
1872 }
1873
1874 /* proc_self() can return NULL for an exiting process */
1875 if (targetp == PROC_NULL) {
1876 return (ESRCH);
1877 }
1878
1879 my_cred = kauth_cred_get();
1880 target_cred = kauth_cred_proc_ref(targetp);
1881
1882 if (!kauth_cred_issuser(my_cred) && kauth_cred_getruid(my_cred) &&
1883 kauth_cred_getuid(my_cred) != kauth_cred_getuid(target_cred) &&
1884 kauth_cred_getruid(my_cred) != kauth_cred_getuid(target_cred)) {
1885 proc_rele(targetp);
1886 kauth_cred_unref(&target_cred);
1887 return (EACCES);
1888 }
1889
1890 switch (uap->flavor) {
1891 case RLIMIT_WAKEUPS_MONITOR:
1892 if ((error = copyin(uap->arg, &wakeupmon_args, sizeof (wakeupmon_args))) != 0) {
1893 break;
1894 }
1895 if ((error = mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp->task, &wakeupmon_args.wm_flags,
1896 &wakeupmon_args.wm_rate))) != 0) {
1897 break;
1898 }
1899 error = copyout(&wakeupmon_args, uap->arg, sizeof (wakeupmon_args));
1900 break;
1901 case RLIMIT_CPU_USAGE_MONITOR:
1902 cpumon_flags = uap->arg; // XXX temporarily stashing flags in argp (12592127)
1903 error = mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp->task, &cpumon_flags));
1904 break;
1905 case RLIMIT_THREAD_CPULIMITS:
1906 cpulimits_flags = (uint32_t)uap->arg; // only need a limited set of bits, pass in void * argument
1907
1908 if (uap->pid != -1) {
1909 error = EINVAL;
1910 break;
1911 }
1912
1913 uint8_t percent = 0;
1914 uint32_t ms_refill = 0;
1915 uint64_t ns_refill;
1916
1917 percent = (uint8_t)(cpulimits_flags & 0xffU); /* low 8 bits for percent */
1918 ms_refill = (cpulimits_flags >> 8) & 0xffffff; /* next 24 bits represent ms refill value */
1919 if (percent >= 100) {
1920 error = EINVAL;
1921 break;
1922 }
1923
1924 ns_refill = ((uint64_t)ms_refill) * NSEC_PER_MSEC;
1925
1926 error = mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK, percent, ns_refill));
1927 break;
1928 default:
1929 error = EINVAL;
1930 break;
1931 }
1932
1933 proc_rele(targetp);
1934 kauth_cred_unref(&target_cred);
1935
1936 /*
1937 * Return value from this function becomes errno to userland caller.
1938 */
1939 return (error);
1940 }
1941
1942 /*
1943 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
1944 */
1945 int thread_selfusage(struct proc *p __unused, struct thread_selfusage_args *uap __unused, uint64_t *retval)
1946 {
1947 uint64_t runtime;
1948
1949 runtime = thread_get_runtime_self();
1950 *retval = runtime;
1951
1952 return (0);
1953 }