-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.c
More file actions
56 lines (43 loc) · 1.14 KB
/
Copy pathmonitor.c
File metadata and controls
56 lines (43 loc) · 1.14 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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#define MAX_FORK 256
ssize_t fork_cnt = 0;
const char *sep =
"============================================================\n";
void sigusr2_handler(int sig) {
(void)(sig);
fork_cnt++;
if (fork_cnt >= MAX_FORK) {
fputs(sep, stderr);
fputs("YOU ARE HITTING THE FORK LIMIT\n", stderr);
fputs(sep, stderr);
kill(0, SIGKILL);
}
}
int main(int argc, const char *argv[]) {
if (argc < 2) {
fputs("Missing executable file\n", stderr);
exit(EXIT_FAILURE);
}
signal(SIGUSR2, sigusr2_handler);
signal(SIGTSTP, SIG_IGN);
signal(SIGINT, SIG_IGN);
char monitor_pid[16];
sprintf(monitor_pid, "%d", getpid());
if (fork() == 0) {
signal(SIGUSR2, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGINT, SIG_DFL);
execl(argv[1], argv[1], monitor_pid, NULL);
exit(EXIT_FAILURE);
}
int wstatus;
while (waitpid(-1, &wstatus, WUNTRACED) > 0) {
if (WIFSTOPPED(wstatus)) {
kill(getpid(), SIGSTOP);
}
}
}