1 /*
2  * Copyright (c) 2010-2018 Apple Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
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.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 #ifndef _LIBPROC_INTERNALH_
24 #define _LIBPROC_INTERNALH_
25 
26 #include <TargetConditionals.h>
27 
28 #include <sys/cdefs.h>
29 #include <libproc.h>
30 #include <libproc_private.h>
31 #include <mach/message.h>
32 
33 __BEGIN_DECLS
34 
35 /* CPU monitor action */
36 #define PROC_SETCPU_ACTION_NONE         0
37 #define PROC_SETCPU_ACTION_THROTTLE     1
38 
39 int proc_setcpu_percentage(pid_t pid, int action, int percentage) __OSX_AVAILABLE_STARTING(__MAC_10_12_2, __IPHONE_5_0);
40 int proc_clear_cpulimits(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_12_2, __IPHONE_5_0);
41 
42 /* CPU limits, applies to current thread only. 0% unsets limit */
43 int proc_setthread_cpupercent(uint8_t percentage, uint32_t ms_refill) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_5_0);
44 
45 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
46 
47 /* CPU monitor action, continued */
48 #define PROC_SETCPU_ACTION_SUSPEND      2
49 #define PROC_SETCPU_ACTION_TERMINATE    3
50 #define PROC_SETCPU_ACTION_NOTIFY       4
51 
52 int proc_setcpu_deadline(pid_t pid, int action, uint64_t deadline) __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_5_0);
53 int proc_setcpu_percentage_withdeadline(pid_t pid, int action, int percentage, uint64_t deadline) __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_5_0);
54 
55 #define PROC_APPSTATE_NONE              0
56 #define PROC_APPSTATE_ACTIVE            1
57 #define PROC_APPSTATE_BACKGROUND        2
58 #define PROC_APPSTATE_NONUI             3
59 #define PROC_APPSTATE_INACTIVE          4
60 
61 int proc_setappstate(int pid, int appstate);
62 int proc_appstate(int pid, int * appstatep);
63 
64 #define PROC_DEVSTATUS_SHORTTERM        1
65 #define PROC_DEVSTATUS_LONGTERM         2
66 
67 int proc_devstatusnotify(int devicestatus);
68 
69 #define PROC_PIDBIND_CLEAR      0
70 #define PROC_PIDBIND_SET        1
71 int proc_pidbind(int pid, uint64_t threadid, int bind);
72 
73 /*
74  * High level check to see if a process is allowed to use HW
75  * resources reserved for foreground applications.
76  * Returns:
77  *	 1 if the PID is allowed
78  *	 0 if the PID is NOT allowed
79  *	<0 on error
80  *
81  *	When 0 is returned, 'reason' is set to indicate why
82  *	the pid is not allowed to use foreground-only hardware.
83  *	Reasons returned by the kernel are found in <sys/proc_info.h>
84  *
85  *	When <0 is returned, errno indicates the reason
86  *	for the failure.
87  */
88 int proc_can_use_foreground_hw(int pid, uint32_t *reason);
89 
90 #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
91 
92 /* resume the process suspend due to low VM resource */
93 int proc_clear_vmpressure(pid_t pid);
94 /* set self as the one who is going to resume suspended processes due to low VM. Need to be root */
95 int proc_set_owner_vmpressure(void);
96 
97 /* mark yourself to delay idle sleep on disk IO */
98 int proc_set_delayidlesleep(void);
99 /* Reset yourself to delay idle sleep on disk IO, if already set */
100 int proc_clear_delayidlesleep(void);
101 
102 
103 /* sub policies for PROC_POLICY_APPTYPE */
104 #define PROC_POLICY_OSX_APPTYPE_NONE            0
105 #define PROC_POLICY_OSX_APPTYPE_TAL             1       /* TAL based launched */
106 #define PROC_POLICY_OSX_APPTYPE_WIDGET          2       /* for dashboard client */
107 #define PROC_POLICY_OSX_APPTYPE_DASHCLIENT      2       /* rename to move away from widget */
108 
109 /*
110  * Resumes the backgrounded TAL or dashboard client. Only priv users can disable TAL apps.
111  * Valid apptype are PROC_POLICY_OSX_APPTYPE_DASHCLIENT and PROC_POLICY_OSX_APPTYPE_TAL.
112  * Returns 0 on success otherwise appropriate error code.
113  */
114 int proc_disable_apptype(pid_t pid, int apptype);
115 int proc_enable_apptype(pid_t pid, int apptype);
116 
117 #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
118 
119 /* mark process as importance donating */
120 int proc_donate_importance_boost(void);
121 
122 /* DEPRECATED: supported for backward compatibility only */
123 /* check the message for an importance boost and take an assertion on it */
124 int proc_importance_assertion_begin_with_msg(mach_msg_header_t  *msg,
125     mach_msg_trailer_t *trailer,
126     uint64_t *assertion_token) __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_8, __MAC_10_10, __IPHONE_6_0, __IPHONE_8_0);
127 
128 /* DEPRECATED: supported for backward compatibility only */
129 /* drop an assertion */
130 int proc_importance_assertion_complete(uint64_t assertion_handle);
131 
132 /* check the message for a App De-Nap boost and take an assertion on it */
133 int proc_denap_assertion_begin_with_msg(mach_msg_header_t  *msg,
134     uint64_t *assertion_token);
135 
136 /* drop a de-nap assertion */
137 int proc_denap_assertion_complete(uint64_t assertion_handle);
138 
139 /* ongoing percent-over-time CPU monitor */
140 int proc_set_cpumon_defaults(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_6_0);
141 int proc_set_cpumon_params(pid_t pid, int percentage, int interval) __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_6_0);
142 int proc_set_cpumon_params_fatal(pid_t pid, int percentage, int interval) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
143 
144 int proc_get_cpumon_params(pid_t pid, int *percentage, int *interval) __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_6_0);
145 int proc_resume_cpumon(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_10_0);
146 int proc_disable_cpumon(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_8, __IPHONE_6_0);
147 
148 /* ongoing wakes/second monitor */
149 int proc_set_wakemon_defaults(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
150 int proc_set_wakemon_params(pid_t pid, int rate_hz, int flags) __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
151 int proc_get_wakemon_params(pid_t pid, int *rate_hz, int *flags) __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
152 int proc_disable_wakemon(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
153 
154 int proc_reset_footprint_interval(pid_t pid) __OSX_AVAILABLE_STARTING(__MAC_10_14, __IPHONE_12_0);
155 
156 /* request trace buffer collection */
157 int proc_trace_log(pid_t pid, uint64_t uniqueid) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
158 
159 /* proc_info call to get the originator information */
160 int proc_pidoriginatorinfo(int flavor, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);
161 
162 int proc_listcoalitions(int flavor, int coaltype, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_11, __IPHONE_8_3);
163 
164 /* get scheduler stats for current thread */
165 int proc_current_thread_schedinfo(void *buffer, size_t buffersize);
166 
167 #if !TARGET_OS_SIMULATOR
168 
169 #define PROC_SUPPRESS_SUCCESS                (0)
170 #define PROC_SUPPRESS_BAD_ARGUMENTS         (-1)
171 #define PROC_SUPPRESS_OLD_GENERATION        (-2)
172 #define PROC_SUPPRESS_ALREADY_SUPPRESSED    (-3)
173 
174 int proc_suppress(pid_t pid, uint64_t *generation);
175 #endif /* !TARGET_OS_SIMULATOR */
176 
177 /* for use by dyld when it relocates itself */
178 int proc_set_dyld_all_image_info(void *buffer, int buffersize);
179 
180 
181 
182 __END_DECLS
183 
184 #endif /* _LIBPROC_INTERNALH_ */
185