2 * Copyright (c) 2000-2008 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 <machine/spl.h>
86 #include <sys/mount_internal.h>
87 #include <sys/sysproto.h>
89 #include <security/audit/audit.h>
91 #include <machine/vmparam.h>
93 #include <mach/mach_types.h>
94 #include <mach/time_value.h>
95 #include <mach/task.h>
96 #include <mach/task_info.h>
97 #include <mach/vm_map.h>
98 #include <mach/mach_vm.h>
99 #include <mach/thread_act.h> /* for thread_policy_set( ) */
100 #include <kern/thread.h>
101 #include <kern/policy_internal.h>
103 #include <kern/task.h>
104 #include <kern/clock.h> /* for absolutetime_to_microtime() */
105 #include <netinet/in.h> /* for TRAFFIC_MGT_SO_* */
106 #include <sys/socketvar.h> /* for struct socket */
108 #include <vm/vm_map.h>
110 #include <kern/assert.h>
111 #include <sys/resource.h>
112 #include <sys/priv.h>
113 #include <IOKit/IOBSD.h>
115 int donice(struct proc
*curp
, struct proc
*chgp
, int n
);
116 int dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
);
117 int uthread_get_background_state(uthread_t
);
118 static void do_background_socket(struct proc
*p
, thread_t thread
);
119 static int do_background_thread(thread_t thread
, int priority
);
120 static int do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
121 static int set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
);
122 static int proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
);
123 static int proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
);
124 static int get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
);
125 int proc_pid_rusage(int pid
, int flavor
, user_addr_t buf
, int32_t *retval
);
126 void gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
);
127 int fill_task_rusage(task_t task
, rusage_info_current
*ri
);
128 void fill_task_billed_usage(task_t task
, rusage_info_current
*ri
);
129 int fill_task_io_rusage(task_t task
, rusage_info_current
*ri
);
130 int fill_task_qos_rusage(task_t task
, rusage_info_current
*ri
);
131 static void rusage_info_conversion(rusage_info_t ri_info
, rusage_info_current
*ri_current
, int flavor
);
133 int proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
);
135 rlim_t maxdmap
= MAXDSIZ
; /* XXX */
136 rlim_t maxsmap
= MAXSSIZ
- PAGE_MAX_SIZE
; /* XXX */
139 * Limits on the number of open files per process, and the number
140 * of child processes per process.
142 * Note: would be in kern/subr_param.c in FreeBSD.
144 __private_extern__
int maxfilesperproc
= OPEN_MAX
; /* per-proc open files limit */
146 SYSCTL_INT(_kern
, KERN_MAXPROCPERUID
, maxprocperuid
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
147 &maxprocperuid
, 0, "Maximum processes allowed per userid" );
149 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
150 &maxfilesperproc
, 0, "Maximum files allowed open per process" );
152 /* Args and fn for proc_iteration callback used in setpriority */
153 struct puser_nice_args
{
160 static int puser_donice_callback(proc_t p
, void * arg
);
163 /* Args and fn for proc_iteration callback used in setpriority */
164 struct ppgrp_nice_args
{
170 static int ppgrp_donice_callback(proc_t p
, void * arg
);
173 * Resource controls and accounting.
176 getpriority(struct proc
*curp
, struct getpriority_args
*uap
, int32_t *retval
)
179 int low
= PRIO_MAX
+ 1;
180 kauth_cred_t my_cred
;
184 /* would also test (uap->who < 0), but id_t is unsigned */
185 if (uap
->who
> 0x7fffffff)
188 switch (uap
->which
) {
195 p
= proc_find(uap
->who
);
205 struct pgrp
*pg
= PGRP_NULL
;
208 /* returns the pgrp to ref */
209 pg
= proc_pgrp(curp
);
210 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
) {
213 /* No need for iteration as it is a simple scan */
215 PGMEMBERS_FOREACH(pg
, p
) {
226 uap
->who
= kauth_cred_getuid(kauth_cred_get());
230 for (p
= allproc
.lh_first
; p
!= 0; p
= p
->p_list
.le_next
) {
231 my_cred
= kauth_cred_proc_ref(p
);
232 if (kauth_cred_getuid(my_cred
) == uap
->who
&&
235 kauth_cred_unref(&my_cred
);
242 case PRIO_DARWIN_THREAD
:
243 /* we currently only support the current thread */
247 low
= proc_get_thread_policy(current_thread(), TASK_POLICY_INTERNAL
, TASK_POLICY_DARWIN_BG
);
251 case PRIO_DARWIN_PROCESS
:
255 p
= proc_find(uap
->who
);
261 error
= get_background_proc(curp
, p
, &low
);
269 case PRIO_DARWIN_ROLE
:
273 p
= proc_find(uap
->who
);
279 error
= proc_get_darwin_role(curp
, p
, &low
);
290 if (low
== PRIO_MAX
+ 1)
296 /* call back function used for proc iteration in PRIO_USER */
298 puser_donice_callback(proc_t p
, void * arg
)
301 struct puser_nice_args
* pun
= (struct puser_nice_args
*)arg
;
302 kauth_cred_t my_cred
;
304 my_cred
= kauth_cred_proc_ref(p
);
305 if (kauth_cred_getuid(my_cred
) == pun
->who
) {
306 error
= donice(pun
->curp
, p
, pun
->prio
);
307 if (pun
->errorp
!= NULL
)
308 *pun
->errorp
= error
;
309 if (pun
->foundp
!= NULL
) {
314 kauth_cred_unref(&my_cred
);
316 return(PROC_RETURNED
);
319 /* call back function used for proc iteration in PRIO_PGRP */
321 ppgrp_donice_callback(proc_t p
, void * arg
)
324 struct ppgrp_nice_args
* pun
= (struct ppgrp_nice_args
*)arg
;
327 error
= donice(pun
->curp
, p
, pun
->prio
);
328 if (pun
->errorp
!= NULL
)
329 *pun
->errorp
= error
;
330 if (pun
->foundp
!= NULL
) {
335 return(PROC_RETURNED
);
347 setpriority(struct proc
*curp
, struct setpriority_args
*uap
, int32_t *retval
)
350 int found
= 0, error
= 0;
353 AUDIT_ARG(cmd
, uap
->which
);
354 AUDIT_ARG(owner
, uap
->who
, 0);
355 AUDIT_ARG(value32
, uap
->prio
);
357 /* would also test (uap->who < 0), but id_t is unsigned */
358 if (uap
->who
> 0x7fffffff)
361 switch (uap
->which
) {
367 p
= proc_find(uap
->who
);
372 error
= donice(curp
, p
, uap
->prio
);
379 struct pgrp
*pg
= PGRP_NULL
;
380 struct ppgrp_nice_args ppgrp
;
383 pg
= proc_pgrp(curp
);
384 } else if ((pg
= pgfind(uap
->who
)) == PGRP_NULL
)
388 ppgrp
.prio
= uap
->prio
;
389 ppgrp
.foundp
= &found
;
390 ppgrp
.errorp
= &error
;
392 /* PGRP_DROPREF drops the reference on process group */
393 pgrp_iterate(pg
, PGRP_DROPREF
, ppgrp_donice_callback
, (void *)&ppgrp
, NULL
, NULL
);
399 struct puser_nice_args punice
;
402 uap
->who
= kauth_cred_getuid(kauth_cred_get());
405 punice
.prio
= uap
->prio
;
406 punice
.who
= uap
->who
;
407 punice
.foundp
= &found
;
409 punice
.errorp
= &error
;
410 proc_iterate(PROC_ALLPROCLIST
, puser_donice_callback
, (void *)&punice
, NULL
, NULL
);
415 case PRIO_DARWIN_THREAD
: {
416 /* we currently only support the current thread */
420 error
= do_background_thread(current_thread(), uap
->prio
);
425 case PRIO_DARWIN_PROCESS
: {
429 p
= proc_find(uap
->who
);
435 error
= do_background_proc(curp
, p
, uap
->prio
);
443 case PRIO_DARWIN_GPU
: {
447 p
= proc_find(uap
->who
);
451 error
= set_gpudeny_proc(curp
, p
, uap
->prio
);
458 case PRIO_DARWIN_ROLE
: {
462 p
= proc_find(uap
->who
);
468 error
= proc_set_darwin_role(curp
, p
, uap
->prio
);
481 if (error
== EIDRM
) {
493 * mac_check_proc_sched:???
496 donice(struct proc
*curp
, struct proc
*chgp
, int n
)
500 kauth_cred_t my_cred
;
502 ucred
= kauth_cred_proc_ref(curp
);
503 my_cred
= kauth_cred_proc_ref(chgp
);
505 if (suser(ucred
, NULL
) && kauth_cred_getruid(ucred
) &&
506 kauth_cred_getuid(ucred
) != kauth_cred_getuid(my_cred
) &&
507 kauth_cred_getruid(ucred
) != kauth_cred_getuid(my_cred
)) {
515 if (n
< chgp
->p_nice
&& suser(ucred
, &curp
->p_acflag
)) {
520 error
= mac_proc_check_sched(curp
, chgp
);
527 (void)resetpriority(chgp
);
529 kauth_cred_unref(&ucred
);
530 kauth_cred_unref(&my_cred
);
535 set_gpudeny_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
539 kauth_cred_t target_cred
;
541 ucred
= kauth_cred_get();
542 target_cred
= kauth_cred_proc_ref(targetp
);
544 /* TODO: Entitlement instead of uid check */
546 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
547 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
548 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
553 if (curp
== targetp
) {
559 error
= mac_proc_check_sched(curp
, targetp
);
565 case PRIO_DARWIN_GPU_DENY
:
566 task_set_gpu_denied(proc_task(targetp
), TRUE
);
568 case PRIO_DARWIN_GPU_ALLOW
:
569 task_set_gpu_denied(proc_task(targetp
), FALSE
);
577 kauth_cred_unref(&target_cred
);
583 proc_set_darwin_role(proc_t curp
, proc_t targetp
, int priority
)
588 kauth_cred_t ucred
, target_cred
;
590 ucred
= kauth_cred_get();
591 target_cred
= kauth_cred_proc_ref(targetp
);
593 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
594 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
595 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
596 if (priv_check_cred(ucred
, PRIV_SETPRIORITY_DARWIN_ROLE
, 0) != 0) {
602 if (curp
!= targetp
) {
604 if ((error
= mac_proc_check_sched(curp
, targetp
)))
609 proc_get_darwinbgstate(proc_task(targetp
), &flagsp
);
610 if ((flagsp
& PROC_FLAG_APPLICATION
) != PROC_FLAG_APPLICATION
) {
617 if ((error
= proc_darwin_role_to_task_role(priority
, &role
)))
620 proc_set_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
,
621 TASK_POLICY_ROLE
, role
);
624 kauth_cred_unref(&target_cred
);
629 proc_get_darwin_role(proc_t curp
, proc_t targetp
, int *priority
)
634 kauth_cred_t ucred
, target_cred
;
636 ucred
= kauth_cred_get();
637 target_cred
= kauth_cred_proc_ref(targetp
);
639 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
640 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
641 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
646 if (curp
!= targetp
) {
648 if ((error
= mac_proc_check_sched(curp
, targetp
)))
653 role
= proc_get_task_policy(proc_task(targetp
), TASK_POLICY_ATTRIBUTE
, TASK_POLICY_ROLE
);
655 *priority
= proc_task_role_to_darwin_role(role
);
658 kauth_cred_unref(&target_cred
);
664 get_background_proc(struct proc
*curp
, struct proc
*targetp
, int *priority
)
668 kauth_cred_t ucred
, target_cred
;
670 ucred
= kauth_cred_get();
671 target_cred
= kauth_cred_proc_ref(targetp
);
673 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
674 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
675 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
)) {
680 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
682 *priority
= proc_get_task_policy(current_task(), external
, TASK_POLICY_DARWIN_BG
);
685 kauth_cred_unref(&target_cred
);
690 do_background_proc(struct proc
*curp
, struct proc
*targetp
, int priority
)
697 kauth_cred_t target_cred
;
701 ucred
= kauth_cred_get();
702 target_cred
= kauth_cred_proc_ref(targetp
);
704 if (!kauth_cred_issuser(ucred
) && kauth_cred_getruid(ucred
) &&
705 kauth_cred_getuid(ucred
) != kauth_cred_getuid(target_cred
) &&
706 kauth_cred_getruid(ucred
) != kauth_cred_getuid(target_cred
))
713 error
= mac_proc_check_sched(curp
, targetp
);
718 external
= (curp
== targetp
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
722 enable
= TASK_POLICY_ENABLE
;
724 case PRIO_DARWIN_NONUI
:
725 /* ignored for compatibility */
728 /* TODO: EINVAL if priority != 0 */
729 enable
= TASK_POLICY_DISABLE
;
733 proc_set_task_policy(proc_task(targetp
), external
, TASK_POLICY_DARWIN_BG
, enable
);
736 kauth_cred_unref(&target_cred
);
741 do_background_socket(struct proc
*p
, thread_t thread
)
744 struct filedesc
*fdp
;
750 if (thread
!= THREAD_NULL
)
751 background
= proc_get_effective_thread_policy(thread
, TASK_POLICY_ALL_SOCKETS_BG
);
753 background
= proc_get_effective_task_policy(proc_task(p
), TASK_POLICY_ALL_SOCKETS_BG
);
757 * For PRIO_DARWIN_PROCESS (thread is NULL), simply mark
758 * the sockets with the background flag. There's nothing
759 * to do here for the PRIO_DARWIN_THREAD case.
761 if (thread
== THREAD_NULL
) {
764 for (i
= 0; i
< fdp
->fd_nfiles
; i
++) {
765 struct socket
*sockp
;
767 fp
= fdp
->fd_ofiles
[i
];
768 if (fp
== NULL
|| (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) != 0 ||
769 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
) {
772 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
773 socket_set_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
774 sockp
->so_background_thread
= NULL
;
778 /* disable networking IO throttle.
779 * NOTE - It is a known limitation of the current design that we
780 * could potentially clear TRAFFIC_MGT_SO_BACKGROUND bit for
781 * sockets created by other threads within this process.
784 for ( i
= 0; i
< fdp
->fd_nfiles
; i
++ ) {
785 struct socket
*sockp
;
787 fp
= fdp
->fd_ofiles
[ i
];
788 if ( fp
== NULL
|| (fdp
->fd_ofileflags
[ i
] & UF_RESERVED
) != 0 ||
789 FILEGLOB_DTYPE(fp
->f_fglob
) != DTYPE_SOCKET
) {
792 sockp
= (struct socket
*)fp
->f_fglob
->fg_data
;
793 /* skip if only clearing this thread's sockets */
794 if ((thread
) && (sockp
->so_background_thread
!= thread
)) {
797 socket_clear_traffic_mgt_flags(sockp
, TRAFFIC_MGT_SO_BACKGROUND
);
798 sockp
->so_background_thread
= NULL
;
804 #pragma unused(p, thread)
810 * do_background_thread
812 * Requires: thread reference
815 * EPERM Tried to background while in vfork
816 * XXX - todo - does this need a MACF hook?
819 do_background_thread(thread_t thread
, int priority
)
822 int enable
, external
;
825 ut
= get_bsdthread_info(thread
);
827 /* Backgrounding is unsupported for threads in vfork */
828 if ((ut
->uu_flag
& UT_VFORK
) != 0)
831 /* Backgrounding is unsupported for workq threads */
832 if (thread_is_static_param(thread
)) {
836 /* Not allowed to combine QoS and DARWIN_BG, doing so strips the QoS */
837 if (thread_has_qos_policy(thread
)) {
838 thread_remove_qos_policy(thread
);
842 /* TODO: Fail if someone passes something besides 0 or PRIO_DARWIN_BG */
843 enable
= (priority
== PRIO_DARWIN_BG
) ? TASK_POLICY_ENABLE
: TASK_POLICY_DISABLE
;
844 external
= (current_thread() == thread
) ? TASK_POLICY_INTERNAL
: TASK_POLICY_EXTERNAL
;
846 proc_set_thread_policy(thread
, external
, TASK_POLICY_DARWIN_BG
, enable
);
859 setrlimit(struct proc
*p
, struct setrlimit_args
*uap
, __unused
int32_t *retval
)
864 if ((error
= copyin(uap
->rlp
, (caddr_t
)&alim
,
865 sizeof (struct rlimit
))))
868 return (dosetrlimit(p
, uap
->which
, &alim
));
874 * ENOMEM Cannot copy limit structure
877 * Notes: EINVAL is returned both for invalid arguments, and in the
878 * case that the current usage (e.g. RLIMIT_STACK) is already
879 * in excess of the requested limit.
882 dosetrlimit(struct proc
*p
, u_int which
, struct rlimit
*limp
)
884 struct rlimit
*alimp
;
887 int posix
= (which
& _RLIMIT_POSIX_FLAG
) ? 1 : 0;
889 /* Mask out POSIX flag, saved above */
890 which
&= ~_RLIMIT_POSIX_FLAG
;
892 if (which
>= RLIM_NLIMITS
)
895 alimp
= &p
->p_rlimit
[which
];
896 if (limp
->rlim_cur
> limp
->rlim_max
)
899 if (limp
->rlim_cur
> alimp
->rlim_max
||
900 limp
->rlim_max
> alimp
->rlim_max
)
901 if ((error
= suser(kauth_cred_get(), &p
->p_acflag
))) {
907 if ((error
= proc_limitreplace(p
)) != 0) {
908 proc_limitunblock(p
);
912 alimp
= &p
->p_rlimit
[which
];
917 if (limp
->rlim_cur
== RLIM_INFINITY
) {
918 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
919 timerclear(&p
->p_rlim_cpu
);
922 task_absolutetime_info_data_t tinfo
;
923 mach_msg_type_number_t count
;
924 struct timeval ttv
, tv
;
926 clock_usec_t tv_usec
;
928 count
= TASK_ABSOLUTETIME_INFO_COUNT
;
929 task_info(p
->task
, TASK_ABSOLUTETIME_INFO
,
930 (task_info_t
)&tinfo
, &count
);
931 absolutetime_to_microtime(tinfo
.total_user
+ tinfo
.total_system
,
934 ttv
.tv_usec
= tv_usec
;
936 tv
.tv_sec
= (limp
->rlim_cur
> __INT_MAX__
? __INT_MAX__
: limp
->rlim_cur
);
938 timersub(&tv
, &ttv
, &p
->p_rlim_cpu
);
941 if (timercmp(&p
->p_rlim_cpu
, &tv
, >))
942 task_vtimer_set(p
->task
, TASK_VTIMER_RLIM
);
944 task_vtimer_clear(p
->task
, TASK_VTIMER_RLIM
);
946 timerclear(&p
->p_rlim_cpu
);
954 if (limp
->rlim_cur
> maxdmap
)
955 limp
->rlim_cur
= maxdmap
;
956 if (limp
->rlim_max
> maxdmap
)
957 limp
->rlim_max
= maxdmap
;
961 /* Disallow illegal stack size instead of clipping */
962 if (limp
->rlim_cur
> maxsmap
||
963 limp
->rlim_max
> maxsmap
) {
970 * 4797860 - workaround poorly written installers by
971 * doing previous implementation (< 10.5) when caller
972 * is non-POSIX conforming.
974 if (limp
->rlim_cur
> maxsmap
)
975 limp
->rlim_cur
= maxsmap
;
976 if (limp
->rlim_max
> maxsmap
)
977 limp
->rlim_max
= maxsmap
;
982 * Stack is allocated to the max at exec time with only
983 * "rlim_cur" bytes accessible. If stack limit is going
984 * up make more accessible, if going down make inaccessible.
986 if (limp
->rlim_cur
> alimp
->rlim_cur
) {
991 size
= round_page_64(limp
->rlim_cur
);
992 size
-= round_page_64(alimp
->rlim_cur
);
994 addr
= p
->user_stack
- round_page_64(limp
->rlim_cur
);
995 kr
= mach_vm_protect(current_map(),
997 FALSE
, VM_PROT_DEFAULT
);
998 if (kr
!= KERN_SUCCESS
) {
1002 } else if (limp
->rlim_cur
< alimp
->rlim_cur
) {
1010 * First check if new stack limit would agree
1011 * with current stack usage.
1012 * Get the current thread's stack pointer...
1014 cur_sp
= thread_adjuserstack(current_thread(),
1016 if (cur_sp
<= p
->user_stack
&&
1017 cur_sp
> (p
->user_stack
-
1018 round_page_64(alimp
->rlim_cur
))) {
1019 /* stack pointer is in main stack */
1020 if (cur_sp
<= (p
->user_stack
-
1021 round_page_64(limp
->rlim_cur
))) {
1023 * New limit would cause
1024 * current usage to be invalid:
1031 /* not on the main stack: reject */
1036 size
= round_page_64(alimp
->rlim_cur
);
1037 size
-= round_page_64(limp
->rlim_cur
);
1039 addr
= p
->user_stack
- round_page_64(alimp
->rlim_cur
);
1041 kr
= mach_vm_protect(current_map(),
1043 FALSE
, VM_PROT_NONE
);
1044 if (kr
!= KERN_SUCCESS
) {
1055 * Only root can set the maxfiles limits, as it is
1056 * systemwide resource. If we are expecting POSIX behavior,
1057 * instead of clamping the value, return EINVAL. We do this
1058 * because historically, people have been able to attempt to
1059 * set RLIM_INFINITY to get "whatever the maximum is".
1061 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1062 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1063 limp
->rlim_cur
> (rlim_t
)maxfiles
) {
1068 limp
->rlim_cur
= maxfiles
;
1070 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1071 limp
->rlim_max
> (rlim_t
)maxfiles
)
1072 limp
->rlim_max
= maxfiles
;
1075 if (limp
->rlim_cur
!= alimp
->rlim_cur
&&
1076 limp
->rlim_cur
> (rlim_t
)maxfilesperproc
) {
1081 limp
->rlim_cur
= maxfilesperproc
;
1083 if (limp
->rlim_max
!= alimp
->rlim_max
&&
1084 limp
->rlim_max
> (rlim_t
)maxfilesperproc
)
1085 limp
->rlim_max
= maxfilesperproc
;
1091 * Only root can set to the maxproc limits, as it is
1092 * systemwide resource; all others are limited to
1093 * maxprocperuid (presumably less than maxproc).
1095 if ( kauth_cred_issuser(kauth_cred_get()) ) {
1096 if (limp
->rlim_cur
> (rlim_t
)maxproc
)
1097 limp
->rlim_cur
= maxproc
;
1098 if (limp
->rlim_max
> (rlim_t
)maxproc
)
1099 limp
->rlim_max
= maxproc
;
1102 if (limp
->rlim_cur
> (rlim_t
)maxprocperuid
)
1103 limp
->rlim_cur
= maxprocperuid
;
1104 if (limp
->rlim_max
> (rlim_t
)maxprocperuid
)
1105 limp
->rlim_max
= maxprocperuid
;
1109 case RLIMIT_MEMLOCK
:
1111 * Tell the Mach VM layer about the new limit value.
1114 vm_map_set_user_wire_limit(current_map(), limp
->rlim_cur
);
1123 proc_limitunblock(p
);
1129 getrlimit(struct proc
*p
, struct getrlimit_args
*uap
, __unused
int32_t *retval
)
1134 * Take out flag now in case we need to use it to trigger variant
1137 uap
->which
&= ~_RLIMIT_POSIX_FLAG
;
1139 if (uap
->which
>= RLIM_NLIMITS
)
1141 proc_limitget(p
, uap
->which
, &lim
);
1142 return (copyout((caddr_t
)&lim
,
1143 uap
->rlp
, sizeof (struct rlimit
)));
1147 * Transform the running time and tick information in proc p into user,
1148 * system, and interrupt time usage.
1150 /* No lock on proc is held for this.. */
1152 calcru(struct proc
*p
, struct timeval
*up
, struct timeval
*sp
, struct timeval
*ip
)
1163 mach_task_basic_info_data_t tinfo
;
1164 task_thread_times_info_data_t ttimesinfo
;
1165 task_events_info_data_t teventsinfo
;
1166 mach_msg_type_number_t task_info_count
, task_ttimes_count
;
1167 mach_msg_type_number_t task_events_count
;
1168 struct timeval ut
,st
;
1170 task_info_count
= MACH_TASK_BASIC_INFO_COUNT
;
1171 task_info(task
, MACH_TASK_BASIC_INFO
,
1172 (task_info_t
)&tinfo
, &task_info_count
);
1173 ut
.tv_sec
= tinfo
.user_time
.seconds
;
1174 ut
.tv_usec
= tinfo
.user_time
.microseconds
;
1175 st
.tv_sec
= tinfo
.system_time
.seconds
;
1176 st
.tv_usec
= tinfo
.system_time
.microseconds
;
1177 timeradd(&ut
, up
, up
);
1178 timeradd(&st
, sp
, sp
);
1180 task_ttimes_count
= TASK_THREAD_TIMES_INFO_COUNT
;
1181 task_info(task
, TASK_THREAD_TIMES_INFO
,
1182 (task_info_t
)&ttimesinfo
, &task_ttimes_count
);
1184 ut
.tv_sec
= ttimesinfo
.user_time
.seconds
;
1185 ut
.tv_usec
= ttimesinfo
.user_time
.microseconds
;
1186 st
.tv_sec
= ttimesinfo
.system_time
.seconds
;
1187 st
.tv_usec
= ttimesinfo
.system_time
.microseconds
;
1188 timeradd(&ut
, up
, up
);
1189 timeradd(&st
, sp
, sp
);
1191 task_events_count
= TASK_EVENTS_INFO_COUNT
;
1192 task_info(task
, TASK_EVENTS_INFO
,
1193 (task_info_t
)&teventsinfo
, &task_events_count
);
1196 * No need to lock "p": this does not need to be
1197 * completely consistent, right ?
1199 p
->p_stats
->p_ru
.ru_minflt
= (teventsinfo
.faults
-
1200 teventsinfo
.pageins
);
1201 p
->p_stats
->p_ru
.ru_majflt
= teventsinfo
.pageins
;
1202 p
->p_stats
->p_ru
.ru_nivcsw
= (teventsinfo
.csw
-
1203 p
->p_stats
->p_ru
.ru_nvcsw
);
1204 if (p
->p_stats
->p_ru
.ru_nivcsw
< 0)
1205 p
->p_stats
->p_ru
.ru_nivcsw
= 0;
1207 p
->p_stats
->p_ru
.ru_maxrss
= tinfo
.resident_size_max
;
1211 __private_extern__
void munge_user64_rusage(struct rusage
*a_rusage_p
, struct user64_rusage
*a_user_rusage_p
);
1212 __private_extern__
void munge_user32_rusage(struct rusage
*a_rusage_p
, struct user32_rusage
*a_user_rusage_p
);
1216 getrusage(struct proc
*p
, struct getrusage_args
*uap
, __unused
int32_t *retval
)
1218 struct rusage
*rup
, rubuf
;
1219 struct user64_rusage rubuf64
;
1220 struct user32_rusage rubuf32
;
1221 size_t retsize
= sizeof(rubuf
); /* default: 32 bits */
1222 caddr_t retbuf
= (caddr_t
)&rubuf
; /* default: 32 bits */
1223 struct timeval utime
;
1224 struct timeval stime
;
1229 calcru(p
, &utime
, &stime
, NULL
);
1231 rup
= &p
->p_stats
->p_ru
;
1232 rup
->ru_utime
= utime
;
1233 rup
->ru_stime
= stime
;
1240 case RUSAGE_CHILDREN
:
1242 rup
= &p
->p_stats
->p_cru
;
1250 if (IS_64BIT_PROCESS(p
)) {
1251 retsize
= sizeof(rubuf64
);
1252 retbuf
= (caddr_t
)&rubuf64
;
1253 munge_user64_rusage(&rubuf
, &rubuf64
);
1255 retsize
= sizeof(rubuf32
);
1256 retbuf
= (caddr_t
)&rubuf32
;
1257 munge_user32_rusage(&rubuf
, &rubuf32
);
1260 return (copyout(retbuf
, uap
->rusage
, retsize
));
1264 ruadd(struct rusage
*ru
, struct rusage
*ru2
)
1269 timeradd(&ru
->ru_utime
, &ru2
->ru_utime
, &ru
->ru_utime
);
1270 timeradd(&ru
->ru_stime
, &ru2
->ru_stime
, &ru
->ru_stime
);
1271 if (ru
->ru_maxrss
< ru2
->ru_maxrss
)
1272 ru
->ru_maxrss
= ru2
->ru_maxrss
;
1273 ip
= &ru
->ru_first
; ip2
= &ru2
->ru_first
;
1274 for (i
= &ru
->ru_last
- &ru
->ru_first
; i
>= 0; i
--)
1279 * Add the rusage stats of child in parent.
1281 * It adds rusage statistics of child process and statistics of all its
1282 * children to its parent.
1284 * Note: proc lock of parent should be held while calling this function.
1287 update_rusage_info_child(struct rusage_info_child
*ri
, rusage_info_current
*ri_current
)
1289 ri
->ri_child_user_time
+= (ri_current
->ri_user_time
+
1290 ri_current
->ri_child_user_time
);
1291 ri
->ri_child_system_time
+= (ri_current
->ri_system_time
+
1292 ri_current
->ri_child_system_time
);
1293 ri
->ri_child_pkg_idle_wkups
+= (ri_current
->ri_pkg_idle_wkups
+
1294 ri_current
->ri_child_pkg_idle_wkups
);
1295 ri
->ri_child_interrupt_wkups
+= (ri_current
->ri_interrupt_wkups
+
1296 ri_current
->ri_child_interrupt_wkups
);
1297 ri
->ri_child_pageins
+= (ri_current
->ri_pageins
+
1298 ri_current
->ri_child_pageins
);
1299 ri
->ri_child_elapsed_abstime
+= ((ri_current
->ri_proc_exit_abstime
-
1300 ri_current
->ri_proc_start_abstime
) + ri_current
->ri_child_elapsed_abstime
);
1304 proc_limitget(proc_t p
, int which
, struct rlimit
* limp
)
1307 limp
->rlim_cur
= p
->p_rlimit
[which
].rlim_cur
;
1308 limp
->rlim_max
= p
->p_rlimit
[which
].rlim_max
;
1314 proc_limitdrop(proc_t p
, int exiting
)
1316 struct plimit
* freelim
= NULL
;
1317 struct plimit
* freeoldlim
= NULL
;
1321 if (--p
->p_limit
->pl_refcnt
== 0) {
1322 freelim
= p
->p_limit
;
1325 if ((exiting
!= 0) && (p
->p_olimit
!= NULL
) && (--p
->p_olimit
->pl_refcnt
== 0)) {
1326 freeoldlim
= p
->p_olimit
;
1331 if (freelim
!= NULL
)
1332 FREE_ZONE(freelim
, sizeof *p
->p_limit
, M_PLIMIT
);
1333 if (freeoldlim
!= NULL
)
1334 FREE_ZONE(freeoldlim
, sizeof *p
->p_olimit
, M_PLIMIT
);
1339 proc_limitfork(proc_t parent
, proc_t child
)
1342 child
->p_limit
= parent
->p_limit
;
1343 child
->p_limit
->pl_refcnt
++;
1344 child
->p_olimit
= NULL
;
1349 proc_limitblock(proc_t p
)
1352 while (p
->p_lflag
& P_LLIMCHANGE
) {
1353 p
->p_lflag
|= P_LLIMWAIT
;
1354 msleep(&p
->p_olimit
, &p
->p_mlock
, 0, "proc_limitblock", NULL
);
1356 p
->p_lflag
|= P_LLIMCHANGE
;
1363 proc_limitunblock(proc_t p
)
1366 p
->p_lflag
&= ~P_LLIMCHANGE
;
1367 if (p
->p_lflag
& P_LLIMWAIT
) {
1368 p
->p_lflag
&= ~P_LLIMWAIT
;
1369 wakeup(&p
->p_olimit
);
1374 /* This is called behind serialization provided by proc_limitblock/unlbock */
1376 proc_limitreplace(proc_t p
)
1378 struct plimit
*copy
;
1383 if (p
->p_limit
->pl_refcnt
== 1) {
1390 MALLOC_ZONE(copy
, struct plimit
*,
1391 sizeof(struct plimit
), M_PLIMIT
, M_WAITOK
);
1397 bcopy(p
->p_limit
->pl_rlimit
, copy
->pl_rlimit
,
1398 sizeof(struct rlimit
) * RLIM_NLIMITS
);
1399 copy
->pl_refcnt
= 1;
1400 /* hang on to reference to old till process exits */
1401 p
->p_olimit
= p
->p_limit
;
1411 * Description: System call MUX for use in manipulating I/O policy attributes of the current process or thread
1413 * Parameters: cmd Policy command
1414 * arg Pointer to policy arguments
1416 * Returns: 0 Success
1417 * EINVAL Invalid command or invalid policy arguments
1422 iopolicysys_disk(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1424 iopolicysys_vfs(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
);
1427 iopolicysys(struct proc
*p
, struct iopolicysys_args
*uap
, int32_t *retval
)
1430 struct _iopol_param_t iop_param
;
1432 if ((error
= copyin(uap
->arg
, &iop_param
, sizeof(iop_param
))) != 0)
1435 switch (iop_param
.iop_iotype
) {
1436 case IOPOL_TYPE_DISK
:
1437 error
= iopolicysys_disk(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1438 if (error
== EIDRM
) {
1445 case IOPOL_TYPE_VFS_HFS_CASE_SENSITIVITY
:
1446 error
= iopolicysys_vfs(p
, uap
->cmd
, iop_param
.iop_scope
, iop_param
.iop_policy
, &iop_param
);
1455 /* Individual iotype handlers are expected to update iop_param, if requested with a GET command */
1456 if (uap
->cmd
== IOPOL_CMD_GET
) {
1457 error
= copyout((caddr_t
)&iop_param
, uap
->arg
, sizeof(iop_param
));
1467 iopolicysys_disk(struct proc
*p __unused
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1473 /* Validate scope */
1475 case IOPOL_SCOPE_PROCESS
:
1476 thread
= THREAD_NULL
;
1477 policy_flavor
= TASK_POLICY_IOPOL
;
1480 case IOPOL_SCOPE_THREAD
:
1481 thread
= current_thread();
1482 policy_flavor
= TASK_POLICY_IOPOL
;
1484 /* Not allowed to combine QoS and (non-PASSIVE) IO policy, doing so strips the QoS */
1485 if (cmd
== IOPOL_CMD_SET
&& thread_has_qos_policy(thread
)) {
1491 case IOPOL_THROTTLE
:
1492 case IOPOL_IMPORTANT
:
1493 case IOPOL_STANDARD
:
1494 if (!thread_is_static_param(thread
)) {
1495 thread_remove_qos_policy(thread
);
1497 * This is not an error case, this is to return a marker to user-space that
1498 * we stripped the thread of its QoS class.
1503 /* otherwise, fall through to the error case. */
1511 case IOPOL_SCOPE_DARWIN_BG
:
1512 thread
= THREAD_NULL
;
1513 policy_flavor
= TASK_POLICY_DARWIN_BG_IOPOL
;
1521 /* Validate policy */
1522 if (cmd
== IOPOL_CMD_SET
) {
1525 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1526 /* the current default BG throttle level is UTILITY */
1527 policy
= IOPOL_UTILITY
;
1529 policy
= IOPOL_IMPORTANT
;
1534 case IOPOL_THROTTLE
:
1535 /* These levels are OK */
1537 case IOPOL_IMPORTANT
:
1539 case IOPOL_STANDARD
:
1542 if (scope
== IOPOL_SCOPE_DARWIN_BG
) {
1543 /* These levels are invalid for BG */
1547 /* OK for other scopes */
1556 /* Perform command */
1559 if (thread
!= THREAD_NULL
)
1560 proc_set_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1562 proc_set_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
, policy
);
1565 if (thread
!= THREAD_NULL
)
1566 policy
= proc_get_thread_policy(thread
, TASK_POLICY_INTERNAL
, policy_flavor
);
1568 policy
= proc_get_task_policy(current_task(), TASK_POLICY_INTERNAL
, policy_flavor
);
1569 iop_param
->iop_policy
= policy
;
1572 error
= EINVAL
; /* unknown command */
1581 iopolicysys_vfs(struct proc
*p
, int cmd
, int scope
, int policy
, struct _iopol_param_t
*iop_param
)
1585 /* Validate scope */
1587 case IOPOL_SCOPE_PROCESS
:
1588 /* Only process OK */
1595 /* Validate policy */
1596 if (cmd
== IOPOL_CMD_SET
) {
1598 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1600 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1601 /* These policies are OK */
1609 /* Perform command */
1612 if (0 == kauth_cred_issuser(kauth_cred_get())) {
1613 /* If it's a non-root process, it needs to have the entitlement to set the policy */
1614 boolean_t entitled
= FALSE
;
1615 entitled
= IOTaskHasEntitlement(current_task(), "com.apple.private.iopol.case_sensitivity");
1623 case IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
:
1624 OSBitAndAtomic16(~((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
), &p
->p_vfs_iopolicy
);
1626 case IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
:
1627 OSBitOrAtomic16((uint32_t)P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
, &p
->p_vfs_iopolicy
);
1636 iop_param
->iop_policy
= (p
->p_vfs_iopolicy
& P_VFS_IOPOLICY_FORCE_HFS_CASE_SENSITIVITY
)
1637 ? IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE
1638 : IOPOL_VFS_HFS_CASE_SENSITIVITY_DEFAULT
;
1641 error
= EINVAL
; /* unknown command */
1649 /* BSD call back function for task_policy networking changes */
1651 proc_apply_task_networkbg(void * bsd_info
, thread_t thread
)
1653 assert(bsd_info
!= PROC_NULL
);
1655 pid_t pid
= proc_pid((proc_t
)bsd_info
);
1657 proc_t p
= proc_find(pid
);
1659 if (p
!= PROC_NULL
) {
1660 assert(p
== (proc_t
)bsd_info
);
1662 do_background_socket(p
, thread
);
1668 gather_rusage_info(proc_t p
, rusage_info_current
*ru
, int flavor
)
1670 struct rusage_info_child
*ri_child
;
1672 assert(p
->p_stats
!= NULL
);
1675 case RUSAGE_INFO_V3
:
1676 fill_task_qos_rusage(p
->task
, ru
);
1677 fill_task_billed_usage(p
->task
, ru
);
1680 case RUSAGE_INFO_V2
:
1681 fill_task_io_rusage(p
->task
, ru
);
1684 case RUSAGE_INFO_V1
:
1686 * p->p_stats->ri_child statistics are protected under proc lock.
1690 ri_child
= &(p
->p_stats
->ri_child
);
1691 ru
->ri_child_user_time
= ri_child
->ri_child_user_time
;
1692 ru
->ri_child_system_time
= ri_child
->ri_child_system_time
;
1693 ru
->ri_child_pkg_idle_wkups
= ri_child
->ri_child_pkg_idle_wkups
;
1694 ru
->ri_child_interrupt_wkups
= ri_child
->ri_child_interrupt_wkups
;
1695 ru
->ri_child_pageins
= ri_child
->ri_child_pageins
;
1696 ru
->ri_child_elapsed_abstime
= ri_child
->ri_child_elapsed_abstime
;
1701 case RUSAGE_INFO_V0
:
1702 proc_getexecutableuuid(p
, (unsigned char *)&ru
->ri_uuid
, sizeof (ru
->ri_uuid
));
1703 fill_task_rusage(p
->task
, ru
);
1704 ru
->ri_proc_start_abstime
= p
->p_stats
->ps_start
;
1709 rusage_info_conversion(rusage_info_t ri_info
, rusage_info_current
*ri_current
, int flavor
)
1711 struct rusage_info_v0
*ri_v0
;
1712 struct rusage_info_v1
*ri_v1
;
1713 struct rusage_info_v2
*ri_v2
;
1717 case RUSAGE_INFO_V2
:
1718 ri_v2
= (struct rusage_info_v2
*)ri_info
;
1719 ri_v2
->ri_diskio_bytesread
= ri_current
->ri_diskio_bytesread
;
1720 ri_v2
->ri_diskio_byteswritten
= ri_current
->ri_diskio_byteswritten
;
1723 case RUSAGE_INFO_V1
:
1724 ri_v1
= (struct rusage_info_v1
*)ri_info
;
1725 ri_v1
->ri_child_user_time
= ri_current
->ri_child_user_time
;
1726 ri_v1
->ri_child_system_time
= ri_current
->ri_child_system_time
;
1727 ri_v1
->ri_child_pkg_idle_wkups
= ri_current
->ri_child_pkg_idle_wkups
;
1728 ri_v1
->ri_child_interrupt_wkups
= ri_current
->ri_child_interrupt_wkups
;
1729 ri_v1
->ri_child_pageins
= ri_current
->ri_child_pageins
;
1730 ri_v1
->ri_child_elapsed_abstime
= ri_current
->ri_child_elapsed_abstime
;
1733 case RUSAGE_INFO_V0
:
1734 ri_v0
= (struct rusage_info_v0
*)ri_info
;
1735 memcpy(&ri_v0
->ri_uuid
[0], &ri_current
->ri_uuid
[0], sizeof(ri_v0
->ri_uuid
));
1736 ri_v0
->ri_user_time
= ri_current
->ri_user_time
;
1737 ri_v0
->ri_system_time
= ri_current
->ri_system_time
;
1738 ri_v0
->ri_pkg_idle_wkups
= ri_current
->ri_pkg_idle_wkups
;
1739 ri_v0
->ri_interrupt_wkups
= ri_current
->ri_interrupt_wkups
;
1740 ri_v0
->ri_pageins
= ri_current
->ri_pageins
;
1741 ri_v0
->ri_wired_size
= ri_current
->ri_wired_size
;
1742 ri_v0
->ri_resident_size
= ri_current
->ri_resident_size
;
1743 ri_v0
->ri_phys_footprint
= ri_current
->ri_phys_footprint
;
1744 ri_v0
->ri_proc_start_abstime
= ri_current
->ri_proc_start_abstime
;
1745 ri_v0
->ri_proc_exit_abstime
= ri_current
->ri_proc_exit_abstime
;
1756 proc_get_rusage(proc_t p
, int flavor
, user_addr_t buffer
, __unused
int is_zombie
)
1758 struct rusage_info_v0 ri_v0
;
1759 struct rusage_info_v1 ri_v1
;
1760 struct rusage_info_v2 ri_v2
;
1761 struct rusage_info_v3 ri_v3
;
1763 rusage_info_current ri_current
;
1768 case RUSAGE_INFO_V0
:
1770 * If task is still alive, collect info from the live task itself.
1771 * Otherwise, look to the cached info in the zombie proc.
1773 if (p
->p_ru
== NULL
) {
1774 gather_rusage_info(p
, &ri_current
, flavor
);
1775 ri_current
.ri_proc_exit_abstime
= 0;
1776 rusage_info_conversion(&ri_v0
, &ri_current
, flavor
);
1778 rusage_info_conversion(&ri_v0
, &p
->p_ru
->ri
, flavor
);
1780 error
= copyout(&ri_v0
, buffer
, sizeof (ri_v0
));
1783 case RUSAGE_INFO_V1
:
1785 * If task is still alive, collect info from the live task itself.
1786 * Otherwise, look to the cached info in the zombie proc.
1788 if (p
->p_ru
== NULL
) {
1789 gather_rusage_info(p
, &ri_current
, flavor
);
1790 ri_current
.ri_proc_exit_abstime
= 0;
1791 rusage_info_conversion(&ri_v1
, &ri_current
, flavor
);
1793 rusage_info_conversion(&ri_v1
, &p
->p_ru
->ri
, flavor
);
1795 error
= copyout(&ri_v1
, buffer
, sizeof (ri_v1
));
1798 case RUSAGE_INFO_V2
:
1800 * If task is still alive, collect info from the live task itself.
1801 * Otherwise, look to the cached info in the zombie proc.
1803 if (p
->p_ru
== NULL
) {
1804 gather_rusage_info(p
, &ri_current
, flavor
);
1805 ri_current
.ri_proc_exit_abstime
= 0;
1806 rusage_info_conversion(&ri_v2
, &ri_current
, flavor
);
1808 rusage_info_conversion(&ri_v2
, &p
->p_ru
->ri
, flavor
);
1810 error
= copyout(&ri_v2
, buffer
, sizeof (ri_v2
));
1813 case RUSAGE_INFO_V3
:
1815 * If task is still alive, collect info from the live task itself.
1816 * Otherwise, look to the cached info in the zombie proc.
1818 if (p
->p_ru
== NULL
) {
1819 gather_rusage_info(p
, &ri_v3
, flavor
);
1820 ri_v3
.ri_proc_exit_abstime
= 0;
1822 ri_v3
= p
->p_ru
->ri
;
1824 error
= copyout(&ri_v3
, buffer
, sizeof (ri_v3
));
1836 mach_to_bsd_rv(int mach_rv
)
1844 case KERN_INVALID_ARGUMENT
:
1848 panic("unknown error %#x", mach_rv
);
1855 * Resource limit controls
1857 * uap->flavor available flavors:
1859 * RLIMIT_WAKEUPS_MONITOR
1862 proc_rlimit_control(__unused
struct proc
*p
, struct proc_rlimit_control_args
*uap
, __unused
int32_t *retval
)
1866 struct proc_rlimit_control_wakeupmon wakeupmon_args
;
1867 uint32_t cpumon_flags
;
1868 uint32_t cpulimits_flags
;
1869 kauth_cred_t my_cred
, target_cred
;
1871 /* -1 implicitly means our own process (perhaps even the current thread for per-thread attributes) */
1872 if (uap
->pid
== -1) {
1873 targetp
= proc_self();
1875 targetp
= proc_find(uap
->pid
);
1878 /* proc_self() can return NULL for an exiting process */
1879 if (targetp
== PROC_NULL
) {
1883 my_cred
= kauth_cred_get();
1884 target_cred
= kauth_cred_proc_ref(targetp
);
1886 if (!kauth_cred_issuser(my_cred
) && kauth_cred_getruid(my_cred
) &&
1887 kauth_cred_getuid(my_cred
) != kauth_cred_getuid(target_cred
) &&
1888 kauth_cred_getruid(my_cred
) != kauth_cred_getuid(target_cred
)) {
1890 kauth_cred_unref(&target_cred
);
1894 switch (uap
->flavor
) {
1895 case RLIMIT_WAKEUPS_MONITOR
:
1896 if ((error
= copyin(uap
->arg
, &wakeupmon_args
, sizeof (wakeupmon_args
))) != 0) {
1899 if ((error
= mach_to_bsd_rv(task_wakeups_monitor_ctl(targetp
->task
, &wakeupmon_args
.wm_flags
,
1900 &wakeupmon_args
.wm_rate
))) != 0) {
1903 error
= copyout(&wakeupmon_args
, uap
->arg
, sizeof (wakeupmon_args
));
1905 case RLIMIT_CPU_USAGE_MONITOR
:
1906 cpumon_flags
= uap
->arg
; // XXX temporarily stashing flags in argp (12592127)
1907 error
= mach_to_bsd_rv(task_cpu_usage_monitor_ctl(targetp
->task
, &cpumon_flags
));
1909 case RLIMIT_THREAD_CPULIMITS
:
1910 cpulimits_flags
= (uint32_t)uap
->arg
; // only need a limited set of bits, pass in void * argument
1912 if (uap
->pid
!= -1) {
1917 uint8_t percent
= 0;
1918 uint32_t ms_refill
= 0;
1921 percent
= (uint8_t)(cpulimits_flags
& 0xffU
); /* low 8 bits for percent */
1922 ms_refill
= (cpulimits_flags
>> 8) & 0xffffff; /* next 24 bits represent ms refill value */
1923 if (percent
>= 100) {
1928 ns_refill
= ((uint64_t)ms_refill
) * NSEC_PER_MSEC
;
1930 error
= mach_to_bsd_rv(thread_set_cpulimit(THREAD_CPULIMIT_BLOCK
, percent
, ns_refill
));
1938 kauth_cred_unref(&target_cred
);
1941 * Return value from this function becomes errno to userland caller.
1947 * Return the current amount of CPU consumed by this thread (in either user or kernel mode)
1949 int thread_selfusage(struct proc
*p __unused
, struct thread_selfusage_args
*uap __unused
, uint64_t *retval
)
1953 runtime
= thread_get_runtime_self();