Skip to content

Commit

Permalink
Reduce useless EINTR wakeups.
Browse files Browse the repository at this point in the history
It makes more sense to simply ignore SIGCHLD and set SA_NOCLDWAIT
rather than to waitpid() each SIGCHLD.

This reduces a lot of meaningless wakeups to clock_nanosleep()
and reduces the useless waitpid() syscalls, too.
  • Loading branch information
niklata committed Sep 8, 2022
1 parent 80518b8 commit b775ca6
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ncron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,14 @@ static void signal_handler(int sig)
pending_save_and_exit = 1;
} else if (sig == SIGHUP) {
pending_reload_config = 1;
} else if (sig == SIGCHLD) {
while (waitpid(-1, NULL, WNOHANG) > 0);
}
errno = serrno;
}

static void fix_signals(void)
{
static const int ss[] = {
SIGCHLD, SIGHUP, SIGINT, SIGTERM, SIGKILL
SIGHUP, SIGINT, SIGTERM, SIGKILL
};
sigset_t mask;
if (sigprocmask(0, 0, &mask) < 0)
Expand All @@ -111,12 +109,16 @@ static void fix_signals(void)
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = signal_handler;
sa.sa_flags = SA_RESTART|SA_NOCLDWAIT;
sa.sa_flags = SA_RESTART;
if (sigemptyset(&sa.sa_mask))
suicide("sigemptyset failed");
for (int i = 0; ss[i] != SIGKILL; ++i)
if (sigaction(ss[i], &sa, NULL))
suicide("sigaction failed");
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDWAIT;
if (sigaction(SIGCHLD, &sa, NULL))
suicide("sigaction failed");
}

static void fail_on_fdne(std::string_view file, int mode)
Expand Down

0 comments on commit b775ca6

Please sign in to comment.