diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index 6f7db1df62..6bcf117b2d 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -418,7 +418,7 @@ BAN::Optional execute_builtin(BAN::Vector& args, int fd_in, in return 0; } -pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_out, bool set_pgrp) +pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_out, pid_t pgrp) { if (args.empty()) return 0; @@ -451,15 +451,23 @@ pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_ close(fd_out); } - if (set_pgrp) + if (pgrp == 0) { - pid_t pgrp = setpgrp(); - if (tcsetpgrp(0, pgrp) == -1) + if(setpgid(0, 0) == -1) + { + perror("setpgid"); + exit(1); + } + if (tcsetpgrp(0, getpgrp()) == -1) { perror("tcsetpgrp"); exit(1); } } + else + { + setpgid(0, pgrp); + } execv(cmd_args.front(), cmd_args.data()); perror("execv"); @@ -471,7 +479,7 @@ pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_ int execute_command(BAN::Vector& args, int fd_in, int fd_out) { - pid_t pid = execute_command_no_wait(args, fd_in, fd_out, true); + pid_t pid = execute_command_no_wait(args, fd_in, fd_out, 0); if (pid == -1) ERROR_RETURN("fork", 1); @@ -503,6 +511,7 @@ int execute_piped_commands(BAN::Vector>& commands) BAN::Vector exit_codes(commands.size(), 0); BAN::Vector processes(commands.size(), -1); + pid_t pgrp = 0; int next_stdin = STDIN_FILENO; for (size_t i = 0; i < commands.size(); i++) @@ -524,8 +533,10 @@ int execute_piped_commands(BAN::Vector>& commands) exit_codes[i] = builtin_ret.value(); else { - pid_t pid = execute_command_no_wait(commands[i], next_stdin, pipefd[1], first); + pid_t pid = execute_command_no_wait(commands[i], next_stdin, pipefd[1], pgrp); processes[i] = pid; + if (pgrp == 0) + pgrp = pid; } if (next_stdin != STDIN_FILENO)