summaryrefslogtreecommitdiff
path: root/src/exec.c
diff options
context:
space:
mode:
authorÁkos Kőrösi <korakos99@gmail.com>2026-05-23 21:08:47 +0200
committerÁkos Kőrösi <korakos99@gmail.com>2026-05-23 21:08:47 +0200
commit8b660b1ca5c72ca4f2e5cfea282d619ed683446c (patch)
tree0e53d312d311a35d7c5927548104686fd343ea16 /src/exec.c
parent7a106ea8332ed6d95504fd3181f7b5f81a6c3072 (diff)
define Channel type
Diffstat (limited to 'src/exec.c')
-rw-r--r--src/exec.c27
1 files changed, 14 insertions, 13 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);