From 46696933aa10d77e302d546e2354d8726d0ff9e2 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 11:26:25 -0800 Subject: [PATCH] Add --log-file --- adsbus/adsbus.c | 20 +++++++++++++++----- adsbus/exec.c | 2 +- adsbus/exec.h | 2 +- adsbus/file.c | 8 ++++---- adsbus/file.h | 6 +++--- adsbus/incoming.c | 2 +- adsbus/incoming.h | 2 +- adsbus/log.c | 39 +++++++++++++++++++++++++++++++++++++- adsbus/log.h | 2 ++ adsbus/opts.c | 48 +++++++++++++++++++++++------------------------ adsbus/opts.h | 32 +++++++++++++++---------------- adsbus/outgoing.c | 2 +- adsbus/outgoing.h | 2 +- adsbus/send.c | 2 +- adsbus/send.h | 2 +- 15 files changed, 110 insertions(+), 61 deletions(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 308ce2b..fbe0895 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -35,6 +35,7 @@ static void print_usage(const char *name) { "\n" "Options:\n" "\t--help\n" + "\n" "\t--connect-receive=HOST/PORT\n" "\t--connect-send=FORMAT=HOST/PORT\n" "\t--connect-send-receive=FORMAT=HOST/PORT\n" @@ -51,6 +52,8 @@ static void print_usage(const char *name) { "\t--exec-send-receive=FORMAT=COMMAND\n" "\t--stdin\n" "\t--stdout=FORMAT\n" + "\n" + "\t--log-file=PATH\n" , name); receive_print_usage(); send_print_usage(); @@ -58,6 +61,7 @@ static void print_usage(const char *name) { static bool parse_opts(int argc, char *argv[]) { static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, {"connect-receive", required_argument, 0, 'c'}, {"connect-send", required_argument, 0, 's'}, {"connect-send-receive", required_argument, 0, 't'}, @@ -74,13 +78,13 @@ static bool parse_opts(int argc, char *argv[]) { {"exec-send-receive", required_argument, 0, 'g'}, {"stdin", no_argument, 0, 'i'}, {"stdout", required_argument, 0, 'o'}, - {"help", no_argument, 0, 'h'}, + {"log-file", required_argument, 0, '1'}, {0, 0, 0, 0 }, }; int opt; while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) { - bool (*handler)(char *) = NULL; + bool (*handler)(const char *) = NULL; switch (opt) { case 'c': handler = opts_add_connect_receive; @@ -146,6 +150,10 @@ static bool parse_opts(int argc, char *argv[]) { handler = opts_add_stdout; break; + case '1': + handler = log_reopen; + break; + case 'h': default: print_usage(argv[0]); @@ -177,10 +185,11 @@ static void reopen(int fd, char *path, int flags) { } int main(int argc, char *argv[]) { - log_init(); - hex_init(); rand_init(); + + log_init(); + resolve_init(); server_init(); wakeup_init(); @@ -200,6 +209,7 @@ int main(int argc, char *argv[]) { reopen(STDIN_FILENO, "/dev/null", O_RDONLY); reopen(STDOUT_FILENO, "/dev/full", O_WRONLY); + reopen(STDERR_FILENO, "/dev/full", O_WRONLY); peer_loop(); @@ -225,7 +235,7 @@ int main(int argc, char *argv[]) { assert(!close(STDIN_FILENO)); assert(!close(STDOUT_FILENO)); - close(STDERR_FILENO); // 2>&1 breaks this + assert(!close(STDERR_FILENO)); return EXIT_SUCCESS; } diff --git a/adsbus/exec.c b/adsbus/exec.c index 55d8647..59c77d9 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -169,7 +169,7 @@ void exec_cleanup() { } } -void exec_new(char *command, struct flow *flow, void *passthrough) { +void exec_new(const char *command, struct flow *flow, void *passthrough) { flow_ref_inc(flow); struct exec *exec = malloc(sizeof(*exec)); diff --git a/adsbus/exec.h b/adsbus/exec.h index 0c8c525..a96ad88 100644 --- a/adsbus/exec.h +++ b/adsbus/exec.h @@ -3,4 +3,4 @@ struct flow; void exec_cleanup(void); -void exec_new(char *, struct flow *, void *); +void exec_new(const char *, struct flow *, void *); diff --git a/adsbus/file.c b/adsbus/file.c index 87e65d1..9af060e 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -107,7 +107,7 @@ static void file_open_wrapper(struct peer *peer) { file_open(file); } -static void file_new(char *path, int flags, struct flow *flow, void *passthrough) { +static void file_new(const char *path, int flags, struct flow *flow, void *passthrough) { flow_ref_inc(flow); struct file *file = malloc(sizeof(*file)); @@ -133,14 +133,14 @@ void file_cleanup() { } } -void file_read_new(char *path, struct flow *flow, void *passthrough) { +void file_read_new(const char *path, struct flow *flow, void *passthrough) { file_new(path, O_RDONLY, flow, passthrough); } -void file_write_new(char *path, struct flow *flow, void *passthrough) { +void file_write_new(const char *path, struct flow *flow, void *passthrough) { file_new(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC, flow, passthrough); } -void file_append_new(char *path, struct flow *flow, void *passthrough) { +void file_append_new(const char *path, struct flow *flow, void *passthrough) { file_new(path, O_WRONLY | O_CREAT | O_NOFOLLOW, flow, passthrough); } diff --git a/adsbus/file.h b/adsbus/file.h index 7727a49..ddcd9c1 100644 --- a/adsbus/file.h +++ b/adsbus/file.h @@ -3,6 +3,6 @@ struct flow; void file_cleanup(void); -void file_read_new(char *, struct flow *, void *); -void file_write_new(char *, struct flow *, void *); -void file_append_new(char *, struct flow *, void *); +void file_read_new(const char *, struct flow *, void *); +void file_write_new(const char *, struct flow *, void *); +void file_append_new(const char *, struct flow *, void *); diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 437cf2d..c2c3dff 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -148,7 +148,7 @@ void incoming_cleanup() { } } -void incoming_new(char *node, char *service, struct flow *flow, void *passthrough) { +void incoming_new(const char *node, const char *service, struct flow *flow, void *passthrough) { flow_ref_inc(flow); struct incoming *incoming = malloc(sizeof(*incoming)); diff --git a/adsbus/incoming.h b/adsbus/incoming.h index 4691c1d..0f6f096 100644 --- a/adsbus/incoming.h +++ b/adsbus/incoming.h @@ -3,4 +3,4 @@ struct flow; void incoming_cleanup(void); -void incoming_new(char *, char *, struct flow *, void *); +void incoming_new(const char *, const char *, struct flow *, void *); diff --git a/adsbus/log.c b/adsbus/log.c index 1671a97..f54d0a2 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -1,22 +1,59 @@ #include #include #include +#include +#include #include #include "log.h" +#include "uuid.h" #pragma GCC diagnostic ignored "-Wformat-nonliteral" static FILE *log_stream = NULL; +static char *log_path = NULL; +static uint8_t log_id[UUID_LEN]; + +static void log_rotate() { + uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN]; + uuid_gen(new_log_id); + log_write('L', log_id, "Switching to new log with ID %s at: %s", new_log_id, log_path); + memcpy(old_log_id, log_id, UUID_LEN); + memcpy(log_id, new_log_id, UUID_LEN); + assert(!fclose(log_stream)); + log_stream = fopen(log_path, "a"); + assert(log_stream); + log_write('L', log_id, "Log start after switch from log ID %s", old_log_id); +} void log_init() { - log_stream = fdopen(STDERR_FILENO, "a"); + int fd = dup(STDERR_FILENO); + assert(fd >= 0); + log_stream = fdopen(fd, "a"); assert(log_stream); setlinebuf(log_stream); + + uuid_gen(log_id); + log_write('L', log_id, "Log start"); } void log_cleanup() { + log_write('L', log_id, "Log end"); assert(!fclose(log_stream)); + if (log_path) { + free(log_path); + log_path = NULL; + } +} + +bool log_reopen(const char *path) { + if (log_path) { + free(log_path); + } + log_path = strdup(path); + assert(log_path); + log_rotate(); + return true; } void log_write(char type, const uint8_t *id, const char *fmt, ...) { diff --git a/adsbus/log.h b/adsbus/log.h index a0d20f5..1dedca3 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -1,8 +1,10 @@ #pragma once +#include #include void log_init(void); void log_cleanup(void); +bool log_reopen(const char *); void log_write(char, const uint8_t *, const char *, ...) __attribute__ ((__format__ (__printf__, 3, 4))); diff --git a/adsbus/opts.c b/adsbus/opts.c index 0b39e88..22f855e 100644 --- a/adsbus/opts.c +++ b/adsbus/opts.c @@ -17,7 +17,7 @@ #include "opts.h" -static char *opts_split(char **arg, char delim) { +static char *opts_split(const char **arg, char delim) { char *split = strchr(*arg, delim); if (!split) { return NULL; @@ -27,7 +27,7 @@ static char *opts_split(char **arg, char delim) { return ret; } -static bool opts_add_listen(char *host_port, struct flow *flow, void *passthrough) { +static bool opts_add_listen(const char *host_port, struct flow *flow, void *passthrough) { char *host = opts_split(&host_port, '/'); if (host) { incoming_new(host, host_port, flow, passthrough); @@ -38,7 +38,7 @@ static bool opts_add_listen(char *host_port, struct flow *flow, void *passthroug return true; } -static bool opts_add_connect(char *host_port, struct flow *flow, void *passthrough) { +static bool opts_add_connect(const char *host_port, struct flow *flow, void *passthrough) { char *host = opts_split(&host_port, '/'); if (!host) { return false; @@ -49,22 +49,22 @@ static bool opts_add_connect(char *host_port, struct flow *flow, void *passthrou return true; } -static bool opts_add_file_write_int(char *path, struct flow *flow, void *passthrough) { +static bool opts_add_file_write_int(const char *path, struct flow *flow, void *passthrough) { file_write_new(path, flow, passthrough); return true; } -static bool opts_add_file_append_int(char *path, struct flow *flow, void *passthrough) { +static bool opts_add_file_append_int(const char *path, struct flow *flow, void *passthrough) { file_append_new(path, flow, passthrough); return true; } -static bool opts_add_exec(char *cmd, struct flow *flow, void *passthrough) { +static bool opts_add_exec(const char *cmd, struct flow *flow, void *passthrough) { exec_new(cmd, flow, passthrough); return true; } -static struct serializer *opts_get_serializer(char **arg) { +static struct serializer *opts_get_serializer(const char **arg) { char *format = opts_split(arg, '='); if (!format) { return NULL; @@ -79,7 +79,7 @@ static struct serializer *opts_get_serializer(char **arg) { return serializer; } -static bool opts_add_send(bool (*next)(char *, struct flow *, void *), struct flow *flow, char *arg) { +static bool opts_add_send(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) { struct serializer *serializer = opts_get_serializer(&arg); if (!serializer) { return false; @@ -87,71 +87,71 @@ static bool opts_add_send(bool (*next)(char *, struct flow *, void *), struct fl return next(arg, flow, serializer); } -bool opts_add_connect_receive(char *arg) { +bool opts_add_connect_receive(const char *arg) { return opts_add_connect(arg, receive_flow, NULL); } -bool opts_add_connect_send(char *arg) { +bool opts_add_connect_send(const char *arg) { return opts_add_send(opts_add_connect, send_flow, arg); } -bool opts_add_connect_send_receive(char *arg) { +bool opts_add_connect_send_receive(const char *arg) { return opts_add_send(opts_add_connect, send_receive_flow, arg); } -bool opts_add_listen_receive(char *arg) { +bool opts_add_listen_receive(const char *arg) { return opts_add_listen(arg, receive_flow, NULL); } -bool opts_add_listen_send(char *arg) { +bool opts_add_listen_send(const char *arg) { return opts_add_send(opts_add_listen, send_flow, arg); } -bool opts_add_listen_send_receive(char *arg) { +bool opts_add_listen_send_receive(const char *arg) { return opts_add_send(opts_add_listen, send_receive_flow, arg); } -bool opts_add_file_read(char *arg) { +bool opts_add_file_read(const char *arg) { file_read_new(arg, receive_flow, NULL); return true; } -bool opts_add_file_write(char *arg) { +bool opts_add_file_write(const char *arg) { return opts_add_send(opts_add_file_write_int, send_flow, arg); } -bool opts_add_file_write_read(char *arg) { +bool opts_add_file_write_read(const char *arg) { return opts_add_send(opts_add_file_write_int, send_receive_flow, arg); } -bool opts_add_file_append(char *arg) { +bool opts_add_file_append(const char *arg) { return opts_add_send(opts_add_file_append_int, send_flow, arg); } -bool opts_add_file_append_read(char *arg) { +bool opts_add_file_append_read(const char *arg) { return opts_add_send(opts_add_file_append_int, send_receive_flow, arg); } -bool opts_add_exec_receive(char *arg) { +bool opts_add_exec_receive(const char *arg) { exec_new(arg, receive_flow, NULL); return true; } -bool opts_add_exec_send(char *arg) { +bool opts_add_exec_send(const char *arg) { return opts_add_send(opts_add_exec, send_flow, arg); } -bool opts_add_exec_send_receive(char *arg) { +bool opts_add_exec_send_receive(const char *arg) { return opts_add_send(opts_add_exec, send_receive_flow, arg); } -bool opts_add_stdin(char __attribute__((unused)) *arg) { +bool opts_add_stdin(const char __attribute__((unused)) *arg) { int fd = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0); assert(fd >= 0); return flow_new_send_hello(fd, receive_flow, NULL, NULL); } -bool opts_add_stdout(char *arg) { +bool opts_add_stdout(const char *arg) { struct serializer *serializer = send_get_serializer(arg); if (!serializer) { return false; diff --git a/adsbus/opts.h b/adsbus/opts.h index 3c03ce8..eb47ff7 100644 --- a/adsbus/opts.h +++ b/adsbus/opts.h @@ -2,19 +2,19 @@ #include -bool opts_add_connect_receive(char *); -bool opts_add_connect_send(char *); -bool opts_add_connect_send_receive(char *); -bool opts_add_listen_receive(char *); -bool opts_add_listen_send(char *); -bool opts_add_listen_send_receive(char *); -bool opts_add_file_read(char *); -bool opts_add_file_write(char *); -bool opts_add_file_write_read(char *); -bool opts_add_file_append(char *); -bool opts_add_file_append_read(char *); -bool opts_add_exec_receive(char *); -bool opts_add_exec_send(char *); -bool opts_add_exec_send_receive(char *); -bool opts_add_stdout(char *); -bool opts_add_stdin(char *); +bool opts_add_connect_receive(const char *); +bool opts_add_connect_send(const char *); +bool opts_add_connect_send_receive(const char *); +bool opts_add_listen_receive(const char *); +bool opts_add_listen_send(const char *); +bool opts_add_listen_send_receive(const char *); +bool opts_add_file_read(const char *); +bool opts_add_file_write(const char *); +bool opts_add_file_write_read(const char *); +bool opts_add_file_append(const char *); +bool opts_add_file_append_read(const char *); +bool opts_add_exec_receive(const char *); +bool opts_add_exec_send(const char *); +bool opts_add_exec_send_receive(const char *); +bool opts_add_stdout(const char *); +bool opts_add_stdin(const char *); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 56e5563..20c47fc 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -158,7 +158,7 @@ void outgoing_cleanup() { } } -void outgoing_new(char *node, char *service, struct flow *flow, void *passthrough) { +void outgoing_new(const char *node, const char *service, struct flow *flow, void *passthrough) { flow_ref_inc(flow); struct outgoing *outgoing = malloc(sizeof(*outgoing)); diff --git a/adsbus/outgoing.h b/adsbus/outgoing.h index c39fdc5..2e88b4d 100644 --- a/adsbus/outgoing.h +++ b/adsbus/outgoing.h @@ -3,4 +3,4 @@ struct flow; void outgoing_cleanup(void); -void outgoing_new(char *, char *, struct flow *, void *); +void outgoing_new(const char *, const char *, struct flow *, void *); diff --git a/adsbus/send.c b/adsbus/send.c index 7a3a44b..121e936 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -141,7 +141,7 @@ void send_cleanup() { } } -void *send_get_serializer(char *name) { +void *send_get_serializer(const char *name) { for (size_t i = 0; i < NUM_SERIALIZERS; i++) { if (strcasecmp(serializers[i].name, name) == 0) { return &serializers[i]; diff --git a/adsbus/send.h b/adsbus/send.h index a3c380f..50657bf 100644 --- a/adsbus/send.h +++ b/adsbus/send.h @@ -6,7 +6,7 @@ struct packet; void send_init(void); void send_cleanup(void); -void *send_get_serializer(char *); +void *send_get_serializer(const char *); void send_get_hello(struct buf **, void *); void send_write(struct packet *); void send_print_usage(void);