-
Notifications
You must be signed in to change notification settings - Fork 28
/
fghack.c
52 lines (43 loc) · 1.07 KB
/
fghack.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
#include <unistd.h>
#include "wait.h"
#include "error.h"
#include "strerr.h"
#include "buffer.h"
#include "pathexec.h"
#define FATAL "fghack: fatal: "
int pid;
int main(int argc,const char * const *argv,const char * const *envp)
{
char ch;
int wstat;
int pi[2];
int i;
int ignored;
if (!argv[1])
strerr_die1x(100,"fghack: usage: fghack child");
if (pipe(pi) == -1)
strerr_die2sys(111,FATAL,"unable to create pipe");
switch(pid = fork()) {
case -1:
strerr_die2sys(111,FATAL,"unable to fork");
case 0:
close(pi[0]);
for (i = 0;i < 30;++i)
ignored = dup(pi[1]);
pathexec_run(argv[1],argv + 1,envp);
strerr_die3sys(111,FATAL,"unable to run ",argv[1]);
}
close(pi[1]);
for (;;) {
i = buffer_unixread(pi[0],&ch,1);
if ((i == -1) && (errno == error_intr)) continue;
if (i == 1) continue;
break;
}
if (wait_pid(&wstat,pid) == -1)
strerr_die2sys(111,FATAL,"wait failed");
if (wait_crashed(wstat))
strerr_die2x(111,FATAL,"child crashed");
_exit(wait_exitcode(wstat));
(void)ignored;
}