MiniDevil As beautiful as a shell
executor_pipe.c File Reference

Pipe execution: forks and handles left / right sides of pipes. More...

#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include "executor.h"
#include "libft.h"
#include "ast.h"
#include "env.h"
#include "signals.h"
#include "get_next_line.h"
+ Include dependency graph for executor_pipe.c:

Detailed Description

Pipe execution: forks and handles left / right sides of pipes.

Definition in file executor_pipe.c.

Functions

void exec_left_pipe_child (t_ast *left, int pipe_fd[2], t_shell *shell)
 Execute the left side of a pipe in a child process. More...
 
void exec_right_pipe_child (t_ast *right, int pipe_fd[2], t_shell *shell)
 Execute the right side of a pipe in a child process. More...
 
static int wait_for_pipe_children (pid_t left_pid, pid_t right_pid)
 Wait for both pipe children and return the right one's status. More...
 
int handle_pipe (t_ast *node, t_shell *shell)
 Execute a pipe node by forking left and right children. More...
 

Function Documentation

◆ exec_left_pipe_child()

void exec_left_pipe_child ( t_ast left,
int  pipe_fd[2],
t_shell shell 
)

Execute the left side of a pipe in a child process.

Closes the read end of the pipe and redirects STDOUT to the write end

Parameters
leftLeft AST subtree to execute
pipe_fdPipe file descriptors: [read-—write]
shellShell context
Note
Never returns (calls exit)

Definition at line 33 of file executor_pipe.c.

References t_shell::current_ast, t_shell::current_input, t_shell::env, executor(), free_ast(), free_env_list(), t_shell::is_child, and reset_child_signals().

Referenced by handle_pipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exec_right_pipe_child()

void exec_right_pipe_child ( t_ast right,
int  pipe_fd[2],
t_shell shell 
)

Execute the right side of a pipe in a child process.

Closes the write end of the pipe and redirects STDIN to the read end

Parameters
rightRight AST subtree to execute
pipe_fdPipe file descriptors: [read-—write]
shellShell context
Note
Never returns (calls exit)

Definition at line 60 of file executor_pipe.c.

References t_shell::current_ast, t_shell::current_input, t_shell::env, executor(), free_ast(), free_env_list(), t_shell::is_child, and reset_child_signals().

Referenced by handle_pipe().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ wait_for_pipe_children()

static int wait_for_pipe_children ( pid_t  left_pid,
pid_t  right_pid 
)
static

Wait for both pipe children and return the right one's status.

Handles signal deaths (SIGINT = newline, SIGQUIT = core dump message)

Parameters
left_pidPID of the left pipe child
right_pidPID of the right pipe child
Returns
Exit status of the right child (0-255)

Definition at line 86 of file executor_pipe.c.

Referenced by handle_pipe().

+ Here is the caller graph for this function:

◆ handle_pipe()

int handle_pipe ( t_ast node,
t_shell shell 
)

Execute a pipe node by forking left and right children.

Creates a pipe, forks 2 children:

  • Left child writes to pipe and right reads from it
  • Parent closes both pipe ends & waits for both children
Parameters
nodeAST pipe node with binary.left and binary.right subtrees
shellShell context
Returns
Exit status of the rightmost command in the pipe

Definition at line 119 of file executor_pipe.c.

References t_ast_data::binary, t_ast::data, exec_left_pipe_child(), exec_right_pipe_child(), t_binary_node::left, pipe_fork_error(), t_binary_node::right, and wait_for_pipe_children().

Referenced by executor().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Go to the source code of this file.