Skip to content

Commit

Permalink
Merge pull request #1032 from matteodelabre/terminate-custom-on-exit
Browse files Browse the repository at this point in the history
Terminate custom module scripts on exit
  • Loading branch information
Alexays authored Feb 23, 2021
2 parents 1026100 + d8706af commit cb1c7ea
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/util/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cb1c7ea

Please sign in to comment.