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