Skip to content
Snippets Groups Projects
Commit b435ebcd authored by julien.seemulle's avatar julien.seemulle :cowboy:
Browse files

Sending SIGINT to foreground jobs working.

parent f430091a
No related branches found
No related tags found
No related merge requests found
...@@ -80,7 +80,7 @@ void exec_builtin(char **argv, int argc) ...@@ -80,7 +80,7 @@ void exec_builtin(char **argv, int argc)
return; return;
} }
void exec_job(cmd_t *cmd, bool foreground) void exec_job(cmd_t *cmd, bool foreground, int* pid_job)
{ {
int child_status; int child_status;
...@@ -111,6 +111,7 @@ void exec_job(cmd_t *cmd, bool foreground) ...@@ -111,6 +111,7 @@ void exec_job(cmd_t *cmd, bool foreground)
// Create child with fork // Create child with fork
pid_t pid = fork(); pid_t pid = fork();
*pid_job = pid;
if (pid == 0) if (pid == 0)
{ {
...@@ -230,7 +231,7 @@ void exec_job(cmd_t *cmd, bool foreground) ...@@ -230,7 +231,7 @@ void exec_job(cmd_t *cmd, bool foreground)
// Function where the piped system commands is executed // 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 // 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 // pipe() syscall needs an array of 2 fd
// 0 is the read file descriptor (used by process 2) // 0 is the read file descriptor (used by process 2)
...@@ -251,6 +252,8 @@ void exec_job_piped(cmd_t *cmd, bool foreground) ...@@ -251,6 +252,8 @@ void exec_job_piped(cmd_t *cmd, bool foreground)
// Fork to prepare running our first command // Fork to prepare running our first command
pid = fork(); pid = fork();
*pid_job = pid;
if (pid < 0) if (pid < 0)
{ {
printf("Error while forking on the first processs\n"); printf("Error while forking on the first processs\n");
......
...@@ -22,11 +22,11 @@ void exec_builtin(char** argv, int argc); ...@@ -22,11 +22,11 @@ void exec_builtin(char** argv, int argc);
//IN: //IN:
// cmd: struct countaining the command to be executed // cmd: struct countaining the command to be executed
// foreground: if the task has to be run in foreground or background ('&' sign) // 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) // Execute a parsed pipe command as job (forks process)
//IN: //IN:
// cmd: struct countaining the commands to be executed and piped together // 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) // [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 #endif
\ No newline at end of file
No preview for this file type
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/signal.h> #include <sys/signal.h>
...@@ -12,6 +13,9 @@ ...@@ -12,6 +13,9 @@
#define MAX_CMD_LEN 255 #define MAX_CMD_LEN 255
// Stores the PID of the current foreground job
int MAIN_JOB_PID = 0;
void sigchld_handler(int signal) void sigchld_handler(int signal)
{ {
int status; int status;
...@@ -28,13 +32,33 @@ void sigchld_handler(int signal) ...@@ -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[]) int main(int argc, char *const argv[])
{ {
printf(BOLDCYAN "Welcome to WaffleShell (#sh), use at your own risk !\n" RESET); printf(BOLDCYAN "Welcome to WaffleShell (#sh), use at your own risk !\n" RESET);
// Ignore "CTRL + C" (Interrupt signal) // Redirect SIGHUP to all (main + subprocesses)
//signal(SIGINT, SIG_IGN); signal(SIGHUP, sighup);
//signal(SIGQUIT, SIG_IGN);
// 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 // Prepare a handler to handle sigchild
struct sigaction sig_action; struct sigaction sig_action;
...@@ -51,6 +75,9 @@ int main(int argc, char *const argv[]) ...@@ -51,6 +75,9 @@ int main(int argc, char *const argv[])
while (true) while (true)
{ {
// Reset main job PID
MAIN_JOB_PID = 0;
char *cmd_str = malloc((size_t)sizeof(char) * MAX_CMD_LEN); char *cmd_str = malloc((size_t)sizeof(char) * MAX_CMD_LEN);
cmd_t *cmd_in = malloc((size_t)sizeof(cmd_t)); cmd_t *cmd_in = malloc((size_t)sizeof(cmd_t));
...@@ -83,12 +110,12 @@ int main(int argc, char *const argv[]) ...@@ -83,12 +110,12 @@ int main(int argc, char *const argv[])
if (cmd_in->type == CMD_PIPE) if (cmd_in->type == CMD_PIPE)
{ {
// Piped job // Piped job
exec_job_piped(cmd_in, cmd_in->foreground); exec_job_piped(cmd_in, cmd_in->foreground, &MAIN_JOB_PID);
} }
else else
{ {
// Regular job (with or without input/output redirection) // 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);
} }
} }
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment