forked from adsr/phpspy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpgrep.c
More file actions
523 lines (439 loc) · 14.7 KB
/
pgrep.c
File metadata and controls
523 lines (439 loc) · 14.7 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "phpspy.h"
#include <poll.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <ctype.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 init_pgrep_output_pipe(void);
static int install_sigusr2_handler(void);
static void handle_sigusr2(int signal);
static int fetch_current_timestamp(char *buf, size_t sz);
static void *run_write_output_thread(void *arg);
static int drain_pipe_to_file(int check_for_done);
static int get_php_procs(pid_t procs_arr[], size_t procs_size);
static int read_file(const char *path, void *buf, size_t size);
static int is_number(const char *str);
static int try_find_match_in_proc_fs(pid_t pid, const char *proc_fs_f_name);
static int *avail_pids = NULL;
static int *attached_pids = NULL;
static pthread_t *work_threads = NULL;
static pthread_t signal_thread;
static pthread_t rotate_output_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 should_rotate = 0;
static int done_pipe[2] = { -1, -1 };
/*
A little brief on how the output works in pgrep mode:
In order to prevent interlaced outputs in large buffers (bigger than PIPE_BUF), we do the following:
A buffer (udata->buf) is allocated on each stack begin, frames of the same stack are being written to this buffer,
When the stack finishes, the buffer and the size of the written data are written to a pipe (unlike the normal PID mode where the buffer
is written directly to stdout), because the data and the size (write_msg_t) is a lot smaller than PIPE_BUF operations are synchronized.
On a polling thread, we poll on the the read side of the pipe where we receive messages (write_msg_t), we read these messages
and "drain" them to the output file, after writing the data we free the buffer that was previously allocated.
The reason we are fully secured is that only a single thread writes to the outputfile in pgrep mode.
*/
static int output_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);
}
if (init_output_fd() != PHPSPY_OK) {
exit(1);
}
init_pgrep_output_pipe();
pthread_create(&rotate_output_thread, NULL, run_write_output_thread, NULL);
install_sigusr2_handler();
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);
pthread_join(rotate_output_thread, NULL);
deinit_work_threads();
close(output_pipe[1]); /* First, close write fd to the pipe so no more stacks will be written */
drain_pipe_to_file(0); /* Drain remaining stacks and free the buffers */
close(output_pipe[0]);
deinit_output_fd();
log_error("main_pgrep finished gracefully\n");
return 0;
}
static int init_pgrep_output_pipe(void) {
int rv;
rv = pipe(output_pipe);
if (rv != 0) {
log_error("Couldn't open output pipe - errno %d\n");
return PHPSPY_ERR;
}
/* Set read fd as non-blocking */
fcntl(output_pipe[0], F_SETFL, fcntl(output_pipe[0], F_GETFL, 0) | O_NONBLOCK);
return PHPSPY_OK;
}
int pgrep_mode_output_write(const char *buf, size_t buf_size) {
int rv = PHPSPY_OK;
struct iovec io_vec;
io_vec.iov_base = (void *) buf;
io_vec.iov_len = buf_size;
if ((rv = write(output_pipe[1], &io_vec, sizeof(io_vec)) != sizeof(io_vec))) {
log_error("FATAL! couldn't write message in a single chunk to the pipe\n");
}
return rv;
}
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 int read_file(const char *path, void *buf, size_t size) {
int rv = PHPSPY_OK;
FILE *f = fopen(path, "rb");
if (f == NULL) {
if (errno != ENOENT) {
perror("Can't open file for reading");
}
return -PHPSPY_ERR;
}
rv = fread(buf, 1, size, f);
if (ferror(f)) {
if (errno != ENOENT) {
perror("Couldn't read data from file");
}
rv = -PHPSPY_ERR;
}
fclose(f);
return rv;
}
static int is_number(const char *str) {
size_t str_l = strlen(str);
int i = 0;
for (i = 0; i < (int) str_l; ++i) {
if (!isdigit(str[i])) {
return 0;
}
}
return 1;
}
static int try_find_match_in_proc_fs(pid_t pid, const char *proc_fs_f_name) {
char proc_path[PATH_MAX];
char proc_fs_read_buf[4096] = {0};
char *match;
snprintf(proc_path, PATH_MAX, "/proc/%d/%s", pid, proc_fs_f_name);
if (read_file(proc_path, proc_fs_read_buf, sizeof(proc_fs_read_buf)) < 0) {
if (errno != ENOENT) {
perror("Couldn't read proc fs file");
}
return -1;
}
/* Ensure null termination so libc string functions will work properly */
proc_fs_read_buf[sizeof(proc_fs_read_buf) - 1] = '\0';
/* Using strstr is ok also for cmdline because cmdline is null splitted between the args. */
if ((match = strstr(proc_fs_read_buf, opt_pgrep_args)) != NULL) {
return 0;
}
return 1;
}
static int get_php_procs(pid_t procs_arr[], size_t procs_size) {
DIR *dp;
pid_t pid;
struct dirent *ep;
size_t num_procs_matched = 0;
dp = opendir("/proc");
if (dp == NULL) {
perror("Couldn't open '/proc' directory");
return -PHPSPY_ERR;
}
while ((ep = readdir(dp)) && (num_procs_matched < procs_size)) {
if (!is_number(ep->d_name)) {
continue;
}
pid = atoi(ep->d_name);
/* First try with comm, then fallback to "argv[0]", like pgrep default behavior */
if (try_find_match_in_proc_fs(pid, "comm") == 0) {
procs_arr[num_procs_matched++] = pid;
continue;
}
if (try_find_match_in_proc_fs(pid, "cmdline") == 0) {
procs_arr[num_procs_matched++] = pid;
}
}
(void)closedir(dp);
return (int) num_procs_matched;
}
static void pgrep_for_pids() {
int pid;
int found;
int num_procs_matched;
struct timespec timeout;
int i;
pid_t *procs_arr = malloc(sizeof(pid_t) * opt_num_workers);
if (procs_arr == NULL) {
log_error("FATAL: pgrep mode: can't allocate pid search buffer\n");
return;
}
while (!done){
if (wait_for_turn('p')) break;
found = 0;
num_procs_matched = get_php_procs(procs_arr, opt_num_workers);
if (num_procs_matched > 0) {
for (i = 0; i < num_procs_matched && avail_pids_count < opt_num_workers; i++) {
pid = procs_arr[i];
if (is_already_attached(pid))
continue;
avail_pids[avail_pids_count++] = pid;
found += 1;
}
}
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(procs_arr);
}
static void *run_work_thread(void *arg) {
int worker_num;
int main_pid_rv;
pid_t pid;
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);
pid = attached_pids[worker_num];
main_pid_rv = main_pid(pid);
/* PHPSPY_ERR_PID_DEAD is sometimes passed ORred with other error(s) */
if (main_pid_rv != PHPSPY_OK && (main_pid_rv & PHPSPY_ERR_PID_DEAD) == 0) {
log_error("error: pgrep mode: main_pid routine returned non ok status and PID (%d) is not dead, rv = %d, errno - %d\n",
pid, main_pid_rv, errno);
}
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;
}
static int install_sigusr2_handler(void) {
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handle_sigusr2;
sigaction(SIGUSR2, &sa, NULL);
return PHPSPY_OK;
}
/* Copied from linux: tools/perf/util/time-utils.c */
static int fetch_current_timestamp(char *buf, size_t sz) {
struct timeval tv;
struct tm tm;
char dt[32];
if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
return -1;
if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
return -1;
snprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
return 0;
}
static void handle_sigusr2(int signal) {
(void)signal;
should_rotate = 1;
}
static void rotate_output(void) {
#define TIMESTAMP_BUF_SIZE (128)
char timestamp_buf[TIMESTAMP_BUF_SIZE];
char new_path[PATH_MAX];
if (strcmp(opt_path_output, STDOUT_OUTPUT) == 0) {
return;
}
deinit_output_fd();
if (fetch_current_timestamp(timestamp_buf, TIMESTAMP_BUF_SIZE) != 0) {
strncpy(timestamp_buf, "UNKNOWN-TS", TIMESTAMP_BUF_SIZE);
}
snprintf(new_path, PATH_MAX, "%s.%s", opt_path_output, timestamp_buf);
rename(opt_path_output, new_path);
if (init_output_fd() != PHPSPY_OK) {
log_error("FATAL! couldn't reopen log output file after rotation\n");
/* flag to exit ASAP */
done = 1;
}
}
static int drain_pipe_to_file(int check_for_done) {
int rv = 0;
struct iovec io_vec;
while ((rv = read(output_pipe[0], &io_vec, sizeof(io_vec))) != -1 && !should_rotate && (!check_for_done || !done)) {
if (rv == 0) { /* EOF */
return PHPSPY_OK;
}
if (rv != sizeof(io_vec)) {
log_error("FATAL! read %d bytes from the pipe which is not a full write_msg_t (sizeof = %d)\n", rv, sizeof(io_vec));
done = 1; /* can't recover from this stage, but really should never happen */
return PHPSPY_ERR;
}
if (write(output_fd, io_vec.iov_base, io_vec.iov_len) != (int)io_vec.iov_len) {
log_error("event_handler_fout: Write failed (%s)\n", errno != 0 ? strerror(errno) : "partial");
}
free(io_vec.iov_base); /* Allocated at event_handler_fout at EVENT_STACK_BEGIN */
}
if (rv != 0 && errno != EAGAIN) {
perror("Error while draining pipe messages to output file");
return PHPSPY_ERR;
}
return PHPSPY_OK;
}
static void *run_write_output_thread(void *arg) {
int rv;
struct pollfd poll_pipe = {0};
(void)arg;
poll_pipe.fd = output_pipe[0];
poll_pipe.events = POLLIN;
while (!done) {
if (should_rotate) {
rotate_output();
should_rotate = 0;
}
/* See explanation above output_pipe declaration */
#define POLL_TIMEOUT_MS (50)
rv = poll(&poll_pipe, 1, POLL_TIMEOUT_MS);
if (rv == 0) { /* timeout - continue */
continue;
} else if (rv < 0) {
if (errno != EINTR) {
perror("poll on pipe fd failed");
}
continue;
}
drain_pipe_to_file(1);
}
return NULL;
}