Shell/init: We now use pgrp instead of pid and init open terminal

This commit is contained in:
Bananymous 2023-08-22 11:37:04 +03:00
parent 42b90ae76c
commit 6ada36d3cb
2 changed files with 25 additions and 17 deletions

View File

@ -418,7 +418,7 @@ BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, in
return 0; return 0;
} }
pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_out) pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_out, bool set_pgrp)
{ {
if (args.empty()) if (args.empty())
return 0; return 0;
@ -451,6 +451,16 @@ pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_
close(fd_out); 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()); execv(cmd_args.front(), cmd_args.data());
perror("execv"); perror("execv");
exit(1); exit(1);
@ -461,18 +471,15 @@ pid_t execute_command_no_wait(BAN::Vector<BAN::String>& args, int fd_in, int fd_
int execute_command(BAN::Vector<BAN::String>& args, int fd_in, int fd_out) int execute_command(BAN::Vector<BAN::String>& 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) if (pid == -1)
ERROR_RETURN("fork", 1); ERROR_RETURN("fork", 1);
if (tcsetpgrp(0, pid) == -1)
ERROR_RETURN("tcsetpgrp", 1);
int status; int status;
if (waitpid(pid, &status, 0) == -1) if (waitpid(pid, &status, 0) == -1)
ERROR_RETURN("waitpid", 1); ERROR_RETURN("waitpid", 1);
if (tcsetpgrp(0, getpid()) == -1) if (tcsetpgrp(0, getpgrp()) == -1)
ERROR_RETURN("tcsetpgrp", 1); ERROR_RETURN("tcsetpgrp", 1);
if (WIFSIGNALED(status)) if (WIFSIGNALED(status))
@ -517,10 +524,8 @@ int execute_piped_commands(BAN::Vector<BAN::Vector<BAN::String>>& commands)
exit_codes[i] = builtin_ret.value(); exit_codes[i] = builtin_ret.value();
else 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; processes[i] = pid;
if (first && tcsetpgrp(0, pid) == -1)
ERROR_RETURN("tcsetpgrp", 1);
} }
if (next_stdin != STDIN_FILENO) if (next_stdin != STDIN_FILENO)
@ -550,7 +555,7 @@ int execute_piped_commands(BAN::Vector<BAN::Vector<BAN::String>>& commands)
exit_codes[i] = WEXITSTATUS(status); exit_codes[i] = WEXITSTATUS(status);
} }
if (tcsetpgrp(0, getpid()) == -1) if (tcsetpgrp(0, getpgrp()) == -1)
ERROR_RETURN("tcsetpgrp", 1); ERROR_RETURN("tcsetpgrp", 1);
return exit_codes.back(); return exit_codes.back();

View File

@ -11,9 +11,8 @@
void initialize_stdio() void initialize_stdio()
{ {
char tty[L_ctermid]; const char* tty = "/dev/tty0";
ctermid(tty); if (open(tty, O_RDONLY | O_TTY_INIT) != 0) _exit(1);
if (open(tty, O_RDONLY) != 0) _exit(1);
if (open(tty, O_WRONLY) != 1) _exit(1); if (open(tty, O_WRONLY) != 1) _exit(1);
if (open(tty, O_WRONLY) != 2) _exit(1); if (open(tty, O_WRONLY) != 2) _exit(1);
} }
@ -65,6 +64,13 @@ int main()
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) if (pid == 0)
{ {
pid_t pgrp = setpgrp();
if (tcsetpgrp(0, pgrp) == -1)
{
perror("tcsetpgrp");
exit(1);
}
printf("Welcome back %s!\n", pwd->pw_name); printf("Welcome back %s!\n", pwd->pw_name);
if (setgid(pwd->pw_gid) == -1) if (setgid(pwd->pw_gid) == -1)
@ -89,13 +95,10 @@ int main()
break; break;
} }
if (tcsetpgrp(0, pid) == -1)
perror("tcsetpgrp");
int status; int status;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (tcsetpgrp(0, getpid()) == -1) if (tcsetpgrp(0, getpgrp()) == -1)
perror("tcsetpgrp"); perror("tcsetpgrp");
} }