xref: /xnu-8796.141.3/libsyscall/wrappers/libproc/libproc.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2006-2018 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*1b191cb5SApple OSS Distributions  * file.
12*1b191cb5SApple OSS Distributions  *
13*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
14*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
19*1b191cb5SApple OSS Distributions  * limitations under the License.
20*1b191cb5SApple OSS Distributions  *
21*1b191cb5SApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*1b191cb5SApple OSS Distributions  */
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions #include <sys/cdefs.h>
25*1b191cb5SApple OSS Distributions #include <unistd.h>
26*1b191cb5SApple OSS Distributions #include <errno.h>
27*1b191cb5SApple OSS Distributions #include <string.h>
28*1b191cb5SApple OSS Distributions #include <strings.h>
29*1b191cb5SApple OSS Distributions #include <stdlib.h>
30*1b191cb5SApple OSS Distributions #include <sys/errno.h>
31*1b191cb5SApple OSS Distributions #include <sys/msgbuf.h>
32*1b191cb5SApple OSS Distributions #include <sys/resource.h>
33*1b191cb5SApple OSS Distributions #include <sys/process_policy.h>
34*1b191cb5SApple OSS Distributions #include <sys/event.h>
35*1b191cb5SApple OSS Distributions #include <mach/message.h>
36*1b191cb5SApple OSS Distributions 
37*1b191cb5SApple OSS Distributions #include "libproc_internal.h"
38*1b191cb5SApple OSS Distributions 
39*1b191cb5SApple OSS Distributions int __proc_info(int callnum, int pid, int flavor, uint64_t arg, void * buffer, int buffersize);
40*1b191cb5SApple OSS Distributions int __proc_info_extended_id(int32_t callnum, int32_t pid, uint32_t flavor, uint32_t flags, uint64_t ext_id, uint64_t arg, user_addr_t buffer, int32_t buffersize);
41*1b191cb5SApple OSS Distributions __private_extern__ int proc_setthreadname(void * buffer, int buffersize);
42*1b191cb5SApple OSS Distributions 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);
43*1b191cb5SApple OSS Distributions int proc_rlimit_control(pid_t pid, int flavor, void *arg);
44*1b191cb5SApple OSS Distributions 
45*1b191cb5SApple OSS Distributions int
proc_listpids(uint32_t type,uint32_t typeinfo,void * buffer,int buffersize)46*1b191cb5SApple OSS Distributions proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize)
47*1b191cb5SApple OSS Distributions {
48*1b191cb5SApple OSS Distributions 	int retval;
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions 	if ((type >= PROC_ALL_PIDS) || (type <= PROC_PPID_ONLY)) {
51*1b191cb5SApple OSS Distributions 		if ((retval = __proc_info(PROC_INFO_CALL_LISTPIDS, type, typeinfo, (uint64_t)0, buffer, buffersize)) == -1) {
52*1b191cb5SApple OSS Distributions 			return 0;
53*1b191cb5SApple OSS Distributions 		}
54*1b191cb5SApple OSS Distributions 	} else {
55*1b191cb5SApple OSS Distributions 		errno = EINVAL;
56*1b191cb5SApple OSS Distributions 		retval = 0;
57*1b191cb5SApple OSS Distributions 	}
58*1b191cb5SApple OSS Distributions 	return retval;
59*1b191cb5SApple OSS Distributions }
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions 
62*1b191cb5SApple OSS Distributions int
proc_listallpids(void * buffer,int buffersize)63*1b191cb5SApple OSS Distributions proc_listallpids(void * buffer, int buffersize)
64*1b191cb5SApple OSS Distributions {
65*1b191cb5SApple OSS Distributions 	int numpids;
66*1b191cb5SApple OSS Distributions 	numpids = proc_listpids(PROC_ALL_PIDS, (uint32_t)0, buffer, buffersize);
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions 	if (numpids == -1) {
69*1b191cb5SApple OSS Distributions 		return -1;
70*1b191cb5SApple OSS Distributions 	} else {
71*1b191cb5SApple OSS Distributions 		return numpids / sizeof(int);
72*1b191cb5SApple OSS Distributions 	}
73*1b191cb5SApple OSS Distributions }
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions int
proc_listpgrppids(pid_t pgrpid,void * buffer,int buffersize)76*1b191cb5SApple OSS Distributions proc_listpgrppids(pid_t pgrpid, void * buffer, int buffersize)
77*1b191cb5SApple OSS Distributions {
78*1b191cb5SApple OSS Distributions 	int numpids;
79*1b191cb5SApple OSS Distributions 	numpids = proc_listpids(PROC_PGRP_ONLY, (uint32_t)pgrpid, buffer, buffersize);
80*1b191cb5SApple OSS Distributions 	if (numpids == -1) {
81*1b191cb5SApple OSS Distributions 		return -1;
82*1b191cb5SApple OSS Distributions 	} else {
83*1b191cb5SApple OSS Distributions 		return numpids / sizeof(int);
84*1b191cb5SApple OSS Distributions 	}
85*1b191cb5SApple OSS Distributions }
86*1b191cb5SApple OSS Distributions 
87*1b191cb5SApple OSS Distributions int
proc_listchildpids(pid_t ppid,void * buffer,int buffersize)88*1b191cb5SApple OSS Distributions proc_listchildpids(pid_t ppid, void * buffer, int buffersize)
89*1b191cb5SApple OSS Distributions {
90*1b191cb5SApple OSS Distributions 	int numpids;
91*1b191cb5SApple OSS Distributions 	numpids = proc_listpids(PROC_PPID_ONLY, (uint32_t)ppid, buffer, buffersize);
92*1b191cb5SApple OSS Distributions 	if (numpids == -1) {
93*1b191cb5SApple OSS Distributions 		return -1;
94*1b191cb5SApple OSS Distributions 	} else {
95*1b191cb5SApple OSS Distributions 		return numpids / sizeof(int);
96*1b191cb5SApple OSS Distributions 	}
97*1b191cb5SApple OSS Distributions }
98*1b191cb5SApple OSS Distributions 
99*1b191cb5SApple OSS Distributions 
100*1b191cb5SApple OSS Distributions int
proc_pidinfo(int pid,int flavor,uint64_t arg,void * buffer,int buffersize)101*1b191cb5SApple OSS Distributions proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize)
102*1b191cb5SApple OSS Distributions {
103*1b191cb5SApple OSS Distributions 	int retval;
104*1b191cb5SApple OSS Distributions 
105*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, flavor, arg, buffer, buffersize)) == -1) {
106*1b191cb5SApple OSS Distributions 		return 0;
107*1b191cb5SApple OSS Distributions 	}
108*1b191cb5SApple OSS Distributions 
109*1b191cb5SApple OSS Distributions 	return retval;
110*1b191cb5SApple OSS Distributions }
111*1b191cb5SApple OSS Distributions 
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions int
proc_pidoriginatorinfo(int flavor,void * buffer,int buffersize)114*1b191cb5SApple OSS Distributions proc_pidoriginatorinfo(int flavor, void *buffer, int buffersize)
115*1b191cb5SApple OSS Distributions {
116*1b191cb5SApple OSS Distributions 	int retval;
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_PIDORIGINATORINFO, getpid(), flavor, 0, buffer, buffersize)) == -1) {
119*1b191cb5SApple OSS Distributions 		return 0;
120*1b191cb5SApple OSS Distributions 	}
121*1b191cb5SApple OSS Distributions 
122*1b191cb5SApple OSS Distributions 	return retval;
123*1b191cb5SApple OSS Distributions }
124*1b191cb5SApple OSS Distributions 
125*1b191cb5SApple OSS Distributions int
proc_listcoalitions(int flavor,int coaltype,void * buffer,int buffersize)126*1b191cb5SApple OSS Distributions proc_listcoalitions(int flavor, int coaltype, void *buffer, int buffersize)
127*1b191cb5SApple OSS Distributions {
128*1b191cb5SApple OSS Distributions 	int retval;
129*1b191cb5SApple OSS Distributions 
130*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_LISTCOALITIONS, flavor, coaltype, 0, buffer, buffersize)) == -1) {
131*1b191cb5SApple OSS Distributions 		return 0;
132*1b191cb5SApple OSS Distributions 	}
133*1b191cb5SApple OSS Distributions 
134*1b191cb5SApple OSS Distributions 	return retval;
135*1b191cb5SApple OSS Distributions }
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions int
proc_pid_rusage(int pid,int flavor,rusage_info_t * buffer)138*1b191cb5SApple OSS Distributions proc_pid_rusage(int pid, int flavor, rusage_info_t *buffer)
139*1b191cb5SApple OSS Distributions {
140*1b191cb5SApple OSS Distributions 	return __proc_info(PROC_INFO_CALL_PIDRUSAGE, pid, flavor, 0, buffer, 0);
141*1b191cb5SApple OSS Distributions }
142*1b191cb5SApple OSS Distributions 
143*1b191cb5SApple OSS Distributions int
proc_setthread_cpupercent(uint8_t percentage,uint32_t ms_refill)144*1b191cb5SApple OSS Distributions proc_setthread_cpupercent(uint8_t percentage, uint32_t ms_refill)
145*1b191cb5SApple OSS Distributions {
146*1b191cb5SApple OSS Distributions 	uint32_t arg = 0;
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions 	/* Pack percentage and refill into a 32-bit number to match existing kernel implementation */
149*1b191cb5SApple OSS Distributions 	if ((percentage >= 100) || (ms_refill & ~0xffffffU)) {
150*1b191cb5SApple OSS Distributions 		errno = EINVAL;
151*1b191cb5SApple OSS Distributions 		return -1;
152*1b191cb5SApple OSS Distributions 	}
153*1b191cb5SApple OSS Distributions 
154*1b191cb5SApple OSS Distributions 	arg = ((ms_refill << 8) | percentage);
155*1b191cb5SApple OSS Distributions 
156*1b191cb5SApple OSS Distributions 	return proc_rlimit_control(-1, RLIMIT_THREAD_CPULIMITS, (void *)(uintptr_t)arg);
157*1b191cb5SApple OSS Distributions }
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions int
proc_pidfdinfo(int pid,int fd,int flavor,void * buffer,int buffersize)160*1b191cb5SApple OSS Distributions proc_pidfdinfo(int pid, int fd, int flavor, void * buffer, int buffersize)
161*1b191cb5SApple OSS Distributions {
162*1b191cb5SApple OSS Distributions 	int retval;
163*1b191cb5SApple OSS Distributions 
164*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_PIDFDINFO, pid, flavor, (uint64_t)fd, buffer, buffersize)) == -1) {
165*1b191cb5SApple OSS Distributions 		return 0;
166*1b191cb5SApple OSS Distributions 	}
167*1b191cb5SApple OSS Distributions 
168*1b191cb5SApple OSS Distributions 	return retval;
169*1b191cb5SApple OSS Distributions }
170*1b191cb5SApple OSS Distributions 
171*1b191cb5SApple OSS Distributions 
172*1b191cb5SApple OSS Distributions int
proc_pidfileportinfo(int pid,uint32_t fileport,int flavor,void * buffer,int buffersize)173*1b191cb5SApple OSS Distributions proc_pidfileportinfo(int pid, uint32_t fileport, int flavor, void *buffer, int buffersize)
174*1b191cb5SApple OSS Distributions {
175*1b191cb5SApple OSS Distributions 	int retval;
176*1b191cb5SApple OSS Distributions 
177*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_PIDFILEPORTINFO, pid, flavor, (uint64_t)fileport, buffer, buffersize)) == -1) {
178*1b191cb5SApple OSS Distributions 		return 0;
179*1b191cb5SApple OSS Distributions 	}
180*1b191cb5SApple OSS Distributions 	return retval;
181*1b191cb5SApple OSS Distributions }
182*1b191cb5SApple OSS Distributions 
183*1b191cb5SApple OSS Distributions int
proc_piddynkqueueinfo(int pid,int flavor,kqueue_id_t kq_id,void * buffer,int buffersize)184*1b191cb5SApple OSS Distributions proc_piddynkqueueinfo(int pid, int flavor, kqueue_id_t kq_id, void *buffer, int buffersize)
185*1b191cb5SApple OSS Distributions {
186*1b191cb5SApple OSS Distributions 	int ret;
187*1b191cb5SApple OSS Distributions 
188*1b191cb5SApple OSS Distributions 	if ((ret = __proc_info(PROC_INFO_CALL_PIDDYNKQUEUEINFO, pid, flavor, (uint64_t)kq_id, buffer, buffersize)) == -1) {
189*1b191cb5SApple OSS Distributions 		return 0;
190*1b191cb5SApple OSS Distributions 	}
191*1b191cb5SApple OSS Distributions 
192*1b191cb5SApple OSS Distributions 	return ret;
193*1b191cb5SApple OSS Distributions }
194*1b191cb5SApple OSS Distributions 
195*1b191cb5SApple OSS Distributions int
proc_udata_info(int pid,int flavor,void * buffer,int buffersize)196*1b191cb5SApple OSS Distributions proc_udata_info(int pid, int flavor, void *buffer, int buffersize)
197*1b191cb5SApple OSS Distributions {
198*1b191cb5SApple OSS Distributions 	return __proc_info(PROC_INFO_CALL_UDATA_INFO, pid, flavor, 0, buffer, buffersize);
199*1b191cb5SApple OSS Distributions }
200*1b191cb5SApple OSS Distributions 
201*1b191cb5SApple OSS Distributions /* only used by dyld which links with libsystem_kernel.a */
202*1b191cb5SApple OSS Distributions __private_extern__ int
proc_set_dyld_all_image_info(void * buffer,int buffersize)203*1b191cb5SApple OSS Distributions proc_set_dyld_all_image_info(void *buffer, int buffersize)
204*1b191cb5SApple OSS Distributions {
205*1b191cb5SApple OSS Distributions 	return __proc_info(PROC_INFO_CALL_SET_DYLD_IMAGES, getpid(), 0, 0, buffer, buffersize);
206*1b191cb5SApple OSS Distributions }
207*1b191cb5SApple OSS Distributions 
208*1b191cb5SApple OSS Distributions 
209*1b191cb5SApple OSS Distributions int
proc_name(int pid,void * buffer,uint32_t buffersize)210*1b191cb5SApple OSS Distributions proc_name(int pid, void * buffer, uint32_t buffersize)
211*1b191cb5SApple OSS Distributions {
212*1b191cb5SApple OSS Distributions 	int retval = 0, len;
213*1b191cb5SApple OSS Distributions 	struct proc_bsdinfo pbsd;
214*1b191cb5SApple OSS Distributions 
215*1b191cb5SApple OSS Distributions 
216*1b191cb5SApple OSS Distributions 	if (buffersize < sizeof(pbsd.pbi_name)) {
217*1b191cb5SApple OSS Distributions 		errno = ENOMEM;
218*1b191cb5SApple OSS Distributions 		return 0;
219*1b191cb5SApple OSS Distributions 	}
220*1b191cb5SApple OSS Distributions 
221*1b191cb5SApple OSS Distributions 	retval = proc_pidinfo(pid, PROC_PIDTBSDINFO, (uint64_t)0, &pbsd, sizeof(struct proc_bsdinfo));
222*1b191cb5SApple OSS Distributions 	if (retval != 0) {
223*1b191cb5SApple OSS Distributions 		if (pbsd.pbi_name[0]) {
224*1b191cb5SApple OSS Distributions 			bcopy(&pbsd.pbi_name, buffer, sizeof(pbsd.pbi_name));
225*1b191cb5SApple OSS Distributions 		} else {
226*1b191cb5SApple OSS Distributions 			bcopy(&pbsd.pbi_comm, buffer, sizeof(pbsd.pbi_comm));
227*1b191cb5SApple OSS Distributions 		}
228*1b191cb5SApple OSS Distributions 		len = (int)strlen(buffer);
229*1b191cb5SApple OSS Distributions 		return len;
230*1b191cb5SApple OSS Distributions 	}
231*1b191cb5SApple OSS Distributions 	return 0;
232*1b191cb5SApple OSS Distributions }
233*1b191cb5SApple OSS Distributions 
234*1b191cb5SApple OSS Distributions int
proc_regionfilename(int pid,uint64_t address,void * buffer,uint32_t buffersize)235*1b191cb5SApple OSS Distributions proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize)
236*1b191cb5SApple OSS Distributions {
237*1b191cb5SApple OSS Distributions 	int retval;
238*1b191cb5SApple OSS Distributions 	struct proc_regionpath path;
239*1b191cb5SApple OSS Distributions 
240*1b191cb5SApple OSS Distributions 	if (buffersize < MAXPATHLEN) {
241*1b191cb5SApple OSS Distributions 		errno = ENOMEM;
242*1b191cb5SApple OSS Distributions 		return 0;
243*1b191cb5SApple OSS Distributions 	}
244*1b191cb5SApple OSS Distributions 
245*1b191cb5SApple OSS Distributions 	retval = proc_pidinfo(pid, PROC_PIDREGIONPATH, (uint64_t)address, &path, sizeof(struct proc_regionpath));
246*1b191cb5SApple OSS Distributions 	if (retval != 0) {
247*1b191cb5SApple OSS Distributions 		return (int)(strlcpy(buffer, path.prpo_path, buffersize));
248*1b191cb5SApple OSS Distributions 	}
249*1b191cb5SApple OSS Distributions 	return 0;
250*1b191cb5SApple OSS Distributions }
251*1b191cb5SApple OSS Distributions 
252*1b191cb5SApple OSS Distributions int
proc_kmsgbuf(void * buffer,uint32_t buffersize)253*1b191cb5SApple OSS Distributions proc_kmsgbuf(void * buffer, uint32_t  buffersize)
254*1b191cb5SApple OSS Distributions {
255*1b191cb5SApple OSS Distributions 	int retval;
256*1b191cb5SApple OSS Distributions 
257*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_KERNMSGBUF, 0, 0, (uint64_t)0, buffer, buffersize)) == -1) {
258*1b191cb5SApple OSS Distributions 		return 0;
259*1b191cb5SApple OSS Distributions 	}
260*1b191cb5SApple OSS Distributions 	return retval;
261*1b191cb5SApple OSS Distributions }
262*1b191cb5SApple OSS Distributions 
263*1b191cb5SApple OSS Distributions int
proc_pidpath(int pid,void * buffer,uint32_t buffersize)264*1b191cb5SApple OSS Distributions proc_pidpath(int pid, void * buffer, uint32_t  buffersize)
265*1b191cb5SApple OSS Distributions {
266*1b191cb5SApple OSS Distributions 	int retval, len;
267*1b191cb5SApple OSS Distributions 
268*1b191cb5SApple OSS Distributions 	if (buffersize < PROC_PIDPATHINFO_SIZE) {
269*1b191cb5SApple OSS Distributions 		errno = ENOMEM;
270*1b191cb5SApple OSS Distributions 		return 0;
271*1b191cb5SApple OSS Distributions 	}
272*1b191cb5SApple OSS Distributions 	if (buffersize > PROC_PIDPATHINFO_MAXSIZE) {
273*1b191cb5SApple OSS Distributions 		errno = EOVERFLOW;
274*1b191cb5SApple OSS Distributions 		return 0;
275*1b191cb5SApple OSS Distributions 	}
276*1b191cb5SApple OSS Distributions 
277*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDPATHINFO, (uint64_t)0, buffer, buffersize);
278*1b191cb5SApple OSS Distributions 	if (retval != -1) {
279*1b191cb5SApple OSS Distributions 		len = (int)strlen(buffer);
280*1b191cb5SApple OSS Distributions 		return len;
281*1b191cb5SApple OSS Distributions 	}
282*1b191cb5SApple OSS Distributions 	return 0;
283*1b191cb5SApple OSS Distributions }
284*1b191cb5SApple OSS Distributions 
285*1b191cb5SApple OSS Distributions int
proc_pidpath_audittoken(audit_token_t * audittoken,void * buffer,uint32_t buffersize)286*1b191cb5SApple OSS Distributions proc_pidpath_audittoken(audit_token_t *audittoken, void * buffer, uint32_t buffersize)
287*1b191cb5SApple OSS Distributions {
288*1b191cb5SApple OSS Distributions 	int retval, len;
289*1b191cb5SApple OSS Distributions 
290*1b191cb5SApple OSS Distributions 	if (buffersize < PROC_PIDPATHINFO_SIZE) {
291*1b191cb5SApple OSS Distributions 		errno = ENOMEM;
292*1b191cb5SApple OSS Distributions 		return 0;
293*1b191cb5SApple OSS Distributions 	}
294*1b191cb5SApple OSS Distributions 	if (buffersize > PROC_PIDPATHINFO_MAXSIZE) {
295*1b191cb5SApple OSS Distributions 		errno = EOVERFLOW;
296*1b191cb5SApple OSS Distributions 		return 0;
297*1b191cb5SApple OSS Distributions 	}
298*1b191cb5SApple OSS Distributions 
299*1b191cb5SApple OSS Distributions 	int pid = audittoken->val[5];
300*1b191cb5SApple OSS Distributions 	int idversion = audittoken->val[7];
301*1b191cb5SApple OSS Distributions 
302*1b191cb5SApple OSS Distributions 	retval = __proc_info_extended_id(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDPATHINFO, PIF_COMPARE_IDVERSION, (uint64_t)idversion,
303*1b191cb5SApple OSS Distributions 	    (uint64_t)0, buffer, buffersize);
304*1b191cb5SApple OSS Distributions 	if (retval != -1) {
305*1b191cb5SApple OSS Distributions 		len = (int)strlen(buffer);
306*1b191cb5SApple OSS Distributions 		return len;
307*1b191cb5SApple OSS Distributions 	}
308*1b191cb5SApple OSS Distributions 	return 0;
309*1b191cb5SApple OSS Distributions }
310*1b191cb5SApple OSS Distributions 
311*1b191cb5SApple OSS Distributions int
proc_current_thread_schedinfo(void * buffer,size_t buffersize)312*1b191cb5SApple OSS Distributions proc_current_thread_schedinfo(void *buffer, size_t buffersize)
313*1b191cb5SApple OSS Distributions {
314*1b191cb5SApple OSS Distributions 	extern uint64_t __thread_selfid(void);
315*1b191cb5SApple OSS Distributions 
316*1b191cb5SApple OSS Distributions 	int retval;
317*1b191cb5SApple OSS Distributions 
318*1b191cb5SApple OSS Distributions 	if (buffersize < PROC_PIDTHREADSCHEDINFO_SIZE) {
319*1b191cb5SApple OSS Distributions 		errno = ENOMEM;
320*1b191cb5SApple OSS Distributions 		return errno;
321*1b191cb5SApple OSS Distributions 	}
322*1b191cb5SApple OSS Distributions 	if (buffersize > PROC_PIDTHREADSCHEDINFO_SIZE) {
323*1b191cb5SApple OSS Distributions 		errno = EOVERFLOW;
324*1b191cb5SApple OSS Distributions 		return errno;
325*1b191cb5SApple OSS Distributions 	}
326*1b191cb5SApple OSS Distributions 
327*1b191cb5SApple OSS Distributions 	pid_t pid = getpid();
328*1b191cb5SApple OSS Distributions 	uint64_t threadid = __thread_selfid();
329*1b191cb5SApple OSS Distributions 
330*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDTHREADSCHEDINFO, threadid, buffer, buffersize);
331*1b191cb5SApple OSS Distributions 
332*1b191cb5SApple OSS Distributions 	if (retval == -1) {
333*1b191cb5SApple OSS Distributions 		return errno;
334*1b191cb5SApple OSS Distributions 	}
335*1b191cb5SApple OSS Distributions 	return 0;
336*1b191cb5SApple OSS Distributions }
337*1b191cb5SApple OSS Distributions 
338*1b191cb5SApple OSS Distributions int
proc_libversion(int * major,int * minor)339*1b191cb5SApple OSS Distributions proc_libversion(int *major, int * minor)
340*1b191cb5SApple OSS Distributions {
341*1b191cb5SApple OSS Distributions 	if (major != NULL) {
342*1b191cb5SApple OSS Distributions 		*major = 1;
343*1b191cb5SApple OSS Distributions 	}
344*1b191cb5SApple OSS Distributions 	if (minor != NULL) {
345*1b191cb5SApple OSS Distributions 		*minor = 1;
346*1b191cb5SApple OSS Distributions 	}
347*1b191cb5SApple OSS Distributions 	return 0;
348*1b191cb5SApple OSS Distributions }
349*1b191cb5SApple OSS Distributions 
350*1b191cb5SApple OSS Distributions int
proc_setpcontrol(const int control)351*1b191cb5SApple OSS Distributions proc_setpcontrol(const int control)
352*1b191cb5SApple OSS Distributions {
353*1b191cb5SApple OSS Distributions 	int retval;
354*1b191cb5SApple OSS Distributions 
355*1b191cb5SApple OSS Distributions 	if (control < PROC_SETPC_NONE || control > PROC_SETPC_TERMINATE) {
356*1b191cb5SApple OSS Distributions 		return EINVAL;
357*1b191cb5SApple OSS Distributions 	}
358*1b191cb5SApple OSS Distributions 
359*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_PCONTROL, (uint64_t)control, NULL, 0)) == -1) {
360*1b191cb5SApple OSS Distributions 		return errno;
361*1b191cb5SApple OSS Distributions 	}
362*1b191cb5SApple OSS Distributions 
363*1b191cb5SApple OSS Distributions 	return 0;
364*1b191cb5SApple OSS Distributions }
365*1b191cb5SApple OSS Distributions 
366*1b191cb5SApple OSS Distributions 
367*1b191cb5SApple OSS Distributions __private_extern__ int
proc_setthreadname(void * buffer,int buffersize)368*1b191cb5SApple OSS Distributions proc_setthreadname(void * buffer, int buffersize)
369*1b191cb5SApple OSS Distributions {
370*1b191cb5SApple OSS Distributions 	int retval;
371*1b191cb5SApple OSS Distributions 
372*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_THREADNAME, (uint64_t)0, buffer, buffersize);
373*1b191cb5SApple OSS Distributions 
374*1b191cb5SApple OSS Distributions 	if (retval == -1) {
375*1b191cb5SApple OSS Distributions 		return errno;
376*1b191cb5SApple OSS Distributions 	} else {
377*1b191cb5SApple OSS Distributions 		return 0;
378*1b191cb5SApple OSS Distributions 	}
379*1b191cb5SApple OSS Distributions }
380*1b191cb5SApple OSS Distributions 
381*1b191cb5SApple OSS Distributions int
proc_track_dirty(pid_t pid,uint32_t flags)382*1b191cb5SApple OSS Distributions proc_track_dirty(pid_t pid, uint32_t flags)
383*1b191cb5SApple OSS Distributions {
384*1b191cb5SApple OSS Distributions 	if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_TRACK, flags, NULL, 0) == -1) {
385*1b191cb5SApple OSS Distributions 		return errno;
386*1b191cb5SApple OSS Distributions 	}
387*1b191cb5SApple OSS Distributions 
388*1b191cb5SApple OSS Distributions 	return 0;
389*1b191cb5SApple OSS Distributions }
390*1b191cb5SApple OSS Distributions 
391*1b191cb5SApple OSS Distributions int
proc_set_dirty(pid_t pid,bool dirty)392*1b191cb5SApple OSS Distributions proc_set_dirty(pid_t pid, bool dirty)
393*1b191cb5SApple OSS Distributions {
394*1b191cb5SApple OSS Distributions 	if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_SET, dirty, NULL, 0) == -1) {
395*1b191cb5SApple OSS Distributions 		return errno;
396*1b191cb5SApple OSS Distributions 	}
397*1b191cb5SApple OSS Distributions 
398*1b191cb5SApple OSS Distributions 	return 0;
399*1b191cb5SApple OSS Distributions }
400*1b191cb5SApple OSS Distributions 
401*1b191cb5SApple OSS Distributions int
proc_get_dirty(pid_t pid,uint32_t * flags)402*1b191cb5SApple OSS Distributions proc_get_dirty(pid_t pid, uint32_t *flags)
403*1b191cb5SApple OSS Distributions {
404*1b191cb5SApple OSS Distributions 	int retval;
405*1b191cb5SApple OSS Distributions 
406*1b191cb5SApple OSS Distributions 	if (!flags) {
407*1b191cb5SApple OSS Distributions 		return EINVAL;
408*1b191cb5SApple OSS Distributions 	}
409*1b191cb5SApple OSS Distributions 
410*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_GET, 0, NULL, 0);
411*1b191cb5SApple OSS Distributions 	if (retval == -1) {
412*1b191cb5SApple OSS Distributions 		return errno;
413*1b191cb5SApple OSS Distributions 	}
414*1b191cb5SApple OSS Distributions 
415*1b191cb5SApple OSS Distributions 	*flags = retval;
416*1b191cb5SApple OSS Distributions 
417*1b191cb5SApple OSS Distributions 	return 0;
418*1b191cb5SApple OSS Distributions }
419*1b191cb5SApple OSS Distributions 
420*1b191cb5SApple OSS Distributions int
proc_clear_dirty(pid_t pid,uint32_t flags)421*1b191cb5SApple OSS Distributions proc_clear_dirty(pid_t pid, uint32_t flags)
422*1b191cb5SApple OSS Distributions {
423*1b191cb5SApple OSS Distributions 	if (__proc_info(PROC_INFO_CALL_DIRTYCONTROL, pid, PROC_DIRTYCONTROL_CLEAR, flags, NULL, 0) == -1) {
424*1b191cb5SApple OSS Distributions 		return errno;
425*1b191cb5SApple OSS Distributions 	}
426*1b191cb5SApple OSS Distributions 
427*1b191cb5SApple OSS Distributions 	return 0;
428*1b191cb5SApple OSS Distributions }
429*1b191cb5SApple OSS Distributions 
430*1b191cb5SApple OSS Distributions int
proc_terminate(pid_t pid,int * sig)431*1b191cb5SApple OSS Distributions proc_terminate(pid_t pid, int *sig)
432*1b191cb5SApple OSS Distributions {
433*1b191cb5SApple OSS Distributions 	int retval;
434*1b191cb5SApple OSS Distributions 
435*1b191cb5SApple OSS Distributions 	if (!sig) {
436*1b191cb5SApple OSS Distributions 		return EINVAL;
437*1b191cb5SApple OSS Distributions 	}
438*1b191cb5SApple OSS Distributions 
439*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_TERMINATE, pid, 0, 0, NULL, 0);
440*1b191cb5SApple OSS Distributions 	if (retval == -1) {
441*1b191cb5SApple OSS Distributions 		return errno;
442*1b191cb5SApple OSS Distributions 	}
443*1b191cb5SApple OSS Distributions 
444*1b191cb5SApple OSS Distributions 	*sig = retval;
445*1b191cb5SApple OSS Distributions 
446*1b191cb5SApple OSS Distributions 	return 0;
447*1b191cb5SApple OSS Distributions }
448*1b191cb5SApple OSS Distributions 
449*1b191cb5SApple OSS Distributions int
proc_terminate_all_rsr(int sig)450*1b191cb5SApple OSS Distributions proc_terminate_all_rsr(int sig)
451*1b191cb5SApple OSS Distributions {
452*1b191cb5SApple OSS Distributions 	int retval = 0;
453*1b191cb5SApple OSS Distributions 
454*1b191cb5SApple OSS Distributions 	if (sig != SIGKILL && sig != SIGTERM) {
455*1b191cb5SApple OSS Distributions 		return EINVAL;
456*1b191cb5SApple OSS Distributions 	}
457*1b191cb5SApple OSS Distributions 
458*1b191cb5SApple OSS Distributions 	retval = __proc_info(PROC_INFO_CALL_TERMINATE_RSR, 0, 0, sig, NULL, 0);
459*1b191cb5SApple OSS Distributions 	if (retval == -1) {
460*1b191cb5SApple OSS Distributions 		return errno;
461*1b191cb5SApple OSS Distributions 	}
462*1b191cb5SApple OSS Distributions 
463*1b191cb5SApple OSS Distributions 	return 0;
464*1b191cb5SApple OSS Distributions }
465*1b191cb5SApple OSS Distributions 
466*1b191cb5SApple OSS Distributions /*
467*1b191cb5SApple OSS Distributions  * XXX the _fatal() variant both checks for an existing monitor
468*1b191cb5SApple OSS Distributions  * (with important policy effects on first party background apps)
469*1b191cb5SApple OSS Distributions  * and validates inputs.
470*1b191cb5SApple OSS Distributions  */
471*1b191cb5SApple OSS Distributions int
proc_set_cpumon_params(pid_t pid,int percentage,int interval)472*1b191cb5SApple OSS Distributions proc_set_cpumon_params(pid_t pid, int percentage, int interval)
473*1b191cb5SApple OSS Distributions {
474*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
475*1b191cb5SApple OSS Distributions 
476*1b191cb5SApple OSS Distributions 	/* no argument validation ...
477*1b191cb5SApple OSS Distributions 	 * task_set_cpuusage() ignores 0 values and squashes negative
478*1b191cb5SApple OSS Distributions 	 * values into uint32_t.
479*1b191cb5SApple OSS Distributions 	 */
480*1b191cb5SApple OSS Distributions 
481*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
482*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_percentage = percentage;
483*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_interval = (uint64_t)interval;
484*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_deadline = 0;
485*1b191cb5SApple OSS Distributions 
486*1b191cb5SApple OSS Distributions 	return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
487*1b191cb5SApple OSS Distributions 	           PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
488*1b191cb5SApple OSS Distributions }
489*1b191cb5SApple OSS Distributions 
490*1b191cb5SApple OSS Distributions int
proc_get_cpumon_params(pid_t pid,int * percentage,int * interval)491*1b191cb5SApple OSS Distributions proc_get_cpumon_params(pid_t pid, int *percentage, int *interval)
492*1b191cb5SApple OSS Distributions {
493*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
494*1b191cb5SApple OSS Distributions 	int ret;
495*1b191cb5SApple OSS Distributions 
496*1b191cb5SApple OSS Distributions 	ret = __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_GET, PROC_POLICY_RESOURCE_USAGE,
497*1b191cb5SApple OSS Distributions 	    PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
498*1b191cb5SApple OSS Distributions 
499*1b191cb5SApple OSS Distributions 	if ((ret == 0) && (attr.ppattr_cpu_attr == PROC_POLICY_RSRCACT_NOTIFY_EXC)) {
500*1b191cb5SApple OSS Distributions 		*percentage = attr.ppattr_cpu_percentage;
501*1b191cb5SApple OSS Distributions 		*interval = (int)attr.ppattr_cpu_attr_interval;
502*1b191cb5SApple OSS Distributions 	} else {
503*1b191cb5SApple OSS Distributions 		*percentage = 0;
504*1b191cb5SApple OSS Distributions 		*interval = 0;
505*1b191cb5SApple OSS Distributions 	}
506*1b191cb5SApple OSS Distributions 
507*1b191cb5SApple OSS Distributions 	return ret;
508*1b191cb5SApple OSS Distributions }
509*1b191cb5SApple OSS Distributions 
510*1b191cb5SApple OSS Distributions int
proc_set_cpumon_defaults(pid_t pid)511*1b191cb5SApple OSS Distributions proc_set_cpumon_defaults(pid_t pid)
512*1b191cb5SApple OSS Distributions {
513*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
514*1b191cb5SApple OSS Distributions 
515*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
516*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_percentage = PROC_POLICY_CPUMON_DEFAULTS;
517*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_interval = 0;
518*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_deadline = 0;
519*1b191cb5SApple OSS Distributions 
520*1b191cb5SApple OSS Distributions 	return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
521*1b191cb5SApple OSS Distributions 	           PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
522*1b191cb5SApple OSS Distributions }
523*1b191cb5SApple OSS Distributions 
524*1b191cb5SApple OSS Distributions int
proc_resume_cpumon(pid_t pid)525*1b191cb5SApple OSS Distributions proc_resume_cpumon(pid_t pid)
526*1b191cb5SApple OSS Distributions {
527*1b191cb5SApple OSS Distributions 	return __process_policy(PROC_POLICY_SCOPE_PROCESS,
528*1b191cb5SApple OSS Distributions 	           PROC_POLICY_ACTION_ENABLE,
529*1b191cb5SApple OSS Distributions 	           PROC_POLICY_RESOURCE_USAGE,
530*1b191cb5SApple OSS Distributions 	           PROC_POLICY_RUSAGE_CPU,
531*1b191cb5SApple OSS Distributions 	           NULL, pid, 0);
532*1b191cb5SApple OSS Distributions }
533*1b191cb5SApple OSS Distributions 
534*1b191cb5SApple OSS Distributions int
proc_disable_cpumon(pid_t pid)535*1b191cb5SApple OSS Distributions proc_disable_cpumon(pid_t pid)
536*1b191cb5SApple OSS Distributions {
537*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
538*1b191cb5SApple OSS Distributions 
539*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr = PROC_POLICY_RSRCACT_NOTIFY_EXC;
540*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_percentage = PROC_POLICY_CPUMON_DISABLE;
541*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_interval = 0;
542*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_deadline = 0;
543*1b191cb5SApple OSS Distributions 
544*1b191cb5SApple OSS Distributions 	return __process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_SET, PROC_POLICY_RESOURCE_USAGE,
545*1b191cb5SApple OSS Distributions 	           PROC_POLICY_RUSAGE_CPU, (proc_policy_attribute_t*)&attr, pid, 0);
546*1b191cb5SApple OSS Distributions }
547*1b191cb5SApple OSS Distributions 
548*1b191cb5SApple OSS Distributions 
549*1b191cb5SApple OSS Distributions /*
550*1b191cb5SApple OSS Distributions  * Turn on the CPU usage monitor using the supplied parameters, and make
551*1b191cb5SApple OSS Distributions  * violations of the monitor fatal.
552*1b191cb5SApple OSS Distributions  *
553*1b191cb5SApple OSS Distributions  * Returns:  0 on success;
554*1b191cb5SApple OSS Distributions  *	    -1 on failure and sets errno
555*1b191cb5SApple OSS Distributions  */
556*1b191cb5SApple OSS Distributions int
proc_set_cpumon_params_fatal(pid_t pid,int percentage,int interval)557*1b191cb5SApple OSS Distributions proc_set_cpumon_params_fatal(pid_t pid, int percentage, int interval)
558*1b191cb5SApple OSS Distributions {
559*1b191cb5SApple OSS Distributions 	int current_percentage = 0;
560*1b191cb5SApple OSS Distributions 	int current_interval = 0;   /* intervals are in seconds */
561*1b191cb5SApple OSS Distributions 	int ret = 0;
562*1b191cb5SApple OSS Distributions 
563*1b191cb5SApple OSS Distributions 	if ((percentage <= 0) || (interval <= 0)) {
564*1b191cb5SApple OSS Distributions 		errno = EINVAL;
565*1b191cb5SApple OSS Distributions 		return -1;
566*1b191cb5SApple OSS Distributions 	}
567*1b191cb5SApple OSS Distributions 
568*1b191cb5SApple OSS Distributions 	/*
569*1b191cb5SApple OSS Distributions 	 * Do a simple query to see if CPU monitoring is
570*1b191cb5SApple OSS Distributions 	 * already active.  If either the percentage or the
571*1b191cb5SApple OSS Distributions 	 * interval is nonzero, then CPU monitoring is
572*1b191cb5SApple OSS Distributions 	 * already in use for this process.
573*1b191cb5SApple OSS Distributions 	 *
574*1b191cb5SApple OSS Distributions 	 * XXX: need set...() and set..fatal() to behave similarly.
575*1b191cb5SApple OSS Distributions 	 * Currently, this check prevents 1st party apps (which get a
576*1b191cb5SApple OSS Distributions 	 * default non-fatal monitor) not to get a fatal monitor.
577*1b191cb5SApple OSS Distributions 	 */
578*1b191cb5SApple OSS Distributions 	(void)proc_get_cpumon_params(pid, &current_percentage, &current_interval);
579*1b191cb5SApple OSS Distributions 	if (current_percentage || current_interval) {
580*1b191cb5SApple OSS Distributions 		/*
581*1b191cb5SApple OSS Distributions 		 * The CPU monitor appears to be active.
582*1b191cb5SApple OSS Distributions 		 * We choose not to disturb those settings.
583*1b191cb5SApple OSS Distributions 		 */
584*1b191cb5SApple OSS Distributions 		errno = EBUSY;
585*1b191cb5SApple OSS Distributions 		return -1;
586*1b191cb5SApple OSS Distributions 	}
587*1b191cb5SApple OSS Distributions 
588*1b191cb5SApple OSS Distributions 	if ((ret = proc_set_cpumon_params(pid, percentage, interval)) != 0) {
589*1b191cb5SApple OSS Distributions 		/* Failed to activate the CPU monitor */
590*1b191cb5SApple OSS Distributions 		return ret;
591*1b191cb5SApple OSS Distributions 	}
592*1b191cb5SApple OSS Distributions 
593*1b191cb5SApple OSS Distributions 	if ((ret = proc_rlimit_control(pid, RLIMIT_CPU_USAGE_MONITOR, (void *)(uintptr_t)CPUMON_MAKE_FATAL)) != 0) {
594*1b191cb5SApple OSS Distributions 		/* Failed to set termination, back out the CPU monitor settings. */
595*1b191cb5SApple OSS Distributions 		(void)proc_disable_cpumon(pid);
596*1b191cb5SApple OSS Distributions 	}
597*1b191cb5SApple OSS Distributions 
598*1b191cb5SApple OSS Distributions 	return ret;
599*1b191cb5SApple OSS Distributions }
600*1b191cb5SApple OSS Distributions 
601*1b191cb5SApple OSS Distributions int
proc_set_wakemon_params(pid_t pid,int rate_hz,int flags __unused)602*1b191cb5SApple OSS Distributions proc_set_wakemon_params(pid_t pid, int rate_hz, int flags __unused)
603*1b191cb5SApple OSS Distributions {
604*1b191cb5SApple OSS Distributions 	struct proc_rlimit_control_wakeupmon params;
605*1b191cb5SApple OSS Distributions 
606*1b191cb5SApple OSS Distributions 	params.wm_flags = WAKEMON_ENABLE;
607*1b191cb5SApple OSS Distributions 	params.wm_rate = rate_hz;
608*1b191cb5SApple OSS Distributions 
609*1b191cb5SApple OSS Distributions 	return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params);
610*1b191cb5SApple OSS Distributions }
611*1b191cb5SApple OSS Distributions 
612*1b191cb5SApple OSS Distributions #ifndef WAKEMON_GET_PARAMS
613*1b191cb5SApple OSS Distributions #define WAKEMON_GET_PARAMS 0x4
614*1b191cb5SApple OSS Distributions #define WAKEMON_SET_DEFAULTS 0x8
615*1b191cb5SApple OSS Distributions #endif
616*1b191cb5SApple OSS Distributions 
617*1b191cb5SApple OSS Distributions int
proc_get_wakemon_params(pid_t pid,int * rate_hz,int * flags)618*1b191cb5SApple OSS Distributions proc_get_wakemon_params(pid_t pid, int *rate_hz, int *flags)
619*1b191cb5SApple OSS Distributions {
620*1b191cb5SApple OSS Distributions 	struct proc_rlimit_control_wakeupmon params;
621*1b191cb5SApple OSS Distributions 	int error;
622*1b191cb5SApple OSS Distributions 
623*1b191cb5SApple OSS Distributions 	params.wm_flags = WAKEMON_GET_PARAMS;
624*1b191cb5SApple OSS Distributions 
625*1b191cb5SApple OSS Distributions 	if ((error = proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params)) != 0) {
626*1b191cb5SApple OSS Distributions 		return error;
627*1b191cb5SApple OSS Distributions 	}
628*1b191cb5SApple OSS Distributions 
629*1b191cb5SApple OSS Distributions 	*rate_hz = params.wm_rate;
630*1b191cb5SApple OSS Distributions 	*flags = params.wm_flags;
631*1b191cb5SApple OSS Distributions 
632*1b191cb5SApple OSS Distributions 	return 0;
633*1b191cb5SApple OSS Distributions }
634*1b191cb5SApple OSS Distributions 
635*1b191cb5SApple OSS Distributions int
proc_set_wakemon_defaults(pid_t pid)636*1b191cb5SApple OSS Distributions proc_set_wakemon_defaults(pid_t pid)
637*1b191cb5SApple OSS Distributions {
638*1b191cb5SApple OSS Distributions 	struct proc_rlimit_control_wakeupmon params;
639*1b191cb5SApple OSS Distributions 
640*1b191cb5SApple OSS Distributions 	params.wm_flags = WAKEMON_ENABLE | WAKEMON_SET_DEFAULTS;
641*1b191cb5SApple OSS Distributions 	params.wm_rate = -1;
642*1b191cb5SApple OSS Distributions 
643*1b191cb5SApple OSS Distributions 	return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params);
644*1b191cb5SApple OSS Distributions }
645*1b191cb5SApple OSS Distributions 
646*1b191cb5SApple OSS Distributions int
proc_disable_wakemon(pid_t pid)647*1b191cb5SApple OSS Distributions proc_disable_wakemon(pid_t pid)
648*1b191cb5SApple OSS Distributions {
649*1b191cb5SApple OSS Distributions 	struct proc_rlimit_control_wakeupmon params;
650*1b191cb5SApple OSS Distributions 
651*1b191cb5SApple OSS Distributions 	params.wm_flags = WAKEMON_DISABLE;
652*1b191cb5SApple OSS Distributions 	params.wm_rate = -1;
653*1b191cb5SApple OSS Distributions 
654*1b191cb5SApple OSS Distributions 	return proc_rlimit_control(pid, RLIMIT_WAKEUPS_MONITOR, &params);
655*1b191cb5SApple OSS Distributions }
656*1b191cb5SApple OSS Distributions 
657*1b191cb5SApple OSS Distributions int
proc_list_uptrs(int pid,uint64_t * buf,uint32_t bufsz)658*1b191cb5SApple OSS Distributions proc_list_uptrs(int pid, uint64_t *buf, uint32_t bufsz)
659*1b191cb5SApple OSS Distributions {
660*1b191cb5SApple OSS Distributions 	return __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDLISTUPTRS, 0,
661*1b191cb5SApple OSS Distributions 	           buf, bufsz);
662*1b191cb5SApple OSS Distributions }
663*1b191cb5SApple OSS Distributions 
664*1b191cb5SApple OSS Distributions int
proc_list_dynkqueueids(int pid,kqueue_id_t * buf,uint32_t bufsz)665*1b191cb5SApple OSS Distributions proc_list_dynkqueueids(int pid, kqueue_id_t *buf, uint32_t bufsz)
666*1b191cb5SApple OSS Distributions {
667*1b191cb5SApple OSS Distributions 	return __proc_info(PROC_INFO_CALL_PIDINFO, pid, PROC_PIDLISTDYNKQUEUES, 0,
668*1b191cb5SApple OSS Distributions 	           buf, bufsz);
669*1b191cb5SApple OSS Distributions }
670*1b191cb5SApple OSS Distributions 
671*1b191cb5SApple OSS Distributions 
672*1b191cb5SApple OSS Distributions int
proc_setcpu_percentage(pid_t pid,int action,int percentage)673*1b191cb5SApple OSS Distributions proc_setcpu_percentage(pid_t pid, int action, int percentage)
674*1b191cb5SApple OSS Distributions {
675*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
676*1b191cb5SApple OSS Distributions 
677*1b191cb5SApple OSS Distributions 	bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
678*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr = action;
679*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_percentage = percentage;
680*1b191cb5SApple OSS Distributions 	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) {
681*1b191cb5SApple OSS Distributions 		return 0;
682*1b191cb5SApple OSS Distributions 	} else {
683*1b191cb5SApple OSS Distributions 		return errno;
684*1b191cb5SApple OSS Distributions 	}
685*1b191cb5SApple OSS Distributions }
686*1b191cb5SApple OSS Distributions 
687*1b191cb5SApple OSS Distributions int
proc_reset_footprint_interval(pid_t pid)688*1b191cb5SApple OSS Distributions proc_reset_footprint_interval(pid_t pid)
689*1b191cb5SApple OSS Distributions {
690*1b191cb5SApple OSS Distributions 	return proc_rlimit_control(pid, RLIMIT_FOOTPRINT_INTERVAL, (void *)(uintptr_t)FOOTPRINT_INTERVAL_RESET);
691*1b191cb5SApple OSS Distributions }
692*1b191cb5SApple OSS Distributions 
693*1b191cb5SApple OSS Distributions int
proc_clear_cpulimits(pid_t pid)694*1b191cb5SApple OSS Distributions proc_clear_cpulimits(pid_t pid)
695*1b191cb5SApple OSS Distributions {
696*1b191cb5SApple OSS Distributions 	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) {
697*1b191cb5SApple OSS Distributions 		return 0;
698*1b191cb5SApple OSS Distributions 	} else {
699*1b191cb5SApple OSS Distributions 		return errno;
700*1b191cb5SApple OSS Distributions 	}
701*1b191cb5SApple OSS Distributions }
702*1b191cb5SApple OSS Distributions 
703*1b191cb5SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
704*1b191cb5SApple OSS Distributions 
705*1b191cb5SApple OSS Distributions int
proc_setcpu_deadline(pid_t pid,int action,uint64_t deadline)706*1b191cb5SApple OSS Distributions proc_setcpu_deadline(pid_t pid, int action, uint64_t deadline)
707*1b191cb5SApple OSS Distributions {
708*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
709*1b191cb5SApple OSS Distributions 
710*1b191cb5SApple OSS Distributions 	bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
711*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr = action;
712*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_deadline = deadline;
713*1b191cb5SApple OSS Distributions 	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) {
714*1b191cb5SApple OSS Distributions 		return 0;
715*1b191cb5SApple OSS Distributions 	} else {
716*1b191cb5SApple OSS Distributions 		return errno;
717*1b191cb5SApple OSS Distributions 	}
718*1b191cb5SApple OSS Distributions }
719*1b191cb5SApple OSS Distributions 
720*1b191cb5SApple OSS Distributions int
proc_setcpu_percentage_withdeadline(pid_t pid,int action,int percentage,uint64_t deadline)721*1b191cb5SApple OSS Distributions proc_setcpu_percentage_withdeadline(pid_t pid, int action, int percentage, uint64_t deadline)
722*1b191cb5SApple OSS Distributions {
723*1b191cb5SApple OSS Distributions 	proc_policy_cpuusage_attr_t attr;
724*1b191cb5SApple OSS Distributions 
725*1b191cb5SApple OSS Distributions 	bzero(&attr, sizeof(proc_policy_cpuusage_attr_t));
726*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr = action;
727*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_percentage = percentage;
728*1b191cb5SApple OSS Distributions 	attr.ppattr_cpu_attr_deadline = deadline;
729*1b191cb5SApple OSS Distributions 	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) {
730*1b191cb5SApple OSS Distributions 		return 0;
731*1b191cb5SApple OSS Distributions 	} else {
732*1b191cb5SApple OSS Distributions 		return errno;
733*1b191cb5SApple OSS Distributions 	}
734*1b191cb5SApple OSS Distributions }
735*1b191cb5SApple OSS Distributions 
736*1b191cb5SApple OSS Distributions int
proc_appstate(int pid,int * appstatep)737*1b191cb5SApple OSS Distributions proc_appstate(int pid, int * appstatep)
738*1b191cb5SApple OSS Distributions {
739*1b191cb5SApple OSS Distributions 	int state;
740*1b191cb5SApple OSS Distributions 
741*1b191cb5SApple OSS Distributions 	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) {
742*1b191cb5SApple OSS Distributions 		if (appstatep != NULL) {
743*1b191cb5SApple OSS Distributions 			*appstatep = state;
744*1b191cb5SApple OSS Distributions 		}
745*1b191cb5SApple OSS Distributions 		return 0;
746*1b191cb5SApple OSS Distributions 	} else {
747*1b191cb5SApple OSS Distributions 		return errno;
748*1b191cb5SApple OSS Distributions 	}
749*1b191cb5SApple OSS Distributions }
750*1b191cb5SApple OSS Distributions 
751*1b191cb5SApple OSS Distributions int
proc_setappstate(int pid,int appstate)752*1b191cb5SApple OSS Distributions proc_setappstate(int pid, int appstate)
753*1b191cb5SApple OSS Distributions {
754*1b191cb5SApple OSS Distributions 	int state = appstate;
755*1b191cb5SApple OSS Distributions 
756*1b191cb5SApple OSS Distributions 	switch (state) {
757*1b191cb5SApple OSS Distributions 	case PROC_APPSTATE_NONE:
758*1b191cb5SApple OSS Distributions 	case PROC_APPSTATE_ACTIVE:
759*1b191cb5SApple OSS Distributions 	case PROC_APPSTATE_INACTIVE:
760*1b191cb5SApple OSS Distributions 	case PROC_APPSTATE_BACKGROUND:
761*1b191cb5SApple OSS Distributions 	case PROC_APPSTATE_NONUI:
762*1b191cb5SApple OSS Distributions 		break;
763*1b191cb5SApple OSS Distributions 	default:
764*1b191cb5SApple OSS Distributions 		return EINVAL;
765*1b191cb5SApple OSS Distributions 	}
766*1b191cb5SApple OSS Distributions 	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) {
767*1b191cb5SApple OSS Distributions 		return 0;
768*1b191cb5SApple OSS Distributions 	} else {
769*1b191cb5SApple OSS Distributions 		return errno;
770*1b191cb5SApple OSS Distributions 	}
771*1b191cb5SApple OSS Distributions }
772*1b191cb5SApple OSS Distributions 
773*1b191cb5SApple OSS Distributions int
proc_devstatusnotify(int devicestatus)774*1b191cb5SApple OSS Distributions proc_devstatusnotify(int devicestatus)
775*1b191cb5SApple OSS Distributions {
776*1b191cb5SApple OSS Distributions 	int state = devicestatus;
777*1b191cb5SApple OSS Distributions 
778*1b191cb5SApple OSS Distributions 	switch (devicestatus) {
779*1b191cb5SApple OSS Distributions 	case PROC_DEVSTATUS_SHORTTERM:
780*1b191cb5SApple OSS Distributions 	case PROC_DEVSTATUS_LONGTERM:
781*1b191cb5SApple OSS Distributions 		break;
782*1b191cb5SApple OSS Distributions 	default:
783*1b191cb5SApple OSS Distributions 		return EINVAL;
784*1b191cb5SApple OSS Distributions 	}
785*1b191cb5SApple OSS Distributions 
786*1b191cb5SApple OSS Distributions 	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) {
787*1b191cb5SApple OSS Distributions 		return 0;
788*1b191cb5SApple OSS Distributions 	} else {
789*1b191cb5SApple OSS Distributions 		return errno;
790*1b191cb5SApple OSS Distributions 	}
791*1b191cb5SApple OSS Distributions }
792*1b191cb5SApple OSS Distributions 
793*1b191cb5SApple OSS Distributions int
proc_pidbind(int pid,uint64_t threadid,int bind)794*1b191cb5SApple OSS Distributions proc_pidbind(int pid, uint64_t threadid, int bind)
795*1b191cb5SApple OSS Distributions {
796*1b191cb5SApple OSS Distributions 	int state = bind;
797*1b191cb5SApple OSS Distributions 	pid_t passpid = pid;
798*1b191cb5SApple OSS Distributions 
799*1b191cb5SApple OSS Distributions 	switch (bind) {
800*1b191cb5SApple OSS Distributions 	case PROC_PIDBIND_CLEAR:
801*1b191cb5SApple OSS Distributions 		passpid = getpid();             /* ignore pid on clear */
802*1b191cb5SApple OSS Distributions 		break;
803*1b191cb5SApple OSS Distributions 	case PROC_PIDBIND_SET:
804*1b191cb5SApple OSS Distributions 		break;
805*1b191cb5SApple OSS Distributions 	default:
806*1b191cb5SApple OSS Distributions 		return EINVAL;
807*1b191cb5SApple OSS Distributions 	}
808*1b191cb5SApple OSS Distributions 	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) {
809*1b191cb5SApple OSS Distributions 		return 0;
810*1b191cb5SApple OSS Distributions 	} else {
811*1b191cb5SApple OSS Distributions 		return errno;
812*1b191cb5SApple OSS Distributions 	}
813*1b191cb5SApple OSS Distributions }
814*1b191cb5SApple OSS Distributions 
815*1b191cb5SApple OSS Distributions int
proc_can_use_foreground_hw(int pid,uint32_t * reason)816*1b191cb5SApple OSS Distributions proc_can_use_foreground_hw(int pid, uint32_t *reason)
817*1b191cb5SApple OSS Distributions {
818*1b191cb5SApple OSS Distributions 	return __proc_info(PROC_INFO_CALL_CANUSEFGHW, pid, 0, 0, reason, sizeof(*reason));
819*1b191cb5SApple OSS Distributions }
820*1b191cb5SApple OSS Distributions #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
821*1b191cb5SApple OSS Distributions 
822*1b191cb5SApple OSS Distributions 
823*1b191cb5SApple OSS Distributions /* Donate importance to adaptive processes from this process */
824*1b191cb5SApple OSS Distributions int
proc_donate_importance_boost(void)825*1b191cb5SApple OSS Distributions proc_donate_importance_boost(void)
826*1b191cb5SApple OSS Distributions {
827*1b191cb5SApple OSS Distributions 	int rval;
828*1b191cb5SApple OSS Distributions 
829*1b191cb5SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
830*1b191cb5SApple OSS Distributions 	rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
831*1b191cb5SApple OSS Distributions 	    PROC_POLICY_ACTION_ENABLE,
832*1b191cb5SApple OSS Distributions 	    PROC_POLICY_APPTYPE,
833*1b191cb5SApple OSS Distributions 	    PROC_POLICY_IOS_DONATEIMP,
834*1b191cb5SApple OSS Distributions 	    NULL, getpid(), (uint64_t)0);
835*1b191cb5SApple OSS Distributions #else /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
836*1b191cb5SApple OSS Distributions 	rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
837*1b191cb5SApple OSS Distributions 	    PROC_POLICY_ACTION_SET,
838*1b191cb5SApple OSS Distributions 	    PROC_POLICY_BOOST,
839*1b191cb5SApple OSS Distributions 	    PROC_POLICY_IMP_DONATION,
840*1b191cb5SApple OSS Distributions 	    NULL, getpid(), 0);
841*1b191cb5SApple OSS Distributions #endif /* (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
842*1b191cb5SApple OSS Distributions 
843*1b191cb5SApple OSS Distributions 	if (rval == 0) {
844*1b191cb5SApple OSS Distributions 		return 0;
845*1b191cb5SApple OSS Distributions 	} else {
846*1b191cb5SApple OSS Distributions 		return errno;
847*1b191cb5SApple OSS Distributions 	}
848*1b191cb5SApple OSS Distributions }
849*1b191cb5SApple OSS Distributions 
850*1b191cb5SApple OSS Distributions static __attribute__((noinline)) void
proc_importance_bad_assertion(char * reason)851*1b191cb5SApple OSS Distributions proc_importance_bad_assertion(char *reason)
852*1b191cb5SApple OSS Distributions {
853*1b191cb5SApple OSS Distributions 	(void)reason;
854*1b191cb5SApple OSS Distributions }
855*1b191cb5SApple OSS Distributions 
856*1b191cb5SApple OSS Distributions /*
857*1b191cb5SApple OSS Distributions  * Use the address of these variables as the token.  This way, they can be
858*1b191cb5SApple OSS Distributions  * printed in the debugger as useful names.
859*1b191cb5SApple OSS Distributions  */
860*1b191cb5SApple OSS Distributions uint64_t important_boost_assertion_token = 0xfafafafafafafafa;
861*1b191cb5SApple OSS Distributions uint64_t normal_boost_assertion_token    = 0xfbfbfbfbfbfbfbfb;
862*1b191cb5SApple OSS Distributions uint64_t non_boost_assertion_token       = 0xfcfcfcfcfcfcfcfc;
863*1b191cb5SApple OSS Distributions uint64_t denap_boost_assertion_token     = 0xfdfdfdfdfdfdfdfd;
864*1b191cb5SApple OSS Distributions 
865*1b191cb5SApple OSS Distributions /*
866*1b191cb5SApple OSS Distributions  * Accept the boost on a message, or request another boost assertion
867*1b191cb5SApple OSS Distributions  * if we have already accepted the implicit boost for this message.
868*1b191cb5SApple OSS Distributions  *
869*1b191cb5SApple OSS Distributions  * Returns EOVERFLOW if an attempt is made to take an extra assertion when not boosted.
870*1b191cb5SApple OSS Distributions  *
871*1b191cb5SApple OSS Distributions  * Returns EIO if the message was not a boosting message.
872*1b191cb5SApple OSS Distributions  * TODO: Return a 'non-boost' token instead.
873*1b191cb5SApple OSS Distributions  */
874*1b191cb5SApple OSS Distributions int
proc_importance_assertion_begin_with_msg(mach_msg_header_t * msg,__unused mach_msg_trailer_t * trailer,uint64_t * assertion_token)875*1b191cb5SApple OSS Distributions proc_importance_assertion_begin_with_msg(mach_msg_header_t  *msg,
876*1b191cb5SApple OSS Distributions     __unused mach_msg_trailer_t *trailer,
877*1b191cb5SApple OSS Distributions     uint64_t           *assertion_token)
878*1b191cb5SApple OSS Distributions {
879*1b191cb5SApple OSS Distributions 	int rval = 0;
880*1b191cb5SApple OSS Distributions 
881*1b191cb5SApple OSS Distributions 	if (assertion_token == NULL) {
882*1b191cb5SApple OSS Distributions 		return EINVAL;
883*1b191cb5SApple OSS Distributions 	}
884*1b191cb5SApple OSS Distributions 
885*1b191cb5SApple OSS Distributions #define LEGACYBOOSTMASK (MACH_MSGH_BITS_VOUCHER_MASK | MACH_MSGH_BITS_RAISEIMP)
886*1b191cb5SApple OSS Distributions #define LEGACYBOOSTED(m) (((m)->msgh_bits & LEGACYBOOSTMASK) == MACH_MSGH_BITS_RAISEIMP)
887*1b191cb5SApple OSS Distributions 
888*1b191cb5SApple OSS Distributions 	/* Is this a legacy boosted message? */
889*1b191cb5SApple OSS Distributions 	if (LEGACYBOOSTED(msg)) {
890*1b191cb5SApple OSS Distributions 		/*
891*1b191cb5SApple OSS Distributions 		 * Have we accepted the implicit boost for this message yet?
892*1b191cb5SApple OSS Distributions 		 * If we haven't accepted it yet, no need to call into kernel.
893*1b191cb5SApple OSS Distributions 		 */
894*1b191cb5SApple OSS Distributions 		if ((msg->msgh_bits & MACH_MSGH_BITS_IMPHOLDASRT) == 0) {
895*1b191cb5SApple OSS Distributions 			msg->msgh_bits |= MACH_MSGH_BITS_IMPHOLDASRT;
896*1b191cb5SApple OSS Distributions 			*assertion_token = (uint64_t) &important_boost_assertion_token;
897*1b191cb5SApple OSS Distributions 			return 0;
898*1b191cb5SApple OSS Distributions 		}
899*1b191cb5SApple OSS Distributions 
900*1b191cb5SApple OSS Distributions 		/* Request an additional boost count */
901*1b191cb5SApple OSS Distributions 		rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
902*1b191cb5SApple OSS Distributions 		    PROC_POLICY_ACTION_HOLD,
903*1b191cb5SApple OSS Distributions 		    PROC_POLICY_BOOST,
904*1b191cb5SApple OSS Distributions 		    PROC_POLICY_IMP_IMPORTANT,
905*1b191cb5SApple OSS Distributions 		    NULL, getpid(), 0);
906*1b191cb5SApple OSS Distributions 		if (rval == 0) {
907*1b191cb5SApple OSS Distributions 			*assertion_token = (uint64_t) &important_boost_assertion_token;
908*1b191cb5SApple OSS Distributions 			return 0;
909*1b191cb5SApple OSS Distributions 		} else if (errno == EOVERFLOW) {
910*1b191cb5SApple OSS Distributions 			proc_importance_bad_assertion("Attempted to take assertion while not boosted");
911*1b191cb5SApple OSS Distributions 			return errno;
912*1b191cb5SApple OSS Distributions 		} else {
913*1b191cb5SApple OSS Distributions 			return errno;
914*1b191cb5SApple OSS Distributions 		}
915*1b191cb5SApple OSS Distributions 	}
916*1b191cb5SApple OSS Distributions 
917*1b191cb5SApple OSS Distributions 	return EIO;
918*1b191cb5SApple OSS Distributions }
919*1b191cb5SApple OSS Distributions 
920*1b191cb5SApple OSS Distributions 
921*1b191cb5SApple OSS Distributions /*
922*1b191cb5SApple OSS Distributions  * Drop a boost assertion.
923*1b191cb5SApple OSS Distributions  * Returns EOVERFLOW on boost assertion underflow.
924*1b191cb5SApple OSS Distributions  */
925*1b191cb5SApple OSS Distributions int
proc_importance_assertion_complete(uint64_t assertion_token)926*1b191cb5SApple OSS Distributions proc_importance_assertion_complete(uint64_t assertion_token)
927*1b191cb5SApple OSS Distributions {
928*1b191cb5SApple OSS Distributions 	int rval = 0;
929*1b191cb5SApple OSS Distributions 
930*1b191cb5SApple OSS Distributions 	if (assertion_token == 0) {
931*1b191cb5SApple OSS Distributions 		return 0;
932*1b191cb5SApple OSS Distributions 	}
933*1b191cb5SApple OSS Distributions 
934*1b191cb5SApple OSS Distributions 	if (assertion_token == (uint64_t) &important_boost_assertion_token) {
935*1b191cb5SApple OSS Distributions 		rval = __process_policy(PROC_POLICY_SCOPE_PROCESS,
936*1b191cb5SApple OSS Distributions 		    PROC_POLICY_ACTION_DROP,
937*1b191cb5SApple OSS Distributions 		    PROC_POLICY_BOOST,
938*1b191cb5SApple OSS Distributions 		    PROC_POLICY_IMP_IMPORTANT,
939*1b191cb5SApple OSS Distributions 		    NULL, getpid(), 0);
940*1b191cb5SApple OSS Distributions 		if (rval == 0) {
941*1b191cb5SApple OSS Distributions 			return 0;
942*1b191cb5SApple OSS Distributions 		} else if (errno == EOVERFLOW) {
943*1b191cb5SApple OSS Distributions 			proc_importance_bad_assertion("Attempted to drop too many assertions");
944*1b191cb5SApple OSS Distributions 			return errno;
945*1b191cb5SApple OSS Distributions 		} else {
946*1b191cb5SApple OSS Distributions 			return errno;
947*1b191cb5SApple OSS Distributions 		}
948*1b191cb5SApple OSS Distributions 	} else {
949*1b191cb5SApple OSS Distributions 		proc_importance_bad_assertion("Attempted to drop assertion with invalid token");
950*1b191cb5SApple OSS Distributions 		return EIO;
951*1b191cb5SApple OSS Distributions 	}
952*1b191cb5SApple OSS Distributions }
953*1b191cb5SApple OSS Distributions 
954*1b191cb5SApple OSS Distributions /*
955*1b191cb5SApple OSS Distributions  * Accept the De-Nap boost on a message, or request another boost assertion
956*1b191cb5SApple OSS Distributions  * if we have already accepted the implicit boost for this message.
957*1b191cb5SApple OSS Distributions  *
958*1b191cb5SApple OSS Distributions  * Interface is deprecated before it really got started - just as synonym
959*1b191cb5SApple OSS Distributions  * for proc_importance_assertion_begin_with_msg() now.
960*1b191cb5SApple OSS Distributions  */
961*1b191cb5SApple OSS Distributions int
proc_denap_assertion_begin_with_msg(mach_msg_header_t * msg,uint64_t * assertion_token)962*1b191cb5SApple OSS Distributions proc_denap_assertion_begin_with_msg(mach_msg_header_t  *msg,
963*1b191cb5SApple OSS Distributions     uint64_t           *assertion_token)
964*1b191cb5SApple OSS Distributions {
965*1b191cb5SApple OSS Distributions #pragma clang diagnostic push
966*1b191cb5SApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated-declarations"
967*1b191cb5SApple OSS Distributions 	return proc_importance_assertion_begin_with_msg(msg, NULL, assertion_token);
968*1b191cb5SApple OSS Distributions #pragma clang diagnostic pop
969*1b191cb5SApple OSS Distributions }
970*1b191cb5SApple OSS Distributions 
971*1b191cb5SApple OSS Distributions 
972*1b191cb5SApple OSS Distributions /*
973*1b191cb5SApple OSS Distributions  * Drop a denap boost assertion.
974*1b191cb5SApple OSS Distributions  *
975*1b191cb5SApple OSS Distributions  * Interface is deprecated before it really got started - just a synonym
976*1b191cb5SApple OSS Distributions  * for proc_importance_assertion_complete() now.
977*1b191cb5SApple OSS Distributions  */
978*1b191cb5SApple OSS Distributions int
proc_denap_assertion_complete(uint64_t assertion_token)979*1b191cb5SApple OSS Distributions proc_denap_assertion_complete(uint64_t assertion_token)
980*1b191cb5SApple OSS Distributions {
981*1b191cb5SApple OSS Distributions 	return proc_importance_assertion_complete(assertion_token);
982*1b191cb5SApple OSS Distributions }
983*1b191cb5SApple OSS Distributions 
984*1b191cb5SApple OSS Distributions #if !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
985*1b191cb5SApple OSS Distributions 
986*1b191cb5SApple OSS Distributions int
proc_clear_vmpressure(pid_t pid)987*1b191cb5SApple OSS Distributions proc_clear_vmpressure(pid_t pid)
988*1b191cb5SApple OSS Distributions {
989*1b191cb5SApple OSS Distributions 	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) {
990*1b191cb5SApple OSS Distributions 		return 0;
991*1b191cb5SApple OSS Distributions 	} else {
992*1b191cb5SApple OSS Distributions 		return errno;
993*1b191cb5SApple OSS Distributions 	}
994*1b191cb5SApple OSS Distributions }
995*1b191cb5SApple OSS Distributions 
996*1b191cb5SApple OSS Distributions /* set the current process as one who can resume suspended processes due to low virtual memory. Need to be root */
997*1b191cb5SApple OSS Distributions int
proc_set_owner_vmpressure(void)998*1b191cb5SApple OSS Distributions proc_set_owner_vmpressure(void)
999*1b191cb5SApple OSS Distributions {
1000*1b191cb5SApple OSS Distributions 	int retval;
1001*1b191cb5SApple OSS Distributions 
1002*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_VMRSRCOWNER, (uint64_t)0, NULL, 0)) == -1) {
1003*1b191cb5SApple OSS Distributions 		return errno;
1004*1b191cb5SApple OSS Distributions 	}
1005*1b191cb5SApple OSS Distributions 
1006*1b191cb5SApple OSS Distributions 	return 0;
1007*1b191cb5SApple OSS Distributions }
1008*1b191cb5SApple OSS Distributions 
1009*1b191cb5SApple OSS Distributions /* mark yourself to delay idle sleep on disk IO */
1010*1b191cb5SApple OSS Distributions int
proc_set_delayidlesleep(void)1011*1b191cb5SApple OSS Distributions proc_set_delayidlesleep(void)
1012*1b191cb5SApple OSS Distributions {
1013*1b191cb5SApple OSS Distributions 	int retval;
1014*1b191cb5SApple OSS Distributions 
1015*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_DELAYIDLESLEEP, (uint64_t)1, NULL, 0)) == -1) {
1016*1b191cb5SApple OSS Distributions 		return errno;
1017*1b191cb5SApple OSS Distributions 	}
1018*1b191cb5SApple OSS Distributions 
1019*1b191cb5SApple OSS Distributions 	return 0;
1020*1b191cb5SApple OSS Distributions }
1021*1b191cb5SApple OSS Distributions 
1022*1b191cb5SApple OSS Distributions /* Reset yourself to delay idle sleep on disk IO, if already set */
1023*1b191cb5SApple OSS Distributions int
proc_clear_delayidlesleep(void)1024*1b191cb5SApple OSS Distributions proc_clear_delayidlesleep(void)
1025*1b191cb5SApple OSS Distributions {
1026*1b191cb5SApple OSS Distributions 	int retval;
1027*1b191cb5SApple OSS Distributions 
1028*1b191cb5SApple OSS Distributions 	if ((retval = __proc_info(PROC_INFO_CALL_SETCONTROL, getpid(), PROC_SELFSET_DELAYIDLESLEEP, (uint64_t)0, NULL, 0)) == -1) {
1029*1b191cb5SApple OSS Distributions 		return errno;
1030*1b191cb5SApple OSS Distributions 	}
1031*1b191cb5SApple OSS Distributions 
1032*1b191cb5SApple OSS Distributions 	return 0;
1033*1b191cb5SApple OSS Distributions }
1034*1b191cb5SApple OSS Distributions 
1035*1b191cb5SApple OSS Distributions /* disable the launch time backgroudn policy and restore the process to default group */
1036*1b191cb5SApple OSS Distributions int
proc_disable_apptype(pid_t pid,int apptype)1037*1b191cb5SApple OSS Distributions proc_disable_apptype(pid_t pid, int apptype)
1038*1b191cb5SApple OSS Distributions {
1039*1b191cb5SApple OSS Distributions 	switch (apptype) {
1040*1b191cb5SApple OSS Distributions 	case PROC_POLICY_OSX_APPTYPE_TAL:
1041*1b191cb5SApple OSS Distributions 	case PROC_POLICY_OSX_APPTYPE_DASHCLIENT:
1042*1b191cb5SApple OSS Distributions 		break;
1043*1b191cb5SApple OSS Distributions 	default:
1044*1b191cb5SApple OSS Distributions 		return EINVAL;
1045*1b191cb5SApple OSS Distributions 	}
1046*1b191cb5SApple OSS Distributions 
1047*1b191cb5SApple OSS Distributions 	if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_DISABLE, PROC_POLICY_APPTYPE, apptype, NULL, pid, (uint64_t)0) != -1) {
1048*1b191cb5SApple OSS Distributions 		return 0;
1049*1b191cb5SApple OSS Distributions 	} else {
1050*1b191cb5SApple OSS Distributions 		return errno;
1051*1b191cb5SApple OSS Distributions 	}
1052*1b191cb5SApple OSS Distributions }
1053*1b191cb5SApple OSS Distributions 
1054*1b191cb5SApple OSS Distributions /* re-enable the launch time background policy if it had been disabled. */
1055*1b191cb5SApple OSS Distributions int
proc_enable_apptype(pid_t pid,int apptype)1056*1b191cb5SApple OSS Distributions proc_enable_apptype(pid_t pid, int apptype)
1057*1b191cb5SApple OSS Distributions {
1058*1b191cb5SApple OSS Distributions 	switch (apptype) {
1059*1b191cb5SApple OSS Distributions 	case PROC_POLICY_OSX_APPTYPE_TAL:
1060*1b191cb5SApple OSS Distributions 	case PROC_POLICY_OSX_APPTYPE_DASHCLIENT:
1061*1b191cb5SApple OSS Distributions 		break;
1062*1b191cb5SApple OSS Distributions 	default:
1063*1b191cb5SApple OSS Distributions 		return EINVAL;
1064*1b191cb5SApple OSS Distributions 	}
1065*1b191cb5SApple OSS Distributions 
1066*1b191cb5SApple OSS Distributions 	if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_ENABLE, PROC_POLICY_APPTYPE, apptype, NULL, pid, (uint64_t)0) != -1) {
1067*1b191cb5SApple OSS Distributions 		return 0;
1068*1b191cb5SApple OSS Distributions 	} else {
1069*1b191cb5SApple OSS Distributions 		return errno;
1070*1b191cb5SApple OSS Distributions 	}
1071*1b191cb5SApple OSS Distributions }
1072*1b191cb5SApple OSS Distributions 
1073*1b191cb5SApple OSS Distributions #if !TARGET_OS_SIMULATOR
1074*1b191cb5SApple OSS Distributions 
1075*1b191cb5SApple OSS Distributions int
proc_suppress(__unused pid_t pid,__unused uint64_t * generation)1076*1b191cb5SApple OSS Distributions proc_suppress(__unused pid_t pid, __unused uint64_t *generation)
1077*1b191cb5SApple OSS Distributions {
1078*1b191cb5SApple OSS Distributions 	return 0;
1079*1b191cb5SApple OSS Distributions }
1080*1b191cb5SApple OSS Distributions 
1081*1b191cb5SApple OSS Distributions #endif /* !TARGET_OS_SIMULATOR */
1082*1b191cb5SApple OSS Distributions 
1083*1b191cb5SApple OSS Distributions #endif /* !(TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) */
1084*1b191cb5SApple OSS Distributions 
1085*1b191cb5SApple OSS Distributions int
proc_set_no_smt(void)1086*1b191cb5SApple OSS Distributions proc_set_no_smt(void)
1087*1b191cb5SApple OSS Distributions {
1088*1b191cb5SApple OSS Distributions 	if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, getpid(), (uint64_t)0) == -1) {
1089*1b191cb5SApple OSS Distributions 		return errno;
1090*1b191cb5SApple OSS Distributions 	}
1091*1b191cb5SApple OSS Distributions 	return 0;
1092*1b191cb5SApple OSS Distributions }
1093*1b191cb5SApple OSS Distributions 
1094*1b191cb5SApple OSS Distributions int
proc_setthread_no_smt(void)1095*1b191cb5SApple OSS Distributions proc_setthread_no_smt(void)
1096*1b191cb5SApple OSS Distributions {
1097*1b191cb5SApple OSS Distributions 	extern uint64_t __thread_selfid(void);
1098*1b191cb5SApple OSS Distributions 	if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, 0, __thread_selfid()) == -1) {
1099*1b191cb5SApple OSS Distributions 		return errno;
1100*1b191cb5SApple OSS Distributions 	}
1101*1b191cb5SApple OSS Distributions 	return 0;
1102*1b191cb5SApple OSS Distributions }
1103*1b191cb5SApple OSS Distributions 
1104*1b191cb5SApple OSS Distributions int
proc_set_csm(uint32_t flags)1105*1b191cb5SApple OSS Distributions proc_set_csm(uint32_t flags)
1106*1b191cb5SApple OSS Distributions {
1107*1b191cb5SApple OSS Distributions 	const uint32_t mask = PROC_CSM_ALL | PROC_CSM_TECS | PROC_CSM_NOSMT;
1108*1b191cb5SApple OSS Distributions 	if ((flags & ~mask) != 0) {
1109*1b191cb5SApple OSS Distributions 		return EINVAL;
1110*1b191cb5SApple OSS Distributions 	}
1111*1b191cb5SApple OSS Distributions 
1112*1b191cb5SApple OSS Distributions 	if (flags & (PROC_CSM_NOSMT | PROC_CSM_ALL)) {
1113*1b191cb5SApple OSS Distributions 		if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, getpid(), (uint64_t)0) == -1) {
1114*1b191cb5SApple OSS Distributions 			return errno;
1115*1b191cb5SApple OSS Distributions 		}
1116*1b191cb5SApple OSS Distributions 	}
1117*1b191cb5SApple OSS Distributions 
1118*1b191cb5SApple OSS Distributions 	if (flags & (PROC_CSM_TECS | PROC_CSM_ALL)) {
1119*1b191cb5SApple OSS Distributions 		if (__process_policy(PROC_POLICY_SCOPE_PROCESS, PROC_POLICY_ACTION_APPLY, PROC_POLICY_TECS, 0, NULL, getpid(), (uint64_t)0) == -1) {
1120*1b191cb5SApple OSS Distributions 			return errno;
1121*1b191cb5SApple OSS Distributions 		}
1122*1b191cb5SApple OSS Distributions 	}
1123*1b191cb5SApple OSS Distributions 
1124*1b191cb5SApple OSS Distributions 	return 0;
1125*1b191cb5SApple OSS Distributions }
1126*1b191cb5SApple OSS Distributions 
1127*1b191cb5SApple OSS Distributions int
proc_setthread_csm(uint32_t flags)1128*1b191cb5SApple OSS Distributions proc_setthread_csm(uint32_t flags)
1129*1b191cb5SApple OSS Distributions {
1130*1b191cb5SApple OSS Distributions 	extern uint64_t __thread_selfid(void);
1131*1b191cb5SApple OSS Distributions 	const uint32_t mask = PROC_CSM_ALL | PROC_CSM_TECS | PROC_CSM_NOSMT;
1132*1b191cb5SApple OSS Distributions 	if ((flags & ~mask) != 0) {
1133*1b191cb5SApple OSS Distributions 		return EINVAL;
1134*1b191cb5SApple OSS Distributions 	}
1135*1b191cb5SApple OSS Distributions 
1136*1b191cb5SApple OSS Distributions 	if (flags & (PROC_CSM_NOSMT | PROC_CSM_ALL)) {
1137*1b191cb5SApple OSS Distributions 		if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_NO_SMT, 0, NULL, 0, __thread_selfid()) == -1) {
1138*1b191cb5SApple OSS Distributions 			return errno;
1139*1b191cb5SApple OSS Distributions 		}
1140*1b191cb5SApple OSS Distributions 	}
1141*1b191cb5SApple OSS Distributions 
1142*1b191cb5SApple OSS Distributions 	if (flags & (PROC_CSM_TECS | PROC_CSM_ALL)) {
1143*1b191cb5SApple OSS Distributions 		if (__process_policy(PROC_POLICY_SCOPE_THREAD, PROC_POLICY_ACTION_APPLY, PROC_POLICY_TECS, 0, NULL, 0, __thread_selfid()) == -1) {
1144*1b191cb5SApple OSS Distributions 			return errno;
1145*1b191cb5SApple OSS Distributions 		}
1146*1b191cb5SApple OSS Distributions 	}
1147*1b191cb5SApple OSS Distributions 
1148*1b191cb5SApple OSS Distributions 	return 0;
1149*1b191cb5SApple OSS Distributions }
1150