From 746379aee59eea29c4cedda1097b7f845424aa54 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 4 Mar 2016 13:02:43 -0800 Subject: [PATCH 01/35] Treat SIGTERM like SIGINT --- adsbus/peer.c | 1 + 1 file changed, 1 insertion(+) diff --git a/adsbus/peer.c b/adsbus/peer.c index 3c8f70e..d2e4066 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -43,6 +43,7 @@ void peer_init() { peer_shutdown_fd = shutdown_fds[1]; signal(SIGINT, peer_shutdown); + signal(SIGTERM, peer_shutdown); } void peer_cleanup() { From 06e73e231957f96a065b6f11df69e8549aa5060a Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 4 Mar 2016 19:17:15 -0800 Subject: [PATCH 02/35] stutterfuzz: Fix crash when shutdown() returns ENOTCONN --- adsbus/send.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adsbus/send.c b/adsbus/send.c index 5522442..d51275d 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -177,8 +177,8 @@ void send_write(struct packet *packet) { } if (write(iter->peer.fd, buf_at(&buf, 0), buf.length) != (ssize_t) buf.length) { // peer_loop() will see this shutdown and call send_del - int res = shutdown(iter->peer.fd, SHUT_WR); - assert(res == 0 || (res == -1 && errno == ENOTSOCK)); + // Ignore error + shutdown(iter->peer.fd, SHUT_WR); } } } From 6480a960fa95800b173d194cf69463386e6f7f92 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 5 Mar 2016 22:54:26 -0800 Subject: [PATCH 03/35] Switch to standard logging framework --- adsbus/Makefile | 2 +- adsbus/adsbus.c | 7 ++++++- adsbus/airspy_adsb.c | 1 - adsbus/beast.c | 1 - adsbus/exec.c | 14 +++++++------- adsbus/file.c | 12 ++++++------ adsbus/incoming.c | 27 +++++++++++++-------------- adsbus/json.c | 1 - adsbus/log.c | 30 ++++++++++++++++++++++++++++++ adsbus/log.h | 7 +++++++ adsbus/outgoing.c | 26 +++++++++++++------------- adsbus/packet.c | 1 - adsbus/peer.c | 10 +++++----- adsbus/proto.c | 6 +++--- adsbus/rand.c | 1 - adsbus/raw.c | 1 - adsbus/receive.c | 11 ++++++----- adsbus/send.c | 5 +++-- adsbus/server.c | 5 ++--- adsbus/wakeup.c | 1 - 20 files changed, 102 insertions(+), 67 deletions(-) create mode 100644 adsbus/log.c create mode 100644 adsbus/log.h diff --git a/adsbus/Makefile b/adsbus/Makefile index fe6dabd..61b62c4 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -13,7 +13,7 @@ ADSBUS_TEST_FLAGS ?= --stdin --stdout=airspy_adsb --stdout=beast --stdout=json - OBJ_TRANSPORT = exec.o file.o incoming.o outgoing.o OBJ_FLOW = flow.o receive.o send.o send_receive.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o proto.o raw.o stats.o -OBJ_UTIL = asyncaddrinfo.o buf.o hex.o list.o opts.o packet.o peer.o rand.o resolve.o server.o socket.o uuid.o wakeup.o +OBJ_UTIL = asyncaddrinfo.o buf.o hex.o list.o log.o opts.o packet.o peer.o rand.o resolve.o server.o socket.o uuid.o wakeup.o OBJ_PROTO = adsb.pb-c.o all: adsbus diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 5621eb3..308ce2b 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -14,6 +14,7 @@ #include "hex.h" #include "incoming.h" #include "json.h" +#include "log.h" #include "opts.h" #include "outgoing.h" #include "peer.h" @@ -176,6 +177,8 @@ static void reopen(int fd, char *path, int flags) { } int main(int argc, char *argv[]) { + log_init(); + hex_init(); rand_init(); resolve_init(); @@ -218,9 +221,11 @@ int main(int argc, char *argv[]) { peer_cleanup(); + log_cleanup(); + assert(!close(STDIN_FILENO)); assert(!close(STDOUT_FILENO)); - assert(!close(STDERR_FILENO)); + close(STDERR_FILENO); // 2>&1 breaks this return EXIT_SUCCESS; } diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index eeac285..af8460e 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -1,5 +1,4 @@ #include -#include #include #include "buf.h" diff --git a/adsbus/beast.c b/adsbus/beast.c index b808210..700adc0 100644 --- a/adsbus/beast.c +++ b/adsbus/beast.c @@ -1,5 +1,4 @@ #include -#include #include #include diff --git a/adsbus/exec.c b/adsbus/exec.c index e072c74..74851ca 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -11,6 +10,7 @@ #include "buf.h" #include "flow.h" #include "list.h" +#include "log.h" #include "peer.h" #include "uuid.h" #include "wakeup.h" @@ -35,7 +35,7 @@ static void exec_del(struct exec *exec) { flow_ref_dec(exec->flow); if (exec->child > 0) { - fprintf(stderr, "E %s: Sending SIGTERM to child process %d\n", exec->id, exec->child); + log_write('E', exec->id, "Sending SIGTERM to child process %d", exec->child); // Racy with the process terminating, so don't assert on it kill(exec->child, SIGTERM); assert(waitpid(exec->child, NULL, 0) == exec->child); @@ -51,20 +51,20 @@ static void exec_close_handler(struct peer *peer) { assert(waitpid(exec->child, &status, WNOHANG) == exec->child); exec->child = -1; if (WIFEXITED(status)) { - fprintf(stderr, "E %s: Client exited with status %d\n", exec->id, WEXITSTATUS(status)); + log_write('E', exec->id, "Client exited with status %d", WEXITSTATUS(status)); } else { assert(WIFSIGNALED(status)); - fprintf(stderr, "E %s: Client exited with signal %d\n", exec->id, WTERMSIG(status)); + log_write('E', exec->id, "Client exited with signal %d", WTERMSIG(status)); } uint32_t delay = wakeup_get_retry_delay_ms(1); - fprintf(stderr, "E %s: Will retry in %ds\n", exec->id, delay / 1000); + log_write('E', exec->id, "Will retry in %ds", delay / 1000); exec->peer.event_handler = exec_spawn_wrapper; wakeup_add((struct peer *) exec, delay); } static void exec_parent(struct exec *exec, pid_t child, int fd) { exec->child = child; - fprintf(stderr, "E %s: Child started as process %d\n", exec->id, exec->child); + log_write('E', exec->id, "Child started as process %d", exec->child); exec->peer.event_handler = exec_close_handler; if (!flow_new_send_hello(fd, exec->flow, exec->passthrough, (struct peer *) exec)) { @@ -91,7 +91,7 @@ static void __attribute__ ((noreturn)) exec_child(const struct exec *exec, int f } static void exec_spawn(struct exec *exec) { - fprintf(stderr, "E %s: Executing: %s\n", exec->id, exec->command); + log_write('E', exec->id, "Executing: %s", exec->id, exec->command); int fds[2]; assert(!socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds)); diff --git a/adsbus/file.c b/adsbus/file.c index 2d09009..87e65d1 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -11,6 +10,7 @@ #include "flow.h" #include "list.h" +#include "log.h" #include "peer.h" #include "receive.h" #include "send.h" @@ -67,14 +67,14 @@ static void file_del(struct file *file) { static void file_retry(struct file *file) { uint32_t delay = wakeup_get_retry_delay_ms(file->attempt++); - fprintf(stderr, "F %s: Will retry in %ds\n", file->id, delay / 1000); + log_write('F', file->id, "Will retry in %ds", delay / 1000); file->peer.event_handler = file_open_wrapper; wakeup_add((struct peer *) file, delay); } static void file_handle_close(struct peer *peer) { struct file *file = (struct file *) peer; - fprintf(stderr, "F %s: File closed: %s\n", file->id, file->path); + log_write('F', file->id, "File closed: %s", file->path); if (file->retry) { file_retry(file); @@ -84,10 +84,10 @@ static void file_handle_close(struct peer *peer) { } static void file_open(struct file *file) { - fprintf(stderr, "F %s: Opening file: %s\n", file->id, file->path); + log_write('F', file->id, "Opening file: %s", file->path); int fd = open(file->path, file->flags | O_CLOEXEC, S_IRUSR | S_IWUSR); if (fd == -1) { - fprintf(stderr, "F %s: Error opening file: %s\n", file->id, strerror(errno)); + log_write('F', file->id, "Error opening file: %s", strerror(errno)); file_retry(file); return; } @@ -96,7 +96,7 @@ static void file_open(struct file *file) { file->peer.event_handler = file_handle_close; file->attempt = 0; if (!flow_new_send_hello(fd, file->flow, file->passthrough, (struct peer *) file)) { - fprintf(stderr, "F %s: Error writing greeting\n", file->id); + log_write('F', file->id, "Error writing greeting"); file_retry(file); return; } diff --git a/adsbus/incoming.c b/adsbus/incoming.c index daced14..437cf2d 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -1,16 +1,16 @@ #include -#include +#include +#include #include +#include #include #include -#include -#include -#include #include #include "buf.h" #include "flow.h" #include "list.h" +#include "log.h" #include "peer.h" #include "resolve.h" #include "socket.h" @@ -36,7 +36,7 @@ static void incoming_resolve_wrapper(struct peer *); static void incoming_retry(struct incoming *incoming) { uint32_t delay = wakeup_get_retry_delay_ms(incoming->attempt++); - fprintf(stderr, "I %s: Will retry in %ds\n", incoming->id, delay / 1000); + log_write('I', incoming->id, "Will retry in %ds", delay / 1000); incoming->peer.event_handler = incoming_resolve_wrapper; wakeup_add((struct peer *) incoming, delay); } @@ -49,7 +49,7 @@ static void incoming_handler(struct peer *peer) { int fd = accept4(incoming->peer.fd, &peer_addr, &peer_addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); if (fd == -1) { - fprintf(stderr, "I %s: Failed to accept new connection on %s/%s: %s\n", incoming->id, incoming->node, incoming->service, strerror(errno)); + log_write('I', incoming->id, "Failed to accept new connection on %s/%s: %s", incoming->node, incoming->service, strerror(errno)); return; } @@ -58,8 +58,7 @@ static void incoming_handler(struct peer *peer) { assert(getnameinfo(&peer_addr, peer_addrlen, peer_hbuf, sizeof(peer_hbuf), peer_sbuf, sizeof(peer_sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); assert(getnameinfo(&local_addr, local_addrlen, local_hbuf, sizeof(local_hbuf), local_sbuf, sizeof(local_sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); - fprintf(stderr, "I %s: New incoming connection on %s/%s (%s/%s) from %s/%s\n", - incoming->id, + log_write('I', incoming->id, "New incoming connection on %s/%s (%s/%s) from %s/%s", incoming->node, incoming->service, local_hbuf, local_sbuf, peer_hbuf, peer_sbuf); @@ -67,7 +66,7 @@ static void incoming_handler(struct peer *peer) { flow_socket_connected(fd, incoming->flow); if (!flow_new_send_hello(fd, incoming->flow, incoming->passthrough, NULL)) { - fprintf(stderr, "I %s: Error writing greeting\n", incoming->id); + log_write('I', incoming->id, "Error writing greeting"); return; } } @@ -89,7 +88,7 @@ static void incoming_listen(struct peer *peer) { struct addrinfo *addrs; int err = resolve_result(peer, &addrs); if (err) { - fprintf(stderr, "I %s: Failed to resolve %s/%s: %s\n", incoming->id, incoming->node, incoming->service, gai_strerror(err)); + log_write('I', incoming->id, "Failed to resolve %s/%s: %s", incoming->node, incoming->service, gai_strerror(err)); incoming_retry(incoming); return; } @@ -98,7 +97,7 @@ static void incoming_listen(struct peer *peer) { for (addr = addrs; addr; addr = addr->ai_next) { char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; assert(getnameinfo(addr->ai_addr, addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); - fprintf(stderr, "I %s: Listening on %s/%s...\n", incoming->id, hbuf, sbuf); + log_write('I', incoming->id, "Listening on %s/%s...", hbuf, sbuf); incoming->peer.fd = socket(addr->ai_family, addr->ai_socktype | SOCK_CLOEXEC, addr->ai_protocol); assert(incoming->peer.fd >= 0); @@ -106,7 +105,7 @@ static void incoming_listen(struct peer *peer) { socket_pre_bind(incoming->peer.fd); if (bind(incoming->peer.fd, addr->ai_addr, addr->ai_addrlen) != 0) { - fprintf(stderr, "I %s: Failed to bind to %s/%s: %s\n", incoming->id, hbuf, sbuf, strerror(errno)); + log_write('I', incoming->id, "Failed to bind to %s/%s: %s", hbuf, sbuf, strerror(errno)); assert(!close(incoming->peer.fd)); continue; } @@ -122,7 +121,7 @@ static void incoming_listen(struct peer *peer) { freeaddrinfo(addrs); if (addr == NULL) { - fprintf(stderr, "I %s: Failed to bind any addresses for %s/%s...\n", incoming->id, incoming->node, incoming->service); + log_write('I', incoming->id, "Failed to bind any addresses for %s/%s...", incoming->node, incoming->service); incoming_retry(incoming); return; } @@ -133,7 +132,7 @@ static void incoming_listen(struct peer *peer) { } static void incoming_resolve(struct incoming *incoming) { - fprintf(stderr, "I %s: Resolving %s/%s...\n", incoming->id, incoming->node, incoming->service); + log_write('I', incoming->id, "Resolving %s/%s...", incoming->node, incoming->service); incoming->peer.event_handler = incoming_listen; resolve((struct peer *) incoming, incoming->node, incoming->service, AI_PASSIVE); } diff --git a/adsbus/json.c b/adsbus/json.c index 98a0836..fb2bfa1 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -1,5 +1,4 @@ #include -#include #include #include diff --git a/adsbus/log.c b/adsbus/log.c new file mode 100644 index 0000000..1b48144 --- /dev/null +++ b/adsbus/log.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#include "log.h" + +#pragma GCC diagnostic ignored "-Wformat-nonliteral" + +static FILE *log_stream = NULL; + +void log_init() { + log_stream = fdopen(STDERR_FILENO, "a"); + assert(log_stream); + setlinebuf(log_stream); +} + +void log_cleanup() { + assert(!fclose(log_stream)); +} + +__attribute__ ((__format__ (__printf__, 3, 4))) +void log_write(char type, const uint8_t *id, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + assert(fprintf(log_stream, "%c %s: ", type, id) > 0); + assert(vfprintf(log_stream, fmt, ap) > 0); + assert(fprintf(log_stream, "\n") == 1); + va_end(ap); +} diff --git a/adsbus/log.h b/adsbus/log.h new file mode 100644 index 0000000..536ff5d --- /dev/null +++ b/adsbus/log.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +void log_init(void); +void log_cleanup(void); +void log_write(char, const uint8_t *, const char *, ...); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 6ccf25b..56e5563 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -1,16 +1,16 @@ -#include -#include #include +#include +#include +#include +#include #include #include -#include -#include -#include #include #include "buf.h" #include "flow.h" #include "list.h" +#include "log.h" #include "peer.h" #include "resolve.h" #include "socket.h" @@ -40,7 +40,7 @@ static void outgoing_resolve_wrapper(struct peer *); static void outgoing_retry(struct outgoing *outgoing) { uint32_t delay = wakeup_get_retry_delay_ms(++outgoing->attempt); - fprintf(stderr, "O %s: Will retry in %ds\n", outgoing->id, delay / 1000); + log_write('O', outgoing->id, "Will retry in %ds", delay / 1000); outgoing->peer.event_handler = outgoing_resolve_wrapper; wakeup_add((struct peer *) outgoing, delay); } @@ -48,14 +48,14 @@ static void outgoing_retry(struct outgoing *outgoing) { static void outgoing_connect_next(struct outgoing *outgoing) { if (outgoing->addr == NULL) { freeaddrinfo(outgoing->addrs); - fprintf(stderr, "O %s: Can't connect to any addresses of %s/%s\n", outgoing->id, outgoing->node, outgoing->service); + log_write('O', outgoing->id, "Can't connect to any addresses of %s/%s", outgoing->node, outgoing->service); outgoing_retry(outgoing); return; } char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; assert(getnameinfo(outgoing->addr->ai_addr, outgoing->addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); - fprintf(stderr, "O %s: Connecting to %s/%s...\n", outgoing->id, hbuf, sbuf); + log_write('O', outgoing->id, "Connecting to %s/%s...", hbuf, sbuf); outgoing->peer.fd = socket(outgoing->addr->ai_family, outgoing->addr->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC, outgoing->addr->ai_protocol); assert(outgoing->peer.fd >= 0); @@ -82,7 +82,7 @@ static void outgoing_disconnect_handler(struct peer *peer) { if (outgoing->peer.fd != -1) { assert(!close(outgoing->peer.fd)); } - fprintf(stderr, "O %s: Peer disconnected; reconnecting...\n", outgoing->id); + log_write('O', outgoing->id, "Peer disconnected; reconnecting..."); outgoing_retry(outgoing); } @@ -91,7 +91,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { assert(getnameinfo(outgoing->addr->ai_addr, outgoing->addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); switch (result) { case 0: - fprintf(stderr, "O %s: Connected to %s/%s\n", outgoing->id, hbuf, sbuf); + log_write('O', outgoing->id, "Connected to %s/%s", hbuf, sbuf); freeaddrinfo(outgoing->addrs); outgoing->attempt = 0; int fd = outgoing->peer.fd; @@ -108,7 +108,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { break; default: - fprintf(stderr, "O %s: Can't connect to %s/%s: %s\n", outgoing->id, hbuf, sbuf, strerror(result)); + log_write('O', outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result)); assert(!close(outgoing->peer.fd)); outgoing->peer.fd = -1; outgoing->addr = outgoing->addr->ai_next; @@ -122,7 +122,7 @@ static void outgoing_resolve_handler(struct peer *peer) { struct outgoing *outgoing = (struct outgoing *) peer; int err = resolve_result(peer, &outgoing->addrs); if (err) { - fprintf(stderr, "O %s: Failed to resolve %s/%s: %s\n", outgoing->id, outgoing->node, outgoing->service, gai_strerror(err)); + log_write('O', outgoing->id, "Failed to resolve %s/%s: %s", outgoing->node, outgoing->service, gai_strerror(err)); outgoing_retry(outgoing); } else { outgoing->addr = outgoing->addrs; @@ -131,7 +131,7 @@ static void outgoing_resolve_handler(struct peer *peer) { } static void outgoing_resolve(struct outgoing *outgoing) { - fprintf(stderr, "O %s: Resolving %s/%s...\n", outgoing->id, outgoing->node, outgoing->service); + log_write('O', outgoing->id, "Resolving %s/%s...", outgoing->node, outgoing->service); outgoing->peer.event_handler = outgoing_resolve_handler; resolve((struct peer *) outgoing, outgoing->node, outgoing->service, 0); } diff --git a/adsbus/packet.c b/adsbus/packet.c index 6232e81..3dfd461 100644 --- a/adsbus/packet.c +++ b/adsbus/packet.c @@ -1,7 +1,6 @@ #pragma GCC diagnostic ignored "-Wtautological-constant-out-of-range-compare" #include -#include #include #include "uuid.h" diff --git a/adsbus/peer.c b/adsbus/peer.c index d2e4066..db27eac 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -3,11 +3,11 @@ #include #include #include -#include #include #include #include +#include "log.h" #include "server.h" #include "uuid.h" #include "wakeup.h" @@ -22,7 +22,7 @@ static bool peer_shutdown_flag = false; static struct list_head peer_always_trigger_head = LIST_HEAD_INIT(peer_always_trigger_head); static void peer_shutdown_handler(struct peer *peer) { - fprintf(stderr, "X %s: Shutting down\n", server_id); + log_write('X', server_id, "Shutting down"); assert(!close(peer->fd)); free(peer); peer_shutdown_flag = true; @@ -96,13 +96,13 @@ void peer_call(struct peer *peer) { } void peer_loop() { - fprintf(stderr, "X %s: Starting event loop\n", server_id); + log_write('X', server_id, "Starting event loop"); while (!peer_shutdown_flag) { if (!(peer_count_in + peer_count_out_in)) { - fprintf(stderr, "X %s: No remaining inputs\n", server_id); + log_write('X', server_id, "No remaining inputs"); peer_shutdown(0); } else if (!(peer_count_out + peer_count_out_in)) { - fprintf(stderr, "X %s: No remaining outputs\n", server_id); + log_write('X', server_id, "No remaining outputs"); peer_shutdown(0); } #define MAX_EVENTS 10 diff --git a/adsbus/proto.c b/adsbus/proto.c index d85cc25..578d4d9 100644 --- a/adsbus/proto.c +++ b/adsbus/proto.c @@ -3,10 +3,10 @@ #include #include -#include #include #include "buf.h" +#include "log.h" #include "packet.h" #include "server.h" #include "uuid.h" @@ -145,12 +145,12 @@ static bool proto_parse_header(AdsbHeader *header, struct packet *packet, struct state->rssi_max = header->rssi_max; if (!strcmp(header->server_id, (const char *) server_id)) { - fprintf(stderr, "R %s: Attempt to receive proto data from our own server ID (%s); loop!\n", packet->source_id, server_id); + log_write('R', packet->source_id, "Attempt to receive proto data from our own server ID (%s); loop!", server_id); return false; } state->have_header = true; - fprintf(stderr, "R %s: Connected to server ID: %s\n", packet->source_id, header->server_id); + log_write('R', packet->source_id, "Connected to server ID: %s", header->server_id); return true; } diff --git a/adsbus/rand.c b/adsbus/rand.c index 82d6563..f232cd9 100644 --- a/adsbus/rand.c +++ b/adsbus/rand.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include diff --git a/adsbus/raw.c b/adsbus/raw.c index bcceb3d..f721d66 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -1,5 +1,4 @@ #include -#include #include #include "buf.h" diff --git a/adsbus/receive.c b/adsbus/receive.c index 8b34095..ed42cc5 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -13,6 +13,7 @@ #include "flow.h" #include "json.h" #include "list.h" +#include "log.h" #include "packet.h" #include "peer.h" #include "proto.h" @@ -92,7 +93,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac for (size_t i = 0; i < NUM_PARSERS; i++) { if (parsers[i].parse(buf, packet, state)) { - fprintf(stderr, "R %s: Detected input format %s\n", receive->id, parsers[i].name); + log_write('R', receive->id, "Detected input format: %s", parsers[i].name); receive->parser_wrapper = receive_parse_wrapper; receive->parser = parsers[i].parse; return true; @@ -105,7 +106,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac } static void receive_del(struct receive *receive) { - fprintf(stderr, "R %s: Connection closed\n", receive->id); + log_write('R', receive->id, "Connection closed"); peer_count_in--; peer_epoll_del((struct peer *) receive); assert(!close(receive->peer.fd)); @@ -134,14 +135,14 @@ static void receive_read(struct peer *peer) { continue; } if (++packet.hops > receive_max_hops) { - fprintf(stderr, "R %s: Packet exceeded hop limit (%u > %u); dropping. You may have a loop in your configuration.\n", receive->id, packet.hops, receive_max_hops); + log_write('R', receive->id, "Packet exceeded hop limit (%u > %u); dropping. You may have a loop in your configuration.", packet.hops, receive_max_hops); continue; } send_write(&packet); } if (receive->buf.length == BUF_LEN_MAX) { - fprintf(stderr, "R %s: Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking.\n", receive->id); + log_write('R', receive->id, "Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking."); receive_del(receive); return; } @@ -166,7 +167,7 @@ static void receive_new(int fd, void __attribute__((unused)) *passthrough, struc peer_epoll_add((struct peer *) receive, EPOLLIN); - fprintf(stderr, "R %s: New receive connection\n", receive->id); + log_write('R', receive->id, "New receive connection"); } void receive_init() { diff --git a/adsbus/send.c b/adsbus/send.c index d51275d..7a3a44b 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -16,6 +16,7 @@ #include "flow.h" #include "json.h" #include "list.h" +#include "log.h" #include "packet.h" #include "peer.h" #include "proto.h" @@ -89,7 +90,7 @@ static struct serializer { #define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers)) static void send_del(struct send *send) { - fprintf(stderr, "S %s (%s): Connection closed\n", send->id, send->serializer->name); + log_write('S', send->id, "Connection closed"); peer_count_out--; peer_epoll_del((struct peer *) send); assert(!close(send->peer.fd)); @@ -121,7 +122,7 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) { peer_epoll_add((struct peer *) send, 0); - fprintf(stderr, "S %s (%s): New send connection\n", send->id, serializer->name); + log_write('S', send->id, "New send connection: %s", serializer->name); } void send_init() { diff --git a/adsbus/server.c b/adsbus/server.c index 288996f..959b018 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -1,7 +1,6 @@ -#include - #include "uuid.h" +#include "log.h" #include "server.h" uint8_t server_id[UUID_LEN]; @@ -9,5 +8,5 @@ char server_version[] = "https://github.com/flamingcowtv/adsb-tools#1"; void server_init() { uuid_gen(server_id); - fprintf(stderr, "X %s: Server start\n", server_id); + log_write('X', server_id, "Server start"); } diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index ed89abd..73218f0 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include From 352d14935f3daa5afa8c099bb842dc4fbd8c016c Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 6 Mar 2016 18:49:41 -0800 Subject: [PATCH 04/35] Fix leak & logic error in send/receive connections. --- adsbus/send_receive.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adsbus/send_receive.c b/adsbus/send_receive.c index 0285cf2..fce20eb 100644 --- a/adsbus/send_receive.c +++ b/adsbus/send_receive.c @@ -5,6 +5,7 @@ #include "flow.h" #include "list.h" +#include "log.h" #include "peer.h" #include "receive.h" #include "send.h" @@ -54,10 +55,10 @@ static void send_receive_new(int fd, void *passthrough, struct peer *on_close) { send_receive->ref_count = 2; list_add(&send_receive->send_receive_list, &send_receive_head); - flow_new(fd, send_flow, passthrough, on_close); + flow_new(fd, send_flow, passthrough, (struct peer *) send_receive); int fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 0); assert(fd2 >= 0); - flow_new(fd2, receive_flow, NULL, on_close); + flow_new(fd2, receive_flow, NULL, (struct peer *) send_receive); } void send_receive_cleanup() { From c7e1754239c9a98428fc9a3a120ee6c033480116 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 6 Mar 2016 18:50:15 -0800 Subject: [PATCH 05/35] Capture & log child output, rather than sharing our stderr with them. --- adsbus/exec.c | 95 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 69 insertions(+), 26 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index 74851ca..b4292b8 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -19,6 +20,7 @@ struct exec { struct peer peer; + struct peer log_peer; uint8_t id[UUID_LEN]; char *command; struct flow *flow; @@ -31,6 +33,24 @@ static struct list_head exec_head = LIST_HEAD_INIT(exec_head); static void exec_spawn_wrapper(struct peer *); +static void exec_harvest(struct exec *exec) { + int status; + if (exec->child) { + assert(waitpid(exec->child, &status, 0) == exec->child); + exec->child = -1; + if (WIFEXITED(status)) { + log_write('E', exec->id, "Client exited with status %d", WEXITSTATUS(status)); + } else { + assert(WIFSIGNALED(status)); + log_write('E', exec->id, "Client exited with signal %d", WTERMSIG(status)); + } + } + if (exec->log_peer.fd >= 0) { + assert(!close(exec->log_peer.fd)); + exec->log_peer.fd = -1; + } +} + static void exec_del(struct exec *exec) { flow_ref_dec(exec->flow); @@ -38,8 +58,8 @@ static void exec_del(struct exec *exec) { log_write('E', exec->id, "Sending SIGTERM to child process %d", exec->child); // Racy with the process terminating, so don't assert on it kill(exec->child, SIGTERM); - assert(waitpid(exec->child, NULL, 0) == exec->child); } + exec_harvest(exec); list_del(&exec->exec_list); free(exec->command); free(exec); @@ -47,62 +67,85 @@ static void exec_del(struct exec *exec) { static void exec_close_handler(struct peer *peer) { struct exec *exec = (struct exec *) peer; - int status; - assert(waitpid(exec->child, &status, WNOHANG) == exec->child); - exec->child = -1; - if (WIFEXITED(status)) { - log_write('E', exec->id, "Client exited with status %d", WEXITSTATUS(status)); - } else { - assert(WIFSIGNALED(status)); - log_write('E', exec->id, "Client exited with signal %d", WTERMSIG(status)); - } + exec_harvest(exec); uint32_t delay = wakeup_get_retry_delay_ms(1); log_write('E', exec->id, "Will retry in %ds", delay / 1000); exec->peer.event_handler = exec_spawn_wrapper; wakeup_add((struct peer *) exec, delay); } -static void exec_parent(struct exec *exec, pid_t child, int fd) { +static void exec_log_handler(struct peer *peer) { + // Do you believe in magic? + struct exec *exec = container_of(peer, struct exec, log_peer); + + char linebuf[4096]; + ssize_t len = read(exec->log_peer.fd, linebuf, 4096); + if (len <= 0) { + log_write('E', exec->id, "Log input stream closed"); + assert(!close(exec->log_peer.fd)); + exec->log_peer.fd = -1; + return; + } + if (linebuf[len - 1] == '\n') { + len--; + } + log_write('E', exec->id, "(child output) %.*s", len, linebuf); +} + +static void exec_parent(struct exec *exec, pid_t child, int data_fd, int log_fd) { exec->child = child; log_write('E', exec->id, "Child started as process %d", exec->child); + exec->log_peer.fd = log_fd; + exec->log_peer.event_handler = exec_log_handler; + peer_epoll_add(&exec->log_peer, EPOLLIN); + exec->peer.event_handler = exec_close_handler; - if (!flow_new_send_hello(fd, exec->flow, exec->passthrough, (struct peer *) exec)) { + if (!flow_new_send_hello(data_fd, exec->flow, exec->passthrough, (struct peer *) exec)) { exec_close_handler((struct peer *) exec); return; } } -static void __attribute__ ((noreturn)) exec_child(const struct exec *exec, int fd) { +static void __attribute__ ((noreturn)) exec_child(const struct exec *exec, int data_fd, int log_fd) { assert(setsid() != -1); // We leave stderr open from child to parent // Other than that, fds should have CLOEXEC set - if (fd != 0) { - assert(dup2(fd, 0) == 0); + if (data_fd != STDIN_FILENO) { + assert(dup2(data_fd, STDIN_FILENO) == STDIN_FILENO); } - if (fd != 1) { - assert(dup2(fd, 1) == 1); + if (data_fd != STDOUT_FILENO) { + assert(dup2(data_fd, STDOUT_FILENO) == STDOUT_FILENO); } - if (fd != 0 && fd != 1) { - assert(!close(fd)); + if (data_fd != STDIN_FILENO && data_fd != STDOUT_FILENO) { + assert(!close(data_fd)); + } + if (log_fd != STDERR_FILENO) { + assert(dup2(log_fd, STDERR_FILENO) == STDERR_FILENO); + assert(!close(log_fd)); } assert(!execl("/bin/sh", "sh", "-c", exec->command, NULL)); abort(); } static void exec_spawn(struct exec *exec) { - log_write('E', exec->id, "Executing: %s", exec->id, exec->command); - int fds[2]; - assert(!socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, fds)); + log_write('E', exec->id, "Executing: %s", exec->command); + int data_fds[2], log_fds[2]; + // Leave these sockets blocking; we move in lock step with subprograms + assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, data_fds)); + assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, log_fds)); int res = fork(); assert(res >= 0); if (res) { - assert(!close(fds[1])); - exec_parent(exec, res, fds[0]); + assert(!close(data_fds[1])); + assert(!close(log_fds[1])); + assert(!shutdown(log_fds[0], SHUT_WR)); + exec_parent(exec, res, data_fds[0], log_fds[0]); } else { - assert(!close(fds[0])); - exec_child(exec, fds[1]); + assert(!close(data_fds[0])); + assert(!close(log_fds[0])); + exec_child(exec, data_fds[1], log_fds[1]); } } From ca754a817e4123168ac222bb0ae9f62df0d2d5fc Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 6 Mar 2016 18:57:37 -0800 Subject: [PATCH 06/35] Cleaner logging of child output --- adsbus/exec.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index b4292b8..90fa5d3 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -34,8 +34,8 @@ static struct list_head exec_head = LIST_HEAD_INIT(exec_head); static void exec_spawn_wrapper(struct peer *); static void exec_harvest(struct exec *exec) { - int status; - if (exec->child) { + if (exec->child > 0) { + int status; assert(waitpid(exec->child, &status, 0) == exec->child); exec->child = -1; if (WIFEXITED(status)) { @@ -79,17 +79,25 @@ static void exec_log_handler(struct peer *peer) { struct exec *exec = container_of(peer, struct exec, log_peer); char linebuf[4096]; - ssize_t len = read(exec->log_peer.fd, linebuf, 4096); - if (len <= 0) { + ssize_t ret = read(exec->log_peer.fd, linebuf, 4096); + if (ret <= 0) { log_write('E', exec->id, "Log input stream closed"); assert(!close(exec->log_peer.fd)); exec->log_peer.fd = -1; return; } - if (linebuf[len - 1] == '\n') { - len--; + size_t len = (size_t) ret; + char *iter = linebuf, *eol; + while ((eol = memchr(iter, '\n', len))) { + assert(eol >= iter); + size_t linelen = (size_t) (eol - iter); + log_write('E', exec->id, "(child output) %.*s", linelen, iter); + iter += (linelen + 1); + len -= (linelen + 1); + } + if (len) { + log_write('E', exec->id, "(child output) %.*s", len, iter); } - log_write('E', exec->id, "(child output) %.*s", len, linebuf); } static void exec_parent(struct exec *exec, pid_t child, int data_fd, int log_fd) { From 51eaabe27adb82f7ab4b49fe214fd35ab2a031ec Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 6 Mar 2016 22:08:04 -0800 Subject: [PATCH 07/35] Format attributes go in the header, and I are dumb. --- adsbus/exec.c | 4 ++-- adsbus/log.c | 1 - adsbus/log.h | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index 90fa5d3..55d8647 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -91,12 +91,12 @@ static void exec_log_handler(struct peer *peer) { while ((eol = memchr(iter, '\n', len))) { assert(eol >= iter); size_t linelen = (size_t) (eol - iter); - log_write('E', exec->id, "(child output) %.*s", linelen, iter); + log_write('E', exec->id, "(child output) %.*s", (int) linelen, iter); iter += (linelen + 1); len -= (linelen + 1); } if (len) { - log_write('E', exec->id, "(child output) %.*s", len, iter); + log_write('E', exec->id, "(child output) %.*s", (int) len, iter); } } diff --git a/adsbus/log.c b/adsbus/log.c index 1b48144..1671a97 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -19,7 +19,6 @@ void log_cleanup() { assert(!fclose(log_stream)); } -__attribute__ ((__format__ (__printf__, 3, 4))) void log_write(char type, const uint8_t *id, const char *fmt, ...) { va_list ap; va_start(ap, fmt); diff --git a/adsbus/log.h b/adsbus/log.h index 536ff5d..a0d20f5 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -4,4 +4,5 @@ void log_init(void); void log_cleanup(void); -void log_write(char, const uint8_t *, const char *, ...); +void log_write(char, const uint8_t *, const char *, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); From 46696933aa10d77e302d546e2354d8726d0ff9e2 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 11:26:25 -0800 Subject: [PATCH 08/35] 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); From db271db748b3eaed989de80b7a6805b770b9ed62 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 14:52:10 -0800 Subject: [PATCH 09/35] Switch from pipe to socketpair for consistency. --- adsbus/peer.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/adsbus/peer.c b/adsbus/peer.c index db27eac..f7a5961 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include "log.h" @@ -33,7 +35,7 @@ void peer_init() { assert(peer_epoll_fd >= 0); int shutdown_fds[2]; - assert(!pipe2(shutdown_fds, O_CLOEXEC)); + assert(!socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, shutdown_fds)); struct peer *shutdown_peer = malloc(sizeof(*shutdown_peer)); assert(shutdown_peer); From ff4f4da60898e269b4ca14bd6772adab94a60e08 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 15:19:00 -0800 Subject: [PATCH 10/35] deheader cleanup --- adsbus/adsbus.c | 1 - adsbus/airspy_adsb.c | 2 -- adsbus/asyncaddrinfo.c | 3 ++- adsbus/beast.c | 1 - adsbus/exec.c | 4 +--- adsbus/file.c | 4 ---- adsbus/incoming.c | 3 +-- adsbus/json.c | 8 ++++---- adsbus/list.c | 2 -- adsbus/list.h | 5 ++--- adsbus/opts.c | 2 -- adsbus/outgoing.c | 5 ++--- adsbus/peer.c | 4 +--- adsbus/proto.c | 10 ++++------ adsbus/raw.c | 2 -- adsbus/receive.c | 4 +--- adsbus/resolve.c | 2 -- adsbus/send.c | 3 --- adsbus/send_receive.c | 3 --- adsbus/socket.c | 3 ++- adsbus/uuid.c | 2 -- adsbus/wakeup.c | 7 ------- 22 files changed, 20 insertions(+), 60 deletions(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index fbe0895..decf416 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include diff --git a/adsbus/airspy_adsb.c b/adsbus/airspy_adsb.c index af8460e..876bbc1 100644 --- a/adsbus/airspy_adsb.c +++ b/adsbus/airspy_adsb.c @@ -1,11 +1,9 @@ #include -#include #include "buf.h" #include "hex.h" #include "packet.h" #include "receive.h" -#include "uuid.h" #include "airspy_adsb.h" diff --git a/adsbus/asyncaddrinfo.c b/adsbus/asyncaddrinfo.c index c2578c4..c8a8b23 100644 --- a/adsbus/asyncaddrinfo.c +++ b/adsbus/asyncaddrinfo.c @@ -4,8 +4,9 @@ #include #include #include -#include #include +#include +#include #include #include "asyncaddrinfo.h" diff --git a/adsbus/beast.c b/adsbus/beast.c index 700adc0..31c2846 100644 --- a/adsbus/beast.c +++ b/adsbus/beast.c @@ -1,6 +1,5 @@ #include #include -#include #include "buf.h" #include "packet.h" diff --git a/adsbus/exec.c b/adsbus/exec.c index 59c77d9..192cc51 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -1,16 +1,14 @@ #include #include -#include #include #include #include +#include #include #include #include -#include "buf.h" #include "flow.h" -#include "list.h" #include "log.h" #include "peer.h" #include "uuid.h" diff --git a/adsbus/file.c b/adsbus/file.c index 9af060e..061932c 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -3,17 +3,13 @@ #include #include #include -#include #include #include #include #include "flow.h" -#include "list.h" #include "log.h" #include "peer.h" -#include "receive.h" -#include "send.h" #include "uuid.h" #include "wakeup.h" diff --git a/adsbus/incoming.c b/adsbus/incoming.c index c2c3dff..41144c0 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -5,11 +5,10 @@ #include #include #include +#include #include -#include "buf.h" #include "flow.h" -#include "list.h" #include "log.h" #include "peer.h" #include "resolve.h" diff --git a/adsbus/json.c b/adsbus/json.c index fb2bfa1..0200951 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -1,15 +1,15 @@ #include +#include #include #include #include "hex.h" #include "buf.h" +#include "log.h" #include "packet.h" #include "rand.h" #include "receive.h" -#include "send.h" #include "server.h" -#include "uuid.h" #include "json.h" @@ -79,11 +79,11 @@ static bool json_parse_header(json_t *in, struct packet *packet, struct json_par } if (!strcmp(json_server_id, (const char *) server_id)) { - fprintf(stderr, "R %s: Attempt to receive json data from our own server ID (%s); loop!\n", packet->source_id, server_id); + log_write('R', packet->source_id, "Attempt to receive json data from our own server ID (%s); loop!", server_id); return false; } - fprintf(stderr, "R %s: Connected to server ID: %s\n", packet->source_id, json_server_id); + log_write('R', packet->source_id, "Connected to server ID: %s", json_server_id); state->mlat_timestamp_mhz = (uint16_t) mlat_timestamp_mhz; state->mlat_timestamp_max = (uint64_t) mlat_timestamp_max; diff --git a/adsbus/list.c b/adsbus/list.c index f8c2873..e37101b 100644 --- a/adsbus/list.c +++ b/adsbus/list.c @@ -1,5 +1,3 @@ -#include - #include "list.h" void list_head_init(struct list_head *head) { diff --git a/adsbus/list.h b/adsbus/list.h index fb9af51..c4f4433 100644 --- a/adsbus/list.h +++ b/adsbus/list.h @@ -1,16 +1,15 @@ #pragma once #include +#include #pragma GCC diagnostic ignored "-Wcast-align" #pragma GCC diagnostic ignored "-Wgnu-statement-expression" #pragma GCC diagnostic ignored "-Wlanguage-extension-token" -#define offset_of(type, member) ((size_t) &((type *) NULL)->member) - #define container_of(ptr, type, member) ({ \ typeof( ((type *) NULL)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offset_of(type, member) );}) + (type *)( (char *)__mptr - offsetof(type, member) );}) struct list_head { struct list_head *next; diff --git a/adsbus/opts.c b/adsbus/opts.c index 22f855e..822fa5f 100644 --- a/adsbus/opts.c +++ b/adsbus/opts.c @@ -2,8 +2,6 @@ #include #include #include -#include -#include #include #include "exec.h" diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 20c47fc..b9bd9c3 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -3,17 +3,16 @@ #include #include #include -#include #include +#include +#include #include #include "buf.h" #include "flow.h" -#include "list.h" #include "log.h" #include "peer.h" #include "resolve.h" -#include "socket.h" #include "wakeup.h" #include "uuid.h" diff --git a/adsbus/peer.c b/adsbus/peer.c index f7a5961..c6e6b11 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -1,17 +1,15 @@ #include #include -#include #include #include #include -#include #include +#include #include #include #include "log.h" #include "server.h" -#include "uuid.h" #include "wakeup.h" #include "peer.h" diff --git a/adsbus/proto.c b/adsbus/proto.c index 578d4d9..be7dcb8 100644 --- a/adsbus/proto.c +++ b/adsbus/proto.c @@ -1,19 +1,17 @@ -#pragma GCC diagnostic ignored "-Wcast-qual" -#pragma GCC diagnostic ignored "-Wpacked" - -#include -#include +#include #include #include "buf.h" #include "log.h" #include "packet.h" #include "server.h" -#include "uuid.h" #include "adsb.pb-c.h" #include "proto.h" +#pragma GCC diagnostic ignored "-Wcast-qual" +#pragma GCC diagnostic ignored "-Wpacked" + #define PROTO_MAGIC "aDsB" struct proto_parser_state { diff --git a/adsbus/raw.c b/adsbus/raw.c index f721d66..b176b06 100644 --- a/adsbus/raw.c +++ b/adsbus/raw.c @@ -1,10 +1,8 @@ #include -#include #include "buf.h" #include "hex.h" #include "packet.h" -#include "uuid.h" #include "raw.h" diff --git a/adsbus/receive.c b/adsbus/receive.c index ed42cc5..6e087ed 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -1,10 +1,9 @@ #include -#include #include #include #include -#include #include +#include #include #include "airspy_adsb.h" @@ -12,7 +11,6 @@ #include "buf.h" #include "flow.h" #include "json.h" -#include "list.h" #include "log.h" #include "packet.h" #include "peer.h" diff --git a/adsbus/resolve.c b/adsbus/resolve.c index 35d4864..57f05c6 100644 --- a/adsbus/resolve.c +++ b/adsbus/resolve.c @@ -1,5 +1,3 @@ -#include -#include #include #include "asyncaddrinfo.h" diff --git a/adsbus/send.c b/adsbus/send.c index 121e936..c8f2ff1 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -1,7 +1,5 @@ #include -#include #include -#include #include #include #include @@ -15,7 +13,6 @@ #include "buf.h" #include "flow.h" #include "json.h" -#include "list.h" #include "log.h" #include "packet.h" #include "peer.h" diff --git a/adsbus/send_receive.c b/adsbus/send_receive.c index fce20eb..408b600 100644 --- a/adsbus/send_receive.c +++ b/adsbus/send_receive.c @@ -1,11 +1,8 @@ #include #include #include -#include #include "flow.h" -#include "list.h" -#include "log.h" #include "peer.h" #include "receive.h" #include "send.h" diff --git a/adsbus/socket.c b/adsbus/socket.c index 207460e..7782ded 100644 --- a/adsbus/socket.c +++ b/adsbus/socket.c @@ -2,8 +2,9 @@ #include #include #include -#include #include +#include +#include #include "socket.h" diff --git a/adsbus/uuid.c b/adsbus/uuid.c index 792bf49..b5c0bca 100644 --- a/adsbus/uuid.c +++ b/adsbus/uuid.c @@ -1,5 +1,3 @@ -#include - #include "hex.h" #include "rand.h" diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index 73218f0..5360a27 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -1,15 +1,8 @@ #include -#include -#include #include #include -#include -#include -#include #include -#include -#include "list.h" #include "peer.h" #include "rand.h" From 602c6f729b40c09ae9d39df8436c1bdc1d0c0690 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 16:14:35 -0800 Subject: [PATCH 11/35] Add log rotation on SIGHUP --- adsbus/adsbus.c | 2 ++ adsbus/log.c | 32 ++++++++++++++++++++++++++++++++ adsbus/log.h | 1 + 3 files changed, 35 insertions(+) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index decf416..06a4888 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -194,6 +194,8 @@ int main(int argc, char *argv[]) { wakeup_init(); peer_init(); + log_init2(); + receive_init(); send_init(); diff --git a/adsbus/log.c b/adsbus/log.c index f54d0a2..8c6a2f0 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -1,11 +1,15 @@ #include +#include #include #include #include #include +#include +#include #include #include "log.h" +#include "peer.h" #include "uuid.h" #pragma GCC diagnostic ignored "-Wformat-nonliteral" @@ -13,6 +17,8 @@ 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 void log_rotate() { uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN]; @@ -23,9 +29,21 @@ static void log_rotate() { assert(!fclose(log_stream)); log_stream = fopen(log_path, "a"); assert(log_stream); + setlinebuf(log_stream); log_write('L', log_id, "Log start after switch from log ID %s", old_log_id); } +static void log_rotate_handler(struct peer *peer) { + char buf[1]; + assert(read(peer->fd, buf, 1) == 1); + assert(buf[0] == 'L'); + log_rotate(); +} + +static void log_rotate_signal(int __attribute__ ((unused)) signum) { + assert(write(log_rotate_fd, "L", 1) == 1); +} + void log_init() { int fd = dup(STDERR_FILENO); assert(fd >= 0); @@ -37,9 +55,23 @@ void log_init() { log_write('L', 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)); + 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); +} + void log_cleanup() { log_write('L', 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; diff --git a/adsbus/log.h b/adsbus/log.h index 1dedca3..fb21b93 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -4,6 +4,7 @@ #include void log_init(void); +void log_init2(void); void log_cleanup(void); bool log_reopen(const char *); void log_write(char, const uint8_t *, const char *, ...) From b434b67dd85c37a8e3307d6286dbf509837027b5 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 17:02:24 -0800 Subject: [PATCH 12/35] Switch to LOG macro --- adsbus/exec.c | 20 +++++++++++--------- adsbus/file.c | 12 +++++++----- adsbus/incoming.c | 20 +++++++++++--------- adsbus/json.c | 6 ++++-- adsbus/log.c | 10 ++++++---- adsbus/log.h | 2 ++ adsbus/outgoing.c | 18 ++++++++++-------- adsbus/peer.c | 10 ++++++---- adsbus/proto.c | 6 ++++-- adsbus/receive.c | 12 +++++++----- adsbus/send.c | 6 ++++-- adsbus/server.c | 4 +++- 12 files changed, 75 insertions(+), 51 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index 192cc51..9531045 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -29,6 +29,8 @@ struct exec { static struct list_head exec_head = LIST_HEAD_INIT(exec_head); +static char log_module = 'E'; + static void exec_spawn_wrapper(struct peer *); static void exec_harvest(struct exec *exec) { @@ -37,10 +39,10 @@ static void exec_harvest(struct exec *exec) { assert(waitpid(exec->child, &status, 0) == exec->child); exec->child = -1; if (WIFEXITED(status)) { - log_write('E', exec->id, "Client exited with status %d", WEXITSTATUS(status)); + LOG(exec->id, "Client exited with status %d", WEXITSTATUS(status)); } else { assert(WIFSIGNALED(status)); - log_write('E', exec->id, "Client exited with signal %d", WTERMSIG(status)); + LOG(exec->id, "Client exited with signal %d", WTERMSIG(status)); } } if (exec->log_peer.fd >= 0) { @@ -53,7 +55,7 @@ static void exec_del(struct exec *exec) { flow_ref_dec(exec->flow); if (exec->child > 0) { - log_write('E', exec->id, "Sending SIGTERM to child process %d", exec->child); + LOG(exec->id, "Sending SIGTERM to child process %d", exec->child); // Racy with the process terminating, so don't assert on it kill(exec->child, SIGTERM); } @@ -67,7 +69,7 @@ static void exec_close_handler(struct peer *peer) { struct exec *exec = (struct exec *) peer; exec_harvest(exec); uint32_t delay = wakeup_get_retry_delay_ms(1); - log_write('E', exec->id, "Will retry in %ds", delay / 1000); + LOG(exec->id, "Will retry in %ds", delay / 1000); exec->peer.event_handler = exec_spawn_wrapper; wakeup_add((struct peer *) exec, delay); } @@ -79,7 +81,7 @@ static void exec_log_handler(struct peer *peer) { char linebuf[4096]; ssize_t ret = read(exec->log_peer.fd, linebuf, 4096); if (ret <= 0) { - log_write('E', exec->id, "Log input stream closed"); + LOG(exec->id, "Log input stream closed"); assert(!close(exec->log_peer.fd)); exec->log_peer.fd = -1; return; @@ -89,18 +91,18 @@ static void exec_log_handler(struct peer *peer) { while ((eol = memchr(iter, '\n', len))) { assert(eol >= iter); size_t linelen = (size_t) (eol - iter); - log_write('E', exec->id, "(child output) %.*s", (int) linelen, iter); + LOG(exec->id, "(child output) %.*s", (int) linelen, iter); iter += (linelen + 1); len -= (linelen + 1); } if (len) { - log_write('E', exec->id, "(child output) %.*s", (int) len, iter); + LOG(exec->id, "(child output) %.*s", (int) len, iter); } } static void exec_parent(struct exec *exec, pid_t child, int data_fd, int log_fd) { exec->child = child; - log_write('E', exec->id, "Child started as process %d", exec->child); + LOG(exec->id, "Child started as process %d", exec->child); exec->log_peer.fd = log_fd; exec->log_peer.event_handler = exec_log_handler; @@ -135,7 +137,7 @@ static void __attribute__ ((noreturn)) exec_child(const struct exec *exec, int d } static void exec_spawn(struct exec *exec) { - log_write('E', exec->id, "Executing: %s", exec->command); + LOG(exec->id, "Executing: %s", exec->command); int data_fds[2], log_fds[2]; // Leave these sockets blocking; we move in lock step with subprograms assert(!socketpair(AF_UNIX, SOCK_STREAM, 0, data_fds)); diff --git a/adsbus/file.c b/adsbus/file.c index 061932c..73f4017 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -29,6 +29,8 @@ struct file { static struct list_head file_head = LIST_HEAD_INIT(file_head); +static char log_module = 'F'; + static void file_open_wrapper(struct peer *); static bool file_should_retry(int fd, struct file *file) { @@ -63,14 +65,14 @@ static void file_del(struct file *file) { static void file_retry(struct file *file) { uint32_t delay = wakeup_get_retry_delay_ms(file->attempt++); - log_write('F', file->id, "Will retry in %ds", delay / 1000); + LOG(file->id, "Will retry in %ds", delay / 1000); file->peer.event_handler = file_open_wrapper; wakeup_add((struct peer *) file, delay); } static void file_handle_close(struct peer *peer) { struct file *file = (struct file *) peer; - log_write('F', file->id, "File closed: %s", file->path); + LOG(file->id, "File closed: %s", file->path); if (file->retry) { file_retry(file); @@ -80,10 +82,10 @@ static void file_handle_close(struct peer *peer) { } static void file_open(struct file *file) { - log_write('F', file->id, "Opening file: %s", file->path); + LOG(file->id, "Opening file: %s", file->path); int fd = open(file->path, file->flags | O_CLOEXEC, S_IRUSR | S_IWUSR); if (fd == -1) { - log_write('F', file->id, "Error opening file: %s", strerror(errno)); + LOG(file->id, "Error opening file: %s", strerror(errno)); file_retry(file); return; } @@ -92,7 +94,7 @@ static void file_open(struct file *file) { file->peer.event_handler = file_handle_close; file->attempt = 0; if (!flow_new_send_hello(fd, file->flow, file->passthrough, (struct peer *) file)) { - log_write('F', file->id, "Error writing greeting"); + LOG(file->id, "Error writing greeting"); file_retry(file); return; } diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 41144c0..6a159b6 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -31,11 +31,13 @@ struct incoming { static struct list_head incoming_head = LIST_HEAD_INIT(incoming_head); +static char log_module = 'I'; + static void incoming_resolve_wrapper(struct peer *); static void incoming_retry(struct incoming *incoming) { uint32_t delay = wakeup_get_retry_delay_ms(incoming->attempt++); - log_write('I', incoming->id, "Will retry in %ds", delay / 1000); + LOG(incoming->id, "Will retry in %ds", delay / 1000); incoming->peer.event_handler = incoming_resolve_wrapper; wakeup_add((struct peer *) incoming, delay); } @@ -48,7 +50,7 @@ static void incoming_handler(struct peer *peer) { int fd = accept4(incoming->peer.fd, &peer_addr, &peer_addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); if (fd == -1) { - log_write('I', incoming->id, "Failed to accept new connection on %s/%s: %s", incoming->node, incoming->service, strerror(errno)); + LOG(incoming->id, "Failed to accept new connection on %s/%s: %s", incoming->node, incoming->service, strerror(errno)); return; } @@ -57,7 +59,7 @@ static void incoming_handler(struct peer *peer) { assert(getnameinfo(&peer_addr, peer_addrlen, peer_hbuf, sizeof(peer_hbuf), peer_sbuf, sizeof(peer_sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); assert(getnameinfo(&local_addr, local_addrlen, local_hbuf, sizeof(local_hbuf), local_sbuf, sizeof(local_sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); - log_write('I', incoming->id, "New incoming connection on %s/%s (%s/%s) from %s/%s", + LOG(incoming->id, "New incoming connection on %s/%s (%s/%s) from %s/%s", incoming->node, incoming->service, local_hbuf, local_sbuf, peer_hbuf, peer_sbuf); @@ -65,7 +67,7 @@ static void incoming_handler(struct peer *peer) { flow_socket_connected(fd, incoming->flow); if (!flow_new_send_hello(fd, incoming->flow, incoming->passthrough, NULL)) { - log_write('I', incoming->id, "Error writing greeting"); + LOG(incoming->id, "Error writing greeting"); return; } } @@ -87,7 +89,7 @@ static void incoming_listen(struct peer *peer) { struct addrinfo *addrs; int err = resolve_result(peer, &addrs); if (err) { - log_write('I', incoming->id, "Failed to resolve %s/%s: %s", incoming->node, incoming->service, gai_strerror(err)); + LOG(incoming->id, "Failed to resolve %s/%s: %s", incoming->node, incoming->service, gai_strerror(err)); incoming_retry(incoming); return; } @@ -96,7 +98,7 @@ static void incoming_listen(struct peer *peer) { for (addr = addrs; addr; addr = addr->ai_next) { char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; assert(getnameinfo(addr->ai_addr, addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); - log_write('I', incoming->id, "Listening on %s/%s...", hbuf, sbuf); + LOG(incoming->id, "Listening on %s/%s...", hbuf, sbuf); incoming->peer.fd = socket(addr->ai_family, addr->ai_socktype | SOCK_CLOEXEC, addr->ai_protocol); assert(incoming->peer.fd >= 0); @@ -104,7 +106,7 @@ static void incoming_listen(struct peer *peer) { socket_pre_bind(incoming->peer.fd); if (bind(incoming->peer.fd, addr->ai_addr, addr->ai_addrlen) != 0) { - log_write('I', incoming->id, "Failed to bind to %s/%s: %s", hbuf, sbuf, strerror(errno)); + LOG(incoming->id, "Failed to bind to %s/%s: %s", hbuf, sbuf, strerror(errno)); assert(!close(incoming->peer.fd)); continue; } @@ -120,7 +122,7 @@ static void incoming_listen(struct peer *peer) { freeaddrinfo(addrs); if (addr == NULL) { - log_write('I', incoming->id, "Failed to bind any addresses for %s/%s...", incoming->node, incoming->service); + LOG(incoming->id, "Failed to bind any addresses for %s/%s...", incoming->node, incoming->service); incoming_retry(incoming); return; } @@ -131,7 +133,7 @@ static void incoming_listen(struct peer *peer) { } static void incoming_resolve(struct incoming *incoming) { - log_write('I', incoming->id, "Resolving %s/%s...", incoming->node, incoming->service); + LOG(incoming->id, "Resolving %s/%s...", incoming->node, incoming->service); incoming->peer.event_handler = incoming_listen; resolve((struct peer *) incoming, incoming->node, incoming->service, AI_PASSIVE); } diff --git a/adsbus/json.c b/adsbus/json.c index 0200951..bc6f7de 100644 --- a/adsbus/json.c +++ b/adsbus/json.c @@ -26,6 +26,8 @@ struct json_parser_state { static json_t *json_prev = NULL; static struct buf json_hello_buf = BUF_INIT; +static char log_module = 'R'; // borrowing + static void json_serialize_to_buf(json_t *obj, struct buf *buf) { assert(json_dump_callback(obj, json_buf_append_callback, buf, 0) == 0); json_decref(obj); @@ -79,11 +81,11 @@ static bool json_parse_header(json_t *in, struct packet *packet, struct json_par } if (!strcmp(json_server_id, (const char *) server_id)) { - log_write('R', packet->source_id, "Attempt to receive json data from our own server ID (%s); loop!", server_id); + LOG(packet->source_id, "Attempt to receive json data from our own server ID (%s); loop!", server_id); return false; } - log_write('R', packet->source_id, "Connected to server ID: %s", json_server_id); + LOG(packet->source_id, "Connected to server ID: %s", json_server_id); state->mlat_timestamp_mhz = (uint16_t) mlat_timestamp_mhz; state->mlat_timestamp_max = (uint64_t) mlat_timestamp_max; diff --git a/adsbus/log.c b/adsbus/log.c index 8c6a2f0..42709f9 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -20,17 +20,19 @@ static uint8_t log_id[UUID_LEN]; static int log_rotate_fd; static struct peer log_rotate_peer; +static char log_module = 'L'; + 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); + LOG(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); setlinebuf(log_stream); - log_write('L', log_id, "Log start after switch from log ID %s", old_log_id); + LOG(log_id, "Log start after switch from log ID %s", old_log_id); } static void log_rotate_handler(struct peer *peer) { @@ -52,7 +54,7 @@ void log_init() { setlinebuf(log_stream); uuid_gen(log_id); - log_write('L', log_id, "Log start"); + LOG(log_id, "Log start"); } void log_init2() { @@ -68,7 +70,7 @@ void log_init2() { } void log_cleanup() { - log_write('L', log_id, "Log end"); + LOG(log_id, "Log end"); assert(!fclose(log_stream)); assert(!close(log_rotate_fd)); assert(!close(log_rotate_peer.fd)); diff --git a/adsbus/log.h b/adsbus/log.h index fb21b93..74682b4 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -3,6 +3,8 @@ #include #include +#define LOG(id, ...) log_write((log_module), (id), __VA_ARGS__) + void log_init(void); void log_init2(void); void log_cleanup(void); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index b9bd9c3..c6f90ca 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -33,13 +33,15 @@ struct outgoing { static struct list_head outgoing_head = LIST_HEAD_INIT(outgoing_head); +static char log_module = 'O'; + static void outgoing_connect_result(struct outgoing *, int); static void outgoing_resolve(struct outgoing *); static void outgoing_resolve_wrapper(struct peer *); static void outgoing_retry(struct outgoing *outgoing) { uint32_t delay = wakeup_get_retry_delay_ms(++outgoing->attempt); - log_write('O', outgoing->id, "Will retry in %ds", delay / 1000); + LOG(outgoing->id, "Will retry in %ds", delay / 1000); outgoing->peer.event_handler = outgoing_resolve_wrapper; wakeup_add((struct peer *) outgoing, delay); } @@ -47,14 +49,14 @@ static void outgoing_retry(struct outgoing *outgoing) { static void outgoing_connect_next(struct outgoing *outgoing) { if (outgoing->addr == NULL) { freeaddrinfo(outgoing->addrs); - log_write('O', outgoing->id, "Can't connect to any addresses of %s/%s", outgoing->node, outgoing->service); + LOG(outgoing->id, "Can't connect to any addresses of %s/%s", outgoing->node, outgoing->service); outgoing_retry(outgoing); return; } char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; assert(getnameinfo(outgoing->addr->ai_addr, outgoing->addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); - log_write('O', outgoing->id, "Connecting to %s/%s...", hbuf, sbuf); + LOG(outgoing->id, "Connecting to %s/%s...", hbuf, sbuf); outgoing->peer.fd = socket(outgoing->addr->ai_family, outgoing->addr->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC, outgoing->addr->ai_protocol); assert(outgoing->peer.fd >= 0); @@ -81,7 +83,7 @@ static void outgoing_disconnect_handler(struct peer *peer) { if (outgoing->peer.fd != -1) { assert(!close(outgoing->peer.fd)); } - log_write('O', outgoing->id, "Peer disconnected; reconnecting..."); + LOG(outgoing->id, "Peer disconnected; reconnecting..."); outgoing_retry(outgoing); } @@ -90,7 +92,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { assert(getnameinfo(outgoing->addr->ai_addr, outgoing->addr->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0); switch (result) { case 0: - log_write('O', outgoing->id, "Connected to %s/%s", hbuf, sbuf); + LOG(outgoing->id, "Connected to %s/%s", hbuf, sbuf); freeaddrinfo(outgoing->addrs); outgoing->attempt = 0; int fd = outgoing->peer.fd; @@ -107,7 +109,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { break; default: - log_write('O', outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result)); + LOG(outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result)); assert(!close(outgoing->peer.fd)); outgoing->peer.fd = -1; outgoing->addr = outgoing->addr->ai_next; @@ -121,7 +123,7 @@ static void outgoing_resolve_handler(struct peer *peer) { struct outgoing *outgoing = (struct outgoing *) peer; int err = resolve_result(peer, &outgoing->addrs); if (err) { - log_write('O', outgoing->id, "Failed to resolve %s/%s: %s", outgoing->node, outgoing->service, gai_strerror(err)); + LOG(outgoing->id, "Failed to resolve %s/%s: %s", outgoing->node, outgoing->service, gai_strerror(err)); outgoing_retry(outgoing); } else { outgoing->addr = outgoing->addrs; @@ -130,7 +132,7 @@ static void outgoing_resolve_handler(struct peer *peer) { } static void outgoing_resolve(struct outgoing *outgoing) { - log_write('O', outgoing->id, "Resolving %s/%s...", outgoing->node, outgoing->service); + LOG(outgoing->id, "Resolving %s/%s...", outgoing->node, outgoing->service); outgoing->peer.event_handler = outgoing_resolve_handler; resolve((struct peer *) outgoing, outgoing->node, outgoing->service, 0); } diff --git a/adsbus/peer.c b/adsbus/peer.c index c6e6b11..2f3e66e 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -14,6 +14,8 @@ #include "peer.h" +static char log_module = 'X'; + uint32_t peer_count_in = 0, peer_count_out = 0, peer_count_out_in = 0; static int peer_epoll_fd; @@ -22,7 +24,7 @@ static bool peer_shutdown_flag = false; static struct list_head peer_always_trigger_head = LIST_HEAD_INIT(peer_always_trigger_head); static void peer_shutdown_handler(struct peer *peer) { - log_write('X', server_id, "Shutting down"); + LOG(server_id, "Shutting down"); assert(!close(peer->fd)); free(peer); peer_shutdown_flag = true; @@ -96,13 +98,13 @@ void peer_call(struct peer *peer) { } void peer_loop() { - log_write('X', server_id, "Starting event loop"); + LOG(server_id, "Starting event loop"); while (!peer_shutdown_flag) { if (!(peer_count_in + peer_count_out_in)) { - log_write('X', server_id, "No remaining inputs"); + LOG(server_id, "No remaining inputs"); peer_shutdown(0); } else if (!(peer_count_out + peer_count_out_in)) { - log_write('X', server_id, "No remaining outputs"); + LOG(server_id, "No remaining outputs"); peer_shutdown(0); } #define MAX_EVENTS 10 diff --git a/adsbus/proto.c b/adsbus/proto.c index be7dcb8..36a6cf9 100644 --- a/adsbus/proto.c +++ b/adsbus/proto.c @@ -22,6 +22,8 @@ struct proto_parser_state { bool have_header; }; +static char log_module = 'R'; // borrowing + static Adsb *proto_prev = NULL; static struct buf proto_hello_buf = BUF_INIT; @@ -143,12 +145,12 @@ static bool proto_parse_header(AdsbHeader *header, struct packet *packet, struct state->rssi_max = header->rssi_max; if (!strcmp(header->server_id, (const char *) server_id)) { - log_write('R', packet->source_id, "Attempt to receive proto data from our own server ID (%s); loop!", server_id); + LOG(packet->source_id, "Attempt to receive proto data from our own server ID (%s); loop!", server_id); return false; } state->have_header = true; - log_write('R', packet->source_id, "Connected to server ID: %s", header->server_id); + LOG(packet->source_id, "Connected to server ID: %s", header->server_id); return true; } diff --git a/adsbus/receive.c b/adsbus/receive.c index 6e087ed..95dbc3b 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -38,6 +38,8 @@ struct receive { }; static struct list_head receive_head = LIST_HEAD_INIT(receive_head); +static char log_module = 'R'; + static void receive_new(int, void *, struct peer *); static struct flow _receive_flow = { @@ -91,7 +93,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac for (size_t i = 0; i < NUM_PARSERS; i++) { if (parsers[i].parse(buf, packet, state)) { - log_write('R', receive->id, "Detected input format: %s", parsers[i].name); + LOG(receive->id, "Detected input format: %s", parsers[i].name); receive->parser_wrapper = receive_parse_wrapper; receive->parser = parsers[i].parse; return true; @@ -104,7 +106,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac } static void receive_del(struct receive *receive) { - log_write('R', receive->id, "Connection closed"); + LOG(receive->id, "Connection closed"); peer_count_in--; peer_epoll_del((struct peer *) receive); assert(!close(receive->peer.fd)); @@ -133,14 +135,14 @@ static void receive_read(struct peer *peer) { continue; } if (++packet.hops > receive_max_hops) { - log_write('R', receive->id, "Packet exceeded hop limit (%u > %u); dropping. You may have a loop in your configuration.", packet.hops, receive_max_hops); + LOG(receive->id, "Packet exceeded hop limit (%u > %u); dropping. You may have a loop in your configuration.", packet.hops, receive_max_hops); continue; } send_write(&packet); } if (receive->buf.length == BUF_LEN_MAX) { - log_write('R', receive->id, "Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking."); + LOG(receive->id, "Input buffer overrun. This probably means that adsbus doesn't understand the protocol that this source is speaking."); receive_del(receive); return; } @@ -165,7 +167,7 @@ static void receive_new(int fd, void __attribute__((unused)) *passthrough, struc peer_epoll_add((struct peer *) receive, EPOLLIN); - log_write('R', receive->id, "New receive connection"); + LOG(receive->id, "New receive connection"); } void receive_init() { diff --git a/adsbus/send.c b/adsbus/send.c index c8f2ff1..d660ea6 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -45,6 +45,8 @@ static struct flow _send_flow = { }; struct flow *send_flow = &_send_flow; +static char log_module = 'S'; + typedef void (*serialize)(struct packet *, struct buf *); typedef void (*hello)(struct buf **); static struct serializer { @@ -87,7 +89,7 @@ static struct serializer { #define NUM_SERIALIZERS (sizeof(serializers) / sizeof(*serializers)) static void send_del(struct send *send) { - log_write('S', send->id, "Connection closed"); + LOG(send->id, "Connection closed"); peer_count_out--; peer_epoll_del((struct peer *) send); assert(!close(send->peer.fd)); @@ -119,7 +121,7 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) { peer_epoll_add((struct peer *) send, 0); - log_write('S', send->id, "New send connection: %s", serializer->name); + LOG(send->id, "New send connection: %s", serializer->name); } void send_init() { diff --git a/adsbus/server.c b/adsbus/server.c index 959b018..f154304 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -6,7 +6,9 @@ uint8_t server_id[UUID_LEN]; char server_version[] = "https://github.com/flamingcowtv/adsb-tools#1"; +static char log_module = 'X'; + void server_init() { uuid_gen(server_id); - log_write('X', server_id, "Server start"); + LOG(server_id, "Server start"); } From 3ef52b16b565e5342356aee31a74e06a4bca854e Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 17:08:36 -0800 Subject: [PATCH 13/35] Log file and line number. --- adsbus/log.c | 4 ++-- adsbus/log.h | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/adsbus/log.c b/adsbus/log.c index 42709f9..fb000fb 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -90,10 +90,10 @@ bool log_reopen(const char *path) { return true; } -void log_write(char type, const uint8_t *id, const char *fmt, ...) { +void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - assert(fprintf(log_stream, "%c %s: ", type, id) > 0); + assert(fprintf(log_stream, "%c [%20s] %s: ", type, loc, id) > 0); assert(vfprintf(log_stream, fmt, ap) > 0); assert(fprintf(log_stream, "\n") == 1); va_end(ap); diff --git a/adsbus/log.h b/adsbus/log.h index 74682b4..47002da 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -3,11 +3,13 @@ #include #include -#define LOG(id, ...) log_write((log_module), (id), __VA_ARGS__) +#define LOG_STR(line) #line +#define LOG_LOC(file, line) (file ":" LOG_STR(line)) +#define LOG(id, ...) log_write((log_module), LOG_LOC(__FILE__, __LINE__), (id), __VA_ARGS__) void log_init(void); void log_init2(void); void log_cleanup(void); bool log_reopen(const char *); -void log_write(char, const uint8_t *, const char *, ...) - __attribute__ ((__format__ (__printf__, 3, 4))); +void log_write(char, const char *, const uint8_t *, const char *, ...) + __attribute__ ((__format__ (__printf__, 4, 5))); From b7de1adca2f52ab2de64c2ed52a73dfb2bb6d239 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 17:18:23 -0800 Subject: [PATCH 14/35] Start to generate version.h --- adsbus/.gitignore | 3 +++ adsbus/Makefile | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/adsbus/.gitignore b/adsbus/.gitignore index 55c3eac..e4375ec 100644 --- a/adsbus/.gitignore +++ b/adsbus/.gitignore @@ -33,3 +33,6 @@ # Binaries adsbus + +# Generated +version.h diff --git a/adsbus/Makefile b/adsbus/Makefile index 61b62c4..b9953dd 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -24,6 +24,10 @@ clean: %.o: %.c *.h $(COMP) -c $(CFLAGS) $< -o $@ +version.h: + echo "#define GIT_LAST_CHANGE \"$$(git log --format=%H -n 1)\"" > version.h + echo "#define GIT_LOCAL_CLEAN $$(git diff --exit-code > /dev/null && echo true || echo false)" >> version.h + adsb.pb-c.c: ../proto/adsb.proto protoc-c --c_out=./ --proto_path=$(dir $<) $< From 31bb8f2a950a796eb310acdea9c15f788e57e8ab Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 17:18:51 -0800 Subject: [PATCH 15/35] Remove version.h on make clean --- adsbus/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adsbus/Makefile b/adsbus/Makefile index b9953dd..85947a5 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -19,7 +19,7 @@ OBJ_PROTO = adsb.pb-c.o all: adsbus clean: - rm -rf *.o adsbus testout findings + rm -rf *.o adsbus testout findings version.h %.o: %.c *.h $(COMP) -c $(CFLAGS) $< -o $@ From 3bcc2b43bad60adfc605104be5a017762f818b33 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 17:38:18 -0800 Subject: [PATCH 16/35] Log more debug data. --- adsbus/.gitignore | 2 +- adsbus/Makefile | 12 +++++++----- adsbus/log.c | 2 +- adsbus/server.c | 19 +++++++++++++++++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/adsbus/.gitignore b/adsbus/.gitignore index e4375ec..14d0f0c 100644 --- a/adsbus/.gitignore +++ b/adsbus/.gitignore @@ -35,4 +35,4 @@ adsbus # Generated -version.h +build.h diff --git a/adsbus/Makefile b/adsbus/Makefile index 85947a5..c311a8b 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -19,14 +19,16 @@ OBJ_PROTO = adsb.pb-c.o all: adsbus clean: - rm -rf *.o adsbus testout findings version.h + rm -rf *.o adsbus testout findings build.h -%.o: %.c *.h +%.o: %.c *.h build.h $(COMP) -c $(CFLAGS) $< -o $@ -version.h: - echo "#define GIT_LAST_CHANGE \"$$(git log --format=%H -n 1)\"" > version.h - echo "#define GIT_LOCAL_CLEAN $$(git diff --exit-code > /dev/null && echo true || echo false)" >> version.h +build.h: + echo "#define GIT_LAST_CHANGE \"$$(git log --format=%H -n 1)\"" > $@ + echo "#define GIT_LOCAL_CLEAN $$(git diff --exit-code > /dev/null && echo true || echo false)" >> $@ + echo "#define HOSTNAME \"$$(hostname --fqdn)\"" >> $@ + echo "#define USERNAME \"$$(whoami)\"" >> $@ adsb.pb-c.c: ../proto/adsb.proto protoc-c --c_out=./ --proto_path=$(dir $<) $< diff --git a/adsbus/log.c b/adsbus/log.c index fb000fb..cfc7595 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -93,7 +93,7 @@ bool log_reopen(const char *path) { void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - assert(fprintf(log_stream, "%c [%20s] %s: ", type, loc, id) > 0); + assert(fprintf(log_stream, "%c [%18s] %s: ", type, loc, id) > 0); assert(vfprintf(log_stream, fmt, ap) > 0); assert(fprintf(log_stream, "\n") == 1); va_end(ap); diff --git a/adsbus/server.c b/adsbus/server.c index f154304..4c0bf68 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -1,8 +1,14 @@ +#include +#include + +#include "build.h" +#include "log.h" #include "uuid.h" -#include "log.h" #include "server.h" +#pragma GCC diagnostic ignored "-Wdate-time" + uint8_t server_id[UUID_LEN]; char server_version[] = "https://github.com/flamingcowtv/adsb-tools#1"; @@ -10,5 +16,14 @@ static char log_module = 'X'; void server_init() { uuid_gen(server_id); - LOG(server_id, "Server start"); + LOG(server_id, "Server start:"); + LOG(server_id, "\tgit_last_change: %s", GIT_LAST_CHANGE); + LOG(server_id, "\tgit_local_clean: %s", GIT_LOCAL_CLEAN ? "true" : "false"); + LOG(server_id, "\tclang_version: %s", __clang_version__); + LOG(server_id, "\tglibc_version: %d.%d", __GLIBC__, __GLIBC_MINOR__); + LOG(server_id, "\tjansson_version: %s", JANSSON_VERSION); + LOG(server_id, "\tprotobuf-c_version: %s", PROTOBUF_C_VERSION); + LOG(server_id, "\tbuild_datetime: %s", __DATE__ " " __TIME__); + LOG(server_id, "\tbuild_username: %s", USERNAME); + LOG(server_id, "\tbuild_hostname: %s", HOSTNAME); } From eb89dafd0d72a1c3816b9b3bb79f10e43d023056 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 20:12:35 -0800 Subject: [PATCH 17/35] Add --log-timestamps --- adsbus/adsbus.c | 5 +++++ adsbus/log.c | 21 ++++++++++++++++++++- adsbus/log.h | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 06a4888..88353ea 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -78,6 +78,7 @@ static bool parse_opts(int argc, char *argv[]) { {"stdin", no_argument, 0, 'i'}, {"stdout", required_argument, 0, 'o'}, {"log-file", required_argument, 0, '1'}, + {"log-timestamps", no_argument, 0, '2'}, {0, 0, 0, 0 }, }; @@ -153,6 +154,10 @@ static bool parse_opts(int argc, char *argv[]) { handler = log_reopen; break; + case '2': + handler = log_enable_timestamps; + break; + case 'h': default: print_usage(argv[0]); diff --git a/adsbus/log.c b/adsbus/log.c index cfc7595..2fe02f1 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -1,11 +1,14 @@ #include #include #include +#include #include #include #include #include +#include #include +#include #include #include "log.h" @@ -19,6 +22,7 @@ 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 char log_module = 'L'; @@ -90,10 +94,25 @@ bool log_reopen(const char *path) { return true; } +bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) { + log_timestamps = true; + return true; +} + void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - assert(fprintf(log_stream, "%c [%18s] %s: ", type, loc, id) > 0); + + char datetime[64] = ""; + if (log_timestamps) { + time_t now; + assert(time(&now) >= 0); + struct tm tmnow; + assert(gmtime_r(&now, &tmnow)); + assert(strftime(datetime, sizeof(datetime), "%FT%TZ ", &tmnow) > 0); + } + + assert(fprintf(log_stream, "%s[%18s] %c %s: ", datetime, loc, type, id) > 0); assert(vfprintf(log_stream, fmt, ap) > 0); assert(fprintf(log_stream, "\n") == 1); va_end(ap); diff --git a/adsbus/log.h b/adsbus/log.h index 47002da..5c9c5a8 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -11,5 +11,6 @@ void log_init(void); void log_init2(void); void log_cleanup(void); bool log_reopen(const char *); +bool log_enable_timestamps(const char *); void log_write(char, const char *, const uint8_t *, const char *, ...) __attribute__ ((__format__ (__printf__, 4, 5))); From 70ac401a2328f7da157e229148f8e981df4bf86c Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 7 Mar 2016 21:42:51 -0800 Subject: [PATCH 18/35] Add missing help --- adsbus/adsbus.c | 1 + 1 file changed, 1 insertion(+) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index 88353ea..e7b30ab 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -53,6 +53,7 @@ static void print_usage(const char *name) { "\t--stdout=FORMAT\n" "\n" "\t--log-file=PATH\n" + "\t--log-timestamps\n" , name); receive_print_usage(); send_print_usage(); From e5af92c331cb4bc9eb5bc89a86a197805ca0b08b Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 11:26:39 -0800 Subject: [PATCH 19/35] Add --detach. Add multi-pass flag parsing that lets us get things in the right order. --- adsbus/Makefile | 2 +- adsbus/adsbus.c | 184 +++++---------------------------------- adsbus/exec.c | 32 +++++++ adsbus/exec.h | 2 + adsbus/file.c | 50 ++++++++++- adsbus/file.h | 2 + adsbus/incoming.c | 38 ++++++++ adsbus/incoming.h | 2 + adsbus/log.c | 53 +++++++---- adsbus/log.h | 3 +- adsbus/opts.c | 217 +++++++++++++++++++--------------------------- adsbus/opts.h | 26 +++--- adsbus/outgoing.c | 38 ++++++++ adsbus/outgoing.h | 2 + adsbus/rand.c | 2 +- adsbus/server.c | 29 +++++++ adsbus/server.h | 1 + adsbus/stdinout.c | 37 ++++++++ adsbus/stdinout.h | 4 + 19 files changed, 395 insertions(+), 329 deletions(-) create mode 100644 adsbus/stdinout.c create mode 100644 adsbus/stdinout.h diff --git a/adsbus/Makefile b/adsbus/Makefile index c311a8b..d5549a0 100644 --- a/adsbus/Makefile +++ b/adsbus/Makefile @@ -10,7 +10,7 @@ VALGRIND ?= valgrind VALGRIND_FLAGS ?= --error-exitcode=1 --trace-children=yes --track-fds=yes --show-leak-kinds=all --leak-check=full ADSBUS_TEST_FLAGS ?= --stdin --stdout=airspy_adsb --stdout=beast --stdout=json --stdout=proto --stdout=raw --stdout=stats -OBJ_TRANSPORT = exec.o file.o incoming.o outgoing.o +OBJ_TRANSPORT = exec.o file.o incoming.o outgoing.o stdinout.o OBJ_FLOW = flow.o receive.o send.o send_receive.o OBJ_PROTOCOL = airspy_adsb.o beast.o json.o proto.o raw.o stats.o OBJ_UTIL = asyncaddrinfo.o buf.o hex.o list.o log.o opts.o packet.o peer.o rand.o resolve.o server.o socket.o uuid.o wakeup.o diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index e7b30ab..b386106 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -1,7 +1,5 @@ #include #include -#include -#include #include #include #include @@ -25,171 +23,31 @@ #include "send_receive.h" #include "server.h" #include "stats.h" +#include "stdinout.h" #include "wakeup.h" -static void print_usage(const char *name) { - fprintf(stderr, - "\n" - "Usage: %s [OPTION]...\n" - "\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" - "\t--listen-receive=[HOST/]PORT\n" - "\t--listen-send=FORMAT=[HOST/]PORT\n" - "\t--listen-send-receive=FORMAT=[HOST/]PORT\n" - "\t--file-read=PATH\n" - "\t--file-write=FORMAT=PATH\n" - "\t--file-write-read=FORMAT=PATH\n" - "\t--file-append=FORMAT=PATH\n" - "\t--file-append-read=FORMAT=PATH\n" - "\t--exec-receive=COMMAND\n" - "\t--exec-send=FORMAT=COMMAND\n" - "\t--exec-send-receive=FORMAT=COMMAND\n" - "\t--stdin\n" - "\t--stdout=FORMAT\n" - "\n" - "\t--log-file=PATH\n" - "\t--log-timestamps\n" - , name); - receive_print_usage(); - send_print_usage(); -} - -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'}, - {"listen-receive", required_argument, 0, 'l'}, - {"listen-send", required_argument, 0, 'm'}, - {"listen-send-receive", required_argument, 0, 'n'}, - {"file-read", required_argument, 0, 'r'}, - {"file-write", required_argument, 0, 'w'}, - {"file-write-read", required_argument, 0, 'x'}, - {"file-append", required_argument, 0, 'a'}, - {"file-append-read", required_argument, 0, 'b'}, - {"exec-receive", required_argument, 0, 'e'}, - {"exec-send", required_argument, 0, 'f'}, - {"exec-send-receive", required_argument, 0, 'g'}, - {"stdin", no_argument, 0, 'i'}, - {"stdout", required_argument, 0, 'o'}, - {"log-file", required_argument, 0, '1'}, - {"log-timestamps", no_argument, 0, '2'}, - {0, 0, 0, 0 }, - }; - - int opt; - while ((opt = getopt_long_only(argc, argv, "", long_options, NULL)) != -1) { - bool (*handler)(const char *) = NULL; - switch (opt) { - case 'c': - handler = opts_add_connect_receive; - break; - - case 's': - handler = opts_add_connect_send; - break; - - case 't': - handler = opts_add_connect_send_receive; - break; - - case 'l': - handler = opts_add_listen_receive; - break; - - case 'm': - handler = opts_add_listen_send; - break; - - case 'n': - handler = opts_add_listen_send_receive; - break; - - case 'r': - handler = opts_add_file_read; - break; - - case 'w': - handler = opts_add_file_write; - break; - - case 'x': - handler = opts_add_file_write_read; - break; - - case 'a': - handler = opts_add_file_append; - break; - - case 'b': - handler = opts_add_file_append_read; - break; - - case 'e': - handler = opts_add_exec_receive; - break; - - case 'f': - handler = opts_add_exec_send; - break; - - case 'g': - handler = opts_add_exec_send_receive; - break; - - case 'i': - handler = opts_add_stdin; - break; - - case 'o': - handler = opts_add_stdout; - break; - - case '1': - handler = log_reopen; - break; - - case '2': - handler = log_enable_timestamps; - break; - - case 'h': - default: - print_usage(argv[0]); - return false; - } - - if (handler) { - if (!handler(optarg)) { - fprintf(stderr, "Invalid flag value: %s\n", optarg); - print_usage(argv[0]); - return false; - } - } - } - - if (optind != argc) { - fprintf(stderr, "Not a flag: %s\n", argv[optind]); - print_usage(argv[0]); - return false; - } - - return true; -} - static void reopen(int fd, char *path, int flags) { // Presumes that all fds < fd are open assert(!close(fd)); - assert(open(path, flags | O_CLOEXEC) == fd); + assert(open(path, flags | O_CLOEXEC | O_NOCTTY) == fd); +} + +static void adsbus_opts_add() { + // This order controls the order in --help, but nothing else. + server_opts_add(); + log_opts_add(); + outgoing_opts_add(); + incoming_opts_add(); + exec_opts_add(); + file_opts_add(); + stdinout_opts_add(); } int main(int argc, char *argv[]) { + adsbus_opts_add(); + + opts_init(argc, argv); + hex_init(); rand_init(); @@ -210,9 +68,11 @@ int main(int argc, char *argv[]) { proto_init(); stats_init(); - if (!parse_opts(argc, argv)) { - peer_shutdown(0); - } + outgoing_init(); + incoming_init(); + exec_init(); + file_init(); + stdinout_init(); reopen(STDIN_FILENO, "/dev/null", O_RDONLY); reopen(STDOUT_FILENO, "/dev/full", O_WRONLY); diff --git a/adsbus/exec.c b/adsbus/exec.c index 9531045..b366ec7 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -10,7 +10,11 @@ #include "flow.h" #include "log.h" +#include "opts.h" #include "peer.h" +#include "receive.h" +#include "send.h" +#include "send_receive.h" #include "uuid.h" #include "wakeup.h" @@ -28,6 +32,7 @@ struct exec { }; static struct list_head exec_head = LIST_HEAD_INIT(exec_head); +static opts_group exec_opts; static char log_module = 'E'; @@ -162,6 +167,33 @@ static void exec_spawn_wrapper(struct peer *peer) { exec_spawn(exec); } +static bool exec_add(const char *cmd, struct flow *flow, void *passthrough) { + exec_new(cmd, flow, passthrough); + return true; +} + +static bool exec_receive(const char *arg) { + return exec_add(arg, receive_flow, NULL); +} + +static bool exec_send(const char *arg) { + return opts_add_send(exec_add, send_flow, arg); +} + +static bool exec_send_receive(const char *arg) { + return opts_add_send(exec_add, send_receive_flow, arg); +} + +void exec_opts_add() { + opts_add("exec-receive", "COMMAND", exec_receive, exec_opts); + opts_add("exec-send", "FORMAT=COMMAND", exec_send, exec_opts); + opts_add("exec-send-receive", "FORMAT=COMMAND", exec_send_receive, exec_opts); +} + +void exec_init() { + opts_call(exec_opts); +} + void exec_cleanup() { struct exec *iter, *next; list_for_each_entry_safe(iter, next, &exec_head, exec_list) { diff --git a/adsbus/exec.h b/adsbus/exec.h index a96ad88..adb0c79 100644 --- a/adsbus/exec.h +++ b/adsbus/exec.h @@ -2,5 +2,7 @@ struct flow; +void exec_opts_add(void); +void exec_init(void); void exec_cleanup(void); void exec_new(const char *, struct flow *, void *); diff --git a/adsbus/file.c b/adsbus/file.c index 73f4017..0d7192b 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -9,7 +9,11 @@ #include "flow.h" #include "log.h" +#include "opts.h" #include "peer.h" +#include "receive.h" +#include "send.h" +#include "send_receive.h" #include "uuid.h" #include "wakeup.h" @@ -28,6 +32,7 @@ struct file { }; static struct list_head file_head = LIST_HEAD_INIT(file_head); +static opts_group file_opts; static char log_module = 'F'; @@ -83,7 +88,7 @@ static void file_handle_close(struct peer *peer) { static void file_open(struct file *file) { LOG(file->id, "Opening file: %s", file->path); - int fd = open(file->path, file->flags | O_CLOEXEC, S_IRUSR | S_IWUSR); + int fd = open(file->path, file->flags | O_CLOEXEC | O_NOCTTY, S_IRUSR | S_IWUSR); if (fd == -1) { LOG(file->id, "Error opening file: %s", strerror(errno)); file_retry(file); @@ -124,6 +129,49 @@ static void file_new(const char *path, int flags, struct flow *flow, void *passt file_open(file); } +static bool file_write_add(const char *path, struct flow *flow, void *passthrough) { + file_write_new(path, flow, passthrough); + return true; +} + +static bool file_append_add(const char *path, struct flow *flow, void *passthrough) { + file_append_new(path, flow, passthrough); + return true; +} + +static bool file_read(const char *arg) { + file_read_new(arg, receive_flow, NULL); + return true; +} + +static bool file_write(const char *arg) { + return opts_add_send(file_write_add, send_flow, arg); +} + +static bool file_write_read(const char *arg) { + return opts_add_send(file_write_add, send_receive_flow, arg); +} + +static bool file_append(const char *arg) { + return opts_add_send(file_append_add, send_flow, arg); +} + +static bool file_append_read(const char *arg) { + return opts_add_send(file_append_add, send_receive_flow, arg); +} + +void file_opts_add() { + opts_add("file-read", "PATH", file_read, file_opts); + opts_add("file-write", "FORMAT=PATH", file_write, file_opts); + opts_add("file-write-read", "FORMAT=PATH", file_write_read, file_opts); + opts_add("file-append", "FORMAT=PATH", file_append, file_opts); + opts_add("file-append-read", "FORMAT=PATH", file_append_read, file_opts); +} + +void file_init() { + opts_call(file_opts); +} + void file_cleanup() { struct file *iter, *next; list_for_each_entry_safe(iter, next, &file_head, file_list) { diff --git a/adsbus/file.h b/adsbus/file.h index ddcd9c1..659b626 100644 --- a/adsbus/file.h +++ b/adsbus/file.h @@ -2,6 +2,8 @@ struct flow; +void file_opts_add(void); +void file_init(void); void file_cleanup(void); void file_read_new(const char *, struct flow *, void *); void file_write_new(const char *, struct flow *, void *); diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 6a159b6..1dd96e9 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -10,8 +10,12 @@ #include "flow.h" #include "log.h" +#include "opts.h" #include "peer.h" +#include "receive.h" #include "resolve.h" +#include "send.h" +#include "send_receive.h" #include "socket.h" #include "wakeup.h" #include "uuid.h" @@ -30,6 +34,7 @@ struct incoming { }; static struct list_head incoming_head = LIST_HEAD_INIT(incoming_head); +static opts_group incoming_opts; static char log_module = 'I'; @@ -142,6 +147,39 @@ static void incoming_resolve_wrapper(struct peer *peer) { incoming_resolve((struct incoming *) peer); } +static bool incoming_add(const char *host_port, struct flow *flow, void *passthrough) { + char *host = opts_split(&host_port, '/'); + if (host) { + incoming_new(host, host_port, flow, passthrough); + free(host); + } else { + incoming_new(NULL, host_port, flow, passthrough); + } + return true; +} + +static bool incoming_listen_receive(const char *arg) { + return incoming_add(arg, receive_flow, NULL); +} + +static bool incoming_listen_send(const char *arg) { + return opts_add_send(incoming_add, send_flow, arg); +} + +static bool incoming_listen_send_receive(const char *arg) { + return opts_add_send(incoming_add, send_receive_flow, arg); +} + +void incoming_opts_add() { + opts_add("listen-receive", "[HOST/]PORT", incoming_listen_receive, incoming_opts); + opts_add("listen-send", "FORMAT=[HOST/]PORT", incoming_listen_send, incoming_opts); + opts_add("listen-send-receive", "FORMAT=[HOST/]PORT", incoming_listen_send_receive, incoming_opts); +} + +void incoming_init() { + opts_call(incoming_opts); +} + void incoming_cleanup() { struct incoming *iter, *next; list_for_each_entry_safe(iter, next, &incoming_head, incoming_list) { diff --git a/adsbus/incoming.h b/adsbus/incoming.h index 0f6f096..785a164 100644 --- a/adsbus/incoming.h +++ b/adsbus/incoming.h @@ -2,5 +2,7 @@ struct flow; +void incoming_opts_add(void); +void incoming_init(void); void incoming_cleanup(void); void incoming_new(const char *, const char *, struct flow *, void *); diff --git a/adsbus/log.c b/adsbus/log.c index 2fe02f1..40fb95a 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -12,6 +13,7 @@ #include #include "log.h" +#include "opts.h" #include "peer.h" #include "uuid.h" @@ -23,10 +25,16 @@ 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; static char log_module = 'L'; static void log_rotate() { + if (!log_path) { + LOG(log_id, "Received SIGHUP but logging to stderr; ignoring"); + return; + } + uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN]; uuid_gen(new_log_id); LOG(log_id, "Switching to new log with ID %s at: %s", new_log_id, log_path); @@ -50,10 +58,34 @@ 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; + } + log_path = strdup(path); + assert(log_path); + return true; +} + +static bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) { + log_timestamps = true; + return true; +} + +void log_opts_add() { + opts_add("log-file", "PATH", log_set_path, log_opts); + opts_add("log-timestamps", NULL, log_enable_timestamps, log_opts); +} + void log_init() { - int fd = dup(STDERR_FILENO); - assert(fd >= 0); - log_stream = fdopen(fd, "a"); + opts_call(log_opts); + if (log_path) { + log_stream = fopen(log_path, "a"); + } else { + int fd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0); + assert(fd >= 0); + log_stream = fdopen(fd, "a"); + } assert(log_stream); setlinebuf(log_stream); @@ -84,21 +116,6 @@ void log_cleanup() { } } -bool log_reopen(const char *path) { - if (log_path) { - free(log_path); - } - log_path = strdup(path); - assert(log_path); - log_rotate(); - return true; -} - -bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) { - log_timestamps = true; - return true; -} - void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { va_list ap; va_start(ap, fmt); diff --git a/adsbus/log.h b/adsbus/log.h index 5c9c5a8..34f2778 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -7,10 +7,9 @@ #define LOG_LOC(file, line) (file ":" LOG_STR(line)) #define LOG(id, ...) log_write((log_module), LOG_LOC(__FILE__, __LINE__), (id), __VA_ARGS__) +void log_opts_add(void); void log_init(void); void log_init2(void); void log_cleanup(void); -bool log_reopen(const char *); -bool log_enable_timestamps(const char *); void log_write(char, const char *, const uint8_t *, const char *, ...) __attribute__ ((__format__ (__printf__, 4, 5))); diff --git a/adsbus/opts.c b/adsbus/opts.c index 822fa5f..5fcfa20 100644 --- a/adsbus/opts.c +++ b/adsbus/opts.c @@ -1,66 +1,26 @@ #include -#include +#include #include +#include #include -#include -#include "exec.h" -#include "file.h" -#include "flow.h" -#include "incoming.h" -#include "outgoing.h" -#include "receive.h" #include "send.h" -#include "send_receive.h" #include "opts.h" -static char *opts_split(const char **arg, char delim) { - char *split = strchr(*arg, delim); - if (!split) { - return NULL; - } - char *ret = strndup(*arg, split - *arg); - *arg = split + 1; - return ret; -} +#define OPTS_MAX 128 -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); - free(host); - } else { - incoming_new(NULL, host_port, flow, passthrough); - } - return true; -} +static struct { + const char *arg_help; + opts_handler handler; + void *group; +} opts[OPTS_MAX]; -static bool opts_add_connect(const char *host_port, struct flow *flow, void *passthrough) { - char *host = opts_split(&host_port, '/'); - if (!host) { - return false; - } - - outgoing_new(host, host_port, flow, passthrough); - free(host); - return true; -} - -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(const char *path, struct flow *flow, void *passthrough) { - file_append_new(path, flow, passthrough); - return true; -} - -static bool opts_add_exec(const char *cmd, struct flow *flow, void *passthrough) { - exec_new(cmd, flow, passthrough); - return true; -} +static struct option opts_long[OPTS_MAX]; +static size_t opts_num = 0; +static int opts_argc; +static char **opts_argv; +static opts_group opts_group_internal; static struct serializer *opts_get_serializer(const char **arg) { char *format = opts_split(arg, '='); @@ -77,84 +37,85 @@ static struct serializer *opts_get_serializer(const char **arg) { return serializer; } -static bool opts_add_send(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) { +static void opts_print_usage() { + fprintf(stderr, + "Usage: %s [OPTION]...\n" + "\n" + "Options:\n" + , opts_argv[0]); + for (size_t i = 0; i < opts_num; i++) { + fprintf(stderr, "\t--%s%s%s\n", opts_long[i].name, opts[i].arg_help ? "=" : "", opts[i].arg_help ? opts[i].arg_help : ""); + } +} + +static bool opts_help(const char __attribute__ ((unused)) *arg) { + opts_print_usage(); + exit(EXIT_SUCCESS); +} + +void opts_init(int argc, char *argv[]) { + opts_argc = argc; + opts_argv = argv; + opts_add("help", NULL, opts_help, opts_group_internal); + + assert(opts_num < OPTS_MAX); + opts_long[opts_num].name = NULL; + opts_long[opts_num].has_arg = 0; + opts_long[opts_num].flag = NULL; + opts_long[opts_num].val = 0; + + opts_call(opts_group_internal); +} + +void opts_add(const char *name, const char *arg_help, opts_handler handler, opts_group group) { + assert(opts_num < OPTS_MAX); + opts[opts_num].arg_help = arg_help; + opts[opts_num].handler = handler; + opts[opts_num].group = group; + opts_long[opts_num].name = name; + opts_long[opts_num].has_arg = arg_help ? required_argument : no_argument; + opts_long[opts_num].flag = NULL; + opts_long[opts_num].val = 0; + opts_num++; +} + +void opts_call(opts_group group) { + optind = 1; + int opt, longindex; + while ((opt = getopt_long_only(opts_argc, opts_argv, "", opts_long, &longindex)) == 0) { + if (opts[longindex].group != group) { + continue; + } + if (!opts[longindex].handler(optarg)) { + fprintf(stderr, "Invalid option value: %s\n", opts_argv[optind - 1]); + exit(EXIT_FAILURE); + } + } + if (opt != -1) { + opts_print_usage(); + exit(EXIT_FAILURE); + } + + if (optind != opts_argc) { + fprintf(stderr, "Not a flag: %s\n", opts_argv[optind]); + exit(EXIT_FAILURE); + } +} + +char *opts_split(const char **arg, char delim) { + char *split = strchr(*arg, delim); + if (!split) { + return NULL; + } + char *ret = strndup(*arg, split - *arg); + *arg = split + 1; + return ret; +} + +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; } return next(arg, flow, serializer); } - -bool opts_add_connect_receive(const char *arg) { - return opts_add_connect(arg, receive_flow, NULL); -} - -bool opts_add_connect_send(const char *arg) { - return opts_add_send(opts_add_connect, send_flow, 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(const char *arg) { - return opts_add_listen(arg, receive_flow, NULL); -} - -bool opts_add_listen_send(const char *arg) { - return opts_add_send(opts_add_listen, send_flow, 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(const char *arg) { - file_read_new(arg, receive_flow, NULL); - return true; -} - -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(const char *arg) { - return opts_add_send(opts_add_file_write_int, send_receive_flow, 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(const char *arg) { - return opts_add_send(opts_add_file_append_int, send_receive_flow, arg); -} - -bool opts_add_exec_receive(const char *arg) { - exec_new(arg, receive_flow, NULL); - return true; -} - -bool opts_add_exec_send(const char *arg) { - return opts_add_send(opts_add_exec, send_flow, 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(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(const char *arg) { - struct serializer *serializer = send_get_serializer(arg); - if (!serializer) { - return false; - } - int fd = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0); - assert(fd >= 0); - return flow_new_send_hello(fd, send_flow, serializer, NULL); -} diff --git a/adsbus/opts.h b/adsbus/opts.h index eb47ff7..0853cec 100644 --- a/adsbus/opts.h +++ b/adsbus/opts.h @@ -2,19 +2,13 @@ #include -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 *); +struct flow; + +typedef bool (*opts_handler)(const char *); +typedef char opts_group[1]; + +void opts_init(int, char *[]); +void opts_add(const char *, const char *, opts_handler, opts_group); +void opts_call(opts_group); +char *opts_split(const char **, char); +bool opts_add_send(bool (*)(const char *, struct flow *, void *), struct flow *, const char *); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index c6f90ca..894db84 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -11,8 +11,12 @@ #include "buf.h" #include "flow.h" #include "log.h" +#include "opts.h" #include "peer.h" +#include "receive.h" #include "resolve.h" +#include "send.h" +#include "send_receive.h" #include "wakeup.h" #include "uuid.h" @@ -32,6 +36,7 @@ struct outgoing { }; static struct list_head outgoing_head = LIST_HEAD_INIT(outgoing_head); +static opts_group outgoing_opts; static char log_module = 'O'; @@ -152,6 +157,39 @@ static void outgoing_del(struct outgoing *outgoing) { free(outgoing); } +static bool outgoing_add(const char *host_port, struct flow *flow, void *passthrough) { + char *host = opts_split(&host_port, '/'); + if (!host) { + return false; + } + + outgoing_new(host, host_port, flow, passthrough); + free(host); + return true; +} + +static bool outgoing_connect_receive(const char *arg) { + return outgoing_add(arg, receive_flow, NULL); +} + +static bool outgoing_connect_send(const char *arg) { + return opts_add_send(outgoing_add, send_flow, arg); +} + +static bool outgoing_connect_send_receive(const char *arg) { + return opts_add_send(outgoing_add, send_receive_flow, arg); +} + +void outgoing_opts_add() { + opts_add("connect-receive", "HOST/PORT", outgoing_connect_receive, outgoing_opts); + opts_add("connect-send", "FORMAT=HOST/PORT", outgoing_connect_send, outgoing_opts); + opts_add("connect-send-receive", "FORMAT=HOST/PORT", outgoing_connect_send_receive, outgoing_opts); +} + +void outgoing_init() { + opts_call(outgoing_opts); +} + void outgoing_cleanup() { struct outgoing *iter, *next; list_for_each_entry_safe(iter, next, &outgoing_head, outgoing_list) { diff --git a/adsbus/outgoing.h b/adsbus/outgoing.h index 2e88b4d..4333dd0 100644 --- a/adsbus/outgoing.h +++ b/adsbus/outgoing.h @@ -2,5 +2,7 @@ struct flow; +void outgoing_opts_add(void); +void outgoing_init(void); void outgoing_cleanup(void); void outgoing_new(const char *, const char *, struct flow *, void *); diff --git a/adsbus/rand.c b/adsbus/rand.c index f232cd9..b4d27f1 100644 --- a/adsbus/rand.c +++ b/adsbus/rand.c @@ -15,7 +15,7 @@ static struct buf rand_buf = BUF_INIT; static int rand_fd; void rand_init() { - rand_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); + rand_fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOCTTY); assert(rand_fd >= 0); assert(read(rand_fd, buf_at(&rand_buf, 0), BUF_LEN_MAX) == BUF_LEN_MAX); rand_buf.length = BUF_LEN_MAX; diff --git a/adsbus/server.c b/adsbus/server.c index 4c0bf68..14a336a 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -1,8 +1,13 @@ +#include +#include +#include + #include #include #include "build.h" #include "log.h" +#include "opts.h" #include "uuid.h" #include "server.h" @@ -11,9 +16,31 @@ uint8_t server_id[UUID_LEN]; char server_version[] = "https://github.com/flamingcowtv/adsb-tools#1"; +static opts_group server_opts; static char log_module = 'X'; +static bool server_detach(const char __attribute__ ((unused)) *arg) { + LOG(server_id, "Detaching"); + + int ret = fork(); + assert(ret >= 0); + if (ret > 0) { + // We are the parent + exit(EXIT_SUCCESS); + } + + LOG(server_id, "Background process ID: %u", getpid()); + + assert(setsid() >= 0); + + return true; +} + +void server_opts_add() { + opts_add("detach", NULL, server_detach, server_opts); +} + void server_init() { uuid_gen(server_id); LOG(server_id, "Server start:"); @@ -26,4 +53,6 @@ void server_init() { LOG(server_id, "\tbuild_datetime: %s", __DATE__ " " __TIME__); LOG(server_id, "\tbuild_username: %s", USERNAME); LOG(server_id, "\tbuild_hostname: %s", HOSTNAME); + + opts_call(server_opts); } diff --git a/adsbus/server.h b/adsbus/server.h index b5fcfc8..8e9e889 100644 --- a/adsbus/server.h +++ b/adsbus/server.h @@ -3,4 +3,5 @@ extern uint8_t server_id[]; extern char server_version[]; +void server_opts_add(void); void server_init(void); diff --git a/adsbus/stdinout.c b/adsbus/stdinout.c new file mode 100644 index 0000000..c1c7f38 --- /dev/null +++ b/adsbus/stdinout.c @@ -0,0 +1,37 @@ +#include +#include +#include + +#include "flow.h" +#include "opts.h" +#include "receive.h" +#include "send.h" + +#include "stdinout.h" + +static opts_group stdinout_opts; + +static bool stdinout_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); +} + +static bool stdinout_stdout(const char *arg) { + struct serializer *serializer = send_get_serializer(arg); + if (!serializer) { + return false; + } + int fd = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 0); + assert(fd >= 0); + return flow_new_send_hello(fd, send_flow, serializer, NULL); +} + +void stdinout_opts_add() { + opts_add("stdin", NULL, stdinout_stdin, stdinout_opts); + opts_add("stdout", "FORMAT", stdinout_stdout, stdinout_opts); +} + +void stdinout_init() { + opts_call(stdinout_opts); +} diff --git a/adsbus/stdinout.h b/adsbus/stdinout.h new file mode 100644 index 0000000..e1291f4 --- /dev/null +++ b/adsbus/stdinout.h @@ -0,0 +1,4 @@ +#pragma once + +void stdinout_opts_add(void); +void stdinout_init(void); From d5eb4a919e9b480acb46928684f997aa8e7e3f4c Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 11:28:21 -0800 Subject: [PATCH 20/35] Initialize server before resolve, since server forks and resolve starts a thread. --- adsbus/adsbus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index b386106..d50207f 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -52,9 +52,9 @@ int main(int argc, char *argv[]) { rand_init(); log_init(); + server_init(); resolve_init(); - server_init(); wakeup_init(); peer_init(); From cd302a174653a8b5c86dd180a93f52d6a4a30d30 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 12:22:46 -0800 Subject: [PATCH 21/35] Move FORMAT parsing into send code. --- adsbus/exec.c | 4 ++-- adsbus/file.c | 8 ++++---- adsbus/incoming.c | 4 ++-- adsbus/opts.c | 25 ------------------------- adsbus/opts.h | 3 --- adsbus/outgoing.c | 4 ++-- adsbus/send.c | 24 ++++++++++++++++++++++++ adsbus/send.h | 3 +++ 8 files changed, 37 insertions(+), 38 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index b366ec7..4248129 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -177,11 +177,11 @@ static bool exec_receive(const char *arg) { } static bool exec_send(const char *arg) { - return opts_add_send(exec_add, send_flow, arg); + return send_add(exec_add, send_flow, arg); } static bool exec_send_receive(const char *arg) { - return opts_add_send(exec_add, send_receive_flow, arg); + return send_add(exec_add, send_receive_flow, arg); } void exec_opts_add() { diff --git a/adsbus/file.c b/adsbus/file.c index 0d7192b..65638a1 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -145,19 +145,19 @@ static bool file_read(const char *arg) { } static bool file_write(const char *arg) { - return opts_add_send(file_write_add, send_flow, arg); + return send_add(file_write_add, send_flow, arg); } static bool file_write_read(const char *arg) { - return opts_add_send(file_write_add, send_receive_flow, arg); + return send_add(file_write_add, send_receive_flow, arg); } static bool file_append(const char *arg) { - return opts_add_send(file_append_add, send_flow, arg); + return send_add(file_append_add, send_flow, arg); } static bool file_append_read(const char *arg) { - return opts_add_send(file_append_add, send_receive_flow, arg); + return send_add(file_append_add, send_receive_flow, arg); } void file_opts_add() { diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 1dd96e9..55130ae 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -163,11 +163,11 @@ static bool incoming_listen_receive(const char *arg) { } static bool incoming_listen_send(const char *arg) { - return opts_add_send(incoming_add, send_flow, arg); + return send_add(incoming_add, send_flow, arg); } static bool incoming_listen_send_receive(const char *arg) { - return opts_add_send(incoming_add, send_receive_flow, arg); + return send_add(incoming_add, send_receive_flow, arg); } void incoming_opts_add() { diff --git a/adsbus/opts.c b/adsbus/opts.c index 5fcfa20..7218fdb 100644 --- a/adsbus/opts.c +++ b/adsbus/opts.c @@ -4,8 +4,6 @@ #include #include -#include "send.h" - #include "opts.h" #define OPTS_MAX 128 @@ -22,21 +20,6 @@ static int opts_argc; static char **opts_argv; static opts_group opts_group_internal; -static struct serializer *opts_get_serializer(const char **arg) { - char *format = opts_split(arg, '='); - if (!format) { - return NULL; - } - - struct serializer *serializer = send_get_serializer(format); - free(format); - if (!serializer) { - return NULL; - } - - return serializer; -} - static void opts_print_usage() { fprintf(stderr, "Usage: %s [OPTION]...\n" @@ -111,11 +94,3 @@ char *opts_split(const char **arg, char delim) { *arg = split + 1; return ret; } - -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; - } - return next(arg, flow, serializer); -} diff --git a/adsbus/opts.h b/adsbus/opts.h index 0853cec..4f832ae 100644 --- a/adsbus/opts.h +++ b/adsbus/opts.h @@ -2,8 +2,6 @@ #include -struct flow; - typedef bool (*opts_handler)(const char *); typedef char opts_group[1]; @@ -11,4 +9,3 @@ void opts_init(int, char *[]); void opts_add(const char *, const char *, opts_handler, opts_group); void opts_call(opts_group); char *opts_split(const char **, char); -bool opts_add_send(bool (*)(const char *, struct flow *, void *), struct flow *, const char *); diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 894db84..cce6dc9 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -173,11 +173,11 @@ static bool outgoing_connect_receive(const char *arg) { } static bool outgoing_connect_send(const char *arg) { - return opts_add_send(outgoing_add, send_flow, arg); + return send_add(outgoing_add, send_flow, arg); } static bool outgoing_connect_send_receive(const char *arg) { - return opts_add_send(outgoing_add, send_receive_flow, arg); + return send_add(outgoing_add, send_receive_flow, arg); } void outgoing_opts_add() { diff --git a/adsbus/send.c b/adsbus/send.c index d660ea6..85bcc47 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -14,6 +14,7 @@ #include "flow.h" #include "json.h" #include "log.h" +#include "opts.h" #include "packet.h" #include "peer.h" #include "proto.h" @@ -124,6 +125,21 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) { LOG(send->id, "New send connection: %s", serializer->name); } +static struct serializer *send_parse_format(const char **arg) { + char *format = opts_split(arg, '='); + if (!format) { + return NULL; + } + + struct serializer *serializer = send_get_serializer(format); + free(format); + if (!serializer) { + return NULL; + } + + return serializer; +} + void send_init() { assert(signal(SIGPIPE, SIG_IGN) != SIG_ERR); for (size_t i = 0; i < NUM_SERIALIZERS; i++) { @@ -190,3 +206,11 @@ void send_print_usage() { fprintf(stderr, "\t%s\n", serializers[i].name); } } + +bool send_add(bool (*next)(const char *, struct flow *, void *), struct flow *flow, const char *arg) { + struct serializer *serializer = send_parse_format(&arg); + if (!serializer) { + return false; + } + return next(arg, flow, serializer); +} diff --git a/adsbus/send.h b/adsbus/send.h index 50657bf..31d6752 100644 --- a/adsbus/send.h +++ b/adsbus/send.h @@ -1,5 +1,7 @@ #pragma once +#include + struct buf; struct flow; struct packet; @@ -10,4 +12,5 @@ void *send_get_serializer(const char *); void send_get_hello(struct buf **, void *); void send_write(struct packet *); void send_print_usage(void); +bool send_add(bool (*)(const char *, struct flow *, void *), struct flow *, const char *); extern struct flow *send_flow; From d39cd629916523d7f9df5115b04112cee8c03646 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 20:41:00 -0800 Subject: [PATCH 22/35] Cleanup how we cast and close peers. --- adsbus/exec.c | 14 +++++--------- adsbus/file.c | 4 ++-- adsbus/incoming.c | 10 ++++------ adsbus/outgoing.c | 18 ++++++------------ adsbus/peer.c | 9 +++++++++ adsbus/peer.h | 1 + adsbus/receive.c | 5 ++--- adsbus/send.c | 5 ++--- adsbus/send_receive.c | 4 ++-- 9 files changed, 33 insertions(+), 37 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index 4248129..b239320 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -50,10 +50,7 @@ static void exec_harvest(struct exec *exec) { LOG(exec->id, "Client exited with signal %d", WTERMSIG(status)); } } - if (exec->log_peer.fd >= 0) { - assert(!close(exec->log_peer.fd)); - exec->log_peer.fd = -1; - } + peer_close(&exec->log_peer); } static void exec_del(struct exec *exec) { @@ -76,7 +73,7 @@ static void exec_close_handler(struct peer *peer) { uint32_t delay = wakeup_get_retry_delay_ms(1); LOG(exec->id, "Will retry in %ds", delay / 1000); exec->peer.event_handler = exec_spawn_wrapper; - wakeup_add((struct peer *) exec, delay); + wakeup_add(&exec->peer, delay); } static void exec_log_handler(struct peer *peer) { @@ -87,8 +84,7 @@ static void exec_log_handler(struct peer *peer) { ssize_t ret = read(exec->log_peer.fd, linebuf, 4096); if (ret <= 0) { LOG(exec->id, "Log input stream closed"); - assert(!close(exec->log_peer.fd)); - exec->log_peer.fd = -1; + peer_close(&exec->log_peer); return; } size_t len = (size_t) ret; @@ -114,8 +110,8 @@ static void exec_parent(struct exec *exec, pid_t child, int data_fd, int log_fd) peer_epoll_add(&exec->log_peer, EPOLLIN); exec->peer.event_handler = exec_close_handler; - if (!flow_new_send_hello(data_fd, exec->flow, exec->passthrough, (struct peer *) exec)) { - exec_close_handler((struct peer *) exec); + if (!flow_new_send_hello(data_fd, exec->flow, exec->passthrough, &exec->peer)) { + exec_close_handler(&exec->peer); return; } } diff --git a/adsbus/file.c b/adsbus/file.c index 65638a1..dd4d2ef 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -72,7 +72,7 @@ static void file_retry(struct file *file) { uint32_t delay = wakeup_get_retry_delay_ms(file->attempt++); LOG(file->id, "Will retry in %ds", delay / 1000); file->peer.event_handler = file_open_wrapper; - wakeup_add((struct peer *) file, delay); + wakeup_add(&file->peer, delay); } static void file_handle_close(struct peer *peer) { @@ -98,7 +98,7 @@ static void file_open(struct file *file) { file->retry = file_should_retry(fd, file); file->peer.event_handler = file_handle_close; file->attempt = 0; - if (!flow_new_send_hello(fd, file->flow, file->passthrough, (struct peer *) file)) { + if (!flow_new_send_hello(fd, file->flow, file->passthrough, &file->peer)) { LOG(file->id, "Error writing greeting"); file_retry(file); return; diff --git a/adsbus/incoming.c b/adsbus/incoming.c index 55130ae..db243bd 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -44,7 +44,7 @@ static void incoming_retry(struct incoming *incoming) { uint32_t delay = wakeup_get_retry_delay_ms(incoming->attempt++); LOG(incoming->id, "Will retry in %ds", delay / 1000); incoming->peer.event_handler = incoming_resolve_wrapper; - wakeup_add((struct peer *) incoming, delay); + wakeup_add(&incoming->peer, delay); } static void incoming_handler(struct peer *peer) { @@ -79,9 +79,7 @@ static void incoming_handler(struct peer *peer) { static void incoming_del(struct incoming *incoming) { flow_ref_dec(incoming->flow); - if (incoming->peer.fd >= 0) { - assert(!close(incoming->peer.fd)); - } + peer_close(&incoming->peer); list_del(&incoming->incoming_list); free(incoming->node); free(incoming->service); @@ -134,13 +132,13 @@ static void incoming_listen(struct peer *peer) { incoming->attempt = 0; incoming->peer.event_handler = incoming_handler; - peer_epoll_add((struct peer *) incoming, EPOLLIN); + peer_epoll_add(&incoming->peer, EPOLLIN); } static void incoming_resolve(struct incoming *incoming) { LOG(incoming->id, "Resolving %s/%s...", incoming->node, incoming->service); incoming->peer.event_handler = incoming_listen; - resolve((struct peer *) incoming, incoming->node, incoming->service, AI_PASSIVE); + resolve(&incoming->peer, incoming->node, incoming->service, AI_PASSIVE); } static void incoming_resolve_wrapper(struct peer *peer) { diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index cce6dc9..7188a29 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -48,7 +48,7 @@ static void outgoing_retry(struct outgoing *outgoing) { uint32_t delay = wakeup_get_retry_delay_ms(++outgoing->attempt); LOG(outgoing->id, "Will retry in %ds", delay / 1000); outgoing->peer.event_handler = outgoing_resolve_wrapper; - wakeup_add((struct peer *) outgoing, delay); + wakeup_add(&outgoing->peer, delay); } static void outgoing_connect_next(struct outgoing *outgoing) { @@ -85,9 +85,6 @@ static void outgoing_connect_handler(struct peer *peer) { static void outgoing_disconnect_handler(struct peer *peer) { struct outgoing *outgoing = (struct outgoing *) peer; - if (outgoing->peer.fd != -1) { - assert(!close(outgoing->peer.fd)); - } LOG(outgoing->id, "Peer disconnected; reconnecting..."); outgoing_retry(outgoing); } @@ -105,18 +102,17 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { outgoing->peer.event_handler = outgoing_disconnect_handler; flow_socket_ready(fd, outgoing->flow); flow_socket_connected(fd, outgoing->flow); - flow_new(fd, outgoing->flow, outgoing->passthrough, (struct peer *) outgoing); + flow_new(fd, outgoing->flow, outgoing->passthrough, &outgoing->peer); break; case EINPROGRESS: outgoing->peer.event_handler = outgoing_connect_handler; - peer_epoll_add((struct peer *) outgoing, EPOLLOUT); + peer_epoll_add(&outgoing->peer, EPOLLOUT); break; default: LOG(outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result)); - assert(!close(outgoing->peer.fd)); - outgoing->peer.fd = -1; + peer_close(&outgoing->peer); outgoing->addr = outgoing->addr->ai_next; // Tail recursion :/ outgoing_connect_next(outgoing); @@ -139,7 +135,7 @@ static void outgoing_resolve_handler(struct peer *peer) { static void outgoing_resolve(struct outgoing *outgoing) { LOG(outgoing->id, "Resolving %s/%s...", outgoing->node, outgoing->service); outgoing->peer.event_handler = outgoing_resolve_handler; - resolve((struct peer *) outgoing, outgoing->node, outgoing->service, 0); + resolve(&outgoing->peer, outgoing->node, outgoing->service, 0); } static void outgoing_resolve_wrapper(struct peer *peer) { @@ -148,9 +144,7 @@ static void outgoing_resolve_wrapper(struct peer *peer) { static void outgoing_del(struct outgoing *outgoing) { flow_ref_dec(outgoing->flow); - if (outgoing->peer.fd >= 0) { - assert(!close(outgoing->peer.fd)); - } + peer_close(&outgoing->peer); list_del(&outgoing->outgoing_list); free(outgoing->node); free(outgoing->service); diff --git a/adsbus/peer.c b/adsbus/peer.c index 2f3e66e..f1aa29a 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -90,6 +90,15 @@ void peer_epoll_del(struct peer *peer) { } } +void peer_close(struct peer *peer) { + if (peer->fd == -1) { + return; + } + peer_epoll_del(peer); + assert(!close(peer->fd)); + peer->fd = -1; +} + void peer_call(struct peer *peer) { if (peer_shutdown_flag || !peer) { return; diff --git a/adsbus/peer.h b/adsbus/peer.h index 1b380ea..8c23cd4 100644 --- a/adsbus/peer.h +++ b/adsbus/peer.h @@ -21,5 +21,6 @@ void peer_cleanup(void); void peer_shutdown(int signal); void peer_epoll_add(struct peer *, uint32_t); void peer_epoll_del(struct peer *); +void peer_close(struct peer *); void peer_call(struct peer *); void peer_loop(void); diff --git a/adsbus/receive.c b/adsbus/receive.c index 95dbc3b..308672e 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -108,8 +108,7 @@ static bool receive_autodetect_parse(struct receive *receive, struct packet *pac static void receive_del(struct receive *receive) { LOG(receive->id, "Connection closed"); peer_count_in--; - peer_epoll_del((struct peer *) receive); - assert(!close(receive->peer.fd)); + peer_close(&receive->peer); list_del(&receive->receive_list); peer_call(receive->on_close); free(receive); @@ -165,7 +164,7 @@ static void receive_new(int fd, void __attribute__((unused)) *passthrough, struc list_add(&receive->receive_list, &receive_head); - peer_epoll_add((struct peer *) receive, EPOLLIN); + peer_epoll_add(&receive->peer, EPOLLIN); LOG(receive->id, "New receive connection"); } diff --git a/adsbus/send.c b/adsbus/send.c index 85bcc47..ffd49f4 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -92,8 +92,7 @@ static struct serializer { static void send_del(struct send *send) { LOG(send->id, "Connection closed"); peer_count_out--; - peer_epoll_del((struct peer *) send); - assert(!close(send->peer.fd)); + peer_close(&send->peer); list_del(&send->send_list); peer_call(send->on_close); free(send); @@ -120,7 +119,7 @@ static void send_new(int fd, void *passthrough, struct peer *on_close) { list_add(&send->send_list, &serializer->send_head); - peer_epoll_add((struct peer *) send, 0); + peer_epoll_add(&send->peer, 0); LOG(send->id, "New send connection: %s", serializer->name); } diff --git a/adsbus/send_receive.c b/adsbus/send_receive.c index 408b600..15af572 100644 --- a/adsbus/send_receive.c +++ b/adsbus/send_receive.c @@ -52,10 +52,10 @@ static void send_receive_new(int fd, void *passthrough, struct peer *on_close) { send_receive->ref_count = 2; list_add(&send_receive->send_receive_list, &send_receive_head); - flow_new(fd, send_flow, passthrough, (struct peer *) send_receive); + flow_new(fd, send_flow, passthrough, &send_receive->peer); int fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 0); assert(fd2 >= 0); - flow_new(fd2, receive_flow, NULL, (struct peer *) send_receive); + flow_new(fd2, receive_flow, NULL, &send_receive->peer); } void send_receive_cleanup() { From 3985182304ca77ca6506e1073b793147744ed1da Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 20:47:22 -0800 Subject: [PATCH 23/35] Stop relying on struct ordering and packing for peer. --- adsbus/exec.c | 4 ++-- adsbus/file.c | 4 ++-- adsbus/incoming.c | 6 +++--- adsbus/outgoing.c | 9 ++++----- adsbus/receive.c | 3 +-- adsbus/send.c | 2 +- adsbus/send_receive.c | 2 +- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/adsbus/exec.c b/adsbus/exec.c index b239320..f5826ed 100644 --- a/adsbus/exec.c +++ b/adsbus/exec.c @@ -68,7 +68,7 @@ static void exec_del(struct exec *exec) { } static void exec_close_handler(struct peer *peer) { - struct exec *exec = (struct exec *) peer; + struct exec *exec = container_of(peer, struct exec, peer); exec_harvest(exec); uint32_t delay = wakeup_get_retry_delay_ms(1); LOG(exec->id, "Will retry in %ds", delay / 1000); @@ -159,7 +159,7 @@ static void exec_spawn(struct exec *exec) { } static void exec_spawn_wrapper(struct peer *peer) { - struct exec *exec = (struct exec *) peer; + struct exec *exec = container_of(peer, struct exec, peer); exec_spawn(exec); } diff --git a/adsbus/file.c b/adsbus/file.c index dd4d2ef..89f1782 100644 --- a/adsbus/file.c +++ b/adsbus/file.c @@ -76,7 +76,7 @@ static void file_retry(struct file *file) { } static void file_handle_close(struct peer *peer) { - struct file *file = (struct file *) peer; + struct file *file = container_of(peer, struct file, peer); LOG(file->id, "File closed: %s", file->path); if (file->retry) { @@ -106,7 +106,7 @@ static void file_open(struct file *file) { } static void file_open_wrapper(struct peer *peer) { - struct file *file = (struct file *) peer; + struct file *file = container_of(peer, struct file, peer); file_open(file); } diff --git a/adsbus/incoming.c b/adsbus/incoming.c index db243bd..60e8cf5 100644 --- a/adsbus/incoming.c +++ b/adsbus/incoming.c @@ -48,7 +48,7 @@ static void incoming_retry(struct incoming *incoming) { } static void incoming_handler(struct peer *peer) { - struct incoming *incoming = (struct incoming *) peer; + struct incoming *incoming = container_of(peer, struct incoming, peer); struct sockaddr peer_addr, local_addr; socklen_t peer_addrlen = sizeof(peer_addr), local_addrlen = sizeof(local_addr); @@ -87,7 +87,7 @@ static void incoming_del(struct incoming *incoming) { } static void incoming_listen(struct peer *peer) { - struct incoming *incoming = (struct incoming *) peer; + struct incoming *incoming = container_of(peer, struct incoming, peer); struct addrinfo *addrs; int err = resolve_result(peer, &addrs); @@ -142,7 +142,7 @@ static void incoming_resolve(struct incoming *incoming) { } static void incoming_resolve_wrapper(struct peer *peer) { - incoming_resolve((struct incoming *) peer); + incoming_resolve(container_of(peer, struct incoming, peer)); } static bool incoming_add(const char *host_port, struct flow *flow, void *passthrough) { diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 7188a29..2df996a 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -6,7 +6,6 @@ #include #include #include -#include #include "buf.h" #include "flow.h" @@ -73,7 +72,7 @@ static void outgoing_connect_next(struct outgoing *outgoing) { } static void outgoing_connect_handler(struct peer *peer) { - struct outgoing *outgoing = (struct outgoing *) peer; + struct outgoing *outgoing = container_of(peer, struct outgoing, peer); peer_epoll_del(&outgoing->peer); @@ -84,7 +83,7 @@ static void outgoing_connect_handler(struct peer *peer) { } static void outgoing_disconnect_handler(struct peer *peer) { - struct outgoing *outgoing = (struct outgoing *) peer; + struct outgoing *outgoing = container_of(peer, struct outgoing, peer); LOG(outgoing->id, "Peer disconnected; reconnecting..."); outgoing_retry(outgoing); } @@ -121,7 +120,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { } static void outgoing_resolve_handler(struct peer *peer) { - struct outgoing *outgoing = (struct outgoing *) peer; + struct outgoing *outgoing = container_of(peer, struct outgoing, peer); int err = resolve_result(peer, &outgoing->addrs); if (err) { LOG(outgoing->id, "Failed to resolve %s/%s: %s", outgoing->node, outgoing->service, gai_strerror(err)); @@ -139,7 +138,7 @@ static void outgoing_resolve(struct outgoing *outgoing) { } static void outgoing_resolve_wrapper(struct peer *peer) { - outgoing_resolve((struct outgoing *) peer); + outgoing_resolve(container_of(peer, struct outgoing, peer)); } static void outgoing_del(struct outgoing *outgoing) { diff --git a/adsbus/receive.c b/adsbus/receive.c index 308672e..189e6b6 100644 --- a/adsbus/receive.c +++ b/adsbus/receive.c @@ -4,7 +4,6 @@ #include #include #include -#include #include "airspy_adsb.h" #include "beast.h" @@ -115,7 +114,7 @@ static void receive_del(struct receive *receive) { } static void receive_read(struct peer *peer) { - struct receive *receive = (struct receive *) peer; + struct receive *receive = container_of(peer, struct receive, peer); if (buf_fill(&receive->buf, receive->peer.fd) <= 0) { receive_del(receive); diff --git a/adsbus/send.c b/adsbus/send.c index ffd49f4..77b0732 100644 --- a/adsbus/send.c +++ b/adsbus/send.c @@ -99,7 +99,7 @@ static void send_del(struct send *send) { } static void send_del_wrapper(struct peer *peer) { - send_del((struct send *) peer); + send_del(container_of(peer, struct send, peer)); } static void send_new(int fd, void *passthrough, struct peer *on_close) { diff --git a/adsbus/send_receive.c b/adsbus/send_receive.c index 15af572..6e72b23 100644 --- a/adsbus/send_receive.c +++ b/adsbus/send_receive.c @@ -35,7 +35,7 @@ static void send_receive_del(struct send_receive *send_receive) { } static void send_receive_on_close(struct peer *peer) { - struct send_receive *send_receive = (struct send_receive *) peer; + struct send_receive *send_receive = container_of(peer, struct send_receive, peer); if (!--(send_receive->ref_count)) { send_receive_del(send_receive); From b915f38dcaee15de2cc66bce69745c88249f8712 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 21:53:57 -0800 Subject: [PATCH 24/35] Switch out custom wakeup system for timerfd. --- adsbus/outgoing.c | 2 +- adsbus/peer.c | 5 +-- adsbus/wakeup.c | 92 ++++++++++++++++++++--------------------------- adsbus/wakeup.h | 2 -- 4 files changed, 41 insertions(+), 60 deletions(-) diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 2df996a..fb1124b 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -111,7 +111,7 @@ static void outgoing_connect_result(struct outgoing *outgoing, int result) { default: LOG(outgoing->id, "Can't connect to %s/%s: %s", hbuf, sbuf, strerror(result)); - peer_close(&outgoing->peer); + assert(!close(outgoing->peer.fd)); outgoing->addr = outgoing->addr->ai_next; // Tail recursion :/ outgoing_connect_next(outgoing); diff --git a/adsbus/peer.c b/adsbus/peer.c index f1aa29a..92a2785 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -10,7 +10,6 @@ #include "log.h" #include "server.h" -#include "wakeup.h" #include "peer.h" @@ -118,7 +117,7 @@ void peer_loop() { } #define MAX_EVENTS 10 struct epoll_event events[MAX_EVENTS]; - int delay = list_is_empty(&peer_always_trigger_head) ? wakeup_get_delay() : 0; + int delay = list_is_empty(&peer_always_trigger_head) ? -1 : 0; int nfds = epoll_wait(peer_epoll_fd, events, MAX_EVENTS, delay); if (nfds == -1 && errno == EINTR) { continue; @@ -136,7 +135,5 @@ void peer_loop() { peer_call(iter); } } - - wakeup_dispatch(); } } diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index 5360a27..8aff73c 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -1,83 +1,69 @@ #include #include #include -#include +#include +#include #include "peer.h" #include "rand.h" #include "wakeup.h" -struct wakeup_entry { - int fd; - uint64_t absolute_time_ms; - struct peer *peer; +struct wakeup { + struct peer peer; + struct peer *inner_peer; struct list_head wakeup_list; }; static struct list_head wakeup_head = LIST_HEAD_INIT(wakeup_head); -static uint64_t wakeup_get_time_ms() { - struct timespec tp; - assert(!clock_gettime(CLOCK_MONOTONIC_COARSE, &tp)); -#define MS_PER_S UINT64_C(1000) -#define NS_PER_MS UINT64_C(1000000) - assert(tp.tv_sec >= 0); - assert(tp.tv_nsec >= 0); - return ((uint64_t) tp.tv_sec * MS_PER_S) + ((uint64_t) tp.tv_nsec / NS_PER_MS); +static void wakeup_handler(struct peer *peer) { + struct wakeup *wakeup = container_of(peer, struct wakeup, peer); + + uint64_t events; + assert(read(wakeup->peer.fd, &events, sizeof(events)) == sizeof(events)); + assert(events == 1); + + peer_close(&wakeup->peer); + peer_call(wakeup->inner_peer); + list_del(&wakeup->wakeup_list); + free(wakeup); } void wakeup_init() { } void wakeup_cleanup() { - struct wakeup_entry *iter, *next; + struct wakeup *iter, *next; list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) { free(iter); } } -int wakeup_get_delay() { - if (list_is_empty(&wakeup_head)) { - return -1; - } - uint64_t now = wakeup_get_time_ms(); - struct wakeup_entry *next_to_fire = list_entry(wakeup_head.next, struct wakeup_entry, wakeup_list); - if (next_to_fire->absolute_time_ms > now) { - uint64_t delta = next_to_fire->absolute_time_ms - now; - assert(delta < INT_MAX); - return (int) delta; - } else { - return 0; - } -} - -void wakeup_dispatch() { - uint64_t now = wakeup_get_time_ms(); - struct wakeup_entry *iter, *next; - list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) { - if (iter->absolute_time_ms > now) { - break; - } - peer_call(iter->peer); - list_del(&iter->wakeup_list); - free(iter); - } -} - void wakeup_add(struct peer *peer, uint32_t delay_ms) { - struct wakeup_entry *entry = malloc(sizeof(*entry)); - entry->absolute_time_ms = wakeup_get_time_ms() + delay_ms; - entry->peer = peer; + struct wakeup *wakeup = malloc(sizeof(*wakeup)); + wakeup->inner_peer = peer; + list_add(&wakeup->wakeup_list, &wakeup_head); - struct wakeup_entry *iter, *next; - list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) { - if (iter->absolute_time_ms > entry->absolute_time_ms) { - list_add(&entry->wakeup_list, &iter->wakeup_list); - return; - } - } - list_add(&entry->wakeup_list, &wakeup_head); + wakeup->peer.fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); + assert(wakeup->peer.fd >= 0); + +#define MS_PER_S UINT64_C(1000) +#define NS_PER_MS UINT64_C(1000000) + const struct itimerspec ts = { + .it_interval = { + .tv_sec = 0, + .tv_nsec = 0, + }, + .it_value = { + .tv_sec = delay_ms / MS_PER_S, + .tv_nsec = (delay_ms % MS_PER_S) * NS_PER_MS, + }, + }; + assert(!timerfd_settime(wakeup->peer.fd, 0, &ts, NULL)); + + wakeup->peer.event_handler = wakeup_handler; + peer_epoll_add(&wakeup->peer, EPOLLIN); } #define RETRY_MIN_MS 2000 diff --git a/adsbus/wakeup.h b/adsbus/wakeup.h index 77ffb3b..c11f7e3 100644 --- a/adsbus/wakeup.h +++ b/adsbus/wakeup.h @@ -6,7 +6,5 @@ struct peer; void wakeup_init(void); void wakeup_cleanup(void); -int wakeup_get_delay(void); -void wakeup_dispatch(void); void wakeup_add(struct peer *, uint32_t); uint32_t wakeup_get_retry_delay_ms(uint32_t); From ba13ebbe5c1b6af6e11da1d736796ab95a27c763 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 22:00:08 -0800 Subject: [PATCH 25/35] deheader --- adsbus/outgoing.c | 1 + adsbus/wakeup.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index fb1124b..3054219 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "buf.h" #include "flow.h" diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index 8aff73c..1b66073 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -1,5 +1,4 @@ #include -#include #include #include #include From e885723c59d6aa2ba37d4a82f1b03360f325fad5 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 22:26:04 -0800 Subject: [PATCH 26/35] Use signalfd to handle SIGINT and SIGTERM shutdown. --- adsbus/asyncaddrinfo.c | 6 ++++++ adsbus/peer.c | 42 ++++++++++++++++++------------------------ adsbus/peer.h | 1 - 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/adsbus/asyncaddrinfo.c b/adsbus/asyncaddrinfo.c index c8a8b23..6e2411a 100644 --- a/adsbus/asyncaddrinfo.c +++ b/adsbus/asyncaddrinfo.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,11 @@ static int asyncaddrinfo_write_fd; static void *asyncaddrinfo_main(void *arg) { int fd = (int) (intptr_t) arg; + + sigset_t sigmask; + assert(!sigfillset(&sigmask)); + assert(!pthread_sigmask(SIG_BLOCK, &sigmask, NULL)); + struct asyncaddrinfo_resolution *res; ssize_t len; while ((len = recv(fd, &res, sizeof(res), 0)) == sizeof(res)) { diff --git a/adsbus/peer.c b/adsbus/peer.c index 92a2785..864e661 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -18,14 +20,15 @@ static char log_module = 'X'; uint32_t peer_count_in = 0, peer_count_out = 0, peer_count_out_in = 0; static int peer_epoll_fd; -static int peer_shutdown_fd; +static struct peer peer_shutdown; static bool peer_shutdown_flag = false; static struct list_head peer_always_trigger_head = LIST_HEAD_INIT(peer_always_trigger_head); static void peer_shutdown_handler(struct peer *peer) { - LOG(server_id, "Shutting down"); - assert(!close(peer->fd)); - free(peer); + struct signalfd_siginfo siginfo; + assert(read(peer->fd, &siginfo, sizeof(siginfo)) == sizeof(siginfo)); + peer_close(peer); + LOG(server_id, "Received signal %u; shutting down", siginfo.ssi_signo); peer_shutdown_flag = true; } @@ -33,31 +36,22 @@ void peer_init() { peer_epoll_fd = epoll_create1(EPOLL_CLOEXEC); assert(peer_epoll_fd >= 0); - int shutdown_fds[2]; - assert(!socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, shutdown_fds)); + sigset_t sigmask; + assert(!sigemptyset(&sigmask)); + assert(!sigaddset(&sigmask, SIGINT)); + assert(!sigaddset(&sigmask, SIGTERM)); + peer_shutdown.fd = signalfd(-1, &sigmask, SFD_NONBLOCK | SFD_CLOEXEC); + assert(peer_shutdown.fd >= 0); + peer_shutdown.event_handler = peer_shutdown_handler; + peer_epoll_add(&peer_shutdown, EPOLLIN); - struct peer *shutdown_peer = malloc(sizeof(*shutdown_peer)); - assert(shutdown_peer); - shutdown_peer->fd = shutdown_fds[0]; - shutdown_peer->event_handler = peer_shutdown_handler; - peer_epoll_add(shutdown_peer, EPOLLRDHUP); - - peer_shutdown_fd = shutdown_fds[1]; - signal(SIGINT, peer_shutdown); - signal(SIGTERM, peer_shutdown); + assert(!sigprocmask(SIG_BLOCK, &sigmask, NULL)); } void peer_cleanup() { assert(!close(peer_epoll_fd)); } -void peer_shutdown(int __attribute__((unused)) signal) { - if (peer_shutdown_fd != -1) { - assert(!close(peer_shutdown_fd)); - peer_shutdown_fd = -1; - } -} - void peer_epoll_add(struct peer *peer, uint32_t events) { struct epoll_event ev = { .events = events, @@ -110,10 +104,10 @@ void peer_loop() { while (!peer_shutdown_flag) { if (!(peer_count_in + peer_count_out_in)) { LOG(server_id, "No remaining inputs"); - peer_shutdown(0); + peer_call(&peer_shutdown); } else if (!(peer_count_out + peer_count_out_in)) { LOG(server_id, "No remaining outputs"); - peer_shutdown(0); + peer_call(&peer_shutdown); } #define MAX_EVENTS 10 struct epoll_event events[MAX_EVENTS]; diff --git a/adsbus/peer.h b/adsbus/peer.h index 8c23cd4..9b39f94 100644 --- a/adsbus/peer.h +++ b/adsbus/peer.h @@ -18,7 +18,6 @@ extern uint32_t peer_count_in, peer_count_out, peer_count_out_in; void peer_init(void); void peer_cleanup(void); -void peer_shutdown(int signal); void peer_epoll_add(struct peer *, uint32_t); void peer_epoll_del(struct peer *); void peer_close(struct peer *); From 9f5a6223bf822031cdebe466c6b0a62e9a2219c6 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 22:30:55 -0800 Subject: [PATCH 27/35] Fix shutdown for lack of input/output. --- adsbus/peer.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/adsbus/peer.c b/adsbus/peer.c index 864e661..067f533 100644 --- a/adsbus/peer.c +++ b/adsbus/peer.c @@ -20,16 +20,20 @@ static char log_module = 'X'; uint32_t peer_count_in = 0, peer_count_out = 0, peer_count_out_in = 0; static int peer_epoll_fd; -static struct peer peer_shutdown; +static struct peer peer_shutdown_peer; static bool peer_shutdown_flag = false; static struct list_head peer_always_trigger_head = LIST_HEAD_INIT(peer_always_trigger_head); +static void peer_shutdown() { + peer_close(&peer_shutdown_peer); + peer_shutdown_flag = true; +} + static void peer_shutdown_handler(struct peer *peer) { struct signalfd_siginfo siginfo; assert(read(peer->fd, &siginfo, sizeof(siginfo)) == sizeof(siginfo)); - peer_close(peer); LOG(server_id, "Received signal %u; shutting down", siginfo.ssi_signo); - peer_shutdown_flag = true; + peer_shutdown(); } void peer_init() { @@ -40,10 +44,10 @@ void peer_init() { assert(!sigemptyset(&sigmask)); assert(!sigaddset(&sigmask, SIGINT)); assert(!sigaddset(&sigmask, SIGTERM)); - peer_shutdown.fd = signalfd(-1, &sigmask, SFD_NONBLOCK | SFD_CLOEXEC); - assert(peer_shutdown.fd >= 0); - peer_shutdown.event_handler = peer_shutdown_handler; - peer_epoll_add(&peer_shutdown, EPOLLIN); + peer_shutdown_peer.fd = signalfd(-1, &sigmask, SFD_NONBLOCK | SFD_CLOEXEC); + assert(peer_shutdown_peer.fd >= 0); + peer_shutdown_peer.event_handler = peer_shutdown_handler; + peer_epoll_add(&peer_shutdown_peer, EPOLLIN); assert(!sigprocmask(SIG_BLOCK, &sigmask, NULL)); } @@ -104,10 +108,12 @@ void peer_loop() { while (!peer_shutdown_flag) { if (!(peer_count_in + peer_count_out_in)) { LOG(server_id, "No remaining inputs"); - peer_call(&peer_shutdown); + peer_shutdown(); + break; } else if (!(peer_count_out + peer_count_out_in)) { LOG(server_id, "No remaining outputs"); - peer_call(&peer_shutdown); + peer_shutdown(); + break; } #define MAX_EVENTS 10 struct epoll_event events[MAX_EVENTS]; From c0c25bd36a3d5b7cbbec414dc36b8df42f60c9fa Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 22:42:15 -0800 Subject: [PATCH 28/35] Clean up wakeup teardown. --- adsbus/outgoing.c | 1 + adsbus/wakeup.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/adsbus/outgoing.c b/adsbus/outgoing.c index 3054219..a868579 100644 --- a/adsbus/outgoing.c +++ b/adsbus/outgoing.c @@ -45,6 +45,7 @@ static void outgoing_resolve(struct outgoing *); static void outgoing_resolve_wrapper(struct peer *); static void outgoing_retry(struct outgoing *outgoing) { + outgoing->peer.fd = -1; uint32_t delay = wakeup_get_retry_delay_ms(++outgoing->attempt); LOG(outgoing->id, "Will retry in %ds", delay / 1000); outgoing->peer.event_handler = outgoing_resolve_wrapper; diff --git a/adsbus/wakeup.c b/adsbus/wakeup.c index 1b66073..1dfb5b9 100644 --- a/adsbus/wakeup.c +++ b/adsbus/wakeup.c @@ -16,6 +16,12 @@ struct wakeup { static struct list_head wakeup_head = LIST_HEAD_INIT(wakeup_head); +static void wakeup_del(struct wakeup *wakeup) { + peer_close(&wakeup->peer); + list_del(&wakeup->wakeup_list); + free(wakeup); +} + static void wakeup_handler(struct peer *peer) { struct wakeup *wakeup = container_of(peer, struct wakeup, peer); @@ -23,10 +29,8 @@ static void wakeup_handler(struct peer *peer) { assert(read(wakeup->peer.fd, &events, sizeof(events)) == sizeof(events)); assert(events == 1); - peer_close(&wakeup->peer); peer_call(wakeup->inner_peer); - list_del(&wakeup->wakeup_list); - free(wakeup); + wakeup_del(wakeup); } void wakeup_init() { @@ -35,7 +39,7 @@ void wakeup_init() { void wakeup_cleanup() { struct wakeup *iter, *next; list_for_each_entry_safe(iter, next, &wakeup_head, wakeup_list) { - free(iter); + wakeup_del(iter); } } From b455826db2e64fb0e48e763939c1278f5d89f6a7 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 23:04:20 -0800 Subject: [PATCH 29/35] Switch to signalfd for SIGHUP --- adsbus/adsbus.c | 4 +++- adsbus/log.c | 34 ++++++++++++++++------------------ adsbus/log.h | 3 ++- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index d50207f..a7b1960 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -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(); diff --git a/adsbus/log.c b/adsbus/log.c index 40fb95a..279cf7b 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -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; diff --git a/adsbus/log.h b/adsbus/log.h index 34f2778..6494eb6 100644 --- a/adsbus/log.h +++ b/adsbus/log.h @@ -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))); From 62ee0a690c36dd7218ee17c428c7401d42a62c16 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 23:17:26 -0800 Subject: [PATCH 30/35] More startup debug info. --- adsbus/server.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/adsbus/server.c b/adsbus/server.c index 14a336a..7319c70 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -43,16 +44,27 @@ void server_opts_add() { void server_init() { uuid_gen(server_id); - LOG(server_id, "Server start:"); + LOG(server_id, "Server start"); + LOG(server_id, "Build data:"); LOG(server_id, "\tgit_last_change: %s", GIT_LAST_CHANGE); LOG(server_id, "\tgit_local_clean: %s", GIT_LOCAL_CLEAN ? "true" : "false"); LOG(server_id, "\tclang_version: %s", __clang_version__); LOG(server_id, "\tglibc_version: %d.%d", __GLIBC__, __GLIBC_MINOR__); LOG(server_id, "\tjansson_version: %s", JANSSON_VERSION); LOG(server_id, "\tprotobuf-c_version: %s", PROTOBUF_C_VERSION); - LOG(server_id, "\tbuild_datetime: %s", __DATE__ " " __TIME__); - LOG(server_id, "\tbuild_username: %s", USERNAME); - LOG(server_id, "\tbuild_hostname: %s", HOSTNAME); + LOG(server_id, "\tdatetime: %s", __DATE__ " " __TIME__); + LOG(server_id, "\tusername: %s", USERNAME); + LOG(server_id, "\thostname: %s", HOSTNAME); + + LOG(server_id, "Runtime data:"); + struct utsname utsname; + assert(!uname(&utsname)); + LOG(server_id, "\tusername: %s", getlogin()); + LOG(server_id, "\thostname: %s", utsname.nodename); + LOG(server_id, "\tsystem: %s", utsname.sysname); + LOG(server_id, "\trelease: %s", utsname.release); + LOG(server_id, "\tversion: %s", utsname.version); + LOG(server_id, "\tmachine: %s", utsname.machine); opts_call(server_opts); } From 7167c080a29e713430c9a83f4ba972c18041d119 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 8 Mar 2016 23:19:21 -0800 Subject: [PATCH 31/35] Log pre-fork process ID. --- adsbus/server.c | 1 + 1 file changed, 1 insertion(+) diff --git a/adsbus/server.c b/adsbus/server.c index 7319c70..6f3d6df 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -61,6 +61,7 @@ void server_init() { assert(!uname(&utsname)); LOG(server_id, "\tusername: %s", getlogin()); LOG(server_id, "\thostname: %s", utsname.nodename); + LOG(server_id, "\tprocess_id: %d", getpid()); LOG(server_id, "\tsystem: %s", utsname.sysname); LOG(server_id, "\trelease: %s", utsname.release); LOG(server_id, "\tversion: %s", utsname.version); From fc14de31ff564a56e89063da99433f7ecd06b665 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 9 Mar 2016 12:55:21 -0800 Subject: [PATCH 32/35] Only grab SIGHUP if we can do something with it. --- adsbus/log.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/adsbus/log.c b/adsbus/log.c index 279cf7b..5ed9ac7 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -90,15 +90,19 @@ void log_init() { } 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); + if (log_path) { + 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); - assert(!sigprocmask(SIG_BLOCK, &sigmask, NULL)); + assert(!sigprocmask(SIG_BLOCK, &sigmask, NULL)); + } else { + log_rotate_peer.fd = -1; + } } void log_cleanup_peer() { From 87f82b032779088d40cb4375c330a1ddf26bfe74 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 9 Mar 2016 13:06:26 -0800 Subject: [PATCH 33/35] Keep fd 2 open as our log fd, so other things that write there (e.g. assert()) work sanely. --- adsbus/adsbus.c | 2 -- adsbus/log.c | 25 ++++++++++++------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/adsbus/adsbus.c b/adsbus/adsbus.c index a7b1960..c1bdded 100644 --- a/adsbus/adsbus.c +++ b/adsbus/adsbus.c @@ -76,7 +76,6 @@ 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(); @@ -104,7 +103,6 @@ int main(int argc, char *argv[]) { assert(!close(STDIN_FILENO)); assert(!close(STDOUT_FILENO)); - assert(!close(STDERR_FILENO)); return EXIT_SUCCESS; } diff --git a/adsbus/log.c b/adsbus/log.c index 5ed9ac7..3dbcc26 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -29,21 +29,25 @@ static opts_group log_opts; static char log_module = 'L'; -static void log_rotate() { +static void log_open() { if (!log_path) { - LOG(log_id, "Asked to rotate logs but not logging to a file; ignoring"); return; } + int fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY | O_CLOEXEC, S_IRUSR | S_IWUSR); + assert(fd >= 0); + assert(dup3(fd, STDERR_FILENO, O_CLOEXEC) == STDERR_FILENO); + assert(!close(fd)); +} + +static void log_rotate() { + assert(log_path); uint8_t old_log_id[UUID_LEN], new_log_id[UUID_LEN]; uuid_gen(new_log_id); LOG(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); - setlinebuf(log_stream); + log_open(); LOG(log_id, "Log start after switch from log ID %s", old_log_id); } @@ -75,13 +79,8 @@ void log_opts_add() { void log_init() { opts_call(log_opts); - if (log_path) { - log_stream = fopen(log_path, "a"); - } else { - int fd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 0); - assert(fd >= 0); - log_stream = fdopen(fd, "a"); - } + log_open(); + log_stream = fdopen(STDERR_FILENO, "a"); assert(log_stream); setlinebuf(log_stream); From cf923fe3fbe2aaa386652491774d2f2def208089 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 9 Mar 2016 13:10:07 -0800 Subject: [PATCH 34/35] Add --quiet --- adsbus/log.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/adsbus/log.c b/adsbus/log.c index 3dbcc26..9fabea2 100644 --- a/adsbus/log.c +++ b/adsbus/log.c @@ -25,6 +25,7 @@ static char *log_path = NULL; static uint8_t log_id[UUID_LEN]; static struct peer log_rotate_peer; static bool log_timestamps = false; +static bool log_quiet = false; static opts_group log_opts; static char log_module = 'L'; @@ -72,9 +73,15 @@ static bool log_enable_timestamps(const char __attribute__ ((unused)) *arg) { return true; } +static bool log_set_quiet(const char __attribute__ ((unused)) *arg) { + log_quiet = true; + return true; +} + void log_opts_add() { opts_add("log-file", "PATH", log_set_path, log_opts); opts_add("log-timestamps", NULL, log_enable_timestamps, log_opts); + opts_add("quiet", NULL, log_set_quiet, log_opts); } void log_init() { @@ -118,6 +125,10 @@ void log_cleanup() { } void log_write(char type, const char *loc, const uint8_t *id, const char *fmt, ...) { + if (log_quiet) { + return; + } + va_list ap; va_start(ap, fmt); From e03abd4f7492826999dbc01d742d1494c285dea8 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 9 Mar 2016 13:15:15 -0800 Subject: [PATCH 35/35] Add --pid-file --- adsbus/server.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/adsbus/server.c b/adsbus/server.c index 6f3d6df..4978c39 100644 --- a/adsbus/server.c +++ b/adsbus/server.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -17,7 +19,7 @@ uint8_t server_id[UUID_LEN]; char server_version[] = "https://github.com/flamingcowtv/adsb-tools#1"; -static opts_group server_opts; +static opts_group server_opts1, server_opts2; static char log_module = 'X'; @@ -38,8 +40,19 @@ static bool server_detach(const char __attribute__ ((unused)) *arg) { return true; } +static bool server_pid_file(const char *path) { + pid_t pid = getpid(); + LOG(server_id, "Writing process ID %d to pid-file: %s", pid, path); + FILE *fh = fopen(path, "w"); + assert(fh); + assert(fprintf(fh, "%d\n", pid) > 0); + assert(!fclose(fh)); + return true; +} + void server_opts_add() { - opts_add("detach", NULL, server_detach, server_opts); + opts_add("detach", NULL, server_detach, server_opts1); + opts_add("pid-file", "PATH", server_pid_file, server_opts2); } void server_init() { @@ -67,5 +80,6 @@ void server_init() { LOG(server_id, "\tversion: %s", utsname.version); LOG(server_id, "\tmachine: %s", utsname.machine); - opts_call(server_opts); + opts_call(server_opts1); + opts_call(server_opts2); }