xref: /xnu-8796.121.2/libsyscall/wrappers/libproc/libproc.h (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2006, 2007, 2010 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*c54f35caSApple OSS Distributions  * file.
12*c54f35caSApple OSS Distributions  *
13*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
14*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
19*c54f35caSApple OSS Distributions  * limitations under the License.
20*c54f35caSApple OSS Distributions  *
21*c54f35caSApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*c54f35caSApple OSS Distributions  */
23*c54f35caSApple OSS Distributions #ifndef _LIBPROC_H_
24*c54f35caSApple OSS Distributions #define _LIBPROC_H_
25*c54f35caSApple OSS Distributions 
26*c54f35caSApple OSS Distributions #include <sys/cdefs.h>
27*c54f35caSApple OSS Distributions #include <sys/param.h>
28*c54f35caSApple OSS Distributions #include <sys/types.h>
29*c54f35caSApple OSS Distributions #include <sys/stat.h>
30*c54f35caSApple OSS Distributions #include <sys/mount.h>
31*c54f35caSApple OSS Distributions #include <sys/resource.h>
32*c54f35caSApple OSS Distributions #include <stdint.h>
33*c54f35caSApple OSS Distributions #include <stdbool.h>
34*c54f35caSApple OSS Distributions #include <mach/message.h> /* for audit_token_t */
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions #include <sys/proc_info.h>
37*c54f35caSApple OSS Distributions 
38*c54f35caSApple OSS Distributions #include <Availability.h>
39*c54f35caSApple OSS Distributions #include <os/availability.h>
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions /*
42*c54f35caSApple OSS Distributions  * This header file contains private interfaces to obtain process information.
43*c54f35caSApple OSS Distributions  * These interfaces are subject to change in future releases.
44*c54f35caSApple OSS Distributions  */
45*c54f35caSApple OSS Distributions 
46*c54f35caSApple OSS Distributions /*!
47*c54f35caSApple OSS Distributions  *       @define PROC_LISTPIDSPATH_PATH_IS_VOLUME
48*c54f35caSApple OSS Distributions  *       @discussion This flag indicates that all processes that hold open
49*c54f35caSApple OSS Distributions  *               file references on the volume associated with the specified
50*c54f35caSApple OSS Distributions  *               path should be returned.
51*c54f35caSApple OSS Distributions  */
52*c54f35caSApple OSS Distributions #define PROC_LISTPIDSPATH_PATH_IS_VOLUME        1
53*c54f35caSApple OSS Distributions 
54*c54f35caSApple OSS Distributions 
55*c54f35caSApple OSS Distributions /*!
56*c54f35caSApple OSS Distributions  *       @define PROC_LISTPIDSPATH_EXCLUDE_EVTONLY
57*c54f35caSApple OSS Distributions  *       @discussion This flag indicates that file references that were opened
58*c54f35caSApple OSS Distributions  *               with the O_EVTONLY flag should be excluded from the matching
59*c54f35caSApple OSS Distributions  *               criteria.
60*c54f35caSApple OSS Distributions  */
61*c54f35caSApple OSS Distributions #define PROC_LISTPIDSPATH_EXCLUDE_EVTONLY       2
62*c54f35caSApple OSS Distributions 
63*c54f35caSApple OSS Distributions __BEGIN_DECLS
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions 
66*c54f35caSApple OSS Distributions /*!
67*c54f35caSApple OSS Distributions  *       @function proc_listpidspath
68*c54f35caSApple OSS Distributions  *       @discussion A function which will search through the current
69*c54f35caSApple OSS Distributions  *               processes looking for open file references which match
70*c54f35caSApple OSS Distributions  *               a specified path or volume.
71*c54f35caSApple OSS Distributions  *       @param type types of processes to be searched (see proc_listpids)
72*c54f35caSApple OSS Distributions  *       @param typeinfo adjunct information for type
73*c54f35caSApple OSS Distributions  *       @param path file or volume path
74*c54f35caSApple OSS Distributions  *       @param pathflags flags to control which files should be considered
75*c54f35caSApple OSS Distributions  *               during the process search.
76*c54f35caSApple OSS Distributions  *       @param buffer a C array of int-sized values to be filled with
77*c54f35caSApple OSS Distributions  *               process identifiers that hold an open file reference
78*c54f35caSApple OSS Distributions  *               matching the specified path or volume.  Pass NULL to
79*c54f35caSApple OSS Distributions  *               obtain the minimum buffer size needed to hold the
80*c54f35caSApple OSS Distributions  *               currently active processes.
81*c54f35caSApple OSS Distributions  *       @param buffersize the size (in bytes) of the provided buffer.
82*c54f35caSApple OSS Distributions  *       @result the number of bytes of data returned in the provided buffer;
83*c54f35caSApple OSS Distributions  *               -1 if an error was encountered;
84*c54f35caSApple OSS Distributions  */
85*c54f35caSApple OSS Distributions int     proc_listpidspath(uint32_t      type,
86*c54f35caSApple OSS Distributions     uint32_t      typeinfo,
87*c54f35caSApple OSS Distributions     const char    *path,
88*c54f35caSApple OSS Distributions     uint32_t      pathflags,
89*c54f35caSApple OSS Distributions     void          *buffer,
90*c54f35caSApple OSS Distributions     int           buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
91*c54f35caSApple OSS Distributions 
92*c54f35caSApple OSS Distributions int proc_listpids(uint32_t type, uint32_t typeinfo, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
93*c54f35caSApple OSS Distributions int proc_listallpids(void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_1);
94*c54f35caSApple OSS Distributions int proc_listpgrppids(pid_t pgrpid, void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_1);
95*c54f35caSApple OSS Distributions int proc_listchildpids(pid_t ppid, void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_1);
96*c54f35caSApple OSS Distributions int proc_pidinfo(int pid, int flavor, uint64_t arg, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
97*c54f35caSApple OSS Distributions int proc_pidfdinfo(int pid, int fd, int flavor, void * buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
98*c54f35caSApple OSS Distributions int proc_pidfileportinfo(int pid, uint32_t fileport, int flavor, void *buffer, int buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
99*c54f35caSApple OSS Distributions int proc_name(int pid, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
100*c54f35caSApple OSS Distributions int proc_regionfilename(int pid, uint64_t address, void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
101*c54f35caSApple OSS Distributions int proc_kmsgbuf(void * buffer, uint32_t buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
102*c54f35caSApple OSS Distributions int proc_pidpath(int pid, void * buffer, uint32_t  buffersize) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
103*c54f35caSApple OSS Distributions int proc_pidpath_audittoken(audit_token_t *audittoken, void * buffer, uint32_t  buffersize) API_AVAILABLE(macos(10.16), ios(14.0), watchos(7.0), tvos(14.0));
104*c54f35caSApple OSS Distributions int proc_libversion(int *major, int * minor) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
105*c54f35caSApple OSS Distributions 
106*c54f35caSApple OSS Distributions /*
107*c54f35caSApple OSS Distributions  * Return resource usage information for the given pid, which can be a live process or a zombie.
108*c54f35caSApple OSS Distributions  *
109*c54f35caSApple OSS Distributions  * Returns 0 on success; or -1 on failure, with errno set to indicate the specific error.
110*c54f35caSApple OSS Distributions  */
111*c54f35caSApple OSS Distributions int proc_pid_rusage(int pid, int flavor, rusage_info_t *buffer) __OSX_AVAILABLE_STARTING(__MAC_10_9, __IPHONE_7_0);
112*c54f35caSApple OSS Distributions 
113*c54f35caSApple OSS Distributions /*
114*c54f35caSApple OSS Distributions  * A process can use the following api to set its own process control
115*c54f35caSApple OSS Distributions  * state on resoure starvation. The argument can have one of the PROC_SETPC_XX values
116*c54f35caSApple OSS Distributions  */
117*c54f35caSApple OSS Distributions #define PROC_SETPC_NONE         0
118*c54f35caSApple OSS Distributions #define PROC_SETPC_THROTTLEMEM  1
119*c54f35caSApple OSS Distributions #define PROC_SETPC_SUSPEND      2
120*c54f35caSApple OSS Distributions #define PROC_SETPC_TERMINATE    3
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions int proc_setpcontrol(const int control) __OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_2);
123*c54f35caSApple OSS Distributions int proc_setpcontrol(const int control);
124*c54f35caSApple OSS Distributions 
125*c54f35caSApple OSS Distributions int proc_track_dirty(pid_t pid, uint32_t flags);
126*c54f35caSApple OSS Distributions int proc_set_dirty(pid_t pid, bool dirty);
127*c54f35caSApple OSS Distributions int proc_get_dirty(pid_t pid, uint32_t *flags);
128*c54f35caSApple OSS Distributions int proc_clear_dirty(pid_t pid, uint32_t flags);
129*c54f35caSApple OSS Distributions 
130*c54f35caSApple OSS Distributions int proc_terminate(pid_t pid, int *sig);
131*c54f35caSApple OSS Distributions int proc_terminate_all_rsr(int sig);
132*c54f35caSApple OSS Distributions 
133*c54f35caSApple OSS Distributions /*
134*c54f35caSApple OSS Distributions  * NO_SMT means that on an SMT CPU, this thread must be scheduled alone,
135*c54f35caSApple OSS Distributions  * with the paired CPU idle.
136*c54f35caSApple OSS Distributions  *
137*c54f35caSApple OSS Distributions  * Set NO_SMT on the current proc (all existing and future threads)
138*c54f35caSApple OSS Distributions  * This attribute is inherited on fork and exec
139*c54f35caSApple OSS Distributions  */
140*c54f35caSApple OSS Distributions int proc_set_no_smt(void) __API_AVAILABLE(macos(10.16));
141*c54f35caSApple OSS Distributions 
142*c54f35caSApple OSS Distributions /* Set NO_SMT on the current thread */
143*c54f35caSApple OSS Distributions int proc_setthread_no_smt(void) __API_AVAILABLE(macos(10.16));
144*c54f35caSApple OSS Distributions 
145*c54f35caSApple OSS Distributions /*
146*c54f35caSApple OSS Distributions  * CPU Security Mitigation APIs
147*c54f35caSApple OSS Distributions  *
148*c54f35caSApple OSS Distributions  * Set CPU security mitigation on the current proc (all existing and future threads)
149*c54f35caSApple OSS Distributions  * This attribute is inherited on fork and exec
150*c54f35caSApple OSS Distributions  */
151*c54f35caSApple OSS Distributions int proc_set_csm(uint32_t flags) __API_AVAILABLE(macos(10.16));
152*c54f35caSApple OSS Distributions 
153*c54f35caSApple OSS Distributions /* Set CPU security mitigation on the current thread */
154*c54f35caSApple OSS Distributions int proc_setthread_csm(uint32_t flags) __API_AVAILABLE(macos(10.16));
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions /*
157*c54f35caSApple OSS Distributions  * flags for CPU Security Mitigation APIs
158*c54f35caSApple OSS Distributions  * PROC_CSM_ALL should be used in most cases,
159*c54f35caSApple OSS Distributions  * the individual flags are provided only for performance evaluation etc
160*c54f35caSApple OSS Distributions  */
161*c54f35caSApple OSS Distributions #define PROC_CSM_ALL         0x0001  /* Set all available mitigations */
162*c54f35caSApple OSS Distributions #define PROC_CSM_NOSMT       0x0002  /* Set NO_SMT - see above */
163*c54f35caSApple OSS Distributions #define PROC_CSM_TECS        0x0004  /* Execute VERW on every return to user mode */
164*c54f35caSApple OSS Distributions 
165*c54f35caSApple OSS Distributions int proc_udata_info(int pid, int flavor, void *buffer, int buffersize);
166*c54f35caSApple OSS Distributions 
167*c54f35caSApple OSS Distributions #if __has_include(<libproc_private.h>)
168*c54f35caSApple OSS Distributions #include <libproc_private.h>
169*c54f35caSApple OSS Distributions #endif
170*c54f35caSApple OSS Distributions 
171*c54f35caSApple OSS Distributions __END_DECLS
172*c54f35caSApple OSS Distributions 
173*c54f35caSApple OSS Distributions #endif /*_LIBPROC_H_ */
174