Skip to content

Commit

Permalink
Atomize output files
Browse files Browse the repository at this point in the history
I feel like I need to relearn signals every time I have to interact with
them. Best get this done while the iron's hot.

1. The ROA file is first written as `<cache>/.roa`.
   The RK file is first written as `<cache>/.rk`.
2. When the validation run is done, `.roa` is renamed to `--output.roa`,
   and `.rk` becomes `--output.bgpsec`.
3. Most terminating signals unlink `.roa` and `.rk`.

Fixes #124.
  • Loading branch information
ydahhrk committed Oct 16, 2024
1 parent b1447b5 commit d07db54
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "http.h"
#include "log.h"
#include "nid.h"
#include "output_printer.h"
#include "print_file.h"
#include "relax_ng.h"
#include "rtr/rtr.h"
Expand Down Expand Up @@ -153,6 +154,9 @@ main(int argc, char **argv)
error = cache_setup();
if (error)
goto revert_vrps;
error = output_setup();
if (error)
goto revert_cache;

/* Meat */

Expand All @@ -169,7 +173,7 @@ main(int argc, char **argv)
}

/* End */

revert_cache:
cache_teardown();
revert_vrps:
vrps_destroy();
Expand Down
47 changes: 41 additions & 6 deletions src/output_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,27 @@ typedef struct json_out {
bool first;
} JSON_OUT;

int
output_setup(void)
{
int err1;
int err2;

errno = 0;
err1 = atexit(output_atexit);
if (err1) {
err2 = errno;
pr_op_err("Cannot register output's exit function.");
pr_op_err("Error message attempt 1: %s", strerror(err1));
pr_op_err("Error message attempt 2: %s", strerror(err2));
return err1;
}

return 0;
}

static FILE *
load_output_file(char const *filename)
load_output_file(char const *filename, char const *tmpname)
{
FILE *out;

Expand All @@ -21,7 +40,7 @@ load_output_file(char const *filename)
if (strcmp(filename, "-") == 0)
return stdout;

return (file_write(filename, "w", &out) == 0) ? out : NULL;
return (file_write(tmpname, "w", &out) == 0) ? out : NULL;
}

static int
Expand Down Expand Up @@ -130,7 +149,7 @@ print_roas(struct db_table const *db)
JSON_OUT json_out;
int error;

out = load_output_file(config_get_output_roa());
out = load_output_file(config_get_output_roa(), ".roa");
if (out == NULL)
return;

Expand All @@ -149,8 +168,12 @@ print_roas(struct db_table const *db)

if (error)
pr_op_err("Error printing ROAs: %s", strerror(error));
if (out != stdout)
if (out != stdout) {
file_close(out);
if (!error && rename(".roa", config_get_output_roa()) < 0)
pr_op_err("Cannot move '.roa' to '%s': %s",
config_get_output_roa(), strerror(errno));
}
}

static void
Expand All @@ -160,7 +183,7 @@ print_router_keys(struct db_table const *db)
JSON_OUT json_out;
int error;

out = load_output_file(config_get_output_bgpsec());
out = load_output_file(config_get_output_bgpsec(), ".rk");
if (out == NULL)
return;

Expand All @@ -179,8 +202,12 @@ print_router_keys(struct db_table const *db)

if (error)
pr_op_err("Error printing Router Keys: %s", strerror(error));
if (out != stdout)
if (out != stdout) {
file_close(out);
if (!error && rename(".rk", config_get_output_bgpsec()) < 0)
pr_op_err("Cannot move '.rk' to '%s': %s",
config_get_output_bgpsec(), strerror(errno));
}
}

void
Expand All @@ -189,3 +216,11 @@ output_print_data(struct db_table const *db)
print_roas(db);
print_router_keys(db);
}

/* THIS FUNCTION CAN BE CALLED FROM A SIGNAL HANDLER. */
void
output_atexit(void)
{
unlink(".roa");
unlink(".rk");
}
2 changes: 2 additions & 0 deletions src/output_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "rtr/db/db_table.h"

int output_setup(void);
void output_print_data(struct db_table const *);
void output_atexit(void);

#endif /* SRC_OUTPUT_PRINTER_H_ */
2 changes: 2 additions & 0 deletions src/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "cache.h"
#include "log.h"
#include "output_printer.h"

/*
* Ensures libgcc is loaded; otherwise backtrace() might allocate
Expand Down Expand Up @@ -55,6 +56,7 @@ do_cleanup(int signum)
print_stack_trace();

cache_atexit();
output_atexit();

/* Trigger default handler */
signal(signum, SIG_DFL);
Expand Down

0 comments on commit d07db54

Please sign in to comment.