Skip to content

Commit

Permalink
Add log rotate support for dumper files (#2876)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt authored Mar 26, 2024
1 parent 693cf76 commit 7da8f63
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/r_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ void start_outputs(struct r_cfg *cfg, char const *const *well_known);

void add_sr_dumper(struct r_cfg *cfg, char const *spec, int overwrite);

void reopen_dumpers(struct r_cfg *cfg);

void close_dumpers(struct r_cfg *cfg);

void add_dumper(struct r_cfg *cfg, char const *spec, int overwrite);
Expand Down
45 changes: 45 additions & 0 deletions src/r_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
#include "fatal.h"
#include "http_server.h"

#ifndef _WIN32
#include <sys/stat.h>
#endif

#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
Expand Down Expand Up @@ -1155,6 +1159,47 @@ void add_sr_dumper(r_cfg_t *cfg, char const *spec, int overwrite)
cfg->sr_execopen = overwrite;
}

void reopen_dumpers(struct r_cfg *cfg)
{
#ifndef _WIN32
for (void **iter = cfg->demod->dumper.elems; iter && *iter; ++iter) {
file_info_t *dumper = *iter;
if (dumper->file && (dumper->file != stdout)) {
// Get current file inode
struct stat old_st = {0};
int ret = fstat(fileno(dumper->file), &old_st);
if (ret) {
fprintf(stderr, "Failed to fstat %s (%d)\n", dumper->path, errno);
exit(1);
}

// Get new path inode if available
struct stat new_st = {0};
stat(dumper->path, &new_st);
// ok for stat() to fail, the file might not exist
if (old_st.st_ino == new_st.st_ino) {
continue;
}

// Reopen the file
print_logf(LOG_INFO, "Dumper", "Reopening \"%s\"", dumper->path);
fclose(dumper->file);
dumper->file = fopen(dumper->path, "wb");
if (!dumper->file) {
fprintf(stderr, "Failed to open %s\n", dumper->path);
exit(1);
}
if (dumper->format == VCD_LOGIC) {
pulse_data_print_vcd_header(dumper->file, cfg->samp_rate);
}
if (dumper->format == PULSE_OOK) {
pulse_data_print_pulse_header(dumper->file);
}
}
}
#endif
}

void close_dumpers(struct r_cfg *cfg)
{
for (void **iter = cfg->demod->dumper.elems; iter && *iter; ++iter) {
Expand Down
10 changes: 10 additions & 0 deletions src/rtl_433.c
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,7 @@ static void parse_conf_option(r_cfg_t *cfg, int opt, char *arg)
}

static r_cfg_t g_cfg;
static volatile sig_atomic_t sig_hup;

// TODO: SIGINFO is not in POSIX...
#ifndef SIGINFO
Expand Down Expand Up @@ -1354,6 +1355,10 @@ static void sighandler(int signum)
write_err("Ignoring received signal SIGPIPE, Broken pipe.\n");
return;
}
else if (signum == SIGHUP) {
sig_hup = 1;
return;
}
else if (signum == SIGINFO/* TODO: maybe SIGUSR1 */) {
g_cfg.stats_now++;
return;
Expand Down Expand Up @@ -1511,6 +1516,10 @@ static void timer_handler(struct mg_connection *nc, int ev, void *ev_data)
{
//fprintf(stderr, "%s: %d, %d, %p, %p\n", __func__, nc->sock, ev, nc->user_data, ev_data);
r_cfg_t *cfg = (r_cfg_t *)nc->user_data;
if (sig_hup) {
reopen_dumpers(cfg);
sig_hup = 0;
}
switch (ev) {
case MG_EV_TIMER: {
double now = *(double *)ev_data;
Expand Down Expand Up @@ -1996,6 +2005,7 @@ int main(int argc, char **argv) {
sigact.sa_handler = sighandler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGHUP, &sigact, NULL);
sigaction(SIGINT, &sigact, NULL);
sigaction(SIGTERM, &sigact, NULL);
sigaction(SIGQUIT, &sigact, NULL);
Expand Down

0 comments on commit 7da8f63

Please sign in to comment.