aboutsummaryrefslogtreecommitdiff
path: root/reaper.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-02-11 17:56:45 +0000
committerDaniel Eklöf <daniel@ekloef.se>2021-02-11 17:56:45 +0000
commite5edf7a52b93f2124eb1a44b56089f7d27826b53 (patch)
tree9920d99ef172122314d80b60b76a8e5173cf4f09 /reaper.c
parentMerge branch 'xassert-false' (diff)
parentreaper: monitor SIGCHLD using the FDM instead of via a signalfd (diff)
Merge branch 'fdm-signal-handling'
Diffstat (limited to 'reaper.c')
-rw-r--r--reaper.c112
1 files changed, 27 insertions, 85 deletions
diff --git a/reaper.c b/reaper.c
index dec15cb2..a2b7bac0 100644
--- a/reaper.c
+++ b/reaper.c
@@ -3,8 +3,8 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/epoll.h>
+#include <signal.h>
#include <sys/wait.h>
-#include <sys/signalfd.h>
#include <tllist.h>
#define LOG_MODULE "reaper"
@@ -20,56 +20,32 @@ struct child {
struct reaper {
struct fdm *fdm;
- int fd;
tll(struct child) children;
};
-static bool fdm_reap(struct fdm *fdm, int fd, int events, void *data);
+static bool fdm_reap(struct fdm *fdm, int signo, void *data);
struct reaper *
reaper_init(struct fdm *fdm)
{
- sigset_t mask;
- sigemptyset(&mask);
- sigaddset(&mask, SIGCHLD);
-
- /* Block normal signal handling - we're using a signalfd instead */
- if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
- LOG_ERRNO("failed to block SIGCHLD");
- return NULL;
- }
-
- int fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
- if (fd < 0) {
- LOG_ERRNO("failed to create signal FD");
- sigprocmask(SIG_UNBLOCK, &mask, NULL);
- return NULL;
- }
-
struct reaper *reaper = malloc(sizeof(*reaper));
if (unlikely(reaper == NULL)) {
LOG_ERRNO("malloc() failed");
- close(fd);
- sigprocmask(SIG_UNBLOCK, &mask, NULL);
return NULL;
}
*reaper = (struct reaper){
.fdm = fdm,
- .fd = fd,
.children = tll_init(),
};
- if (!fdm_add(fdm, fd, EPOLLIN, &fdm_reap, reaper)){
- LOG_ERR("failed to register with the FDM");
+ if (!fdm_signal_add(fdm, SIGCHLD, &fdm_reap, reaper))
goto err;
- }
return reaper;
err:
tll_free(reaper->children);
- close(fd);
free(reaper);
return NULL;
}
@@ -80,14 +56,9 @@ reaper_destroy(struct reaper *reaper)
if (reaper == NULL)
return;
- fdm_del(reaper->fdm, reaper->fd);
+ fdm_signal_del(reaper->fdm, SIGCHLD);
tll_free(reaper->children);
free(reaper);
-
- sigset_t mask;
- sigemptyset(&mask);
- sigaddset(&mask, SIGCHLD);
- sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
void
@@ -111,69 +82,40 @@ reaper_del(struct reaper *reaper, pid_t pid)
}
static bool
-fdm_reap(struct fdm *fdm, int fd, int events, void *data)
+fdm_reap(struct fdm *fdm, int signo, void *data)
{
struct reaper *reaper = data;
- bool pollin = events & EPOLLIN;
- bool hup = events & EPOLLHUP;
-
- if (hup && !pollin)
- return false;
-
- xassert(pollin);
-
- struct signalfd_siginfo info;
- ssize_t amount = read(reaper->fd, &info, sizeof(info));
-
- if (amount < 0) {
- LOG_ERRNO("failed to read");
- return false;
- }
-
- xassert((size_t)amount >= sizeof(info));
-
- if (info.ssi_signo != SIGCHLD) {
- LOG_WARN("got non-SIGCHLD signal: %d", info.ssi_signo);
- return true;
- }
-
- tll_foreach(reaper->children, it) {
- struct child *_child = &it->item;
+ while (true) {
+ int status;
+ pid_t pid = waitpid(-1, &status, WNOHANG);
+ if (pid <= 0)
+ break;
- if (_child->pid != (pid_t)info.ssi_pid)
- continue;
+ if (WIFEXITED(status))
+ LOG_DBG("pid=%d: exited with status=%d", pid, WEXITSTATUS(status));
+ else if (WIFSIGNALED(status))
+ LOG_DBG("pid=%d: killed by signal=%d", pid, WTERMSIG(status));
+ else
+ LOG_DBG("pid=%d: died of unknown resason", pid);
- /* Make sure we remove it *before* the callback, since it too
- * may remove it */
- struct child child = it->item;
- tll_remove(reaper->children, it);
+ tll_foreach(reaper->children, it) {
+ struct child *_child = &it->item;
- bool reap_ourselves = true;
- if (child.cb != NULL)
- reap_ourselves = !child.cb(reaper, child.pid, child.cb_data);
+ if (_child->pid != pid)
+ continue;
- if (reap_ourselves) {
- int result;
- int res = waitpid(child.pid, &result, WNOHANG);
+ /* Make sure we remove it *before* the callback, since it too
+ * may remove it */
+ struct child child = it->item;
+ tll_remove(reaper->children, it);
- if (res <= 0) {
- if (res < 0)
- LOG_ERRNO("waitpid failed for pid=%d", child.pid);
- continue;
- }
+ if (child.cb != NULL)
+ child.cb(reaper, child.pid, status, child.cb_data);
- else if (WIFEXITED(result))
- LOG_DBG("pid=%d: exited with status=%d", pid, WEXITSTATUS(result));
- else if (WIFSIGNALED(result))
- LOG_DBG("pid=%d: killed by signal=%d", pid, WTERMSIG(result));
- else
- LOG_DBG("pid=%d: died of unknown resason", pid);
+ break;
}
}
- if (hup)
- return false;
-
return true;
}