2 * Copyright (c) 2006-2018 Apple Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/cdefs.h>
30 #include <sys/errno.h>
31 #include <sys/msgbuf.h>
32 #include <sys/resource.h>
33 #include <sys/process_policy.h>
34 #include <sys/event.h>
35 #include <mach/message.h>
37 #include "libproc_internal.h"
39 int __proc_info(int callnum
, int pid
, int flavor
, uint64_t arg
, void * buffer
, int buffersize
);
40 __private_extern__
int proc_setthreadname(void * buffer
, int buffersize
);
41 int __process_policy(int scope
, int action
, int policy
, int policy_subtype
, proc_policy_attribute_t
* attrp
, pid_t target_pid
, uint64_t target_threadid
);
42 int proc_rlimit_control(pid_t pid
, int flavor
, void *arg
);
45 proc_listpids(uint32_t type
, uint32_t typeinfo
, void *buffer
, int buffersize
)
49 if ((type
>= PROC_ALL_PIDS
) || (type
<= PROC_PPID_ONLY
)) {
50 if ((retval
= __proc_info(PROC_INFO_CALL_LISTPIDS
, type
, typeinfo
, (uint64_t)0, buffer
, buffersize
)) == -1) {
62 proc_listallpids(void * buffer
, int buffersize
)
65 numpids
= proc_listpids(PROC_ALL_PIDS
, (uint32_t)0, buffer
, buffersize
);
70 return numpids
/ sizeof(int);
75 proc_listpgrppids(pid_t pgrpid
, void * buffer
, int buffersize
)
78 numpids
= proc_listpids(PROC_PGRP_ONLY
, (uint32_t)pgrpid
, buffer
, buffersize
);
82 return numpids
/ sizeof(int);
87 proc_listchildpids(pid_t ppid
, void * buffer
, int buffersize
)
90 numpids
= proc_listpids(PROC_PPID_ONLY
, (uint32_t)ppid
, buffer
, buffersize
);
94 return numpids
/ sizeof(int);
100 proc_pidinfo(int pid
, int flavor
, uint64_t arg
, void *buffer
, int buffersize
)
104 if ((retval
= __proc_info(PROC_INFO_CALL_PIDINFO
, pid
, flavor
, arg
, buffer
, buffersize
)) == -1) {
113 proc_pidoriginatorinfo(int flavor
, void *buffer
, int buffersize
)
117 if ((retval
= __proc_info(PROC_INFO_CALL_PIDORIGINATORINFO
, getpid(), flavor
, 0, buffer
, buffersize
)) == -1) {
125 proc_listcoalitions(int flavor
, int coaltype
, void *buffer
, int buffersize
)
129 if ((retval
= __proc_info(PROC_INFO_CALL_LISTCOALITIONS
, flavor
, coaltype
, 0, buffer
, buffersize
)) == -1) {
137 proc_pid_rusage(int pid
, int flavor
, rusage_info_t
*buffer
)
139 return __proc_info(PROC_INFO_CALL_PIDRUSAGE
, pid
, flavor
, 0, buffer
, 0);
143 proc_setthread_cpupercent(uint8_t percentage
, uint32_t ms_refill
)
147 /* Pack percentage and refill into a 32-bit number to match existing kernel implementation */
148 if ((percentage
>= 100) || (ms_refill
& ~0xffffffU
)) {
153 arg
= ((ms_refill
<< 8) | percentage
);
155 return proc_rlimit_control(-1, RLIMIT_THREAD_CPULIMITS
, (void *)(uintptr_t)arg
);
159 proc_pidfdinfo(int pid
, int fd
, int flavor
, void * buffer
, int buffersize
)
163 if ((retval
= __proc_info(PROC_INFO_CALL_PIDFDINFO
, pid
, flavor
, (uint64_t)fd
, buffer
, buffersize
)) == -1) {
172 proc_pidfileportinfo(int pid
, uint32_t fileport
, int flavor
, void *buffer
, int buffersize
)
176 if ((retval
= __proc_info(PROC_INFO_CALL_PIDFILEPORTINFO
, pid
, flavor
, (uint64_t)fileport
, buffer
, buffersize
)) == -1) {
183 proc_piddynkqueueinfo(int pid
, int flavor
, kqueue_id_t kq_id
, void *buffer
, int buffersize
)
187 if ((ret
= __proc_info(PROC_INFO_CALL_PIDDYNKQUEUEINFO
, pid
, flavor
, (uint64_t)kq_id
, buffer
, buffersize
)) == -1) {
195 proc_udata_info(int pid
, int flavor
, void *buffer
, int buffersize
)
197 return __proc_info(PROC_INFO_CALL_UDATA_INFO
, pid
, flavor
, 0, buffer
, buffersize
);
201 proc_name(int pid
, void * buffer
, uint32_t buffersize
)
204 struct proc_bsdinfo pbsd
;
207 if (buffersize
< sizeof(pbsd
.pbi_name
)) {
212 retval
= proc_pidinfo(pid
, PROC_PIDTBSDINFO
, (uint64_t)0, &pbsd
, sizeof(struct proc_bsdinfo
));
214 if (pbsd
.pbi_name
[0]) {
215 bcopy(&pbsd
.pbi_name
, buffer
, sizeof(pbsd
.pbi_name
));
217 bcopy(&pbsd
.pbi_comm
, buffer
, sizeof(pbsd
.pbi_comm
));
219 len
= (int)strlen(buffer
);
226 proc_regionfilename(int pid
, uint64_t address
, void * buffer
, uint32_t buffersize
)
229 struct proc_regionwithpathinfo reginfo
;
231 if (buffersize
< MAXPATHLEN
) {
236 retval
= proc_pidinfo(pid
, PROC_PIDREGIONPATHINFO2
, (uint64_t)address
, ®info
, sizeof(struct proc_regionwithpathinfo
));
238 return (int)(strlcpy(buffer
, reginfo
.prp_vip
.vip_path
, MAXPATHLEN
));
244 proc_kmsgbuf(void * buffer
, uint32_t buffersize
)
248 if ((retval
= __proc_info(PROC_INFO_CALL_KERNMSGBUF
, 0, 0, (uint64_t)0, buffer
, buffersize
)) == -1) {
255 proc_pidpath(int pid
, void * buffer
, uint32_t buffersize
)
259 if (buffersize
< PROC_PIDPATHINFO_SIZE
) {
263 if (buffersize
> PROC_PIDPATHINFO_MAXSIZE
) {
268 retval
= __proc_info(PROC_INFO_CALL_PIDINFO
, pid
, PROC_PIDPATHINFO
, (uint64_t)0, buffer
, buffersize
);
270 len
= (int)strlen(buffer
);
278 proc_libversion(int *major
, int * minor
)
290 proc_setpcontrol(const int control
)
294 if (control
< PROC_SETPC_NONE
|| control
> PROC_SETPC_TERMINATE
) {
298 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_PCONTROL
, (uint64_t)control
, NULL
, 0)) == -1) {
306 __private_extern__
int
307 proc_setthreadname(void * buffer
, int buffersize
)
311 retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_THREADNAME
, (uint64_t)0, buffer
, buffersize
);
321 proc_track_dirty(pid_t pid
, uint32_t flags
)
323 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_TRACK
, flags
, NULL
, 0) == -1) {
331 proc_set_dirty(pid_t pid
, bool dirty
)
333 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_SET
, dirty
, NULL
, 0) == -1) {
341 proc_get_dirty(pid_t pid
, uint32_t *flags
)
349 retval
= __proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_GET
, 0, NULL
, 0);
360 proc_clear_dirty(pid_t pid
, uint32_t flags
)
362 if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL
, pid
, PROC_DIRTYCONTROL_CLEAR
, flags
, NULL
, 0) == -1) {
370 proc_terminate(pid_t pid
, int *sig
)
378 retval
= __proc_info(PROC_INFO_CALL_TERMINATE
, pid
, 0, 0, NULL
, 0);
389 * XXX the _fatal() variant both checks for an existing monitor
390 * (with important policy effects on first party background apps)
391 * and validates inputs.
394 proc_set_cpumon_params(pid_t pid
, int percentage
, int interval
)
396 proc_policy_cpuusage_attr_t attr
;
398 /* no argument validation ...
399 * task_set_cpuusage() ignores 0 values and squashes negative
400 * values into uint32_t.
403 attr
.ppattr_cpu_attr
= PROC_POLICY_RSRCACT_NOTIFY_EXC
;
404 attr
.ppattr_cpu_percentage
= percentage
;
405 attr
.ppattr_cpu_attr_interval
= (uint64_t)interval
;
406 attr
.ppattr_cpu_attr_deadline
= 0;
408 return __process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_SET
, PROC_POLICY_RESOURCE_USAGE
,
409 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0);
413 proc_get_cpumon_params(pid_t pid
, int *percentage
, int *interval
)
415 proc_policy_cpuusage_attr_t attr
;
418 ret
= __process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_GET
, PROC_POLICY_RESOURCE_USAGE
,
419 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0);
421 if ((ret
== 0) && (attr
.ppattr_cpu_attr
== PROC_POLICY_RSRCACT_NOTIFY_EXC
)) {
422 *percentage
= attr
.ppattr_cpu_percentage
;
423 *interval
= (int)attr
.ppattr_cpu_attr_interval
;
433 proc_set_cpumon_defaults(pid_t pid
)
435 proc_policy_cpuusage_attr_t attr
;
437 attr
.ppattr_cpu_attr
= PROC_POLICY_RSRCACT_NOTIFY_EXC
;
438 attr
.ppattr_cpu_percentage
= PROC_POLICY_CPUMON_DEFAULTS
;
439 attr
.ppattr_cpu_attr_interval
= 0;
440 attr
.ppattr_cpu_attr_deadline
= 0;
442 return __process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_SET
, PROC_POLICY_RESOURCE_USAGE
,
443 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0);
447 proc_resume_cpumon(pid_t pid
)
449 return __process_policy(PROC_POLICY_SCOPE_PROCESS
,
450 PROC_POLICY_ACTION_ENABLE
,
451 PROC_POLICY_RESOURCE_USAGE
,
452 PROC_POLICY_RUSAGE_CPU
,
457 proc_disable_cpumon(pid_t pid
)
459 proc_policy_cpuusage_attr_t attr
;
461 attr
.ppattr_cpu_attr
= PROC_POLICY_RSRCACT_NOTIFY_EXC
;
462 attr
.ppattr_cpu_percentage
= PROC_POLICY_CPUMON_DISABLE
;
463 attr
.ppattr_cpu_attr_interval
= 0;
464 attr
.ppattr_cpu_attr_deadline
= 0;
466 return __process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_SET
, PROC_POLICY_RESOURCE_USAGE
,
467 PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, 0);
472 * Turn on the CPU usage monitor using the supplied parameters, and make
473 * violations of the monitor fatal.
475 * Returns: 0 on success;
476 * -1 on failure and sets errno
479 proc_set_cpumon_params_fatal(pid_t pid
, int percentage
, int interval
)
481 int current_percentage
= 0;
482 int current_interval
= 0; /* intervals are in seconds */
485 if ((percentage
<= 0) || (interval
<= 0)) {
491 * Do a simple query to see if CPU monitoring is
492 * already active. If either the percentage or the
493 * interval is nonzero, then CPU monitoring is
494 * already in use for this process.
496 * XXX: need set...() and set..fatal() to behave similarly.
497 * Currently, this check prevents 1st party apps (which get a
498 * default non-fatal monitor) not to get a fatal monitor.
500 (void)proc_get_cpumon_params(pid
, ¤t_percentage
, ¤t_interval
);
501 if (current_percentage
|| current_interval
) {
503 * The CPU monitor appears to be active.
504 * We choose not to disturb those settings.
510 if ((ret
= proc_set_cpumon_params(pid
, percentage
, interval
)) != 0) {
511 /* Failed to activate the CPU monitor */
515 if ((ret
= proc_rlimit_control(pid
, RLIMIT_CPU_USAGE_MONITOR
, (void *)(uintptr_t)CPUMON_MAKE_FATAL
)) != 0) {
516 /* Failed to set termination, back out the CPU monitor settings. */
517 (void)proc_disable_cpumon(pid
);
524 proc_set_wakemon_params(pid_t pid
, int rate_hz
, int flags __unused
)
526 struct proc_rlimit_control_wakeupmon params
;
528 params
.wm_flags
= WAKEMON_ENABLE
;
529 params
.wm_rate
= rate_hz
;
531 return proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
);
534 #ifndef WAKEMON_GET_PARAMS
535 #define WAKEMON_GET_PARAMS 0x4
536 #define WAKEMON_SET_DEFAULTS 0x8
540 proc_get_wakemon_params(pid_t pid
, int *rate_hz
, int *flags
)
542 struct proc_rlimit_control_wakeupmon params
;
545 params
.wm_flags
= WAKEMON_GET_PARAMS
;
547 if ((error
= proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
)) != 0) {
551 *rate_hz
= params
.wm_rate
;
552 *flags
= params
.wm_flags
;
558 proc_set_wakemon_defaults(pid_t pid
)
560 struct proc_rlimit_control_wakeupmon params
;
562 params
.wm_flags
= WAKEMON_ENABLE
| WAKEMON_SET_DEFAULTS
;
565 return proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
);
569 proc_disable_wakemon(pid_t pid
)
571 struct proc_rlimit_control_wakeupmon params
;
573 params
.wm_flags
= WAKEMON_DISABLE
;
576 return proc_rlimit_control(pid
, RLIMIT_WAKEUPS_MONITOR
, ¶ms
);
580 proc_list_uptrs(int pid
, uint64_t *buf
, uint32_t bufsz
)
582 return __proc_info(PROC_INFO_CALL_PIDINFO
, pid
, PROC_PIDLISTUPTRS
, 0,
587 proc_list_dynkqueueids(int pid
, kqueue_id_t
*buf
, uint32_t bufsz
)
589 return __proc_info(PROC_INFO_CALL_PIDINFO
, pid
, PROC_PIDLISTDYNKQUEUES
, 0,
595 proc_setcpu_percentage(pid_t pid
, int action
, int percentage
)
597 proc_policy_cpuusage_attr_t attr
;
599 bzero(&attr
, sizeof(proc_policy_cpuusage_attr_t
));
600 attr
.ppattr_cpu_attr
= action
;
601 attr
.ppattr_cpu_percentage
= percentage
;
602 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_APPLY
, PROC_POLICY_RESOURCE_USAGE
, PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, (uint64_t)0) != -1) {
610 proc_reset_footprint_interval(pid_t pid
)
612 return proc_rlimit_control(pid
, RLIMIT_FOOTPRINT_INTERVAL
, (void *)(uintptr_t)FOOTPRINT_INTERVAL_RESET
);
616 proc_clear_cpulimits(pid_t pid
)
618 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_RESTORE
, PROC_POLICY_RESOURCE_USAGE
, PROC_POLICY_RUSAGE_CPU
, NULL
, pid
, (uint64_t)0) != -1) {
625 #if TARGET_OS_EMBEDDED
628 proc_setcpu_deadline(pid_t pid
, int action
, uint64_t deadline
)
630 proc_policy_cpuusage_attr_t attr
;
632 bzero(&attr
, sizeof(proc_policy_cpuusage_attr_t
));
633 attr
.ppattr_cpu_attr
= action
;
634 attr
.ppattr_cpu_attr_deadline
= deadline
;
635 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_APPLY
, PROC_POLICY_RESOURCE_USAGE
, PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, (uint64_t)0) != -1) {
643 proc_setcpu_percentage_withdeadline(pid_t pid
, int action
, int percentage
, uint64_t deadline
)
645 proc_policy_cpuusage_attr_t attr
;
647 bzero(&attr
, sizeof(proc_policy_cpuusage_attr_t
));
648 attr
.ppattr_cpu_attr
= action
;
649 attr
.ppattr_cpu_percentage
= percentage
;
650 attr
.ppattr_cpu_attr_deadline
= deadline
;
651 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_APPLY
, PROC_POLICY_RESOURCE_USAGE
, PROC_POLICY_RUSAGE_CPU
, (proc_policy_attribute_t
*)&attr
, pid
, (uint64_t)0) != -1) {
659 proc_appstate(int pid
, int * appstatep
)
663 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_GET
, PROC_POLICY_APP_LIFECYCLE
, PROC_POLICY_APPLIFE_STATE
, (proc_policy_attribute_t
*)&state
, pid
, (uint64_t)0) != -1) {
664 if (appstatep
!= NULL
) {
674 proc_setappstate(int pid
, int appstate
)
676 int state
= appstate
;
679 case PROC_APPSTATE_NONE
:
680 case PROC_APPSTATE_ACTIVE
:
681 case PROC_APPSTATE_INACTIVE
:
682 case PROC_APPSTATE_BACKGROUND
:
683 case PROC_APPSTATE_NONUI
:
688 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_APPLY
, PROC_POLICY_APP_LIFECYCLE
, PROC_POLICY_APPLIFE_STATE
, (proc_policy_attribute_t
*)&state
, pid
, (uint64_t)0) != -1) {
696 proc_devstatusnotify(int devicestatus
)
698 int state
= devicestatus
;
700 switch (devicestatus
) {
701 case PROC_DEVSTATUS_SHORTTERM
:
702 case PROC_DEVSTATUS_LONGTERM
:
708 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_APPLY
, PROC_POLICY_APP_LIFECYCLE
, PROC_POLICY_APPLIFE_DEVSTATUS
, (proc_policy_attribute_t
*)&state
, getpid(), (uint64_t)0) != -1) {
716 proc_pidbind(int pid
, uint64_t threadid
, int bind
)
722 case PROC_PIDBIND_CLEAR
:
723 passpid
= getpid(); /* ignore pid on clear */
725 case PROC_PIDBIND_SET
:
730 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_APPLY
, PROC_POLICY_APP_LIFECYCLE
, PROC_POLICY_APPLIFE_PIDBIND
, (proc_policy_attribute_t
*)&state
, passpid
, threadid
) != -1) {
738 proc_can_use_foreground_hw(int pid
, uint32_t *reason
)
740 return __proc_info(PROC_INFO_CALL_CANUSEFGHW
, pid
, 0, 0, reason
, sizeof(*reason
));
742 #endif /* TARGET_OS_EMBEDDED */
745 /* Donate importance to adaptive processes from this process */
747 proc_donate_importance_boost()
751 #if TARGET_OS_EMBEDDED
752 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
753 PROC_POLICY_ACTION_ENABLE
,
755 PROC_POLICY_IOS_DONATEIMP
,
756 NULL
, getpid(), (uint64_t)0);
757 #else /* TARGET_OS_EMBEDDED */
758 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
759 PROC_POLICY_ACTION_SET
,
761 PROC_POLICY_IMP_DONATION
,
763 #endif /* TARGET_OS_EMBEDDED */
772 static __attribute__((noinline
)) void
773 proc_importance_bad_assertion(char *reason
)
779 * Use the address of these variables as the token. This way, they can be
780 * printed in the debugger as useful names.
782 uint64_t important_boost_assertion_token
= 0xfafafafafafafafa;
783 uint64_t normal_boost_assertion_token
= 0xfbfbfbfbfbfbfbfb;
784 uint64_t non_boost_assertion_token
= 0xfcfcfcfcfcfcfcfc;
785 uint64_t denap_boost_assertion_token
= 0xfdfdfdfdfdfdfdfd;
788 * Accept the boost on a message, or request another boost assertion
789 * if we have already accepted the implicit boost for this message.
791 * Returns EOVERFLOW if an attempt is made to take an extra assertion when not boosted.
793 * Returns EIO if the message was not a boosting message.
794 * TODO: Return a 'non-boost' token instead.
797 proc_importance_assertion_begin_with_msg(mach_msg_header_t
*msg
,
798 __unused mach_msg_trailer_t
*trailer
,
799 uint64_t *assertion_token
)
803 if (assertion_token
== NULL
) {
807 #define LEGACYBOOSTMASK (MACH_MSGH_BITS_VOUCHER_MASK | MACH_MSGH_BITS_RAISEIMP)
808 #define LEGACYBOOSTED(m) (((m)->msgh_bits & LEGACYBOOSTMASK) == MACH_MSGH_BITS_RAISEIMP)
810 /* Is this a legacy boosted message? */
811 if (LEGACYBOOSTED(msg
)) {
813 * Have we accepted the implicit boost for this message yet?
814 * If we haven't accepted it yet, no need to call into kernel.
816 if ((msg
->msgh_bits
& MACH_MSGH_BITS_IMPHOLDASRT
) == 0) {
817 msg
->msgh_bits
|= MACH_MSGH_BITS_IMPHOLDASRT
;
818 *assertion_token
= (uint64_t) &important_boost_assertion_token
;
822 /* Request an additional boost count */
823 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
824 PROC_POLICY_ACTION_HOLD
,
826 PROC_POLICY_IMP_IMPORTANT
,
829 *assertion_token
= (uint64_t) &important_boost_assertion_token
;
831 } else if (errno
== EOVERFLOW
) {
832 proc_importance_bad_assertion("Attempted to take assertion while not boosted");
844 * Drop a boost assertion.
845 * Returns EOVERFLOW on boost assertion underflow.
848 proc_importance_assertion_complete(uint64_t assertion_token
)
852 if (assertion_token
== 0) {
856 if (assertion_token
== (uint64_t) &important_boost_assertion_token
) {
857 rval
= __process_policy(PROC_POLICY_SCOPE_PROCESS
,
858 PROC_POLICY_ACTION_DROP
,
860 PROC_POLICY_IMP_IMPORTANT
,
864 } else if (errno
== EOVERFLOW
) {
865 proc_importance_bad_assertion("Attempted to drop too many assertions");
871 proc_importance_bad_assertion("Attempted to drop assertion with invalid token");
877 * Accept the De-Nap boost on a message, or request another boost assertion
878 * if we have already accepted the implicit boost for this message.
880 * Interface is deprecated before it really got started - just as synonym
881 * for proc_importance_assertion_begin_with_msg() now.
884 proc_denap_assertion_begin_with_msg(mach_msg_header_t
*msg
,
885 uint64_t *assertion_token
)
887 #pragma clang diagnostic push
888 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
889 return proc_importance_assertion_begin_with_msg(msg
, NULL
, assertion_token
);
890 #pragma clang diagnostic pop
895 * Drop a denap boost assertion.
897 * Interface is deprecated before it really got started - just a synonym
898 * for proc_importance_assertion_complete() now.
901 proc_denap_assertion_complete(uint64_t assertion_token
)
903 return proc_importance_assertion_complete(assertion_token
);
906 #if !TARGET_OS_EMBEDDED
909 proc_clear_vmpressure(pid_t pid
)
911 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_RESTORE
, PROC_POLICY_RESOURCE_STARVATION
, PROC_POLICY_RS_VIRTUALMEM
, NULL
, pid
, (uint64_t)0) != -1) {
918 /* set the current process as one who can resume suspended processes due to low virtual memory. Need to be root */
920 proc_set_owner_vmpressure(void)
924 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_VMRSRCOWNER
, (uint64_t)0, NULL
, 0)) == -1) {
931 /* mark yourself to delay idle sleep on disk IO */
933 proc_set_delayidlesleep(void)
937 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_DELAYIDLESLEEP
, (uint64_t)1, NULL
, 0)) == -1) {
944 /* Reset yourself to delay idle sleep on disk IO, if already set */
946 proc_clear_delayidlesleep(void)
950 if ((retval
= __proc_info(PROC_INFO_CALL_SETCONTROL
, getpid(), PROC_SELFSET_DELAYIDLESLEEP
, (uint64_t)0, NULL
, 0)) == -1) {
957 /* disable the launch time backgroudn policy and restore the process to default group */
959 proc_disable_apptype(pid_t pid
, int apptype
)
962 case PROC_POLICY_OSX_APPTYPE_TAL
:
963 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT
:
969 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_DISABLE
, PROC_POLICY_APPTYPE
, apptype
, NULL
, pid
, (uint64_t)0) != -1) {
976 /* re-enable the launch time background policy if it had been disabled. */
978 proc_enable_apptype(pid_t pid
, int apptype
)
981 case PROC_POLICY_OSX_APPTYPE_TAL
:
982 case PROC_POLICY_OSX_APPTYPE_DASHCLIENT
:
988 if (__process_policy(PROC_POLICY_SCOPE_PROCESS
, PROC_POLICY_ACTION_ENABLE
, PROC_POLICY_APPTYPE
, apptype
, NULL
, pid
, (uint64_t)0) != -1) {
995 #if !TARGET_IPHONE_SIMULATOR
998 proc_suppress(__unused pid_t pid
, __unused
uint64_t *generation
)
1003 #endif /* !TARGET_IPHONE_SIMULATOR */
1005 #endif /* !TARGET_OS_EMBEDDED */