diff --git a/bin/command.c b/bin/command.c index 3a2e21fa044c19213654d28502c557ae660d6e98..2f39fcce45ffa64c83d6abd2f4560a6a90ee16e9 100644 --- a/bin/command.c +++ b/bin/command.c @@ -80,7 +80,7 @@ void exec_builtin(char **argv, int argc) return; } -void exec_job(cmd_t *cmd, bool foreground) +void exec_job(cmd_t *cmd, bool foreground, int* pid_job) { int child_status; @@ -111,6 +111,7 @@ void exec_job(cmd_t *cmd, bool foreground) // Create child with fork pid_t pid = fork(); + *pid_job = pid; if (pid == 0) { @@ -230,7 +231,7 @@ void exec_job(cmd_t *cmd, bool foreground) // Function where the piped system commands is executed // We use the pipe function to get two file descriptors, then we redirect the output fd to the input fd -void exec_job_piped(cmd_t *cmd, bool foreground) +void exec_job_piped(cmd_t *cmd, bool foreground, int* pid_job) { // pipe() syscall needs an array of 2 fd // 0 is the read file descriptor (used by process 2) @@ -251,6 +252,8 @@ void exec_job_piped(cmd_t *cmd, bool foreground) // Fork to prepare running our first command pid = fork(); + *pid_job = pid; + if (pid < 0) { printf("Error while forking on the first processs\n"); diff --git a/bin/command.h b/bin/command.h index 05a3c3b9cc35f0cf1efc4b05a650d9f84777dacd..2bed089193d712de6c863936ec0ed4f485bb1f9b 100644 --- a/bin/command.h +++ b/bin/command.h @@ -22,11 +22,11 @@ void exec_builtin(char** argv, int argc); //IN: // cmd: struct countaining the command to be executed // foreground: if the task has to be run in foreground or background ('&' sign) -void exec_job(cmd_t *cmd, bool foreground); +void exec_job(cmd_t *cmd, bool foreground, pid_t* pid_job); // Execute a parsed pipe command as job (forks process) //IN: // cmd: struct countaining the commands to be executed and piped together // [UNUSED !!!] foreground: if the task has to be run in foreground or background ('&' sign) -void exec_job_piped(cmd_t *cmd, bool foreground); +void exec_job_piped(cmd_t *cmd, bool foreground, pid_t* pid_job); #endif \ No newline at end of file diff --git a/bin/command.o b/bin/command.o index 1751a4c63304c86ed0be32c7477bd4c7013b7ee9..a763b9c62676e45dea396c215356f48a026e4852 100644 Binary files a/bin/command.o and b/bin/command.o differ diff --git a/bin/main.c b/bin/main.c index 97baeaa9034f786db6b2bee8b598d617a0fd6dc9..293f5a9649058a98a4acd53f94751e3ca549451d 100644 --- a/bin/main.c +++ b/bin/main.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <errno.h> #include <signal.h> +#include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/signal.h> @@ -12,6 +13,9 @@ #define MAX_CMD_LEN 255 +// Stores the PID of the current foreground job +int MAIN_JOB_PID = 0; + void sigchld_handler(int signal) { int status; @@ -28,13 +32,33 @@ void sigchld_handler(int signal) } } +void sighup(int signum) +{ + exit(EXIT_SUCCESS); +} + +void sigint_job(int signum) +{ + if (MAIN_JOB_PID) + { + kill(MAIN_JOB_PID, signum); + printf("\nForeground job interrupted.\n"); + } +} + int main(int argc, char *const argv[]) { printf(BOLDCYAN "Welcome to WaffleShell (#sh), use at your own risk !\n" RESET); - // Ignore "CTRL + C" (Interrupt signal) - //signal(SIGINT, SIG_IGN); - //signal(SIGQUIT, SIG_IGN); + // Redirect SIGHUP to all (main + subprocesses) + signal(SIGHUP, sighup); + + // Redirect SIGINT to the foreground job, else it's ignored + signal(SIGINT, sigint_job); + + // Ignore SIGTERM and SIGQUIT (default bash behaviour) + signal(SIGTERM, SIG_IGN); + signal(SIGQUIT, SIG_IGN); // Prepare a handler to handle sigchild struct sigaction sig_action; @@ -51,6 +75,9 @@ int main(int argc, char *const argv[]) while (true) { + // Reset main job PID + MAIN_JOB_PID = 0; + char *cmd_str = malloc((size_t)sizeof(char) * MAX_CMD_LEN); cmd_t *cmd_in = malloc((size_t)sizeof(cmd_t)); @@ -83,12 +110,12 @@ int main(int argc, char *const argv[]) if (cmd_in->type == CMD_PIPE) { // Piped job - exec_job_piped(cmd_in, cmd_in->foreground); + exec_job_piped(cmd_in, cmd_in->foreground, &MAIN_JOB_PID); } else { // Regular job (with or without input/output redirection) - exec_job(cmd_in, cmd_in->foreground); + exec_job(cmd_in, cmd_in->foreground, &MAIN_JOB_PID); } } diff --git a/bin/main.o b/bin/main.o index 312f10a69228e8b2dc97183a4a6b9b5fc6fe3e30..8df1e450f301e084e53be7dab3bd5ddfe111a195 100644 Binary files a/bin/main.o and b/bin/main.o differ diff --git a/wshell b/wshell index 4e2e1cc542418ab40cde9d06bee4b0ea6e5a5c36..0a312f72640baae2e87db5369b9672783b95029c 100644 Binary files a/wshell and b/wshell differ