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 <sys/wait.h>
#include <spdlog/spdlog.h>
#include <unistd.h>
#include <array>
@ -29,16 +30,22 @@ inline std::string read(FILE* fp) {
}
inline int close(FILE* fp, pid_t pid) {
int stat;
int stat = -1;
fclose(fp);
while (waitpid(pid, &stat, 0) == -1) {
if (errno != EINTR) {
stat = 0;
do {
if (WIFEXITED(stat)) {
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;
}
}
} while (!WIFEXITED(stat) && !WIFSIGNALED(stat));
return stat;
}
@ -58,7 +65,7 @@ inline FILE* open(const std::string cmd, int& pid) {
::close(fd[0]);
dup2(fd[1], 1);
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);
} else {
::close(fd[1]);
@ -73,10 +80,7 @@ inline struct res exec(std::string cmd) {
if (!fp) return {-1, ""};
auto output = command::read(fp);
auto stat = command::close(fp, pid);
if (WIFEXITED(stat)) {
return {WEXITSTATUS(stat), output};
}
return {-1, output};
return {WEXITSTATUS(stat), output};
}
inline int32_t forkExec(std::string cmd) {

View File

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