From 75c3db6a55685361ac54f6d7f7f795729f50152f Mon Sep 17 00:00:00 2001 From: _ov4 Date: Mon, 19 Oct 2026 10:38:11 +0000 Subject: adapt selfpipe and signal process to sigchld handler --- src/line/input.cc | 2 ++ src/main.cc | 3 +++ src/signal/handler.cc | 32 ++++++++++++++++++++++++++++++++ src/signal/sigchld_handler.cc | 41 +++++++++++++++++++++++------------------ src/signal/signal_status.cc | 22 ++++++++++++++++++++++ 5 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 src/signal/handler.cc create mode 100644 src/signal/signal_status.cc (limited to 'src') diff --git a/src/line/input.cc b/src/line/input.cc index 1b9520b..c080869 100644 --- a/src/line/input.cc +++ b/src/line/input.cc @@ -7,6 +7,7 @@ #include "history.h" #include "util/non_block_input.h" #include "pipe.h" +#include "signal_handler.h" using namespace std; @@ -33,6 +34,7 @@ string T_line_editor::readline(int prompt_len) if (src == self_pipe_fd[0]) { check_window_update(); + process_signal(); continue; } if (iscntrl(c)) diff --git a/src/main.cc b/src/main.cc index 6fe48a4..7dc8e52 100644 --- a/src/main.cc +++ b/src/main.cc @@ -14,6 +14,7 @@ #include "all.h" #include "test.h" #include "line/line_editor.h" +#include "signal_handler.h" using namespace std; using namespace ov4; @@ -48,6 +49,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) while (1) { /* Read command line */ + cout << endl; int len = print_prompt(); T_line_editor line_editor_instance; @@ -55,6 +57,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) enable_raw(); string s = line_editor_instance.readline(len); disable_raw(); + process_signal(); cout << "\n\n\n\n" << endl; cout << s << endl; if (s == "quit") diff --git a/src/signal/handler.cc b/src/signal/handler.cc new file mode 100644 index 0000000..294ae53 --- /dev/null +++ b/src/signal/handler.cc @@ -0,0 +1,32 @@ +#include "signal_handler.h" + +#include +#include +#include + +using namespace std; + +namespace ov4 +{ + +void process_signal() +{ + /* + * do NOT swap these statements + * otherwise, if process then unset flag, and another signal delivered + * it won't be processed + */ + if (check_signal_delivered(SIGCHLD)) + { + unset_signal_delivered(SIGCHLD); + process_sigchld(); + } +} + +void process_sigchld() +{ + print("hi\r\n"); + fflush(stdout); +} + +} \ No newline at end of file diff --git a/src/signal/sigchld_handler.cc b/src/signal/sigchld_handler.cc index e11ed26..c1a7740 100644 --- a/src/signal/sigchld_handler.cc +++ b/src/signal/sigchld_handler.cc @@ -2,6 +2,8 @@ #include +#include "pipe.h" + namespace ov4 { @@ -24,24 +26,27 @@ void sigchld_handler([[maybe_unused]] int sig) while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) { + set_signal_delivered(SIGCHLD); + self_pipe_write(); + //safe_print("-----------"); // 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"); + //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 @@ -50,7 +55,7 @@ void sigchld_handler([[maybe_unused]] int sig) // stopped if (WIFSTOPPED(status)) { - safe_output("Process suspended PID: ", pid, "\n"); + //safe_output("Process suspended PID: ", pid, "\n"); sigset_t prev_inner; block_all(&prev_inner); job_suspend(pid); @@ -59,7 +64,7 @@ void sigchld_handler([[maybe_unused]] int sig) // other type of signal'd // or normally quit else { - safe_output("Process terminated PID: ", pid, "\n"); + //safe_output("Process terminated PID: ", pid, "\n"); sigset_t prev_inner; block_all(&prev_inner); deletejob(pid); @@ -69,7 +74,7 @@ void sigchld_handler([[maybe_unused]] int sig) // so that exit code should be available if (WIFEXITED(status)) { - safe_output("Exit code: ", WEXITSTATUS(status), "\n"); + //safe_output("Exit code: ", WEXITSTATUS(status), "\n"); if (pid == exit_required_pid) { exit_code = WEXITSTATUS(status); @@ -79,11 +84,11 @@ void sigchld_handler([[maybe_unused]] int sig) // if signal'd else if (WIFSIGNALED(status)) { - safe_output("Signal code: ", WTERMSIG(status), "\n"); + //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"); + //safe_logger("sigchld_handler: required PID detected: ", pid, "\n"); } } diff --git a/src/signal/signal_status.cc b/src/signal/signal_status.cc new file mode 100644 index 0000000..c47af48 --- /dev/null +++ b/src/signal/signal_status.cc @@ -0,0 +1,22 @@ +#include "signal_handler.h" + +namespace ov4 +{ + +bool check_signal_delivered(int sig) +{ + return signal_mask[sig]; +} + +void set_signal_delivered(int sig) +{ + signal_mask[sig] = true; +} + +void unset_signal_delivered(int sig) +{ + signal_mask[sig] = false; +} + + +} \ No newline at end of file -- cgit v1.3.1-4-g156e