-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
183 lines (146 loc) · 4.21 KB
/
main.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
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
static char *prog_name;
static volatile sigset_t set_block;
static volatile sigset_t set_normal;
static volatile int last_sig = 0;
// forwarded signal
static const int fwd_list[] = {
SIGHUP,
SIGINT,
SIGQUIT,
SIGTERM,
SIGUSR1,
SIGUSR2,
SIGWINCH,
};
// unpause signal
static const int unpause_list[] = {
SIGINT,
SIGTERM,
SIGCHLD,
};
static const int sig_alrm[] = {
SIGALRM,
};
// helper function
static void exit_error(int line, char *msg) { fprintf(stderr, "[ERROR %s] line %d: %s\n", prog_name, line, msg); _exit(1); }
static void exit_errno(int line) { exit_error(line, strerror(errno)); }
static void block_signal() { sigprocmask(SIG_SETMASK, (sigset_t*)(&set_block), NULL); }
static void unblock_signals() { sigprocmask(SIG_SETMASK, (sigset_t*)(&set_normal), NULL); }
static void handle_signal(int signo) {
last_sig = signo;
block_signal();
}
static void register_signal_handler(const int list[], const int list_len) {
sigemptyset((sigset_t*)(&set_block));
for (int i = 0; i < list_len; ++i) sigaddset((sigset_t*)(&set_block), list[i]);
sigprocmask(SIG_SETMASK, NULL, (sigset_t*)(&set_normal));
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = handle_signal;
act.sa_mask = set_block;
for (int i = 0; i < list_len; ++i) {
if (sigaction(list[i], &act, NULL) == -1) exit_errno(__LINE__);
}
}
static void reset_signal_handler(const int list[], const int list_len) {
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_DFL;
for (int i = 0; i < list_len; ++i) {
if (sigaction(list[i], &act, NULL) == -1) exit_errno(__LINE__);
}
unblock_signals();
}
static void kill_all_and_exit(int basic_info_mode, int wstatus) {
kill(-1, SIGCONT);
kill(-1, SIGTERM);
register_signal_handler(sig_alrm, sizeof(sig_alrm) / sizeof(sig_alrm[0]));
alarm(5);
while (wait(NULL) != -1);
int complete = errno == ECHILD;
alarm(0);
reset_signal_handler(sig_alrm, sizeof(sig_alrm) / sizeof(sig_alrm[0]));
if (basic_info_mode) {
if (!complete) _exit(1);
_exit(0);
}
if (WIFEXITED(wstatus)) _exit(WEXITSTATUS(wstatus));
char *sig = strsignal(WTERMSIG(wstatus));
if (sig == NULL) sig = "NULL";
fprintf(stderr, "Terminated by signal %s\n", sig);
_exit(128 + WTERMSIG(wstatus));
}
static void main_sleep() {
register_signal_handler(unpause_list, sizeof(unpause_list) / sizeof(unpause_list[0]));
for (;;) {
unblock_signals();
pause();
if (last_sig != SIGCHLD) {
reset_signal_handler(unpause_list, sizeof(unpause_list) / sizeof(unpause_list[0]));
kill_all_and_exit(1, 0);
}
while (waitpid(-1, 0, WNOHANG) > 0);
}
}
static void exec_child(int argc, char *argv[]) {
char *new_arg[argc];
for(int i = 1; i < argc; ++i) new_arg[i - 1] = argv[i];
new_arg[argc - 1] = 0;
execvp(new_arg[0], new_arg);
}
static void main_with_child(int argc, char *argv[]) {
int cpid = fork();
if (cpid == -1) exit_errno(__LINE__);
else if (cpid) {
// parent process
register_signal_handler(fwd_list, sizeof(fwd_list) / sizeof(fwd_list[0]));
for (;;) {
unblock_signals();
int status;
int pid = wait(&status);
if (pid == -1) {
// forward signal
if (errno == EINTR) kill(cpid, last_sig);
else exit_errno(__LINE__);
} else if (pid == cpid && (WIFEXITED(status) || WIFSIGNALED(status))) {
reset_signal_handler(fwd_list, sizeof(fwd_list) / sizeof(fwd_list[0]));
kill_all_and_exit(0, status);
}
}
} else {
// child process
setsid();
for(int fd = 0; fd < 3; fd++) ioctl(fd, TIOCSCTTY, 0);
exec_child(argc, argv);
exit_errno(__LINE__);
}
exit_error(__LINE__, "DEAD CODE !!");
}
int main(int argc, char *argv[]) {
prog_name = argv[0];
for(int i = 1; i < argc; i++) if(strcmp("--", argv[i]) == 0) {
argc -= i;
argv[i] = argv[0];
argv = &argv[i];
break;
}
if (getpid() != 1) {
fprintf(stderr, "[WARNING]: %s will not working unless running as pid 1\n", prog_name);
if (argc > 1) {
exec_child(argc, argv);
exit_errno(__LINE__);
}
_exit(0);
}
for(int fd = 0; fd < 3; fd++) ioctl(fd, TIOCNOTTY);
if (argc == 1) main_sleep();
main_with_child(argc, argv);
}