From c109164a9f68935098a4915e7237828cafdef5f7 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 6 Oct 2026 11:28:06 +0000 Subject: fix non-signal-safe exit, and refactor signal handlers --- include/all.h | 2 +- include/ground.h | 2 +- include/handler.h | 40 -------- include/signal_handler.h | 32 +++++++ src/eval/eval.cc | 2 +- src/eval/eval_exe.cc | 2 + src/handler.cc | 214 ------------------------------------------ src/main.cc | 3 - src/signal/misc.cc | 61 ++++++++++++ src/signal/sigchld_handler.cc | 99 +++++++++++++++++++ src/signal/sigint_handler.cc | 37 ++++++++ src/signal/sigquit_handler | 17 ++++ src/signal/sigtstp_handler.cc | 32 +++++++ 13 files changed, 283 insertions(+), 260 deletions(-) delete mode 100644 include/handler.h create mode 100644 include/signal_handler.h delete mode 100644 src/handler.cc create mode 100644 src/signal/misc.cc create mode 100644 src/signal/sigchld_handler.cc create mode 100644 src/signal/sigint_handler.cc create mode 100644 src/signal/sigquit_handler create mode 100644 src/signal/sigtstp_handler.cc 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/handler.h deleted file mode 100644 index 71db1b9..0000000 --- a/include/handler.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "all.h" - -namespace ov4 -{ - -typedef void handler_t(int); - -extern sigset_t - block_sig_TTOU, - block_job, - block_io; - -inline std::atomic_int exit_code, exit_required_pid; - -inline constexpr int SIGNAL_EXIT_CODE_BASE = 128; - -void signal_init(); -handler_t *Signal(int signum, handler_t *handler); -void sigchld_handler(int sig); -void sigint_handler(int sig); -void sigtstp_handler(int sig); -void sigquit_handler(int sig); - -int block_all(sigset_t *prev); -int block_handler(sigset_t *prev); - -} diff --git a/include/signal_handler.h b/include/signal_handler.h new file mode 100644 index 0000000..12ab0a5 --- /dev/null +++ b/include/signal_handler.h @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +#include "all.h" + +namespace ov4 +{ + +typedef void handler_t(int); + +inline sigset_t + block_sig_TTOU, + block_job, + block_io; + +inline std::atomic_int exit_code, exit_required_pid; + +inline constexpr int SIGNAL_EXIT_CODE_BASE = 128; + +void signal_init(); +handler_t *Signal(int signum, handler_t *handler); +void sigchld_handler(int sig); +void sigint_handler(int sig); +void sigtstp_handler(int sig); +void sigquit_handler(int sig); + +int block_all(sigset_t *prev); +int block_handler(sigset_t *prev); + +} 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 +#include +#include #include "error.h" #include "lang_analysis/lexer_class.h" diff --git a/src/handler.cc b/src/handler.cc deleted file mode 100644 index 7dc96a8..0000000 --- a/src/handler.cc +++ /dev/null @@ -1,214 +0,0 @@ -#include "handler.h" - -#include -#include - -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 - * received a SIGSTOP or SIGTSTP signal. The handler reaps all - * available zombie children, but doesn't wait for any other - * currently running children to terminate. - */ -void sigchld_handler([[maybe_unused]] int sig) -{ - int errno_backup = errno; - - sigset_t prev; - block_handler(&prev); - - int status, pid; - safe_debug("SIGCHLD\n"); - - while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) - { - // signal'd or stopped or normally quit - // print terminated/stopped message - if (WIFSIGNALED(status) || WIFSTOPPED(status)) - { - int jid = pid2jid(pid); - safe_output("Job [", jid, "] (", pid, ") "); - if (!WIFSTOPPED(status) && WTERMSIG(status) == SIGINT) - safe_print("terminated"); - else if (WIFSTOPPED(status)) - safe_print("stopped"); - else - safe_print("idk"); - safe_print(" by signal "); - if (WIFSTOPPED(status)) - safe_print(WSTOPSIG(status)); - else - safe_print(WTERMSIG(status)); - safe_print("\n"); - } - - // handle job table - // and save exit/signal code - - // stopped - if (WIFSTOPPED(status)) - { - safe_output("Process suspended PID: ", pid, "\n"); - sigset_t prev_inner; - block_all(&prev_inner); - job_suspend(pid); - sigprocmask(SIG_SETMASK, &prev_inner, nullptr); - } - // other type of signal'd - // or normally quit - else { - safe_output("Process terminated PID: ", pid, "\n"); - sigset_t prev_inner; - block_all(&prev_inner); - deletejob(pid); - sigprocmask(SIG_SETMASK, &prev_inner, nullptr); - - // terminated normally - // so that exit code should be available - if (WIFEXITED(status)) - { - safe_output("Exit code: ", WEXITSTATUS(status), "\n"); - if (pid == exit_required_pid) - { - exit_code = WEXITSTATUS(status); - safe_logger("sigchld_handler: required PID detected: ", pid, "\n"); - } - } - // if signal'd - else if (WIFSIGNALED(status)) - { - safe_output("Signal code: ", WTERMSIG(status), "\n"); - if (pid == exit_required_pid) - { - exit_code = SIGNAL_EXIT_CODE_BASE + WTERMSIG(status); - safe_logger("sigchld_handler: required PID detected: ", pid, "\n"); - } - } - - } - } - - sigprocmask(SIG_SETMASK, &prev, nullptr); - - errno = errno_backup; - 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); -} - -} 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 + +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/signal/sigchld_handler.cc b/src/signal/sigchld_handler.cc new file mode 100644 index 0000000..e11ed26 --- /dev/null +++ b/src/signal/sigchld_handler.cc @@ -0,0 +1,99 @@ +#include "signal_handler.h" + +#include + +namespace ov4 +{ + +/* + * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever + * a child job terminates (becomes a zombie), or stops because it + * received a SIGSTOP or SIGTSTP signal. The handler reaps all + * available zombie children, but doesn't wait for any other + * currently running children to terminate. + */ +void sigchld_handler([[maybe_unused]] int sig) +{ + int errno_backup = errno; + + sigset_t prev; + block_handler(&prev); + + int status, pid; + safe_debug("SIGCHLD\n"); + + while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) + { + // signal'd or stopped or normally quit + // print terminated/stopped message + if (WIFSIGNALED(status) || WIFSTOPPED(status)) + { + int jid = pid2jid(pid); + safe_output("Job [", jid, "] (", pid, ") "); + if (!WIFSTOPPED(status) && WTERMSIG(status) == SIGINT) + safe_print("terminated"); + else if (WIFSTOPPED(status)) + safe_print("stopped"); + else + safe_print("idk"); + safe_print(" by signal "); + if (WIFSTOPPED(status)) + safe_print(WSTOPSIG(status)); + else + safe_print(WTERMSIG(status)); + safe_print("\n"); + } + + // handle job table + // and save exit/signal code + + // stopped + if (WIFSTOPPED(status)) + { + safe_output("Process suspended PID: ", pid, "\n"); + sigset_t prev_inner; + block_all(&prev_inner); + job_suspend(pid); + sigprocmask(SIG_SETMASK, &prev_inner, nullptr); + } + // other type of signal'd + // or normally quit + else { + safe_output("Process terminated PID: ", pid, "\n"); + sigset_t prev_inner; + block_all(&prev_inner); + deletejob(pid); + sigprocmask(SIG_SETMASK, &prev_inner, nullptr); + + // terminated normally + // so that exit code should be available + if (WIFEXITED(status)) + { + safe_output("Exit code: ", WEXITSTATUS(status), "\n"); + if (pid == exit_required_pid) + { + exit_code = WEXITSTATUS(status); + safe_logger("sigchld_handler: required PID detected: ", pid, "\n"); + } + } + // if signal'd + else if (WIFSIGNALED(status)) + { + safe_output("Signal code: ", WTERMSIG(status), "\n"); + if (pid == exit_required_pid) + { + exit_code = SIGNAL_EXIT_CODE_BASE + WTERMSIG(status); + safe_logger("sigchld_handler: required PID detected: ", pid, "\n"); + } + } + + } + } + + sigprocmask(SIG_SETMASK, &prev, nullptr); + + errno = errno_backup; + return; +} + +} \ 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 +#include +#include + +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 + +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 + +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 -- cgit v1.3.1-4-g156e