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);