summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/exec.c27
-rw-r--r--src/syntax.h13
2 files changed, 26 insertions, 14 deletions
diff --git a/src/exec.c b/src/exec.c
index fb579ad..995a969 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -100,8 +100,11 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) {
setpgid(0, *pgid);
}
- dup2(cmd.stdin_fd, STDIN_FILENO);
- dup2(cmd.stdout_fd, STDOUT_FILENO);
+ dup2(cmd.read_cha.fd, STDIN_FILENO);
+ dup2(cmd.write_cha.fd, STDOUT_FILENO);
+
+ if (cmd.read_cha.child_proc_closable) close(cmd.read_cha.fd);
+ if (cmd.write_cha.child_proc_closable) close(cmd.write_cha.fd);
for (int i = 0; i < cmd.redirects.count; i++) {
FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirects, i);
@@ -225,33 +228,31 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* sh
void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid, ShellState* shstate) {
- int trunk_fd = stdin_fd;
+
+ Channel trunk_read_cha = {.fd = stdin_fd, .parent_proc_closable = false, .child_proc_closable = false}; // TODO: change the signature to expect (arbitrary) Channel-s
for (int i = 0; i < pipeline.count; i++) {
PlCommand cmd = *get_from_Pipeline(&pipeline, i);
ExecutableCommand exec_cmd = process_pl_command(cmd, pgid, shstate);
- exec_cmd.stdin_fd = trunk_fd;
- if (i > 0) {
- push_to_IntVec(&exec_cmd.parent_open_fds, trunk_fd);
- push_to_IntVec(&exec_cmd.child_open_fds, trunk_fd);
- }
+ exec_cmd.read_cha = trunk_read_cha;
- exec_cmd.stdout_fd = stdout_fd;
+ exec_cmd.write_cha = (Channel){.fd = stdout_fd, .parent_proc_closable = false, .child_proc_closable = false};
if (cmd.conn == CONN_PIPE) {
int pipe_fds[2];
pipe(pipe_fds);
- exec_cmd.stdout_fd = pipe_fds[1];
- push_to_IntVec(&exec_cmd.parent_open_fds, pipe_fds[1]);
- push_to_IntVec(&exec_cmd.child_open_fds, pipe_fds[1]);
- trunk_fd = pipe_fds[0];
+ exec_cmd.write_cha = (Channel){.fd = pipe_fds[1], .parent_proc_closable = true, .child_proc_closable = true};
+ trunk_read_cha = (Channel){.fd = pipe_fds[0], .parent_proc_closable = true, .child_proc_closable = true};
}
if (!handle_builtin(exec_cmd, shstate)) {
execute_cmd(exec_cmd, pgid);
}
+ if (exec_cmd.read_cha.parent_proc_closable) close(exec_cmd.read_cha.fd);
+ if (exec_cmd.write_cha.parent_proc_closable) close(exec_cmd.write_cha.fd);
+
for (int j = 0; j < exec_cmd.parent_open_fds.count; j++) {
int fd = *get_from_IntVec(&exec_cmd.parent_open_fds, j);
close(fd);
diff --git a/src/syntax.h b/src/syntax.h
index 80ab067..07d5437 100644
--- a/src/syntax.h
+++ b/src/syntax.h
@@ -1,5 +1,6 @@
# pragma once
+#include <unistd.h>
#include "utils.h"
@@ -190,10 +191,20 @@ typedef struct {
DECLARE_VEC(FdRedirect, FdRedirectVec)
+
+typedef struct {
+ int fd;
+ bool parent_proc_closable;
+ bool child_proc_closable;
+} Channel;
+
+static const Channel STDIN_CHA = {.fd = STDIN_FILENO, .parent_proc_closable = false, .child_proc_closable = false};
+static const Channel STDOUT_CHA = {.fd = STDOUT_FILENO, .parent_proc_closable = false, .child_proc_closable = false};
+
typedef struct {
char** args;
FdRedirectVec redirects;
- int stdin_fd, stdout_fd;
+ Channel read_cha, write_cha;
IntVec parent_open_fds;
IntVec child_open_fds;
} ExecutableCommand;