forked from adsr/phpspy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgrep.c
235 lines (206 loc) · 6.12 KB
/
pgrep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "phpspy.h"
static int wait_for_turn(char producer_or_consumer);
static void pgrep_for_pids();
static void *run_work_thread(void *arg);
static int is_already_attached(int pid);
static void init_work_threads();
static void deinit_work_threads();
static int block_all_signals();
static void handle_signal(int signum);
static void *run_signal_thread(void *arg);
static int *avail_pids = NULL;
static int *attached_pids = NULL;
static pthread_t *work_threads = NULL;
static pthread_t signal_thread;
static int avail_pids_count = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t can_produce = PTHREAD_COND_INITIALIZER;
static pthread_cond_t can_consume = PTHREAD_COND_INITIALIZER;
static int done_pipe[2] = { -1, -1 };
int main_pgrep() {
long i;
if (opt_num_workers < 1) {
log_error("Expected max concurrent workers (-T) > 0\n");
exit(1);
}
pthread_create(&signal_thread, NULL, run_signal_thread, NULL);
block_all_signals();
init_work_threads();
for (i = 0; i < opt_num_workers; i++) {
pthread_create(&work_threads[i], NULL, run_work_thread, (void*)i);
}
if (opt_time_limit_ms > 0) {
alarm(PHPSPY_MAX(1, opt_time_limit_ms / 1000));
}
pgrep_for_pids();
for (i = 0; i < opt_num_workers; i++) {
pthread_join(work_threads[i], NULL);
}
pthread_join(signal_thread, NULL);
deinit_work_threads();
log_error("main_pgrep finished gracefully\n");
return 0;
}
static int wait_for_turn(char producer_or_consumer) {
struct timespec timeout;
pthread_mutex_lock(&mutex);
while (!done) {
if (producer_or_consumer == 'p' && avail_pids_count < opt_num_workers) {
break;
} else if (avail_pids_count > 0) {
break;
}
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 2;
pthread_cond_timedwait(
producer_or_consumer == 'p' ? &can_produce : &can_consume,
&mutex,
&timeout
);
}
if (done) {
pthread_mutex_unlock(&mutex);
return 1;
}
return 0;
}
static void pgrep_for_pids() {
FILE *pcmd;
char *pgrep_cmd;
char line[64];
int pid;
int found;
struct timespec timeout;
if (asprintf(&pgrep_cmd, "pgrep %s", opt_pgrep_args) < 0) {
errno = ENOMEM;
perror("asprintf");
exit(1);
}
while (!done) {
if (wait_for_turn('p')) break;
found = 0;
if ((pcmd = popen(pgrep_cmd, "r")) != NULL) {
while (avail_pids_count < opt_num_workers && fgets(line, sizeof(line), pcmd) != NULL) {
if (strlen(line) < 1 || *line == '\n') continue;
pid = atoi(line);
if (is_already_attached(pid)) continue;
avail_pids[avail_pids_count++] = pid;
found += 1;
}
pclose(pcmd);
}
if (found > 0) {
pthread_cond_broadcast(&can_consume);
} else {
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 2;
pthread_cond_timedwait(
&can_produce,
&mutex,
&timeout
);
}
pthread_mutex_unlock(&mutex);
}
free(pgrep_cmd);
}
static void *run_work_thread(void *arg) {
int worker_num;
worker_num = (long)arg;
while (!done) {
if (wait_for_turn('c')) break;
attached_pids[worker_num] = avail_pids[--avail_pids_count];
pthread_cond_signal(&can_produce);
pthread_mutex_unlock(&mutex);
main_pid(attached_pids[worker_num]);
attached_pids[worker_num] = 0;
}
return NULL;
}
static int is_already_attached(int pid) {
int i;
for (i = 0; i < opt_num_workers; i++) {
if (attached_pids[i] == pid) {
return 1;
} else if (i < avail_pids_count && avail_pids[i] == pid) {
return 1;
}
}
return 0;
}
static void init_work_threads() {
avail_pids = calloc(opt_num_workers, sizeof(int));
attached_pids = calloc(opt_num_workers, sizeof(int));
work_threads = calloc(opt_num_workers, sizeof(pthread_t));
if (!avail_pids || !attached_pids || !work_threads) {
errno = ENOMEM;
perror("calloc");
exit(1);
}
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&can_produce, NULL);
pthread_cond_init(&can_consume, NULL);
}
static void deinit_work_threads() {
free(avail_pids);
free(attached_pids);
free(work_threads);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&can_produce);
pthread_cond_destroy(&can_consume);
}
static int block_all_signals() {
int rv;
sigset_t set;
try(rv, sigfillset(&set));
try(rv, sigprocmask(SIG_BLOCK, &set, NULL));
return 0;
}
void write_done_pipe() {
int rv, ignore;
if (done_pipe[1] >= 0) {
ignore = 1;
rv = write(done_pipe[1], &ignore, sizeof(int));
}
(void)rv;
}
static void handle_signal(int signum) {
(void)signum;
write_done_pipe();
}
static void *run_signal_thread(void *arg) {
int rv, ignore;
fd_set rfds;
struct timeval tv;
struct sigaction sa;
(void)arg;
/* Create done_pipe */
rv = pipe(done_pipe);
rv = fcntl(done_pipe[1], F_SETFL, O_NONBLOCK);
/* Install signal handler */
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGALRM, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, NULL);
/* Wait for write on done_pipe from write_done_pipe */
do {
FD_ZERO(&rfds);
FD_SET(done_pipe[0], &rfds);
tv.tv_sec = 1;
tv.tv_usec = 0;
rv = select(done_pipe[0]+1, &rfds, NULL, NULL, &tv);
} while (rv < 1);
/* Read pipe for fun */
rv = read(done_pipe[0], &ignore, sizeof(int));
/* Set done flag; wake up all threads */
done = 1;
pthread_mutex_lock(&mutex);
pthread_cond_broadcast(&can_consume);
pthread_cond_broadcast(&can_produce);
pthread_mutex_unlock(&mutex);
return NULL;
}