2 * Copyright (c) 2000-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
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.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
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,
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 <sys/mount_internal.h>
85 #include <sys/sysproto.h>
87 #include <security/audit/audit.h>
89 #include <machine/vmparam.h>
91 #include <mach/mach_types.h>
92 #include <mach/time_value.h>
93 #include <mach/task.h>
94 #include <mach/task_info.h>
95 #include <mach/vm_map.h>
96 #include <mach/mach_vm.h>
97 #include <mach/thread_act.h> /* for thread_policy_set( ) */
98 #include <kern/thread.h>
99 #include <kern/policy_internal.h>
101 #include <kern/task.h>
102 #include <kern/clock.h> /* for absolutetime_to_microtime() */
103 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
104 #include <sys/socketvar.h> /* for struct socket */
106 #include <net/necp.h>
109 #include <vm/vm_map.h>
111 #include <kern/assert.h>
112 #include <sys/resource.h>
113 #include <sys/priv.h>
114 #include <IOKit/IOBSD.h>
117 #include <security/mac_framework.h>
120 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
121 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
122 int uthread_get_background_state(uthread_t
);
123 static void do_background_socket(struct proc
*p
, thread_t thread
);
124 static int do_background_thread(thread_t thread
, int priority
);
125 static int do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
126 static int set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
127 static int proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
);
128 static int proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
);
129 static int get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
);
130 int proc_pid_rusage(int pid
, int flavor
, user_addr_t buf
, int32_t *retval
);
131 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
132 int fill_task_rusage(task_t task
, rusage_info_current
*ri
);
133 void fill_task_billed_usage(task_t task
, rusage_info_current
*ri
);
134 int fill_task_io_rusage(task_t task
, rusage_info_current
*ri
);
135 int fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
);
136 uint64_t get_task_logical_writes(task_t task
);
137 void fill_task_monotonic_rusage(task_t task
, rusage_info_current
*ri
);
139 int proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
);
141 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
142 rlim_t maxsmap
= MAXSSIZ
- PAGE_MAX_SIZE
; /* XXX */
145 * Limits on the number of open files per process, and the number
146 * of child processes per process.
148 * Note: would be in kern/subr_param.c in FreeBSD.
150 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
152 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
153 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
155 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
156 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
158 /* Args and fn for proc_iteration callback used in setpriority */
159 struct puser_nice_args
{
166 static int puser_donice_callback(proc_t p
, void * arg
);
169 /* Args and fn for proc_iteration callback used in setpriority */
170 struct ppgrp_nice_args
{
176 static int ppgrp_donice_callback(proc_t p
, void * arg
);
179 * Resource controls and accounting.
182 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
185 int low
= PRIO_MAX
+ 1;
186 kauth_cred_t my_cred
;
190 /* would also test (uap->who < 0), but id_t is unsigned */
191 if (uap
->who
> 0x7fffffff)
194 switch (uap
->which
) {
201 p
= proc_find(uap
->who
);
211 struct pgrp
*pg
= PGRP_NULL
;
214 /* returns the pgrp to ref */
215 pg
= proc_pgrp(curp
);
216 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
219 /* No need for iteration as it is a simple scan */
221 PGMEMBERS_FOREACH(pg
, p
) {
232 uap
->who
= kauth_cred_getuid(kauth_cred_get());
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
&&
241 kauth_cred_unref(&my_cred
);
248 case PRIO_DARWIN_THREAD
:
249 /* we currently only support the current thread */
253 low
= proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_DARWIN_BG
);
257 case PRIO_DARWIN_PROCESS
:
261 p
= proc_find(uap
->who
);
267 error
= get_background_proc(curp
, p
, &low
);
275 case PRIO_DARWIN_ROLE
:
279 p
= proc_find(uap
->who
);
285 error
= proc_get_darwin_role(curp
, p
, &low
);
296 if (low
== PRIO_MAX
+ 1)
302 /* call back function used for proc iteration in PRIO_USER */
304 puser_donice_callback(proc_t p
, void * arg
)
307 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
308 kauth_cred_t my_cred
;
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
) {
320 kauth_cred_unref(&my_cred
);
322 return(PROC_RETURNED
);
325 /* call back function used for proc iteration in PRIO_PGRP */
327 ppgrp_donice_callback(proc_t p
, void * arg
)
330 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
333 error
= donice(pun
->curp
, p
, pun
->prio
);
334 if (pun
->errorp
!= NULL
)
335 *pun
->errorp
= error
;
336 if (pun
->foundp
!= NULL
) {
341 return(PROC_RETURNED
);
353 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, int32_t *retval
)
356 int found
= 0, error
= 0;
359 AUDIT_ARG(cmd
, uap
->which
);
360 AUDIT_ARG(owner
, uap
->who
, 0);
361 AUDIT_ARG(value32
, uap
->prio
);
363 /* would also test (uap->who < 0), but id_t is unsigned */
364 if (uap
->who
> 0x7fffffff)
367 switch (uap
->which
) {
373 p
= proc_find(uap
->who
);
378 error
= donice(curp
, p
, uap
->prio
);
385 struct pgrp
*pg
= PGRP_NULL
;
386 struct ppgrp_nice_args ppgrp
;
389 pg
= proc_pgrp(curp
);
390 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
)
394 ppgrp
.prio
= uap
->prio
;
395 ppgrp
.foundp
= &found
;
396 ppgrp
.errorp
= &error
;
398 /* PGRP_DROPREF drops the reference on process group */
399 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
405 struct puser_nice_args punice
;
408 uap
->who
= kauth_cred_getuid(kauth_cred_get());
411 punice
.prio
= uap
->prio
;
412 punice
.who
= uap
->who
;
413 punice
.foundp
= &found
;
415 punice
.errorp
= &error
;
416 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
421 case PRIO_DARWIN_THREAD
: {
422 /* we currently only support the current thread */
426 error
= do_background_thread(current_thread(), uap
->prio
);
431 case PRIO_DARWIN_PROCESS
: {
435 p
= proc_find(uap
->who
);
441 error
= do_background_proc(curp
, p
, uap
->prio
);
449 case PRIO_DARWIN_GPU
: {
453 p
= proc_find(uap
->who
);
457 error
= set_gpudeny_proc(curp
, p
, uap
->prio
);
464 case PRIO_DARWIN_ROLE
: {
468 p
= proc_find(uap
->who
);
474 error
= proc_set_darwin_role(curp
, p
, uap
->prio
);
487 if (error
== EIDRM
) {
499 * mac_check_proc_sched:???
502 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
506 kauth_cred_t my_cred
;
508 ucred
= kauth_cred_proc_ref(curp
);
509 my_cred
= kauth_cred_proc_ref(chgp
);
511 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
512 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
513 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
521 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
526 error
= mac_proc_check_sched(curp
, chgp
);
533 (void)resetpriority(chgp
);
535 kauth_cred_unref(&ucred
);
536 kauth_cred_unref(&my_cred
);
541 set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
545 kauth_cred_t target_cred
;
547 ucred
= kauth_cred_get();
548 target_cred
= kauth_cred_proc_ref(targetp
);
550 /* TODO: Entitlement instead of uid check */
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
)) {
559 if (curp
== targetp
) {
565 error
= mac_proc_check_sched(curp
, targetp
);
571 case PRIO_DARWIN_GPU_DENY
:
572 task_set_gpu_denied(proc_task(targetp
), TRUE
);
574 case PRIO_DARWIN_GPU_ALLOW
:
575 task_set_gpu_denied(proc_task(targetp
), FALSE
);
583 kauth_cred_unref(&target_cred
);
589 proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
)
594 kauth_cred_t ucred
, target_cred
;
596 ucred
= kauth_cred_get();
597 target_cred
= kauth_cred_proc_ref(targetp
);
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
)) {
602 if (priv_check_cred(ucred
, PRIV_SETPRIORITY_DARWIN_ROLE
, 0) != 0) {
608 if (curp
!= targetp
) {
610 if ((error
= mac_proc_check_sched(curp
, targetp
)))
615 proc_get_darwinbgstate(proc_task(targetp
), &flagsp
);
616 if ((flagsp
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
623 if ((error
= proc_darwin_role_to_task_role(priority
, &role
)))
626 proc_set_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
,
627 TASK_POLICY_ROLE
, role
);
630 kauth_cred_unref(&target_cred
);
635 proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
)
640 kauth_cred_t ucred
, target_cred
;
642 ucred
= kauth_cred_get();
643 target_cred
= kauth_cred_proc_ref(targetp
);
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
)) {
652 if (curp
!= targetp
) {
654 if ((error
= mac_proc_check_sched(curp
, targetp
)))
659 role
= proc_get_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
661 *priority
= proc_task_role_to_darwin_role(role
);
664 kauth_cred_unref(&target_cred
);
670 get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
)
674 kauth_cred_t ucred
, target_cred
;
676 ucred
= kauth_cred_get();
677 target_cred
= kauth_cred_proc_ref(targetp
);
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
)) {
686 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
688 *priority
= proc_get_task_policy(current_task(), external
, TASK_POLICY_DARWIN_BG
);
691 kauth_cred_unref(&target_cred
);
696 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
703 kauth_cred_t target_cred
;
707 ucred
= kauth_cred_get();
708 target_cred
= kauth_cred_proc_ref(targetp
);
710 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
711 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
712 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
))
719 error
= mac_proc_check_sched(curp
, targetp
);
724 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
728 enable
= TASK_POLICY_ENABLE
;
730 case PRIO_DARWIN_NONUI
:
731 /* ignored for compatibility */
734 /* TODO: EINVAL if priority != 0 */
735 enable
= TASK_POLICY_DISABLE
;
739 proc_set_task_policy(proc_task(targetp
), external
, TASK_POLICY_DARWIN_BG
, enable
);
742 kauth_cred_unref(&target_cred
);
747 do_background_socket(struct proc
*p
, thread_t thread
)
750 struct filedesc
*fdp
;
756 if (thread
!= THREAD_NULL
)
757 background
= proc_get_effective_thread_policy(thread
, TASK_POLICY_ALL_SOCKETS_BG
);
759 background
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ALL_SOCKETS_BG
);
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.
767 if (thread
== THREAD_NULL
) {
770 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
771 fp
= fdp
->fd_ofiles
[i
];
772 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0) {
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
;
781 else if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_NETPOLICY
) {
782 necp_set_client_as_background(p
, fp
, background
);
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.
794 for ( i
= 0; i
< fdp
->fd_nfiles
; i
++ ) {
795 struct socket
*sockp
;
797 fp
= fdp
->fd_ofiles
[ i
];
798 if (fp
== NULL
|| (fdp
->fd_ofileflags
[ i
] & UF_RESERVED
) != 0) {
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
)) {
807 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
808 sockp
->so_background_thread
= NULL
;
811 else if (FILEGLOB_DTYPE(fp
->f_fglob
) == DTYPE_NETPOLICY
) {
812 necp_set_client_as_background(p
, fp
, background
);
820 #pragma unused(p, thread)
826 * do_background_thread
828 * Requires: thread reference
831 * EPERM Tried to background while in vfork
832 * XXX - todo - does this need a MACF hook?
835 do_background_thread(thread_t thread
, int priority
)
838 int enable
, external
;
841 ut
= get_bsdthread_info(thread
);
843 /* Backgrounding is unsupported for threads in vfork */
844 if ((ut
->uu_flag
& UT_VFORK
) != 0)
847 /* Backgrounding is unsupported for workq threads */
848 if (thread_is_static_param(thread
)) {
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
);
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
;
862 proc_set_thread_policy(thread
, external
, TASK_POLICY_DARWIN_BG
, enable
);
875 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
880 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
881 sizeof (struct rlimit
))))
884 return (dosetrlimit(p
, uap
->which
, &alim
));
890 * ENOMEM Cannot copy limit structure
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.
898 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
900 struct rlimit
*alimp
;
903 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
905 /* Mask out POSIX flag, saved above */
906 which
&= ~_RLIMIT_POSIX_FLAG
;
908 if (which
>= RLIM_NLIMITS
)
911 alimp
= &p
->p_rlimit
[which
];
912 if (limp
->rlim_cur
> limp
->rlim_max
)
915 if (limp
->rlim_cur
> alimp
->rlim_max
||
916 limp
->rlim_max
> alimp
->rlim_max
)
917 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
923 if ((error
= proc_limitreplace(p
)) != 0) {
924 proc_limitunblock(p
);
928 alimp
= &p
->p_rlimit
[which
];
933 if (limp
->rlim_cur
== RLIM_INFINITY
) {
934 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
935 timerclear(&p
->p_rlim_cpu
);
938 task_absolutetime_info_data_t tinfo
;
939 mach_msg_type_number_t count
;
940 struct timeval ttv
, tv
;
942 clock_usec_t tv_usec
;
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
,
950 ttv
.tv_usec
= tv_usec
;
952 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
954 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
957 if (timercmp(&p
->p_rlim_cpu
, &tv
, >))
958 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
960 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
962 timerclear(&p
->p_rlim_cpu
);
970 if (limp
->rlim_cur
> maxdmap
)
971 limp
->rlim_cur
= maxdmap
;
972 if (limp
->rlim_max
> maxdmap
)
973 limp
->rlim_max
= maxdmap
;
977 /* Disallow illegal stack size instead of clipping */
978 if (limp
->rlim_cur
> maxsmap
||
979 limp
->rlim_max
> maxsmap
) {
986 * 4797860 - workaround poorly written installers by
987 * doing previous implementation (< 10.5) when caller
988 * is non-POSIX conforming.
990 if (limp
->rlim_cur
> maxsmap
)
991 limp
->rlim_cur
= maxsmap
;
992 if (limp
->rlim_max
> maxsmap
)
993 limp
->rlim_max
= maxsmap
;
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.
1002 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
1007 size
= round_page_64(limp
->rlim_cur
);
1008 size
-= round_page_64(alimp
->rlim_cur
);
1010 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
1011 kr
= mach_vm_protect(current_map(),
1013 FALSE
, VM_PROT_DEFAULT
);
1014 if (kr
!= KERN_SUCCESS
) {
1018 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
1026 * First check if new stack limit would agree
1027 * with current stack usage.
1028 * Get the current thread's stack pointer...
1030 cur_sp
= thread_adjuserstack(current_thread(),
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
))) {
1039 * New limit would cause
1040 * current usage to be invalid:
1047 /* not on the main stack: reject */
1052 size
= round_page_64(alimp
->rlim_cur
);
1053 size
-= round_page_64(limp
->rlim_cur
);
1055 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
1057 kr
= mach_vm_protect(current_map(),
1059 FALSE
, VM_PROT_NONE
);
1060 if (kr
!= KERN_SUCCESS
) {
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".
1077 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1078 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1079 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
1084 limp
->rlim_cur
= maxfiles
;
1086 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1087 limp
->rlim_max
> (rlim_t
)maxfiles
)
1088 limp
->rlim_max
= maxfiles
;
1091 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1092 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
1097 limp
->rlim_cur
= maxfilesperproc
;
1099 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1100 limp
->rlim_max
> (rlim_t
)maxfilesperproc
)
1101 limp
->rlim_max
= maxfilesperproc
;
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).
1111 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1112 if (limp
->rlim_cur
> (rlim_t
)maxproc
)
1113 limp
->rlim_cur
= maxproc
;
1114 if (limp
->rlim_max
> (rlim_t
)maxproc
)
1115 limp
->rlim_max
= maxproc
;
1118 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
)
1119 limp
->rlim_cur
= maxprocperuid
;
1120 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
)
1121 limp
->rlim_max
= maxprocperuid
;
1125 case RLIMIT_MEMLOCK
:
1127 * Tell the Mach VM layer about the new limit value.
1130 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
1139 proc_limitunblock(p
);
1145 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1147 struct rlimit lim
= {};
1150 * Take out flag now in case we need to use it to trigger variant
1153 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1155 if (uap
->which
>= RLIM_NLIMITS
)
1157 proc_limitget(p
, uap
->which
, &lim
);
1158 return (copyout((caddr_t
)&lim
,
1159 uap
->rlp
, sizeof (struct rlimit
)));
1163 * Transform the running time and tick information in proc p into user,
1164 * system, and interrupt time usage.
1166 /* No lock on proc is held for this.. */
1168 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1179 mach_task_basic_info_data_t tinfo
;
1180 task_thread_times_info_data_t ttimesinfo
;
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
;
1184 struct timeval ut
,st
;
1186 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
1187 task_info(task
, MACH_TASK_BASIC_INFO
,
1188 (task_info_t
)&tinfo
, &task_info_count
);
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
;
1193 timeradd(&ut
, up
, up
);
1194 timeradd(&st
, sp
, sp
);
1196 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1197 task_info(task
, TASK_THREAD_TIMES_INFO
,
1198 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
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
;
1204 timeradd(&ut
, up
, up
);
1205 timeradd(&st
, sp
, sp
);
1207 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1208 task_info(task
, TASK_EVENTS_INFO
,
1209 (task_info_t
)&teventsinfo
, &task_events_count
);
1212 * No need to lock "p": this does not need to be
1213 * completely consistent, right ?
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;
1223 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size_max
;
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
);
1232 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1234 struct rusage
*rup
, rubuf
;
1235 struct user64_rusage rubuf64
= {};
1236 struct user32_rusage rubuf32
= {};
1237 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1238 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1239 struct timeval utime
;
1240 struct timeval stime
;
1245 calcru(p
, &utime
, &stime
, NULL
);
1247 rup
= &p
->p_stats
->p_ru
;
1248 rup
->ru_utime
= utime
;
1249 rup
->ru_stime
= stime
;
1256 case RUSAGE_CHILDREN
:
1258 rup
= &p
->p_stats
->p_cru
;
1266 if (IS_64BIT_PROCESS(p
)) {
1267 retsize
= sizeof(rubuf64
);
1268 retbuf
= (caddr_t
)&rubuf64
;
1269 munge_user64_rusage(&rubuf
, &rubuf64
);
1271 retsize
= sizeof(rubuf32
);
1272 retbuf
= (caddr_t
)&rubuf32
;
1273 munge_user32_rusage(&rubuf
, &rubuf32
);
1276 return (copyout(retbuf
, uap
->rusage
, retsize
));
1280 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
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
--)
1295 * Add the rusage stats of child in parent.
1297 * It adds rusage statistics of child process and statistics of all its
1298 * children to its parent.
1300 * Note: proc lock of parent should be held while calling this function.
1303 update_rusage_info_child(struct rusage_info_child
*ri
, rusage_info_current
*ri_current
)
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
);
1320 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1323 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1324 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1330 proc_limitdrop(proc_t p
, int exiting
)
1332 struct plimit
* freelim
= NULL
;
1333 struct plimit
* freeoldlim
= NULL
;
1337 if (--p
->p_limit
->pl_refcnt
== 0) {
1338 freelim
= p
->p_limit
;
1341 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1342 freeoldlim
= p
->p_olimit
;
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
);
1355 proc_limitfork(proc_t parent
, proc_t child
)
1358 child
->p_limit
= parent
->p_limit
;
1359 child
->p_limit
->pl_refcnt
++;
1360 child
->p_olimit
= NULL
;
1365 proc_limitblock(proc_t 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
);
1372 p
->p_lflag
|= P_LLIMCHANGE
;
1379 proc_limitunblock(proc_t 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
);
1390 /* This is called behind serialization provided by proc_limitblock/unlbock */
1392 proc_limitreplace(proc_t p
)
1394 struct plimit
*copy
;
1399 if (p
->p_limit
->pl_refcnt
== 1) {
1406 MALLOC_ZONE(copy
, struct plimit
*,
1407 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1413 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1414 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1415 copy
->pl_refcnt
= 1;
1416 /* hang on to reference to old till process exits */
1417 p
->p_olimit
= p
->p_limit
;
1425 iopolicysys_disk(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1427 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1429 iopolicysys_vfs_atime_updates(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1434 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1436 * Parameters: cmd Policy command
1437 * arg Pointer to policy arguments
1439 * Returns: 0 Success
1440 * EINVAL Invalid command or invalid policy arguments
1444 iopolicysys(struct proc
*p
, struct iopolicysys_args
*uap
, int32_t *retval
)
1447 struct _iopol_param_t iop_param
;
1449 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0)
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
);
1455 if (error
== EIDRM
) {
1462 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY
:
1463 error
= iopolicysys_vfs_hfs_case_sensitivity(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
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
);
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
));
1489 iopolicysys_disk(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1495 /* Validate scope */
1497 case IOPOL_SCOPE_PROCESS
:
1498 thread
= THREAD_NULL
;
1499 policy_flavor
= TASK_POLICY_IOPOL
;
1502 case IOPOL_SCOPE_THREAD
:
1503 thread
= current_thread();
1504 policy_flavor
= TASK_POLICY_IOPOL
;
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
)) {
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
);
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.
1525 /* otherwise, fall through to the error case. */
1533 case IOPOL_SCOPE_DARWIN_BG
:
1535 /* Embedded doesn't want this as BG is always IOPOL_THROTTLE */
1538 #else /* CONFIG_EMBEDDED */
1539 thread
= THREAD_NULL
;
1540 policy_flavor
= TASK_POLICY_DARWIN_BG_IOPOL
;
1542 #endif /* CONFIG_EMBEDDED */
1549 /* Validate policy */
1550 if (cmd
== IOPOL_CMD_SET
) {
1553 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1554 /* the current default BG throttle level is UTILITY */
1555 policy
= IOPOL_UTILITY
;
1557 policy
= IOPOL_IMPORTANT
;
1562 case IOPOL_THROTTLE
:
1563 /* These levels are OK */
1565 case IOPOL_IMPORTANT
:
1567 case IOPOL_STANDARD
:
1570 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1571 /* These levels are invalid for BG */
1575 /* OK for other scopes */
1584 /* Perform command */
1587 if (thread
!= THREAD_NULL
)
1588 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1590 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1593 if (thread
!= THREAD_NULL
)
1594 policy
= proc_get_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
);
1596 policy
= proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
);
1597 iop_param
->iop_policy
= policy
;
1600 error
= EINVAL
; /* unknown command */
1609 iopolicysys_vfs_hfs_case_sensitivity(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1613 /* Validate scope */
1615 case IOPOL_SCOPE_PROCESS
:
1616 /* Only process OK */
1623 /* Validate policy */
1624 if (cmd
== IOPOL_CMD_SET
) {
1626 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1628 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1629 /* These policies are OK */
1637 /* Perform command */
1640 if (0 == kauth_cred_issuser(kauth_cred_get())) {
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");
1651 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1652 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
), &p
->p_vfs_iopolicy
);
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
);
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
;
1669 error
= EINVAL
; /* unknown command */
1678 get_thread_atime_policy(struct uthread
*ut
)
1680 return (ut
->uu_flag
& UT_ATIME_UPDATE
)? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1684 set_thread_atime_policy(struct uthread
*ut
, int policy
)
1686 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1687 ut
->uu_flag
|= UT_ATIME_UPDATE
;
1689 ut
->uu_flag
&= ~UT_ATIME_UPDATE
;
1694 set_task_atime_policy(struct proc
*p
, int policy
)
1696 if (policy
== IOPOL_ATIME_UPDATES_OFF
) {
1697 OSBitOrAtomic16((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
, &p
->p_vfs_iopolicy
);
1699 OSBitAndAtomic16(~((uint16_t)P_VFS_IOPOLICY_ATIME_UPDATES
), &p
->p_vfs_iopolicy
);
1704 get_task_atime_policy(struct proc
*p
)
1706 return (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_ATIME_UPDATES
)? IOPOL_ATIME_UPDATES_OFF
: IOPOL_ATIME_UPDATES_DEFAULT
;
1710 iopolicysys_vfs_atime_updates(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1715 /* Validate scope */
1717 case IOPOL_SCOPE_THREAD
:
1718 thread
= current_thread();
1720 case IOPOL_SCOPE_PROCESS
:
1721 thread
= THREAD_NULL
;
1728 /* Validate policy */
1729 if (cmd
== IOPOL_CMD_SET
) {
1731 case IOPOL_ATIME_UPDATES_DEFAULT
:
1732 case IOPOL_ATIME_UPDATES_OFF
:
1740 /* Perform command */
1743 if (thread
!= THREAD_NULL
)
1744 set_thread_atime_policy(get_bsdthread_info(thread
), policy
);
1746 set_task_atime_policy(p
, policy
);
1749 if (thread
!= THREAD_NULL
)
1750 policy
= get_thread_atime_policy(get_bsdthread_info(thread
));
1752 policy
= get_task_atime_policy(p
);
1753 iop_param
->iop_policy
= policy
;
1756 error
= EINVAL
; /* unknown command */
1764 /* BSD call back function for task_policy networking changes */
1766 proc_apply_task_networkbg(void * bsd_info
, thread_t thread
)
1768 assert(bsd_info
!= PROC_NULL
);
1770 pid_t pid
= proc_pid((proc_t
)bsd_info
);
1772 proc_t p
= proc_find(pid
);
1774 if (p
!= PROC_NULL
) {
1775 assert(p
== (proc_t
)bsd_info
);
1777 do_background_socket(p
, thread
);
1783 gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
)
1785 struct rusage_info_child
*ri_child
;
1787 assert(p
->p_stats
!= NULL
);
1788 memset(ru
, 0, sizeof(*ru
));
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
);
1793 #if CONFIG_LEDGER_INTERVAL_MAX
1794 ru
->ri_interval_max_phys_footprint
= get_task_phys_footprint_interval_max(p
->task
, FALSE
);
1796 fill_task_monotonic_rusage(p
->task
, ru
);
1799 case RUSAGE_INFO_V3
:
1800 fill_task_qos_rusage(p
->task
, ru
);
1801 fill_task_billed_usage(p
->task
, ru
);
1804 case RUSAGE_INFO_V2
:
1805 fill_task_io_rusage(p
->task
, ru
);
1808 case RUSAGE_INFO_V1
:
1810 * p->p_stats->ri_child statistics are protected under proc lock.
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
;
1825 case RUSAGE_INFO_V0
:
1826 proc_getexecutableuuid(p
, (unsigned char *)&ru
->ri_uuid
, sizeof (ru
->ri_uuid
));
1827 fill_task_rusage(p
->task
, ru
);
1828 ru
->ri_proc_start_abstime
= p
->p_stats
->ps_start
;
1833 proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
)
1835 rusage_info_current ri_current
= {};
1841 case RUSAGE_INFO_V0
:
1842 size
= sizeof(struct rusage_info_v0
);
1845 case RUSAGE_INFO_V1
:
1846 size
= sizeof(struct rusage_info_v1
);
1849 case RUSAGE_INFO_V2
:
1850 size
= sizeof(struct rusage_info_v2
);
1853 case RUSAGE_INFO_V3
:
1854 size
= sizeof(struct rusage_info_v3
);
1857 case RUSAGE_INFO_V4
:
1858 size
= sizeof(struct rusage_info_v4
);
1870 * If task is still alive, collect info from the live task itself.
1871 * Otherwise, look to the cached info in the zombie proc.
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
);
1878 ri_current
= p
->p_ru
->ri
;
1879 error
= copyout(&p
->p_ru
->ri
, buffer
, size
);
1886 mach_to_bsd_rv(int mach_rv
)
1894 case KERN_INVALID_ARGUMENT
:
1898 panic("unknown error %#x", mach_rv
);
1905 * Resource limit controls
1907 * uap->flavor available flavors:
1909 * RLIMIT_WAKEUPS_MONITOR
1910 * RLIMIT_CPU_USAGE_MONITOR
1911 * RLIMIT_THREAD_CPULIMITS
1912 * RLIMIT_FOOTPRINT_INTERVAL
1915 proc_rlimit_control(__unused
struct proc
*p
, struct proc_rlimit_control_args
*uap
, __unused
int32_t *retval
)
1919 struct proc_rlimit_control_wakeupmon wakeupmon_args
;
1920 uint32_t cpumon_flags
;
1921 uint32_t cpulimits_flags
;
1922 kauth_cred_t my_cred
, target_cred
;
1923 #if CONFIG_LEDGER_INTERVAL_MAX
1924 uint32_t footprint_interval_flags
;
1925 uint64_t interval_max_footprint
;
1926 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
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();
1932 targetp
= proc_find(uap
->pid
);
1935 /* proc_self() can return NULL for an exiting process */
1936 if (targetp
== PROC_NULL
) {
1940 my_cred
= kauth_cred_get();
1941 target_cred
= kauth_cred_proc_ref(targetp
);
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
)) {
1947 kauth_cred_unref(&target_cred
);
1951 switch (uap
->flavor
) {
1952 case RLIMIT_WAKEUPS_MONITOR
:
1953 if ((error
= copyin(uap
->arg
, &wakeupmon_args
, sizeof (wakeupmon_args
))) != 0) {
1956 if ((error
= mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp
->task
, &wakeupmon_args
.wm_flags
,
1957 &wakeupmon_args
.wm_rate
))) != 0) {
1960 error
= copyout(&wakeupmon_args
, uap
->arg
, sizeof (wakeupmon_args
));
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
));
1966 case RLIMIT_THREAD_CPULIMITS
:
1967 cpulimits_flags
= (uint32_t)uap
->arg
; // only need a limited set of bits, pass in void * argument
1969 if (uap
->pid
!= -1) {
1974 uint8_t percent
= 0;
1975 uint32_t ms_refill
= 0;
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) {
1985 ns_refill
= ((uint64_t)ms_refill
) * NSEC_PER_MSEC
;
1987 error
= mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK
, percent
, ns_refill
));
1990 #if CONFIG_LEDGER_INTERVAL_MAX
1991 case RLIMIT_FOOTPRINT_INTERVAL
:
1992 footprint_interval_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
1994 * There is currently only one option for this flavor.
1996 if ((footprint_interval_flags
& FOOTPRINT_INTERVAL_RESET
) == 0) {
2000 interval_max_footprint
= get_task_phys_footprint_interval_max(targetp
->task
, TRUE
);
2002 #endif /* CONFIG_LEDGER_INTERVAL_MAX */
2009 kauth_cred_unref(&target_cred
);
2012 * Return value from this function becomes errno to userland caller.
2018 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
2020 int thread_selfusage(struct proc
*p __unused
, struct thread_selfusage_args
*uap __unused
, uint64_t *retval
)
2024 runtime
= thread_get_runtime_self();
2031 int thread_selfcounts(__unused
struct proc
*p
, __unused
struct thread_selfcounts_args
*uap
, __unused
int *ret_out
)
2035 #endif /* !MONOTONIC */