diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/exec.c | 55 | ||||
| -rw-r--r-- | src/syntax.h | 5 |
2 files changed, 40 insertions, 20 deletions
@@ -111,9 +111,9 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) { dup2(fd_rd.to, fd_rd.redirected); } - for (int j = 0; j < cmd.child_open_fds.count; j++) { - int fd = *get_from_IntVec(&cmd.child_open_fds, j); - close(fd); + for (int j = 0; j < cmd.other_open_channels.count; j++) { + Channel cha = *get_from_ChannelVec(&cmd.other_open_channels, j); + if (cha.child_proc_closable) close(cha.fd); } signal(SIGINT, SIG_DFL); @@ -136,15 +136,16 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) { ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* shstate) { + StrVec arg_vec = make_StrVec(256); FdRedirectVec fd_redirs = make_FdRedirectVec(256); - IntVec parent_open_fds = make_IntVec(256); - IntVec child_open_fds = make_IntVec(256); + ChannelVec other_open_channels = make_ChannelVec(256); int ps_pipe_fds[2]; for (int i = 0; i < pl_cmd.args.count; i++) { Arg arg = *get_from_ArgVec(&pl_cmd.args, i); + Channel ps_cha; switch (arg.kind) { case ARG_LIT: push_to_StrVec(&arg_vec, arg.as.lit_str); @@ -154,9 +155,10 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* sh Channel write_cha = {.fd = ps_pipe_fds[1], .parent_proc_closable = true, .child_proc_closable = true}; execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_CHA, write_cha, false, pgid, shstate); //close(ps_pipe_fds[1]); + + push_to_ChannelVec(&other_open_channels, (Channel){.fd = ps_pipe_fds[0], .parent_proc_closable = true, .child_proc_closable = false}); char pname[64]; snprintf(pname, sizeof(pname), "/dev/fd/%d", ps_pipe_fds[0]); - push_to_IntVec(&parent_open_fds, ps_pipe_fds[0]); push_to_StrVec(&arg_vec, strdup(pname)); break; case ARG_PS_OUT: @@ -167,11 +169,11 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* sh fcntl(ps_pipe_fds[1], F_SETFD, FD_CLOEXEC); Channel read_cha = {.fd = ps_pipe_fds[0], .parent_proc_closable = true, .child_proc_closable = true}; execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, read_cha, STDOUT_CHA, false, pgid, shstate); - //close(ps_pipe_fds[0]); fcntl(ps_pipe_fds[1], F_SETFD, 0); + push_to_ChannelVec(&other_open_channels, (Channel){.fd = ps_pipe_fds[0], .parent_proc_closable = true, .child_proc_closable = false}); + //close(ps_pipe_fds[0]); char name[64]; snprintf(name, sizeof(name), "/dev/fd/%d", ps_pipe_fds[1]); - push_to_IntVec(&parent_open_fds, ps_pipe_fds[1]); push_to_StrVec(&arg_vec, strdup(name)); break; default: @@ -183,23 +185,36 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* sh Redirect redir = *get_from_RedirectVec(&pl_cmd.redirects, j); FdRedirect fd_redir; int fd; + Channel file_cha; switch (redir.kind) { case REDIR_READ: fd = open(redir.as.filename, O_RDONLY, 0644); - push_to_IntVec(&parent_open_fds, fd); - push_to_IntVec(&child_open_fds, fd); + file_cha = (Channel){ + .fd = fd, + .parent_proc_closable = true, + .child_proc_closable = true, + }; + push_to_ChannelVec(&other_open_channels, file_cha); fd_redir = (FdRedirect){.to = fd, .redirected = STDIN_FILENO}; break; case REDIR_WRITE_APPEND: fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_APPEND, 0644); - push_to_IntVec(&parent_open_fds, fd); - push_to_IntVec(&child_open_fds, fd); + file_cha = (Channel){ + .fd = fd, + .parent_proc_closable = true, + .child_proc_closable = true, + }; + push_to_ChannelVec(&other_open_channels, file_cha); fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO}; break; case REDIR_WRITE_TRUNC: fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); - push_to_IntVec(&parent_open_fds, fd); - push_to_IntVec(&child_open_fds, fd); + file_cha = (Channel){ + .fd = fd, + .parent_proc_closable = true, + .child_proc_closable = true, + }; + push_to_ChannelVec(&other_open_channels, file_cha); fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO}; break; default: @@ -225,7 +240,11 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* sh } args[arg_vec.count] = NULL; - return (ExecutableCommand){.args = args, .redirects = fd_redirs, .parent_open_fds = parent_open_fds, .child_open_fds = child_open_fds}; + return (ExecutableCommand){ + .args = args, + .redirects = fd_redirs, + .other_open_channels = other_open_channels + }; } @@ -255,9 +274,9 @@ void execute_command_pipeline(Pipeline pipeline, Channel read_cha, Channel write 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); + for (int j = 0; j < exec_cmd.other_open_channels.count; j++) { + Channel cha = *get_from_ChannelVec(&exec_cmd.other_open_channels, j); + if (cha.parent_proc_closable) close(cha.fd); } } diff --git a/src/syntax.h b/src/syntax.h index 07d5437..02e3145 100644 --- a/src/syntax.h +++ b/src/syntax.h @@ -201,10 +201,11 @@ typedef struct { 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}; +DECLARE_VEC(Channel, ChannelVec) + typedef struct { char** args; FdRedirectVec redirects; Channel read_cha, write_cha; - IntVec parent_open_fds; - IntVec child_open_fds; + ChannelVec other_open_channels; } ExecutableCommand; |
