From b60d6eecb30db5d8c6c5411677a4c0a70d138653 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 6 Jan 2022 19:45:12 +0100 Subject: use pipe to trigger logger rescan --- rvnit.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/rvnit.c b/rvnit.c index 74af39b..0626ce3 100644 --- a/rvnit.c +++ b/rvnit.c @@ -49,6 +49,7 @@ pthread_t socket_thread; pthread_t logger_thread; int selflogfd[2]; +int newlogfd[2]; sig_atomic_t want_shutdown; sig_atomic_t want_rescan; @@ -62,12 +63,6 @@ on_sigint(int sig) want_shutdown = 1; } -void -on_sigusr2(int sig) -{ - (void)sig; -} - void on_sigcont(int sig) { @@ -95,7 +90,7 @@ restart(int i) fcntl(services[i].logfd[0], F_SETFL, O_NONBLOCK); fcntl(services[i].logfd[1], F_SETFL, O_NONBLOCK); - pthread_kill(logger_thread, SIGUSR2); + write(newlogfd[1], "!", 1); } if (services[i].name[2] == 'L') { @@ -292,12 +287,16 @@ logger_loop(void* ignored) struct pollfd fds[MAX_SV]; nfds_t nfds = 0; + fds[nfds].fd = newlogfd[0]; + fds[nfds].events = POLLIN; + nfds++; + fds[nfds].fd = selflogfd[0]; fds[nfds].events = POLLIN; nfds++; for (int i = 0; i < MAX_SV; i++) { - if (/*services[i].pid > 0 && */ !services[i].logged && services[i].logfd[0] > 0) { + if (!services[i].logged && services[i].logfd[0] > 0) { fds[nfds].fd = services[i].logfd[0]; fds[nfds].events = POLLIN; nfds++; @@ -305,11 +304,21 @@ logger_loop(void* ignored) } int n = poll(fds, nfds, -1); - if (n < 0) + if (n < 0) { + perror("poll"); + sleep(1); + continue; + } + + if (fds[0].revents & POLLIN) { + /* data on newlogfd, restart */ + char c; + read(fds[0].fd, &c, 1); continue; + } - if (n == 1 && (fds[0].revents & POLLHUP) && - !(fds[0].revents & POLLIN)) { + if (n == 1 && (fds[1].revents & POLLHUP) && + !(fds[1].revents & POLLIN)) { /* selflog was closed, end thread */ break; } @@ -469,7 +478,11 @@ main() fcntl(selflogfd[0], F_SETFD, FD_CLOEXEC); fcntl(selflogfd[1], F_SETFD, FD_CLOEXEC); - sigaction(SIGUSR2, &(struct sigaction){.sa_handler=on_sigusr2}, 0); + pipe(newlogfd); + fcntl(newlogfd[0], F_SETFL, O_NONBLOCK); + fcntl(newlogfd[1], F_SETFL, O_NONBLOCK); + fcntl(newlogfd[0], F_SETFD, FD_CLOEXEC); + fcntl(newlogfd[1], F_SETFD, FD_CLOEXEC); main_thread = pthread_self(); pthread_create(&socket_thread, 0, socket_loop, 0); -- cgit 1.4.1