1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 2007, 2008 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 <stdlib.h>
25*1b191cb5SApple OSS Distributions #include <unistd.h>
26*1b191cb5SApple OSS Distributions #include <sys/fcntl.h>
27*1b191cb5SApple OSS Distributions #include <sys/errno.h>
28*1b191cb5SApple OSS Distributions #include <sys/param.h>
29*1b191cb5SApple OSS Distributions #include <sys/mount.h>
30*1b191cb5SApple OSS Distributions #include <libproc.h>
31*1b191cb5SApple OSS Distributions
32*1b191cb5SApple OSS Distributions
33*1b191cb5SApple OSS Distributions typedef struct {
34*1b191cb5SApple OSS Distributions // process IDs
35*1b191cb5SApple OSS Distributions int *pids;
36*1b191cb5SApple OSS Distributions int pids_count;
37*1b191cb5SApple OSS Distributions size_t pids_size;
38*1b191cb5SApple OSS Distributions
39*1b191cb5SApple OSS Distributions // threads
40*1b191cb5SApple OSS Distributions uint64_t *threads;
41*1b191cb5SApple OSS Distributions int thr_count;
42*1b191cb5SApple OSS Distributions size_t thr_size;
43*1b191cb5SApple OSS Distributions
44*1b191cb5SApple OSS Distributions // open file descriptors
45*1b191cb5SApple OSS Distributions struct proc_fdinfo *fds;
46*1b191cb5SApple OSS Distributions int fds_count;
47*1b191cb5SApple OSS Distributions size_t fds_size;
48*1b191cb5SApple OSS Distributions
49*1b191cb5SApple OSS Distributions // file/volume of interest
50*1b191cb5SApple OSS Distributions struct stat match_stat;
51*1b191cb5SApple OSS Distributions
52*1b191cb5SApple OSS Distributions // flags
53*1b191cb5SApple OSS Distributions uint32_t flags;
54*1b191cb5SApple OSS Distributions } fdOpenInfo, *fdOpenInfoRef;
55*1b191cb5SApple OSS Distributions
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions /*
58*1b191cb5SApple OSS Distributions * check_init
59*1b191cb5SApple OSS Distributions */
60*1b191cb5SApple OSS Distributions static fdOpenInfoRef
check_init(const char * path,uint32_t flags)61*1b191cb5SApple OSS Distributions check_init(const char *path, uint32_t flags)
62*1b191cb5SApple OSS Distributions {
63*1b191cb5SApple OSS Distributions fdOpenInfoRef info;
64*1b191cb5SApple OSS Distributions int status;
65*1b191cb5SApple OSS Distributions
66*1b191cb5SApple OSS Distributions info = malloc(sizeof(*info));
67*1b191cb5SApple OSS Distributions if (!info) {
68*1b191cb5SApple OSS Distributions return NULL;
69*1b191cb5SApple OSS Distributions }
70*1b191cb5SApple OSS Distributions
71*1b191cb5SApple OSS Distributions info->pids = NULL;
72*1b191cb5SApple OSS Distributions info->pids_count = 0;
73*1b191cb5SApple OSS Distributions info->pids_size = 0;
74*1b191cb5SApple OSS Distributions
75*1b191cb5SApple OSS Distributions info->threads = NULL;
76*1b191cb5SApple OSS Distributions info->thr_count = 0;
77*1b191cb5SApple OSS Distributions info->thr_size = 0;
78*1b191cb5SApple OSS Distributions
79*1b191cb5SApple OSS Distributions info->fds = NULL;
80*1b191cb5SApple OSS Distributions info->fds_count = 0;
81*1b191cb5SApple OSS Distributions info->fds_size = 0;
82*1b191cb5SApple OSS Distributions
83*1b191cb5SApple OSS Distributions status = stat(path, &info->match_stat);
84*1b191cb5SApple OSS Distributions if (status == -1) {
85*1b191cb5SApple OSS Distributions goto fail;
86*1b191cb5SApple OSS Distributions }
87*1b191cb5SApple OSS Distributions
88*1b191cb5SApple OSS Distributions info->flags = flags;
89*1b191cb5SApple OSS Distributions
90*1b191cb5SApple OSS Distributions return info;
91*1b191cb5SApple OSS Distributions
92*1b191cb5SApple OSS Distributions fail:
93*1b191cb5SApple OSS Distributions
94*1b191cb5SApple OSS Distributions free(info);
95*1b191cb5SApple OSS Distributions return NULL;
96*1b191cb5SApple OSS Distributions }
97*1b191cb5SApple OSS Distributions
98*1b191cb5SApple OSS Distributions
99*1b191cb5SApple OSS Distributions /*
100*1b191cb5SApple OSS Distributions * check_free
101*1b191cb5SApple OSS Distributions */
102*1b191cb5SApple OSS Distributions static void
check_free(fdOpenInfoRef info)103*1b191cb5SApple OSS Distributions check_free(fdOpenInfoRef info)
104*1b191cb5SApple OSS Distributions {
105*1b191cb5SApple OSS Distributions if (info->pids != NULL) {
106*1b191cb5SApple OSS Distributions free(info->pids);
107*1b191cb5SApple OSS Distributions }
108*1b191cb5SApple OSS Distributions
109*1b191cb5SApple OSS Distributions if (info->threads != NULL) {
110*1b191cb5SApple OSS Distributions free(info->threads);
111*1b191cb5SApple OSS Distributions }
112*1b191cb5SApple OSS Distributions
113*1b191cb5SApple OSS Distributions if (info->fds != NULL) {
114*1b191cb5SApple OSS Distributions free(info->fds);
115*1b191cb5SApple OSS Distributions }
116*1b191cb5SApple OSS Distributions
117*1b191cb5SApple OSS Distributions free(info);
118*1b191cb5SApple OSS Distributions
119*1b191cb5SApple OSS Distributions return;
120*1b191cb5SApple OSS Distributions }
121*1b191cb5SApple OSS Distributions
122*1b191cb5SApple OSS Distributions
123*1b191cb5SApple OSS Distributions /*
124*1b191cb5SApple OSS Distributions * check_file
125*1b191cb5SApple OSS Distributions * check if a process vnode is of interest
126*1b191cb5SApple OSS Distributions *
127*1b191cb5SApple OSS Distributions * in : vnode stat(2)
128*1b191cb5SApple OSS Distributions * out : -1 if error
129*1b191cb5SApple OSS Distributions * 0 if no match
130*1b191cb5SApple OSS Distributions * 1 if match
131*1b191cb5SApple OSS Distributions */
132*1b191cb5SApple OSS Distributions static int
check_file(fdOpenInfoRef info,struct vinfo_stat * sb)133*1b191cb5SApple OSS Distributions check_file(fdOpenInfoRef info, struct vinfo_stat *sb)
134*1b191cb5SApple OSS Distributions {
135*1b191cb5SApple OSS Distributions if (sb->vst_dev == 0) {
136*1b191cb5SApple OSS Distributions // if no info
137*1b191cb5SApple OSS Distributions return 0;
138*1b191cb5SApple OSS Distributions }
139*1b191cb5SApple OSS Distributions
140*1b191cb5SApple OSS Distributions if (sb->vst_dev != info->match_stat.st_dev) {
141*1b191cb5SApple OSS Distributions // if not the requested filesystem
142*1b191cb5SApple OSS Distributions return 0;
143*1b191cb5SApple OSS Distributions }
144*1b191cb5SApple OSS Distributions
145*1b191cb5SApple OSS Distributions if (!(info->flags & PROC_LISTPIDSPATH_PATH_IS_VOLUME) &&
146*1b191cb5SApple OSS Distributions (sb->vst_ino != info->match_stat.st_ino)) {
147*1b191cb5SApple OSS Distributions // if not the requested file
148*1b191cb5SApple OSS Distributions return 0;
149*1b191cb5SApple OSS Distributions }
150*1b191cb5SApple OSS Distributions
151*1b191cb5SApple OSS Distributions return 1;
152*1b191cb5SApple OSS Distributions }
153*1b191cb5SApple OSS Distributions
154*1b191cb5SApple OSS Distributions
155*1b191cb5SApple OSS Distributions /*
156*1b191cb5SApple OSS Distributions * check_process_vnodes
157*1b191cb5SApple OSS Distributions * check [process] current working directory
158*1b191cb5SApple OSS Distributions * check [process] root directory
159*1b191cb5SApple OSS Distributions *
160*1b191cb5SApple OSS Distributions * in : pid
161*1b191cb5SApple OSS Distributions * out : -1 if error
162*1b191cb5SApple OSS Distributions * 0 if no match
163*1b191cb5SApple OSS Distributions * 1 if match
164*1b191cb5SApple OSS Distributions */
165*1b191cb5SApple OSS Distributions static int
check_process_vnodes(fdOpenInfoRef info,int pid)166*1b191cb5SApple OSS Distributions check_process_vnodes(fdOpenInfoRef info, int pid)
167*1b191cb5SApple OSS Distributions {
168*1b191cb5SApple OSS Distributions int buf_used;
169*1b191cb5SApple OSS Distributions int status;
170*1b191cb5SApple OSS Distributions struct proc_vnodepathinfo vpi;
171*1b191cb5SApple OSS Distributions
172*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, 0, &vpi, sizeof(vpi));
173*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
174*1b191cb5SApple OSS Distributions if (errno == ESRCH) {
175*1b191cb5SApple OSS Distributions // if the process is gone
176*1b191cb5SApple OSS Distributions return 0;
177*1b191cb5SApple OSS Distributions }
178*1b191cb5SApple OSS Distributions return -1;
179*1b191cb5SApple OSS Distributions } else if (buf_used < sizeof(vpi)) {
180*1b191cb5SApple OSS Distributions // if we didn't get enough information
181*1b191cb5SApple OSS Distributions return -1;
182*1b191cb5SApple OSS Distributions }
183*1b191cb5SApple OSS Distributions
184*1b191cb5SApple OSS Distributions // processing current working directory
185*1b191cb5SApple OSS Distributions status = check_file(info, &vpi.pvi_cdir.vip_vi.vi_stat);
186*1b191cb5SApple OSS Distributions if (status != 0) {
187*1b191cb5SApple OSS Distributions // if error or match
188*1b191cb5SApple OSS Distributions return status;
189*1b191cb5SApple OSS Distributions }
190*1b191cb5SApple OSS Distributions
191*1b191cb5SApple OSS Distributions // processing root directory
192*1b191cb5SApple OSS Distributions status = check_file(info, &vpi.pvi_rdir.vip_vi.vi_stat);
193*1b191cb5SApple OSS Distributions if (status != 0) {
194*1b191cb5SApple OSS Distributions // if error or match
195*1b191cb5SApple OSS Distributions return status;
196*1b191cb5SApple OSS Distributions }
197*1b191cb5SApple OSS Distributions
198*1b191cb5SApple OSS Distributions return 0;
199*1b191cb5SApple OSS Distributions }
200*1b191cb5SApple OSS Distributions
201*1b191cb5SApple OSS Distributions
202*1b191cb5SApple OSS Distributions /*
203*1b191cb5SApple OSS Distributions * check_process_text
204*1b191cb5SApple OSS Distributions * check [process] text (memory)
205*1b191cb5SApple OSS Distributions *
206*1b191cb5SApple OSS Distributions * in : pid
207*1b191cb5SApple OSS Distributions * out : -1 if error
208*1b191cb5SApple OSS Distributions * 0 if no match
209*1b191cb5SApple OSS Distributions * 1 if match
210*1b191cb5SApple OSS Distributions */
211*1b191cb5SApple OSS Distributions static int
check_process_text(fdOpenInfoRef info,int pid)212*1b191cb5SApple OSS Distributions check_process_text(fdOpenInfoRef info, int pid)
213*1b191cb5SApple OSS Distributions {
214*1b191cb5SApple OSS Distributions int status;
215*1b191cb5SApple OSS Distributions int buf_used;
216*1b191cb5SApple OSS Distributions struct proc_regionwithpathinfo rwpi;
217*1b191cb5SApple OSS Distributions
218*1b191cb5SApple OSS Distributions if (info->flags & PROC_LISTPIDSPATH_PATH_IS_VOLUME) {
219*1b191cb5SApple OSS Distributions // ask for first memory region that matches mountpoint
220*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDREGIONPATHINFO3, info->match_stat.st_dev, &rwpi, sizeof(rwpi));
221*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
222*1b191cb5SApple OSS Distributions if ((errno == ESRCH) || (errno == EINVAL)) {
223*1b191cb5SApple OSS Distributions // if no more text information is available for this process.
224*1b191cb5SApple OSS Distributions return 0;
225*1b191cb5SApple OSS Distributions }
226*1b191cb5SApple OSS Distributions return -1;
227*1b191cb5SApple OSS Distributions } else if (buf_used < sizeof(rwpi)) {
228*1b191cb5SApple OSS Distributions // if we didn't get enough information
229*1b191cb5SApple OSS Distributions return -1;
230*1b191cb5SApple OSS Distributions }
231*1b191cb5SApple OSS Distributions
232*1b191cb5SApple OSS Distributions status = check_file(info, &rwpi.prp_vip.vip_vi.vi_stat);
233*1b191cb5SApple OSS Distributions if (status != 0) {
234*1b191cb5SApple OSS Distributions // if error or match
235*1b191cb5SApple OSS Distributions return status;
236*1b191cb5SApple OSS Distributions }
237*1b191cb5SApple OSS Distributions } else {
238*1b191cb5SApple OSS Distributions uint64_t a = 0;
239*1b191cb5SApple OSS Distributions
240*1b191cb5SApple OSS Distributions while (1) { // for all memory regions
241*1b191cb5SApple OSS Distributions // processing next address
242*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDREGIONPATHINFO2, a, &rwpi, sizeof(rwpi));
243*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
244*1b191cb5SApple OSS Distributions if ((errno == ESRCH) || (errno == EINVAL)) {
245*1b191cb5SApple OSS Distributions // if no more text information is available for this process.
246*1b191cb5SApple OSS Distributions break;
247*1b191cb5SApple OSS Distributions }
248*1b191cb5SApple OSS Distributions return -1;
249*1b191cb5SApple OSS Distributions } else if (buf_used < sizeof(rwpi)) {
250*1b191cb5SApple OSS Distributions // if we didn't get enough information
251*1b191cb5SApple OSS Distributions return -1;
252*1b191cb5SApple OSS Distributions }
253*1b191cb5SApple OSS Distributions
254*1b191cb5SApple OSS Distributions status = check_file(info, &rwpi.prp_vip.vip_vi.vi_stat);
255*1b191cb5SApple OSS Distributions if (status != 0) {
256*1b191cb5SApple OSS Distributions // if error or match
257*1b191cb5SApple OSS Distributions return status;
258*1b191cb5SApple OSS Distributions }
259*1b191cb5SApple OSS Distributions
260*1b191cb5SApple OSS Distributions a = rwpi.prp_prinfo.pri_address + rwpi.prp_prinfo.pri_size;
261*1b191cb5SApple OSS Distributions }
262*1b191cb5SApple OSS Distributions }
263*1b191cb5SApple OSS Distributions
264*1b191cb5SApple OSS Distributions return 0;
265*1b191cb5SApple OSS Distributions }
266*1b191cb5SApple OSS Distributions
267*1b191cb5SApple OSS Distributions
268*1b191cb5SApple OSS Distributions /*
269*1b191cb5SApple OSS Distributions * check_process_fds
270*1b191cb5SApple OSS Distributions * check [process] open file descriptors
271*1b191cb5SApple OSS Distributions *
272*1b191cb5SApple OSS Distributions * in : pid
273*1b191cb5SApple OSS Distributions * out : -1 if error
274*1b191cb5SApple OSS Distributions * 0 if no match
275*1b191cb5SApple OSS Distributions * 1 if match
276*1b191cb5SApple OSS Distributions */
277*1b191cb5SApple OSS Distributions static int
check_process_fds(fdOpenInfoRef info,int pid)278*1b191cb5SApple OSS Distributions check_process_fds(fdOpenInfoRef info, int pid)
279*1b191cb5SApple OSS Distributions {
280*1b191cb5SApple OSS Distributions int buf_used;
281*1b191cb5SApple OSS Distributions int i;
282*1b191cb5SApple OSS Distributions int status;
283*1b191cb5SApple OSS Distributions
284*1b191cb5SApple OSS Distributions // get list of open file descriptors
285*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0);
286*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
287*1b191cb5SApple OSS Distributions return -1;
288*1b191cb5SApple OSS Distributions }
289*1b191cb5SApple OSS Distributions
290*1b191cb5SApple OSS Distributions while (1) {
291*1b191cb5SApple OSS Distributions if (buf_used > info->fds_size) {
292*1b191cb5SApple OSS Distributions // if we need to allocate [more] space
293*1b191cb5SApple OSS Distributions while (buf_used > info->fds_size) {
294*1b191cb5SApple OSS Distributions info->fds_size += (sizeof(struct proc_fdinfo) * 32);
295*1b191cb5SApple OSS Distributions }
296*1b191cb5SApple OSS Distributions
297*1b191cb5SApple OSS Distributions if (info->fds == NULL) {
298*1b191cb5SApple OSS Distributions info->fds = malloc(info->fds_size);
299*1b191cb5SApple OSS Distributions } else {
300*1b191cb5SApple OSS Distributions info->fds = reallocf(info->fds, info->fds_size);
301*1b191cb5SApple OSS Distributions }
302*1b191cb5SApple OSS Distributions if (info->fds == NULL) {
303*1b191cb5SApple OSS Distributions return -1;
304*1b191cb5SApple OSS Distributions }
305*1b191cb5SApple OSS Distributions }
306*1b191cb5SApple OSS Distributions
307*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, info->fds, (int)info->fds_size);
308*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
309*1b191cb5SApple OSS Distributions return -1;
310*1b191cb5SApple OSS Distributions }
311*1b191cb5SApple OSS Distributions
312*1b191cb5SApple OSS Distributions if ((buf_used + sizeof(struct proc_fdinfo)) >= info->fds_size) {
313*1b191cb5SApple OSS Distributions // if not enough room in the buffer for an extra fd
314*1b191cb5SApple OSS Distributions buf_used = (int)(info->fds_size + sizeof(struct proc_fdinfo));
315*1b191cb5SApple OSS Distributions continue;
316*1b191cb5SApple OSS Distributions }
317*1b191cb5SApple OSS Distributions
318*1b191cb5SApple OSS Distributions info->fds_count = (int)(buf_used / sizeof(struct proc_fdinfo));
319*1b191cb5SApple OSS Distributions break;
320*1b191cb5SApple OSS Distributions }
321*1b191cb5SApple OSS Distributions
322*1b191cb5SApple OSS Distributions // iterate through each file descriptor
323*1b191cb5SApple OSS Distributions for (i = 0; i < info->fds_count; i++) {
324*1b191cb5SApple OSS Distributions struct proc_fdinfo *fdp;
325*1b191cb5SApple OSS Distributions
326*1b191cb5SApple OSS Distributions fdp = &info->fds[i];
327*1b191cb5SApple OSS Distributions switch (fdp->proc_fdtype) {
328*1b191cb5SApple OSS Distributions case PROX_FDTYPE_VNODE: {
329*1b191cb5SApple OSS Distributions int buf_used;
330*1b191cb5SApple OSS Distributions struct vnode_fdinfo vi;
331*1b191cb5SApple OSS Distributions
332*1b191cb5SApple OSS Distributions buf_used = proc_pidfdinfo(pid, fdp->proc_fd, PROC_PIDFDVNODEINFO, &vi, sizeof(vi));
333*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
334*1b191cb5SApple OSS Distributions if (errno == ENOENT) {
335*1b191cb5SApple OSS Distributions /*
336*1b191cb5SApple OSS Distributions * The file descriptor's vnode may have been revoked. This is a
337*1b191cb5SApple OSS Distributions * bit of a hack, since an ENOENT error might not always mean the
338*1b191cb5SApple OSS Distributions * descriptor's vnode has been revoked. As the libproc API
339*1b191cb5SApple OSS Distributions * matures, this code may need to be revisited.
340*1b191cb5SApple OSS Distributions */
341*1b191cb5SApple OSS Distributions continue;
342*1b191cb5SApple OSS Distributions }
343*1b191cb5SApple OSS Distributions return -1;
344*1b191cb5SApple OSS Distributions } else if (buf_used < sizeof(vi)) {
345*1b191cb5SApple OSS Distributions // if we didn't get enough information
346*1b191cb5SApple OSS Distributions return -1;
347*1b191cb5SApple OSS Distributions }
348*1b191cb5SApple OSS Distributions
349*1b191cb5SApple OSS Distributions if ((info->flags & PROC_LISTPIDSPATH_EXCLUDE_EVTONLY) &&
350*1b191cb5SApple OSS Distributions (vi.pfi.fi_openflags & O_EVTONLY)) {
351*1b191cb5SApple OSS Distributions // if this file should be excluded
352*1b191cb5SApple OSS Distributions continue;
353*1b191cb5SApple OSS Distributions }
354*1b191cb5SApple OSS Distributions
355*1b191cb5SApple OSS Distributions status = check_file(info, &vi.pvi.vi_stat);
356*1b191cb5SApple OSS Distributions if (status != 0) {
357*1b191cb5SApple OSS Distributions // if error or match
358*1b191cb5SApple OSS Distributions return status;
359*1b191cb5SApple OSS Distributions }
360*1b191cb5SApple OSS Distributions break;
361*1b191cb5SApple OSS Distributions }
362*1b191cb5SApple OSS Distributions default:
363*1b191cb5SApple OSS Distributions break;
364*1b191cb5SApple OSS Distributions }
365*1b191cb5SApple OSS Distributions }
366*1b191cb5SApple OSS Distributions
367*1b191cb5SApple OSS Distributions return 0;
368*1b191cb5SApple OSS Distributions }
369*1b191cb5SApple OSS Distributions
370*1b191cb5SApple OSS Distributions
371*1b191cb5SApple OSS Distributions /*
372*1b191cb5SApple OSS Distributions * check_process_threads
373*1b191cb5SApple OSS Distributions * check [process] thread working directories
374*1b191cb5SApple OSS Distributions *
375*1b191cb5SApple OSS Distributions * in : pid
376*1b191cb5SApple OSS Distributions * out : -1 if error
377*1b191cb5SApple OSS Distributions * 0 if no match
378*1b191cb5SApple OSS Distributions * 1 if match
379*1b191cb5SApple OSS Distributions */
380*1b191cb5SApple OSS Distributions static int
check_process_threads(fdOpenInfoRef info,int pid)381*1b191cb5SApple OSS Distributions check_process_threads(fdOpenInfoRef info, int pid)
382*1b191cb5SApple OSS Distributions {
383*1b191cb5SApple OSS Distributions int buf_used;
384*1b191cb5SApple OSS Distributions int status;
385*1b191cb5SApple OSS Distributions struct proc_taskallinfo tai;
386*1b191cb5SApple OSS Distributions
387*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &tai, sizeof(tai));
388*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
389*1b191cb5SApple OSS Distributions if (errno == ESRCH) {
390*1b191cb5SApple OSS Distributions // if the process is gone
391*1b191cb5SApple OSS Distributions return 0;
392*1b191cb5SApple OSS Distributions }
393*1b191cb5SApple OSS Distributions return -1;
394*1b191cb5SApple OSS Distributions } else if (buf_used < sizeof(tai)) {
395*1b191cb5SApple OSS Distributions // if we didn't get enough information
396*1b191cb5SApple OSS Distributions return -1;
397*1b191cb5SApple OSS Distributions }
398*1b191cb5SApple OSS Distributions
399*1b191cb5SApple OSS Distributions // check thread info
400*1b191cb5SApple OSS Distributions if (tai.pbsd.pbi_flags & PROC_FLAG_THCWD) {
401*1b191cb5SApple OSS Distributions int i;
402*1b191cb5SApple OSS Distributions
403*1b191cb5SApple OSS Distributions // get list of threads
404*1b191cb5SApple OSS Distributions buf_used = tai.ptinfo.pti_threadnum * sizeof(uint64_t);
405*1b191cb5SApple OSS Distributions
406*1b191cb5SApple OSS Distributions while (1) {
407*1b191cb5SApple OSS Distributions if (buf_used > info->thr_size) {
408*1b191cb5SApple OSS Distributions // if we need to allocate [more] space
409*1b191cb5SApple OSS Distributions while (buf_used > info->thr_size) {
410*1b191cb5SApple OSS Distributions info->thr_size += (sizeof(uint64_t) * 32);
411*1b191cb5SApple OSS Distributions }
412*1b191cb5SApple OSS Distributions
413*1b191cb5SApple OSS Distributions if (info->threads == NULL) {
414*1b191cb5SApple OSS Distributions info->threads = malloc(info->thr_size);
415*1b191cb5SApple OSS Distributions } else {
416*1b191cb5SApple OSS Distributions info->threads = reallocf(info->threads, info->thr_size);
417*1b191cb5SApple OSS Distributions }
418*1b191cb5SApple OSS Distributions if (info->threads == NULL) {
419*1b191cb5SApple OSS Distributions return -1;
420*1b191cb5SApple OSS Distributions }
421*1b191cb5SApple OSS Distributions }
422*1b191cb5SApple OSS Distributions
423*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDLISTTHREADS, 0, info->threads, (int)info->thr_size);
424*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
425*1b191cb5SApple OSS Distributions return -1;
426*1b191cb5SApple OSS Distributions }
427*1b191cb5SApple OSS Distributions
428*1b191cb5SApple OSS Distributions if ((buf_used + sizeof(uint64_t)) >= info->thr_size) {
429*1b191cb5SApple OSS Distributions // if not enough room in the buffer for an extra thread
430*1b191cb5SApple OSS Distributions buf_used = (int)(info->thr_size + sizeof(uint64_t));
431*1b191cb5SApple OSS Distributions continue;
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions
434*1b191cb5SApple OSS Distributions info->thr_count = buf_used / sizeof(uint64_t);
435*1b191cb5SApple OSS Distributions break;
436*1b191cb5SApple OSS Distributions }
437*1b191cb5SApple OSS Distributions
438*1b191cb5SApple OSS Distributions // iterate through each thread
439*1b191cb5SApple OSS Distributions for (i = 0; i < info->thr_count; i++) {
440*1b191cb5SApple OSS Distributions uint64_t thr = info->threads[i];
441*1b191cb5SApple OSS Distributions struct proc_threadwithpathinfo tpi;
442*1b191cb5SApple OSS Distributions
443*1b191cb5SApple OSS Distributions buf_used = proc_pidinfo(pid, PROC_PIDTHREADPATHINFO, thr, &tpi, sizeof(tpi));
444*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
445*1b191cb5SApple OSS Distributions if ((errno == ESRCH) || (errno == EINVAL)) {
446*1b191cb5SApple OSS Distributions // if the process or thread is gone
447*1b191cb5SApple OSS Distributions continue;
448*1b191cb5SApple OSS Distributions }
449*1b191cb5SApple OSS Distributions } else if (buf_used < sizeof(tai)) {
450*1b191cb5SApple OSS Distributions // if we didn't get enough information
451*1b191cb5SApple OSS Distributions return -1;
452*1b191cb5SApple OSS Distributions }
453*1b191cb5SApple OSS Distributions
454*1b191cb5SApple OSS Distributions status = check_file(info, &tpi.pvip.vip_vi.vi_stat);
455*1b191cb5SApple OSS Distributions if (status != 0) {
456*1b191cb5SApple OSS Distributions // if error or match
457*1b191cb5SApple OSS Distributions return status;
458*1b191cb5SApple OSS Distributions }
459*1b191cb5SApple OSS Distributions }
460*1b191cb5SApple OSS Distributions }
461*1b191cb5SApple OSS Distributions
462*1b191cb5SApple OSS Distributions return 0;
463*1b191cb5SApple OSS Distributions }
464*1b191cb5SApple OSS Distributions
465*1b191cb5SApple OSS Distributions
466*1b191cb5SApple OSS Distributions /*
467*1b191cb5SApple OSS Distributions * check_process_phase1
468*1b191cb5SApple OSS Distributions * check [process] process-wide current working and root directories
469*1b191cb5SApple OSS Distributions * check [process] open file descriptors
470*1b191cb5SApple OSS Distributions * check [process] per-thread current working and root directories
471*1b191cb5SApple OSS Distributions *
472*1b191cb5SApple OSS Distributions * in : pid
473*1b191cb5SApple OSS Distributions * out : -1 if error
474*1b191cb5SApple OSS Distributions * 0 if no match
475*1b191cb5SApple OSS Distributions * 1 if match
476*1b191cb5SApple OSS Distributions */
477*1b191cb5SApple OSS Distributions static int
check_process_phase1(fdOpenInfoRef info,int pid)478*1b191cb5SApple OSS Distributions check_process_phase1(fdOpenInfoRef info, int pid)
479*1b191cb5SApple OSS Distributions {
480*1b191cb5SApple OSS Distributions int status;
481*1b191cb5SApple OSS Distributions
482*1b191cb5SApple OSS Distributions // check root and current working directory
483*1b191cb5SApple OSS Distributions status = check_process_vnodes(info, pid);
484*1b191cb5SApple OSS Distributions if (status != 0) {
485*1b191cb5SApple OSS Distributions // if error or match
486*1b191cb5SApple OSS Distributions return status;
487*1b191cb5SApple OSS Distributions }
488*1b191cb5SApple OSS Distributions
489*1b191cb5SApple OSS Distributions // check open file descriptors
490*1b191cb5SApple OSS Distributions status = check_process_fds(info, pid);
491*1b191cb5SApple OSS Distributions if (status != 0) {
492*1b191cb5SApple OSS Distributions // if error or match
493*1b191cb5SApple OSS Distributions return status;
494*1b191cb5SApple OSS Distributions }
495*1b191cb5SApple OSS Distributions
496*1b191cb5SApple OSS Distributions // check per-thread working directories
497*1b191cb5SApple OSS Distributions status = check_process_threads(info, pid);
498*1b191cb5SApple OSS Distributions if (status != 0) {
499*1b191cb5SApple OSS Distributions // if error or match
500*1b191cb5SApple OSS Distributions return status;
501*1b191cb5SApple OSS Distributions }
502*1b191cb5SApple OSS Distributions
503*1b191cb5SApple OSS Distributions return 0;
504*1b191cb5SApple OSS Distributions }
505*1b191cb5SApple OSS Distributions
506*1b191cb5SApple OSS Distributions /*
507*1b191cb5SApple OSS Distributions * check_process_phase2
508*1b191cb5SApple OSS Distributions * check [process] text (memory)
509*1b191cb5SApple OSS Distributions *
510*1b191cb5SApple OSS Distributions * in : pid
511*1b191cb5SApple OSS Distributions * out : -1 if error
512*1b191cb5SApple OSS Distributions * 0 if no match
513*1b191cb5SApple OSS Distributions * 1 if match
514*1b191cb5SApple OSS Distributions */
515*1b191cb5SApple OSS Distributions static int
check_process_phase2(fdOpenInfoRef info,int pid)516*1b191cb5SApple OSS Distributions check_process_phase2(fdOpenInfoRef info, int pid)
517*1b191cb5SApple OSS Distributions {
518*1b191cb5SApple OSS Distributions int status;
519*1b191cb5SApple OSS Distributions
520*1b191cb5SApple OSS Distributions // check process text (memory)
521*1b191cb5SApple OSS Distributions status = check_process_text(info, pid);
522*1b191cb5SApple OSS Distributions if (status != 0) {
523*1b191cb5SApple OSS Distributions // if error or match
524*1b191cb5SApple OSS Distributions return status;
525*1b191cb5SApple OSS Distributions }
526*1b191cb5SApple OSS Distributions
527*1b191cb5SApple OSS Distributions return 0;
528*1b191cb5SApple OSS Distributions }
529*1b191cb5SApple OSS Distributions
530*1b191cb5SApple OSS Distributions /*
531*1b191cb5SApple OSS Distributions * proc_listpidspath
532*1b191cb5SApple OSS Distributions *
533*1b191cb5SApple OSS Distributions * in : type
534*1b191cb5SApple OSS Distributions * : typeinfo
535*1b191cb5SApple OSS Distributions * : path
536*1b191cb5SApple OSS Distributions * : pathflags
537*1b191cb5SApple OSS Distributions * : buffer
538*1b191cb5SApple OSS Distributions * : buffersize
539*1b191cb5SApple OSS Distributions * out : buffer filled with process IDs that have open file
540*1b191cb5SApple OSS Distributions * references that match the specified path or volume;
541*1b191cb5SApple OSS Distributions * return value is the bytes of the returned buffer
542*1b191cb5SApple OSS Distributions * that contains valid information.
543*1b191cb5SApple OSS Distributions */
544*1b191cb5SApple OSS Distributions int
proc_listpidspath(uint32_t type,uint32_t typeinfo,const char * path,uint32_t pathflags,void * buffer,int buffersize)545*1b191cb5SApple OSS Distributions proc_listpidspath(uint32_t type,
546*1b191cb5SApple OSS Distributions uint32_t typeinfo,
547*1b191cb5SApple OSS Distributions const char *path,
548*1b191cb5SApple OSS Distributions uint32_t pathflags,
549*1b191cb5SApple OSS Distributions void *buffer,
550*1b191cb5SApple OSS Distributions int buffersize)
551*1b191cb5SApple OSS Distributions {
552*1b191cb5SApple OSS Distributions int buf_used;
553*1b191cb5SApple OSS Distributions int *buf_next = (int *)buffer;
554*1b191cb5SApple OSS Distributions int i;
555*1b191cb5SApple OSS Distributions fdOpenInfoRef info;
556*1b191cb5SApple OSS Distributions int status = -1;
557*1b191cb5SApple OSS Distributions
558*1b191cb5SApple OSS Distributions if (buffer == NULL) {
559*1b191cb5SApple OSS Distributions // if this is a sizing request
560*1b191cb5SApple OSS Distributions return proc_listpids(type, typeinfo, NULL, 0);
561*1b191cb5SApple OSS Distributions }
562*1b191cb5SApple OSS Distributions
563*1b191cb5SApple OSS Distributions buffersize -= (buffersize % sizeof(int)); // make whole number of ints
564*1b191cb5SApple OSS Distributions if (buffersize < sizeof(int)) {
565*1b191cb5SApple OSS Distributions // if we can't even return a single PID
566*1b191cb5SApple OSS Distributions errno = ENOMEM;
567*1b191cb5SApple OSS Distributions return -1;
568*1b191cb5SApple OSS Distributions }
569*1b191cb5SApple OSS Distributions
570*1b191cb5SApple OSS Distributions // init
571*1b191cb5SApple OSS Distributions info = check_init(path, pathflags);
572*1b191cb5SApple OSS Distributions if (info == NULL) {
573*1b191cb5SApple OSS Distributions return -1;
574*1b191cb5SApple OSS Distributions }
575*1b191cb5SApple OSS Distributions
576*1b191cb5SApple OSS Distributions // get list of processes
577*1b191cb5SApple OSS Distributions buf_used = proc_listpids(type, typeinfo, NULL, 0);
578*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
579*1b191cb5SApple OSS Distributions goto done;
580*1b191cb5SApple OSS Distributions }
581*1b191cb5SApple OSS Distributions
582*1b191cb5SApple OSS Distributions while (1) {
583*1b191cb5SApple OSS Distributions if (buf_used > info->pids_size) {
584*1b191cb5SApple OSS Distributions // if we need to allocate [more] space
585*1b191cb5SApple OSS Distributions while (buf_used > info->pids_size) {
586*1b191cb5SApple OSS Distributions info->pids_size += (sizeof(int) * 32);
587*1b191cb5SApple OSS Distributions }
588*1b191cb5SApple OSS Distributions
589*1b191cb5SApple OSS Distributions if (info->pids == NULL) {
590*1b191cb5SApple OSS Distributions info->pids = malloc(info->pids_size);
591*1b191cb5SApple OSS Distributions } else {
592*1b191cb5SApple OSS Distributions info->pids = reallocf(info->pids, info->pids_size);
593*1b191cb5SApple OSS Distributions }
594*1b191cb5SApple OSS Distributions if (info->pids == NULL) {
595*1b191cb5SApple OSS Distributions goto done;
596*1b191cb5SApple OSS Distributions }
597*1b191cb5SApple OSS Distributions }
598*1b191cb5SApple OSS Distributions
599*1b191cb5SApple OSS Distributions buf_used = proc_listpids(type, typeinfo, info->pids, (int)info->pids_size);
600*1b191cb5SApple OSS Distributions if (buf_used <= 0) {
601*1b191cb5SApple OSS Distributions goto done;
602*1b191cb5SApple OSS Distributions }
603*1b191cb5SApple OSS Distributions
604*1b191cb5SApple OSS Distributions if ((buf_used + sizeof(int)) >= info->pids_size) {
605*1b191cb5SApple OSS Distributions // if not enough room in the buffer for an extra pid
606*1b191cb5SApple OSS Distributions buf_used = (int)(info->pids_size + sizeof(int));
607*1b191cb5SApple OSS Distributions continue;
608*1b191cb5SApple OSS Distributions }
609*1b191cb5SApple OSS Distributions
610*1b191cb5SApple OSS Distributions info->pids_count = buf_used / sizeof(int);
611*1b191cb5SApple OSS Distributions break;
612*1b191cb5SApple OSS Distributions }
613*1b191cb5SApple OSS Distributions
614*1b191cb5SApple OSS Distributions // iterate through each process
615*1b191cb5SApple OSS Distributions buf_used = 0;
616*1b191cb5SApple OSS Distributions for (i = info->pids_count - 1; i >= 0; i--) {
617*1b191cb5SApple OSS Distributions int pid;
618*1b191cb5SApple OSS Distributions int pstatus;
619*1b191cb5SApple OSS Distributions
620*1b191cb5SApple OSS Distributions pid = info->pids[i];
621*1b191cb5SApple OSS Distributions if (pid == 0) {
622*1b191cb5SApple OSS Distributions continue;
623*1b191cb5SApple OSS Distributions }
624*1b191cb5SApple OSS Distributions
625*1b191cb5SApple OSS Distributions pstatus = check_process_phase1(info, pid);
626*1b191cb5SApple OSS Distributions if (pstatus != 1) {
627*1b191cb5SApple OSS Distributions // if not a match
628*1b191cb5SApple OSS Distributions continue;
629*1b191cb5SApple OSS Distributions }
630*1b191cb5SApple OSS Distributions
631*1b191cb5SApple OSS Distributions *buf_next++ = pid;
632*1b191cb5SApple OSS Distributions buf_used += sizeof(int);
633*1b191cb5SApple OSS Distributions
634*1b191cb5SApple OSS Distributions if (buf_used >= buffersize) {
635*1b191cb5SApple OSS Distributions // if we have filled the buffer
636*1b191cb5SApple OSS Distributions break;
637*1b191cb5SApple OSS Distributions }
638*1b191cb5SApple OSS Distributions }
639*1b191cb5SApple OSS Distributions
640*1b191cb5SApple OSS Distributions if (buf_used >= buffersize) {
641*1b191cb5SApple OSS Distributions // if we have filled the buffer
642*1b191cb5SApple OSS Distributions status = buf_used;
643*1b191cb5SApple OSS Distributions goto done;
644*1b191cb5SApple OSS Distributions }
645*1b191cb5SApple OSS Distributions
646*1b191cb5SApple OSS Distributions // do a more expensive search if we still have buffer space
647*1b191cb5SApple OSS Distributions for (i = info->pids_count - 1; i >= 0; i--) {
648*1b191cb5SApple OSS Distributions int pid;
649*1b191cb5SApple OSS Distributions int pstatus;
650*1b191cb5SApple OSS Distributions
651*1b191cb5SApple OSS Distributions pid = info->pids[i];
652*1b191cb5SApple OSS Distributions if (pid == 0) {
653*1b191cb5SApple OSS Distributions continue;
654*1b191cb5SApple OSS Distributions }
655*1b191cb5SApple OSS Distributions
656*1b191cb5SApple OSS Distributions pstatus = check_process_phase2(info, pid);
657*1b191cb5SApple OSS Distributions if (pstatus != 1) {
658*1b191cb5SApple OSS Distributions // if not a match
659*1b191cb5SApple OSS Distributions continue;
660*1b191cb5SApple OSS Distributions }
661*1b191cb5SApple OSS Distributions
662*1b191cb5SApple OSS Distributions *buf_next++ = pid;
663*1b191cb5SApple OSS Distributions buf_used += sizeof(int);
664*1b191cb5SApple OSS Distributions
665*1b191cb5SApple OSS Distributions if (buf_used >= buffersize) {
666*1b191cb5SApple OSS Distributions // if we have filled the buffer
667*1b191cb5SApple OSS Distributions break;
668*1b191cb5SApple OSS Distributions }
669*1b191cb5SApple OSS Distributions }
670*1b191cb5SApple OSS Distributions
671*1b191cb5SApple OSS Distributions status = buf_used;
672*1b191cb5SApple OSS Distributions
673*1b191cb5SApple OSS Distributions done:
674*1b191cb5SApple OSS Distributions
675*1b191cb5SApple OSS Distributions // cleanup
676*1b191cb5SApple OSS Distributions check_free(info);
677*1b191cb5SApple OSS Distributions
678*1b191cb5SApple OSS Distributions return status;
679*1b191cb5SApple OSS Distributions }
680