Terminate custom module scripts on exit
(Fixes #358.) Subprocesses created for custom module scripts were previously left running when the parent Waybar process exited. This patch sets the parent-death signal of child processes (PR_SET_PDEATHSIG on Linux, PROC_PDEATHSIG_CTL on FreeBSD) to SIGTERM. Caveats: * This uses Linux-specific or FreeBSD-specific calls. I don’t know if this project targets other systems? * There is a possibility that Waybar exits after calling `fork()`, but before calling `prctl` to set the parent-death signal. In this case, the child will not receive the SIGTERM signal and will continue to run. I did not handle this case as I consider it quite unlikely, since module scripts are usually launched only when Waybar starts. Please let me know if you think it needs to be handled. Testing: * With `htop` open, run Waybar v0.9.5 with a custom module that has an `exec` script. Terminate the Waybar process and notice that the script’s subprocess stays alive and is now a child of the init process. * Run Waybar with this patch and follow the same steps as above. Notice that this time the script’s subprocess terminates when the parent exits.pull/1032/head
parent
08e19602f7
commit
d8706af2ea
|
@ -5,6 +5,13 @@
|
|||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
#ifdef __FreeBSD__
|
||||
#include <sys/procctl.h>
|
||||
#endif
|
||||
|
||||
#include <array>
|
||||
|
||||
extern std::mutex reap_mtx;
|
||||
|
@ -77,6 +84,18 @@ inline FILE* open(const std::string& cmd, int& pid) {
|
|||
// Reset sigmask
|
||||
err = pthread_sigmask(SIG_UNBLOCK, &mask, nullptr);
|
||||
if (err != 0) spdlog::error("pthread_sigmask in open failed: {}", strerror(err));
|
||||
// Kill child if Waybar exits
|
||||
int deathsig = SIGTERM;
|
||||
#ifdef __linux__
|
||||
if (prctl(PR_SET_PDEATHSIG, deathsig) != 0) {
|
||||
spdlog::error("prctl(PR_SET_PDEATHSIG) in open failed: {}", strerror(errno));
|
||||
}
|
||||
#endif
|
||||
#ifdef __FreeBSD__
|
||||
if (procctl(P_PID, 0, PROC_PDEATHSIG_CTL, reinterpret_cast<void*>(&deathsig)) == -1) {
|
||||
spdlog::error("procctl(PROC_PDEATHSIG_CTL) in open failed: {}", strerror(errno));
|
||||
}
|
||||
#endif
|
||||
::close(fd[0]);
|
||||
dup2(fd[1], 1);
|
||||
setpgid(child_pid, child_pid);
|
||||
|
|
Loading…
Reference in New Issue