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