feat: debug cmd

This commit is contained in:
Alex 2020-05-22 20:57:41 +02:00
parent b251c51936
commit 6b32aca094
2 changed files with 16 additions and 12 deletions

View File

@ -2,6 +2,7 @@
#include <giomm.h> #include <giomm.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <spdlog/spdlog.h>
#include <unistd.h> #include <unistd.h>
#include <array> #include <array>
@ -29,16 +30,22 @@ inline std::string read(FILE* fp) {
} }
inline int close(FILE* fp, pid_t pid) { inline int close(FILE* fp, pid_t pid) {
int stat; int stat = -1;
fclose(fp); fclose(fp);
while (waitpid(pid, &stat, 0) == -1) { do {
if (errno != EINTR) { if (WIFEXITED(stat)) {
stat = 0; spdlog::debug("%s exited with code %d", WEXITSTATUS(stat));
} else if (WIFSIGNALED(stat)) {
spdlog::debug("%s killed by %d", WTERMSIG(stat));
} else if (WIFSTOPPED(stat)) {
spdlog::debug("%s stopped by %d", WSTOPSIG(stat));
} else if (WIFCONTINUED(stat)) {
spdlog::debug("%s continued");
} else {
break; break;
} }
} } while (!WIFEXITED(stat) && !WIFSIGNALED(stat));
return stat; return stat;
} }
@ -58,7 +65,7 @@ inline FILE* open(const std::string cmd, int& pid) {
::close(fd[0]); ::close(fd[0]);
dup2(fd[1], 1); dup2(fd[1], 1);
setpgid(child_pid, child_pid); setpgid(child_pid, child_pid);
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0); execlp("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
exit(0); exit(0);
} else { } else {
::close(fd[1]); ::close(fd[1]);
@ -73,11 +80,8 @@ inline struct res exec(std::string cmd) {
if (!fp) return {-1, ""}; if (!fp) return {-1, ""};
auto output = command::read(fp); auto output = command::read(fp);
auto stat = command::close(fp, pid); auto stat = command::close(fp, pid);
if (WIFEXITED(stat)) {
return {WEXITSTATUS(stat), output}; return {WEXITSTATUS(stat), output};
} }
return {-1, output};
}
inline int32_t forkExec(std::string cmd) { inline int32_t forkExec(std::string cmd) {
if (cmd == "") return -1; if (cmd == "") return -1;

View File

@ -16,7 +16,7 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
waybar::modules::Custom::~Custom() { waybar::modules::Custom::~Custom() {
if (pid_ != -1) { if (pid_ != -1) {
kill(-pid_, 9); killpg(pid_, SIGTERM);
pid_ = -1; pid_ = -1;
} }
} }