2018-08-18 15:54:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-01-08 20:05:44 +00:00
|
|
|
#include <giomm.h>
|
2020-05-22 18:57:41 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2020-05-24 19:33:38 +00:00
|
|
|
#include <sys/wait.h>
|
2018-12-26 10:13:36 +00:00
|
|
|
#include <unistd.h>
|
2020-05-24 19:33:38 +00:00
|
|
|
|
2021-02-12 19:15:38 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/procctl.h>
|
|
|
|
#endif
|
|
|
|
|
2019-08-09 09:05:34 +00:00
|
|
|
#include <array>
|
2018-08-18 15:54:20 +00:00
|
|
|
|
2020-07-25 11:02:59 +00:00
|
|
|
extern std::mutex reap_mtx;
|
2020-07-21 02:36:48 +00:00
|
|
|
extern std::list<pid_t> reap;
|
|
|
|
|
2018-08-18 15:54:20 +00:00
|
|
|
namespace waybar::util::command {
|
|
|
|
|
2018-09-05 17:20:19 +00:00
|
|
|
struct res {
|
2019-04-18 15:52:00 +00:00
|
|
|
int exit_code;
|
2018-08-18 15:54:20 +00:00
|
|
|
std::string out;
|
|
|
|
};
|
|
|
|
|
2019-04-23 13:56:38 +00:00
|
|
|
inline std::string read(FILE* fp) {
|
2018-08-18 15:54:20 +00:00
|
|
|
std::array<char, 128> buffer = {0};
|
2019-04-18 15:52:00 +00:00
|
|
|
std::string output;
|
2018-08-18 15:54:20 +00:00
|
|
|
while (feof(fp) == 0) {
|
|
|
|
if (fgets(buffer.data(), 128, fp) != nullptr) {
|
|
|
|
output += buffer.data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove last newline
|
2019-04-18 15:52:00 +00:00
|
|
|
if (!output.empty() && output[output.length() - 1] == '\n') {
|
|
|
|
output.erase(output.length() - 1);
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
2019-04-23 13:56:38 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int close(FILE* fp, pid_t pid) {
|
2020-05-22 18:57:41 +00:00
|
|
|
int stat = -1;
|
2020-07-21 02:36:48 +00:00
|
|
|
pid_t ret;
|
2019-04-23 13:56:38 +00:00
|
|
|
|
|
|
|
fclose(fp);
|
2020-05-22 18:57:41 +00:00
|
|
|
do {
|
2020-07-21 02:36:48 +00:00
|
|
|
ret = waitpid(pid, &stat, WCONTINUED | WUNTRACED);
|
2020-05-24 16:27:10 +00:00
|
|
|
|
2020-05-22 18:57:41 +00:00
|
|
|
if (WIFEXITED(stat)) {
|
2020-05-24 20:14:17 +00:00
|
|
|
spdlog::debug("Cmd exited with code {}", WEXITSTATUS(stat));
|
2020-05-22 18:57:41 +00:00
|
|
|
} else if (WIFSIGNALED(stat)) {
|
2020-05-24 20:14:17 +00:00
|
|
|
spdlog::debug("Cmd killed by {}", WTERMSIG(stat));
|
2020-05-22 18:57:41 +00:00
|
|
|
} else if (WIFSTOPPED(stat)) {
|
2020-05-24 20:14:17 +00:00
|
|
|
spdlog::debug("Cmd stopped by {}", WSTOPSIG(stat));
|
2020-05-22 18:57:41 +00:00
|
|
|
} else if (WIFCONTINUED(stat)) {
|
2020-05-24 20:14:17 +00:00
|
|
|
spdlog::debug("Cmd continued");
|
2020-07-21 02:36:48 +00:00
|
|
|
} else if (ret == -1) {
|
|
|
|
spdlog::debug("waitpid failed: {}", strerror(errno));
|
2020-05-22 18:57:41 +00:00
|
|
|
} else {
|
2019-04-23 13:56:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-22 18:57:41 +00:00
|
|
|
} while (!WIFEXITED(stat) && !WIFSIGNALED(stat));
|
2019-04-23 13:56:38 +00:00
|
|
|
return stat;
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 19:33:38 +00:00
|
|
|
inline FILE* open(const std::string& cmd, int& pid) {
|
2019-04-23 13:56:38 +00:00
|
|
|
if (cmd == "") return nullptr;
|
|
|
|
int fd[2];
|
2021-11-09 17:38:07 +00:00
|
|
|
if (pipe(fd) != 0){
|
|
|
|
throw std::runtime_error("Couldn't open a file descriptor");
|
|
|
|
}
|
2019-04-23 13:56:38 +00:00
|
|
|
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
|
|
|
|
if (child_pid < 0) {
|
2020-05-24 20:14:17 +00:00
|
|
|
spdlog::error("Unable to exec cmd {}, error {}", cmd.c_str(), strerror(errno));
|
2019-04-23 13:56:38 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!child_pid) {
|
2020-07-25 11:02:59 +00:00
|
|
|
int err;
|
|
|
|
sigset_t mask;
|
|
|
|
sigfillset(&mask);
|
|
|
|
// Reset sigmask
|
|
|
|
err = pthread_sigmask(SIG_UNBLOCK, &mask, nullptr);
|
|
|
|
if (err != 0) spdlog::error("pthread_sigmask in open failed: {}", strerror(err));
|
2021-02-12 19:15:38 +00:00
|
|
|
// 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
|
2019-04-23 13:56:38 +00:00
|
|
|
::close(fd[0]);
|
|
|
|
dup2(fd[1], 1);
|
|
|
|
setpgid(child_pid, child_pid);
|
2020-05-22 18:57:41 +00:00
|
|
|
execlp("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
2019-04-23 13:56:38 +00:00
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
::close(fd[1]);
|
|
|
|
}
|
|
|
|
pid = child_pid;
|
|
|
|
return fdopen(fd[0], "r");
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:33:38 +00:00
|
|
|
inline struct res exec(const std::string& cmd) {
|
2019-04-23 13:56:38 +00:00
|
|
|
int pid;
|
|
|
|
auto fp = command::open(cmd, pid);
|
|
|
|
if (!fp) return {-1, ""};
|
|
|
|
auto output = command::read(fp);
|
|
|
|
auto stat = command::close(fp, pid);
|
2020-05-22 18:57:41 +00:00
|
|
|
return {WEXITSTATUS(stat), output};
|
2019-04-23 13:56:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 19:33:38 +00:00
|
|
|
inline struct res execNoRead(const std::string& cmd) {
|
2020-05-24 16:27:10 +00:00
|
|
|
int pid;
|
|
|
|
auto fp = command::open(cmd, pid);
|
|
|
|
if (!fp) return {-1, ""};
|
|
|
|
auto stat = command::close(fp, pid);
|
|
|
|
return {WEXITSTATUS(stat), ""};
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:33:38 +00:00
|
|
|
inline int32_t forkExec(const std::string& cmd) {
|
2019-04-23 13:56:38 +00:00
|
|
|
if (cmd == "") return -1;
|
2018-10-29 17:04:09 +00:00
|
|
|
|
2020-07-25 11:02:59 +00:00
|
|
|
pid_t pid = fork();
|
2018-10-29 17:04:09 +00:00
|
|
|
|
|
|
|
if (pid < 0) {
|
2020-05-24 20:14:17 +00:00
|
|
|
spdlog::error("Unable to exec cmd {}, error {}", cmd.c_str(), strerror(errno));
|
2019-04-23 13:56:38 +00:00
|
|
|
return pid;
|
2018-10-29 17:04:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Child executes the command
|
2019-04-23 13:56:38 +00:00
|
|
|
if (!pid) {
|
2020-07-25 11:02:59 +00:00
|
|
|
int err;
|
|
|
|
sigset_t mask;
|
|
|
|
sigfillset(&mask);
|
|
|
|
// Reset sigmask
|
|
|
|
err = pthread_sigmask(SIG_UNBLOCK, &mask, nullptr);
|
|
|
|
if (err != 0) spdlog::error("pthread_sigmask in forkExec failed: {}", strerror(err));
|
2019-04-23 13:56:38 +00:00
|
|
|
setpgid(pid, pid);
|
|
|
|
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
|
|
|
exit(0);
|
2019-06-05 12:35:25 +00:00
|
|
|
} else {
|
2020-07-25 11:02:59 +00:00
|
|
|
reap_mtx.lock();
|
2020-07-21 02:36:48 +00:00
|
|
|
reap.push_back(pid);
|
2020-07-25 11:02:59 +00:00
|
|
|
reap_mtx.unlock();
|
|
|
|
spdlog::debug("Added child to reap list: {}", pid);
|
2019-04-23 13:56:38 +00:00
|
|
|
}
|
2018-10-29 17:04:09 +00:00
|
|
|
|
2019-04-23 13:56:38 +00:00
|
|
|
return pid;
|
2018-08-18 15:54:20 +00:00
|
|
|
}
|
2018-10-29 17:04:09 +00:00
|
|
|
|
|
|
|
} // namespace waybar::util::command
|