-
Notifications
You must be signed in to change notification settings - Fork 3
/
reaper.cpp
65 lines (55 loc) · 1.06 KB
/
reaper.cpp
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
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#if !defined( PR_SET_CHILD_SUBREAPER )
#define PR_SET_CHILD_SUBREAPER 36
#endif
int main( int argc, char **argv )
{
int sub_command_argc = 0;
for ( int i = 0; i < argc; i++ )
{
if ( strcmp( "--", argv[ i ] ) == 0 && i + 1 < argc )
{
sub_command_argc = i + 1;
break;
}
}
if ( sub_command_argc == 0 )
{
fprintf( stderr, "reaper: no sub-command!\n" );
exit( 1 );
}
// (Don't Lose) The Children
if ( prctl( PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0 ) == -1 )
{
fprintf( stderr, "reaper: prctl() failed!\n" );
exit( 1 );
}
pid_t pid = fork();
if ( pid == -1 )
{
fprintf( stderr, "reaper: fork() failed!\n" );
exit( 1 );
}
// Are we in the child?
if ( pid == 0 )
{
execvp( argv[ sub_command_argc ], &argv[ sub_command_argc ] );
}
pid_t wait_ret;
while( true )
{
wait_ret = wait( NULL );
if ( wait_ret == -1 && errno == ECHILD )
{
// No more children.
break;
}
}
}