diff --git a/include/eventloop.hpp b/include/eventloop.hpp index 61770a81..fce07c7c 100644 --- a/include/eventloop.hpp +++ b/include/eventloop.hpp @@ -29,8 +29,6 @@ class EventLoop int fd_stdout = STDOUT_FILENO; std::string pipe_filename; - sigset_t wait_mask; - // // std::map stdin_subs; std::vector stdin_subs; diff --git a/include/utils/proc.hpp b/include/utils/proc.hpp index c9ffd575..a33b82a7 100644 --- a/include/utils/proc.hpp +++ b/include/utils/proc.hpp @@ -31,6 +31,7 @@ namespace proc pid_t wait(int *status); pid_t wait_for_completion(pid_t pid, int *status, int options = 0); pid_t wait_for_completion(int *status, int options = 0); + pid_t wait_for_completion(pid_t pid); pid_t wait_for_completion_nohang(pid_t pid, int *status); pid_t wait_for_completion_nohang(int *status); pid_t wait_for_completion_nohang(); diff --git a/src/eventloop.cpp b/src/eventloop.cpp index acb8dcfa..44ed35f3 100644 --- a/src/eventloop.cpp +++ b/src/eventloop.cpp @@ -75,16 +75,20 @@ void EventLoop::wait() int sig = 0; - sigemptyset(&this->wait_mask); - sigaddset(&this->wait_mask, SIGINT); - sigaddset(&this->wait_mask, SIGQUIT); - sigaddset(&this->wait_mask, SIGTERM); + sigset_t wait_mask; + sigemptyset(&wait_mask); + sigaddset(&wait_mask, SIGINT); + sigaddset(&wait_mask, SIGQUIT); + sigaddset(&wait_mask, SIGTERM); - if (pthread_sigmask(SIG_BLOCK, &this->wait_mask, nullptr) == -1) + if (pthread_sigmask(SIG_BLOCK, &wait_mask, nullptr) == -1) logger->fatal(StrErrno()); + // Ignore SIGPIPE since we'll handle it manually + signal(SIGPIPE, SIG_IGN); + // Wait for termination signal - sigwait(&this->wait_mask, &sig); + sigwait(&wait_mask, &sig); this->logger->info("Termination signal received... Shutting down"); } @@ -134,6 +138,11 @@ void EventLoop::loop_write() this->write_stdout(); } catch (Exception &e) { this->logger->error(e.what()); + + auto pid = proc::get_process_id(); + proc::kill(pid, SIGTERM); + proc::wait_for_completion(pid); + return; } } @@ -207,11 +216,10 @@ void EventLoop::write_stdout() if (!this->running()) return; - // dprintf(this->fd_stdout, "\033[2J\033[1;1H\033[0mCleared! \033[35;1m %s\n", data.c_str()); - dprintf(this->fd_stdout, "%s\n", data.c_str()); + if (dprintf(this->fd_stdout, "%s\n", data.c_str()) == -1) + throw EventLoopTerminate("Failed to write to stdout"); } catch (RegistryError &e) { this->logger->error(e.what()); - return; } } diff --git a/src/lemonbuddy.cpp b/src/lemonbuddy.cpp index 239260e2..e959569f 100644 --- a/src/lemonbuddy.cpp +++ b/src/lemonbuddy.cpp @@ -51,12 +51,6 @@ int main(int argc, char **argv) int retval = EXIT_SUCCESS; auto logger = get_logger(); - sigset_t pipe_mask; - sigemptyset(&pipe_mask); - sigaddset(&pipe_mask, SIGPIPE); - if (pthread_sigmask(SIG_BLOCK, &pipe_mask, nullptr) == -1) - logger->fatal(StrErrno()); - try { auto usage = "Usage: "+ std::string(argv[0]) + " bar_name [OPTION...]"; @@ -201,5 +195,7 @@ int main(int argc, char **argv) while (proc::wait_for_completion_nohang() > 0); + log_trace("Reached end of application"); + return retval; } diff --git a/src/utils/proc.cpp b/src/utils/proc.cpp index f64c22fa..66725c16 100644 --- a/src/utils/proc.cpp +++ b/src/utils/proc.cpp @@ -95,6 +95,12 @@ namespace proc return wait_for_completion(-1, status, options); } + pid_t wait_for_completion(pid_t pid) + { + int status; + return wait_for_completion(pid, &status); + } + pid_t wait_for_completion_nohang(pid_t pid, int *status) { return wait_for_completion(pid, status, WNOHANG); }