From 6ada36d3cba52801fc974f498ee3303a50dec2ca Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 22 Aug 2023 11:37:04 +0300 Subject: [PATCH] Shell/init: We now use pgrp instead of pid and init open terminal --- userspace/Shell/main.cpp | 25 +++++++++++++++---------- userspace/init/main.cpp | 17 ++++++++++------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index 5791b87b0..6f7db1df6 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) +pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_out, bool set_pgrp) { if (args.empty()) return 0; @@ -451,6 +451,16 @@ pid_t execute_command_no_wait(BAN::Vector& args, int fd_in, int fd_ close(fd_out); } + if (set_pgrp) + { + pid_t pgrp = setpgrp(); + if (tcsetpgrp(0, pgrp) == -1) + { + perror("tcsetpgrp"); + exit(1); + } + } + execv(cmd_args.front(), cmd_args.data()); perror("execv"); exit(1); @@ -461,18 +471,15 @@ 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); + pid_t pid = execute_command_no_wait(args, fd_in, fd_out, true); if (pid == -1) ERROR_RETURN("fork", 1); - if (tcsetpgrp(0, pid) == -1) - ERROR_RETURN("tcsetpgrp", 1); - int status; if (waitpid(pid, &status, 0) == -1) ERROR_RETURN("waitpid", 1); - if (tcsetpgrp(0, getpid()) == -1) + if (tcsetpgrp(0, getpgrp()) == -1) ERROR_RETURN("tcsetpgrp", 1); if (WIFSIGNALED(status)) @@ -517,10 +524,8 @@ 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]); + pid_t pid = execute_command_no_wait(commands[i], next_stdin, pipefd[1], first); processes[i] = pid; - if (first && tcsetpgrp(0, pid) == -1) - ERROR_RETURN("tcsetpgrp", 1); } if (next_stdin != STDIN_FILENO) @@ -550,7 +555,7 @@ int execute_piped_commands(BAN::Vector>& commands) exit_codes[i] = WEXITSTATUS(status); } - if (tcsetpgrp(0, getpid()) == -1) + if (tcsetpgrp(0, getpgrp()) == -1) ERROR_RETURN("tcsetpgrp", 1); return exit_codes.back(); diff --git a/userspace/init/main.cpp b/userspace/init/main.cpp index b09917966..92f0f29bf 100644 --- a/userspace/init/main.cpp +++ b/userspace/init/main.cpp @@ -11,9 +11,8 @@ void initialize_stdio() { - char tty[L_ctermid]; - ctermid(tty); - if (open(tty, O_RDONLY) != 0) _exit(1); + const char* tty = "/dev/tty0"; + if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1); if (open(tty, O_WRONLY) != 1) _exit(1); if (open(tty, O_WRONLY) != 2) _exit(1); } @@ -65,6 +64,13 @@ int main() pid_t pid = fork(); if (pid == 0) { + pid_t pgrp = setpgrp(); + if (tcsetpgrp(0, pgrp) == -1) + { + perror("tcsetpgrp"); + exit(1); + } + printf("Welcome back %s!\n", pwd->pw_name); if (setgid(pwd->pw_gid) == -1) @@ -89,13 +95,10 @@ int main() break; } - if (tcsetpgrp(0, pid) == -1) - perror("tcsetpgrp"); - int status; waitpid(pid, &status, 0); - if (tcsetpgrp(0, getpid()) == -1) + if (tcsetpgrp(0, getpgrp()) == -1) perror("tcsetpgrp"); }