-
Notifications
You must be signed in to change notification settings - Fork 2
/
php8trace.c
117 lines (97 loc) · 2.29 KB
/
php8trace.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
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/wait.h>
static struct termios orig_termios;
static int ttyfd = STDIN_FILENO;
void tty_reset() {
tcsetattr(ttyfd,TCSAFLUSH,&orig_termios);
}
void tty_raw() {
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(tty_reset);
struct termios raw = orig_termios;
raw.c_lflag &= ~( ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int g_child_pid;
void child_exit(int signal) {
int wstatus;
pid_t pid = waitpid(-1, &wstatus, WNOHANG);
// printf("child %d exit WIFSTOPPED=%d WIFCONTINUED=%d WIFEXITED=%d WIFSIGNALED=%d WTERMSIG=%d\n",
// pid,
// WIFSTOPPED(wstatus),
// WIFCONTINUED(wstatus),
// WIFEXITED(wstatus),
// WIFSIGNALED(wstatus),
// WTERMSIG(wstatus));
if (pid == 0)
return ;
if (pid == g_child_pid && WIFSIGNALED(wstatus)) {
if (WTERMSIG(wstatus) < SIGSYS) {
exit(1);
}
}
if (pid == g_child_pid && WIFEXITED(wstatus))
exit(WEXITSTATUS(wstatus));
}
void on_sigstop(int signal) {
puts("forwarding SIGTSTP");
kill(g_child_pid, SIGTSTP);
}
void on_sigint(int signal) {
puts("forwarding SIGINT");
kill(g_child_pid, SIGINT);
}
int main(int argc, char **argv) {
int pipe_stdin[2];
pipe(pipe_stdin);
if (!(g_child_pid = fork())) {
printf("starting php process with pid [ %d ]\n",getpid());
close(pipe_stdin[1]);
close(0);
dup2(pipe_stdin[0], 0);
char **cloned_argv = calloc((argc+5) * sizeof(char *), 1);
int i;
cloned_argv[0] = "php";
cloned_argv[1] = "-d";
cloned_argv[2] = "extension=observer.so";
cloned_argv[3] = "-d";
cloned_argv[4] = "observer.instrument=1";
for(i=1;i<argc;i++) {
cloned_argv[i+4] = strdup(argv[i]);
}
execvp("php", cloned_argv);
perror("woot");
}
signal(SIGCHLD, child_exit);
signal(SIGTSTP, on_sigstop);
signal(SIGINT, on_sigint);
close(pipe_stdin[0]);
tty_raw();
uint8_t c;
while (read(STDIN_FILENO, &c, 1) == 1) {
if (iscntrl(c)) {
if (c == '') {
kill(g_child_pid, SIGUSR1);
}
if (c == '') {
kill(g_child_pid, SIGUSR2);
}
if (c == '') {
close(pipe_stdin[1]);
}
if (c == '') {
kill(g_child_pid, SIGCONT);
}
}
write(pipe_stdin[1], &c, 1);
}
kill(g_child_pid, SIGKILL);
return 0;
}