-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaper.c
215 lines (157 loc) · 3.34 KB
/
reaper.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
/*
Tool reaping the dead processes in a pid namespaces
Author: R. Koucha
Date: 11-Apr-2020
*/
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include "util.h"
static const char *signame[] = {
"",
"HUP",
"INT",
"QUIT",
"ILL",
"TRAP",
"ABRT",
"BUS",
"FPE",
"KILL",
"USR1",
"SEGV",
"USR2",
"PIPE",
"ALRM",
"TERM",
"STKFLT",
"CHLD",
"CONT",
"STOP",
"TSTP",
#define MAX_SIG SIGTSTP
NULL
};
static int sig_str2type(char *s)
{
int i = 0;
for (i = 0; signame[i]; i ++) {
if (!strcmp(signame[i], s)) {
return i;
}
} // End for
return -1;
} // sig_str2type
#define REAPER_NAME "Reaper"
static pid_t pid_sh;
static void sig_hdl(
int sig,
siginfo_t *info,
void *p
)
{
pid_t pid;
int status;
(void)p;
switch(sig) {
case SIGCHLD: {
// Reap all the childs
while(1) {
pid = waitpid(-1, &status, WNOHANG);
if (pid < 0) {
ERR("waitpid(): '%m' (%d)\n", errno);
exit(1);
}
if (0 == pid) {
// No more dead childs
return;
}
// If it is our shell, exit...
if (pid_sh == pid) {
printf("%s: Exiting as the main shell#%d exited with status 0x%x (%d)\n",
REAPER_NAME,
pid,
status, status);
exit(0);
} else {
printf("%s: Process#%d died with status 0x%x (%d)\n",
REAPER_NAME,
pid,
status, status);
}
} // End while
}
break;
default: {
printf("Received signal %s (%d) from process#%d\n", strsignal(sig), sig, info->si_pid);
}
break;
} // End switch
} // sig_hdl
int main(int ac, char *av[])
{
int sig;
int rc;
int i;
struct sigaction action;
sigset_t sigset;
if (ac < 2) {
fprintf(stderr, "Usage: %s sig1 sig2...\n", basename(av[0]));
return 1;
}
for (i = 1; av[i]; i ++) {
sig = sig_str2type(av[i]);
if (sig > 0) {
sigemptyset(&sigset);
action.sa_sigaction = sig_hdl;
action.sa_mask = sigset;
action.sa_flags = SA_SIGINFO;
printf("Capturing signal '%s' (%d)\n", av[i], sig);
rc = sigaction(sig, &action, NULL);
if (rc != 0) {
ERR("sigaction(%s): '%m' (%d)\n", av[i], errno);
return 1;
}
} else {
fprintf(stderr, "Unknown signal '%s'\n", av[i]);
return 1;
}
} // End for
printf("%s#%d: forking a shell...\n", REAPER_NAME, getpid());
// Create a child process for the shell
pid_sh = fork();
switch(pid_sh) {
// Error
case -1 : {
fprintf(stderr, "fork(): '%m' (%d)\n", errno);
return 1;
}
break;
// Child
case 0: {
char *avs[2] = {
"/bin/sh",
NULL
};
extern char **environ;
execve(avs[0], avs, environ);
_exit(1);
}
break;
// Father
default : {
// Main loop
while(1) {
pause();
} // End while
}
break;
} // End switch
return 0;
} // main