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