diff options
| author | Ákos Kőrösi <korakos99@gmail.com> | 2026-05-21 17:22:29 +0200 |
|---|---|---|
| committer | Ákos Kőrösi <korakos99@gmail.com> | 2026-05-21 17:22:29 +0200 |
| commit | 9c2bb0514c0413e897c46d3a58d5188d359b1938 (patch) | |
| tree | fe9d00edc98ca234b955f2c633d709328ff5ab5b /src/exec.c | |
| parent | f4b209f1622afd61cf146f7e52bc455ed790c703 (diff) | |
add waiting flag to pipeline executor
- also update gitignore with sample/
Diffstat (limited to 'src/exec.c')
| -rw-r--r-- | src/exec.c | 26 |
1 files changed, 14 insertions, 12 deletions
@@ -7,7 +7,7 @@ #include "syntax.h" #include "utils.h" -void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd); +void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid); void execute_cmd(ExecutableCommand cmd, int* pgid) { @@ -54,7 +54,7 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) { } -ExecutableCommand process_pl_command(PlCommand pl_cmd) { +ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid) { StrVec arg_vec = make_StrVec(256); FdRedirectVec fd_redirs = make_FdRedirectVec(256); IntVec parent_open_fds = make_IntVec(256); @@ -70,7 +70,7 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) { break; case ARG_PS_IN: pipe(ps_pipe_fds); - execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1]); + execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1], false, pgid); close(ps_pipe_fds[1]); char pname[64]; snprintf(pname, sizeof(pname), "/dev/fd/%d", ps_pipe_fds[0]); @@ -83,7 +83,7 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) { // TODO: don't do fcntl? fcntl(ps_pipe_fds[1], F_SETFD, FD_CLOEXEC); - execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, ps_pipe_fds[0], STDOUT_FILENO); + execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, ps_pipe_fds[0], STDOUT_FILENO, false, pgid); fcntl(ps_pipe_fds[1], F_SETFD, 0); close(ps_pipe_fds[0]); char name[64]; @@ -135,13 +135,12 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) { } -void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) { +void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid) { int trunk_fd = stdin_fd; - int pgid = -1; for (int i = 0; i < pipeline.count; i++) { PlCommand cmd = *get_from_Pipeline(&pipeline, i); - ExecutableCommand exec_cmd = process_pl_command(cmd); + ExecutableCommand exec_cmd = process_pl_command(cmd, pgid); exec_cmd.stdin_fd = trunk_fd; if (i > 0) { @@ -160,7 +159,7 @@ void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) { trunk_fd = pipe_fds[0]; } - execute_cmd(exec_cmd, &pgid); + execute_cmd(exec_cmd, pgid); for (int j = 0; j < exec_cmd.parent_open_fds.count; j++) { int fd = *get_from_IntVec(&exec_cmd.parent_open_fds, j); @@ -168,13 +167,16 @@ void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) { } } - tcsetpgrp(STDIN_FILENO, pgid); - while (waitpid(-pgid, NULL, 0) > 0); - tcsetpgrp(STDIN_FILENO, getpgrp()); + if (wait_here) { + tcsetpgrp(STDIN_FILENO, *pgid); + while (waitpid(-*pgid, NULL, 0) > 0); + tcsetpgrp(STDIN_FILENO, getpgrp()); + } } void execute_pipeline_tree(PipelineTree pltree) { + int pgid = -1; signal(SIGTTIN, SIG_IGN); signal(SIGTTOU, SIG_IGN); - execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO); + execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO, true, &pgid); } |
