diff options
| -rw-r--r-- | include/all.h | 2 | ||||
| -rw-r--r-- | include/ground.h | 2 | ||||
| -rw-r--r-- | include/signal_handler.h (renamed from include/handler.h) | 10 | ||||
| -rw-r--r-- | src/eval/eval.cc | 2 | ||||
| -rw-r--r-- | src/eval/eval_exe.cc | 2 | ||||
| -rw-r--r-- | src/main.cc | 3 | ||||
| -rw-r--r-- | src/signal/misc.cc | 61 | ||||
| -rw-r--r-- | src/signal/sigchld_handler.cc (renamed from src/handler.cc) | 121 | ||||
| -rw-r--r-- | src/signal/sigint_handler.cc | 37 | ||||
| -rw-r--r-- | src/signal/sigquit_handler | 17 | ||||
| -rw-r--r-- | src/signal/sigtstp_handler.cc | 32 |
11 files changed, 156 insertions, 133 deletions
diff --git a/include/all.h b/include/all.h index 742eb00..3ee78ef 100644 --- a/include/all.h +++ b/include/all.h @@ -9,7 +9,7 @@ #include "eval.h" #include "global.h" #include "ground.h" -#include "handler.h" +#include "signal_handler.h" #include "init.h" #include "job.h" #include "path.h" diff --git a/include/ground.h b/include/ground.h index a1985eb..e2e07d8 100644 --- a/include/ground.h +++ b/include/ground.h @@ -8,7 +8,7 @@ #include "global.h" #include "util/io.h" #include "job.h" -#include "handler.h" +#include "signal_handler.h" namespace ov4 { diff --git a/include/handler.h b/include/signal_handler.h index 71db1b9..12ab0a5 100644 --- a/include/handler.h +++ b/include/signal_handler.h @@ -1,14 +1,6 @@ #pragma once -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <ctype.h> #include <signal.h> -#include <sys/types.h> -#include <sys/wait.h> -#include <errno.h> #include <atomic> #include "all.h" @@ -18,7 +10,7 @@ namespace ov4 typedef void handler_t(int); -extern sigset_t +inline sigset_t block_sig_TTOU, block_job, block_io; diff --git a/src/eval/eval.cc b/src/eval/eval.cc index 83eccd7..1551d1a 100644 --- a/src/eval/eval.cc +++ b/src/eval/eval.cc @@ -19,7 +19,7 @@ #include "lang_analysis/lexer_class.h" #include "lang_analysis/ast.h" #include "lang_analysis/token.h" -#include "handler.h" +#include "signal_handler.h" using namespace std; diff --git a/src/eval/eval_exe.cc b/src/eval/eval_exe.cc index 9a2a592..65e5a34 100644 --- a/src/eval/eval_exe.cc +++ b/src/eval/eval_exe.cc @@ -1,6 +1,8 @@ #include "eval.h" #include <string> +#include <signal.h> +#include <sys/ioctl.h> #include "error.h" #include "lang_analysis/lexer_class.h" diff --git a/src/main.cc b/src/main.cc index 088b4ca..07a3f1a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -56,9 +56,6 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) - /* This one provides a clean way to kill the shell */ - Signal(SIGQUIT, sigquit_handler); - /* Initialize the job list */ initjobs(); diff --git a/src/signal/misc.cc b/src/signal/misc.cc new file mode 100644 index 0000000..e8b780e --- /dev/null +++ b/src/signal/misc.cc @@ -0,0 +1,61 @@ +#include "signal_handler.h" + +#include <errno.h> + +namespace ov4 +{ + +void signal_init() +{ + /* Install the signal handlers */ + + sigemptyset(&block_job); + sigemptyset(&block_sig_TTOU); + sigemptyset(&block_io); + + sigaddset(&block_job, SIGINT); + sigaddset(&block_job, SIGTSTP); + sigaddset(&block_job, SIGCHLD); + + sigaddset(&block_sig_TTOU, SIGTTOU); + + sigaddset(&block_io, SIGTTOU); + sigaddset(&block_io, SIGTTIN); + + Signal(SIGINT, sigint_handler); /* ctrl-c */ + Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ + Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ + + /* This one provides a clean way to kill the shell */ + Signal(SIGQUIT, sigquit_handler); +} + +/* + * Signal - wrapper for the sigaction function + */ +handler_t *Signal([[maybe_unused]] int signum, handler_t *handler) +{ + struct sigaction action, old_action; + + action.sa_handler = handler; + sigemptyset(&action.sa_mask); /* block sigs of type being handled */ + action.sa_flags = SA_RESTART; /* restart syscalls if possible */ + + if (sigaction(signum, &action, &old_action) < 0) + unix_error("Signal error"); + return (old_action.sa_handler); +} + +int block_all(sigset_t *prev) +{ + sigset_t set; + sigfillset(&set); + return sigprocmask(SIG_SETMASK, &set, prev); +} + +int block_handler(sigset_t *prev) +{ + return sigprocmask(SIG_BLOCK, &block_job, prev); +} + +}
\ No newline at end of file diff --git a/src/handler.cc b/src/signal/sigchld_handler.cc index 7dc96a8..e11ed26 100644 --- a/src/handler.cc +++ b/src/signal/sigchld_handler.cc @@ -1,55 +1,10 @@ -#include "handler.h" +#include "signal_handler.h" -#include <termios.h> -#include <sys/ioctl.h> +#include <errno.h> namespace ov4 { -sigset_t - block_sig_TTOU, - block_job, - block_io; - -void signal_init() -{ - /* Install the signal handlers */ - - sigemptyset(&block_job); - sigemptyset(&block_sig_TTOU); - sigemptyset(&block_io); - - sigaddset(&block_job, SIGINT); - sigaddset(&block_job, SIGTSTP); - sigaddset(&block_job, SIGCHLD); - - sigaddset(&block_sig_TTOU, SIGTTOU); - - sigaddset(&block_io, SIGTTOU); - sigaddset(&block_io, SIGTTIN); - - Signal(SIGINT, sigint_handler); /* ctrl-c */ - Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ - Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ -} - -/* - * Signal - wrapper for the sigaction function - */ - -handler_t *Signal([[maybe_unused]] int signum, handler_t *handler) -{ - struct sigaction action, old_action; - - action.sa_handler = handler; - sigemptyset(&action.sa_mask); /* block sigs of type being handled */ - action.sa_flags = SA_RESTART; /* restart syscalls if possible */ - - if (sigaction(signum, &action, &old_action) < 0) - unix_error("Signal error"); - return (old_action.sa_handler); -} - /* * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever * a child job terminates (becomes a zombie), or stops because it @@ -141,74 +96,4 @@ void sigchld_handler([[maybe_unused]] int sig) return; } -/* - * sigint_handler - The kernel sends a SIGINT to the shell whenver the - * user types ctrl-c at the keyboard. Catch it and send it along - * to the foreground job. - */ -void sigint_handler([[maybe_unused]] int sig) -{ - int errno_backup = errno; - - sigset_t prev; - block_handler(&prev); - - int pid = fgpid(); - if (pid != 0) - { - kill(-pid, SIGINT); - } else { - tcflush(STDIN_FILENO, TCIFLUSH); - ioctl(STDIN_FILENO, TIOCSTI, "\n"); - } - - sigprocmask(SIG_SETMASK, &prev, nullptr); - - errno = errno_backup; - return; -} - -/* - * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever - * the user types ctrl-z at the keyboard. Catch it and suspend the - * foreground job by sending it a SIGTSTP. - */ -void sigtstp_handler([[maybe_unused]] int sig) -{ - int errno_backup = errno; - - sigset_t prev; - block_handler(&prev); - - int pid = fgpid(); - if (pid != 0) - { - kill(-pid, SIGTSTP); - } - - sigprocmask(SIG_SETMASK, &prev, nullptr); - - errno = errno_backup; - return; -} - -int block_all(sigset_t *prev) -{ - sigset_t set; - sigfillset(&set); - return sigprocmask(SIG_SETMASK, &set, prev); -} - -int block_handler(sigset_t *prev) -{ - return sigprocmask(SIG_BLOCK, &block_job, prev); -} - -void sigquit_handler([[maybe_unused]] int sig) -{ - //printf("Terminating after receipt of SIGQUIT signal\n"); - safe_output("Terminating after receipt of SIGQUIT signal\n"); - exit(1); -} - -} +}
\ No newline at end of file diff --git a/src/signal/sigint_handler.cc b/src/signal/sigint_handler.cc new file mode 100644 index 0000000..706c041 --- /dev/null +++ b/src/signal/sigint_handler.cc @@ -0,0 +1,37 @@ +#include "signal_handler.h" + +#include <errno.h> +#include <termios.h> +#include <sys/ioctl.h> + +namespace ov4 +{ + +/* + * sigint_handler - The kernel sends a SIGINT to the shell whenver the + * user types ctrl-c at the keyboard. Catch it and send it along + * to the foreground job. + */ +void sigint_handler([[maybe_unused]] int sig) +{ + int errno_backup = errno; + + sigset_t prev; + block_handler(&prev); + + int pid = fgpid(); + if (pid != 0) + { + kill(-pid, SIGINT); + } else { + tcflush(STDIN_FILENO, TCIFLUSH); + ioctl(STDIN_FILENO, TIOCSTI, "\n"); + } + + sigprocmask(SIG_SETMASK, &prev, nullptr); + + errno = errno_backup; + return; +} + +}
\ No newline at end of file diff --git a/src/signal/sigquit_handler b/src/signal/sigquit_handler new file mode 100644 index 0000000..73296eb --- /dev/null +++ b/src/signal/sigquit_handler @@ -0,0 +1,17 @@ +#include "signal_handler.h" + +#include <errno.h> + +namespace ov4 +{ + +void sigquit_handler([[maybe_unused]] int sig) +{ + //printf("Terminating after receipt of SIGQUIT signal\n"); + safe_output("Terminating after receipt of SIGQUIT signal\n"); + // UNDERSCORE'd version should be safe + // but exit() is unsafe + _exit(1); +} + +}
\ No newline at end of file diff --git a/src/signal/sigtstp_handler.cc b/src/signal/sigtstp_handler.cc new file mode 100644 index 0000000..7b32d31 --- /dev/null +++ b/src/signal/sigtstp_handler.cc @@ -0,0 +1,32 @@ +#include "signal_handler.h" + +#include <errno.h> + +namespace ov4 +{ + +/* + * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever + * the user types ctrl-z at the keyboard. Catch it and suspend the + * foreground job by sending it a SIGTSTP. + */ +void sigtstp_handler([[maybe_unused]] int sig) +{ + int errno_backup = errno; + + sigset_t prev; + block_handler(&prev); + + int pid = fgpid(); + if (pid != 0) + { + kill(-pid, SIGTSTP); + } + + sigprocmask(SIG_SETMASK, &prev, nullptr); + + errno = errno_backup; + return; +} + +}
\ No newline at end of file |
