1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-25 13:55:47 -05:00

refactor(eventloop): Pipe handling

This commit is contained in:
Michael Carlberg 2016-06-14 12:12:09 +02:00
parent 266705e016
commit 3bc408e3cf
5 changed files with 26 additions and 17 deletions

View file

@ -29,8 +29,6 @@ class EventLoop
int fd_stdout = STDOUT_FILENO; int fd_stdout = STDOUT_FILENO;
std::string pipe_filename; std::string pipe_filename;
sigset_t wait_mask;
// <tag, module_name> // <tag, module_name>
// std::map<std::string, std::string> stdin_subs; // std::map<std::string, std::string> stdin_subs;
std::vector<std::string> stdin_subs; std::vector<std::string> stdin_subs;

View file

@ -31,6 +31,7 @@ namespace proc
pid_t wait(int *status); pid_t wait(int *status);
pid_t wait_for_completion(pid_t pid, int *status, int options = 0); 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(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(pid_t pid, int *status);
pid_t wait_for_completion_nohang(int *status); pid_t wait_for_completion_nohang(int *status);
pid_t wait_for_completion_nohang(); pid_t wait_for_completion_nohang();

View file

@ -75,16 +75,20 @@ void EventLoop::wait()
int sig = 0; int sig = 0;
sigemptyset(&this->wait_mask); sigset_t wait_mask;
sigaddset(&this->wait_mask, SIGINT); sigemptyset(&wait_mask);
sigaddset(&this->wait_mask, SIGQUIT); sigaddset(&wait_mask, SIGINT);
sigaddset(&this->wait_mask, SIGTERM); 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()); logger->fatal(StrErrno());
// Ignore SIGPIPE since we'll handle it manually
signal(SIGPIPE, SIG_IGN);
// Wait for termination signal // Wait for termination signal
sigwait(&this->wait_mask, &sig); sigwait(&wait_mask, &sig);
this->logger->info("Termination signal received... Shutting down"); this->logger->info("Termination signal received... Shutting down");
} }
@ -134,6 +138,11 @@ void EventLoop::loop_write()
this->write_stdout(); this->write_stdout();
} catch (Exception &e) { } catch (Exception &e) {
this->logger->error(e.what()); this->logger->error(e.what());
auto pid = proc::get_process_id();
proc::kill(pid, SIGTERM);
proc::wait_for_completion(pid);
return; return;
} }
} }
@ -207,11 +216,10 @@ void EventLoop::write_stdout()
if (!this->running()) if (!this->running())
return; return;
// dprintf(this->fd_stdout, "\033[2J\033[1;1H\033[0mCleared! \033[35;1m %s\n", data.c_str()); if (dprintf(this->fd_stdout, "%s\n", data.c_str()) == -1)
dprintf(this->fd_stdout, "%s\n", data.c_str()); throw EventLoopTerminate("Failed to write to stdout");
} catch (RegistryError &e) { } catch (RegistryError &e) {
this->logger->error(e.what()); this->logger->error(e.what());
return;
} }
} }

View file

@ -51,12 +51,6 @@ int main(int argc, char **argv)
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
auto logger = get_logger(); 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 { try {
auto usage = "Usage: "+ std::string(argv[0]) + " bar_name [OPTION...]"; 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); while (proc::wait_for_completion_nohang() > 0);
log_trace("Reached end of application");
return retval; return retval;
} }

View file

@ -95,6 +95,12 @@ namespace proc
return wait_for_completion(-1, status, options); 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) { pid_t wait_for_completion_nohang(pid_t pid, int *status) {
return wait_for_completion(pid, status, WNOHANG); return wait_for_completion(pid, status, WNOHANG);
} }