-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.c
102 lines (91 loc) · 2.76 KB
/
pipe.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
/**
* Author: Samuel Marsh
* Date: 30/10/2015
*/
#include "mysh.h"
#define FD_READ 0
#define FD_WRITE 1
/**
* Executes a command tree node, passing one child's stdout to the other's stdin
* (piping the execution).
*
* @param t the command tree, with root node of type N_PIPE
* @return the exit status of the right hand side of the tree (the process
* which is receiving stdin from the other side's stdout)
*/
int execute_pipe(CMDTREE *t)
{
int fd[2];
//index 0 (FD_READ) is for reading, index 1 (FD_WRITE) is for writing
if (pipe(fd) == -1)
{
MYSH_PERROR("execute_pipe");
return EXIT_FAILURE;
}
switch (fork())
{
case FORK_FAILURE:
{
MYSH_PERROR("execute_pipe");
return EXIT_FAILURE;
}
case FORK_CHILD:
{
//set stdout to write to the pipe
if (dup2(fd[FD_WRITE], fileno(stdout)) == -1)
{
MYSH_PERROR("execute_pipe");
exit(EXIT_FAILURE);
}
//close both piped files - we have stdout now which is all we need
close(fd[FD_READ]);
close(fd[FD_WRITE]);
//not really important what status we exit with, this value is
//ignored anyway
exit(execute_cmdtree(t->left));
break;
}
default:
{
//after much debugging, should fork again in parent... was forking again
//in child, but led to weird race condition (?) where WEXITSTATUS in
//execute_external_command would return exit status of wrong command
switch (fork())
{
case FORK_FAILURE:
{
MYSH_PERROR("execute_pipe");
return EXIT_FAILURE;
}
case FORK_CHILD:
{
//set stdin to read from the pipe
if (dup2(fd[FD_READ], fileno(stdin)) == -1)
{
MYSH_PERROR("execute_pipe");
exit(EXIT_FAILURE);
}
//close both piped files - we have stdin now which is all we need
close(fd[FD_READ]);
close(fd[FD_WRITE]);
exit(execute_cmdtree(t->right));
break;
}
default:
{
//close both in parent as well
close(fd[FD_READ]);
close(fd[FD_WRITE]);
int exit_status;
//we are in the original shell - just wait for the children to finish
while (wait(&exit_status) > 0);
exit_status = WEXITSTATUS(exit_status);
return exit_status;
}
}
}
}
//we are only here if one of the children somehow failed to exit, (probably) won't ever happen
fprintf(stderr, "failed to exit process\n");
return EXIT_FAILURE;
}