Switch to signalfd for SIGHUP

This commit is contained in:
Ian Gulliver
2016-03-08 23:04:20 -08:00
parent c0c25bd36a
commit b455826db2
3 changed files with 21 additions and 20 deletions

View File

@@ -58,7 +58,7 @@ int main(int argc, char *argv[]) {
wakeup_init();
peer_init();
log_init2();
log_init_peer();
receive_init();
send_init();
@@ -96,6 +96,8 @@ int main(int argc, char *argv[]) {
rand_cleanup();
wakeup_cleanup();
log_cleanup_peer();
peer_cleanup();
log_cleanup();

View File

@@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/signalfd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -22,7 +23,6 @@
static FILE *log_stream = NULL;
static char *log_path = NULL;
static uint8_t log_id[UUID_LEN];
static int log_rotate_fd;
static struct peer log_rotate_peer;
static bool log_timestamps = false;
static opts_group log_opts;
@@ -31,7 +31,7 @@ static char log_module = 'L';
static void log_rotate() {
if (!log_path) {
LOG(log_id, "Received SIGHUP but logging to stderr; ignoring");
LOG(log_id, "Asked to rotate logs but not logging to a file; ignoring");
return;
}
@@ -48,16 +48,12 @@ static void log_rotate() {
}
static void log_rotate_handler(struct peer *peer) {
char buf[1];
assert(read(peer->fd, buf, 1) == 1);
assert(buf[0] == 'L');
struct signalfd_siginfo siginfo;
assert(read(peer->fd, &siginfo, sizeof(siginfo)) == sizeof(siginfo));
LOG(log_id, "Received signal %u; rotating logs", siginfo.ssi_signo);
log_rotate();
}
static void log_rotate_signal(int __attribute__ ((unused)) signum) {
assert(write(log_rotate_fd, "L", 1) == 1);
}
static bool log_set_path(const char *path) {
if (log_path) {
return false;
@@ -93,23 +89,25 @@ void log_init() {
LOG(log_id, "Log start");
}
void log_init2() {
int rotate_fds[2];
assert(!socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, rotate_fds));
log_rotate_peer.fd = rotate_fds[0];
assert(!shutdown(log_rotate_peer.fd, SHUT_WR));
void log_init_peer() {
sigset_t sigmask;
assert(!sigemptyset(&sigmask));
assert(!sigaddset(&sigmask, SIGHUP));
log_rotate_peer.fd = signalfd(-1, &sigmask, SFD_NONBLOCK | SFD_CLOEXEC);
assert(log_rotate_peer.fd >= 0);
log_rotate_peer.event_handler = log_rotate_handler;
peer_epoll_add(&log_rotate_peer, EPOLLIN);
log_rotate_fd = rotate_fds[1];
signal(SIGHUP, log_rotate_signal);
assert(!sigprocmask(SIG_BLOCK, &sigmask, NULL));
}
void log_cleanup_peer() {
peer_close(&log_rotate_peer);
}
void log_cleanup() {
LOG(log_id, "Log end");
assert(!fclose(log_stream));
assert(!close(log_rotate_fd));
assert(!close(log_rotate_peer.fd));
if (log_path) {
free(log_path);
log_path = NULL;

View File

@@ -9,7 +9,8 @@
void log_opts_add(void);
void log_init(void);
void log_init2(void);
void log_cleanup(void);
void log_init_peer(void);
void log_cleanup_peer(void);
void log_write(char, const char *, const uint8_t *, const char *, ...)
__attribute__ ((__format__ (__printf__, 4, 5)));