Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 1 | #include <unistd.h> |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 2 | #include <signal.h> |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 3 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 4 | #define _GNU_SOURCE |
| 5 | #include <getopt.h> |
| 6 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 7 | #include <sys/eventfd.h> |
| 8 | |
| 9 | #include <osmocom/core/utils.h> |
| 10 | #include <osmocom/core/talloc.h> |
| 11 | #include <osmocom/core/application.h> |
| 12 | #include <osmocom/core/select.h> |
| 13 | |
| 14 | #include "debug.h" |
| 15 | #include "slotmap.h" |
| 16 | #include "rest_api.h" |
| 17 | #include "rspro_server.h" |
| 18 | |
| 19 | struct rspro_server *g_rps; |
| 20 | void *g_tall_ctx; |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 21 | __thread void *talloc_asn1_ctx; |
| 22 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 23 | struct osmo_fd g_event_ofd; |
| 24 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 25 | static void handle_sig_usr1(int signal) |
| 26 | { |
| 27 | OSMO_ASSERT(signal == SIGUSR1); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 28 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 31 | static void print_help() |
| 32 | { |
| 33 | printf( " Some useful help...\n" |
| 34 | " -h --help This text\n" |
| 35 | " -V --version Print version of the program\n" |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | static void handle_options(int argc, char **argv) |
| 40 | { |
| 41 | while (1) { |
| 42 | int option_index = 0, c; |
| 43 | static struct option long_options[] = { |
| 44 | { "help", 0, 0, 'h' }, |
| 45 | { "version", 0, 0, 'V' }, |
| 46 | {0, 0, 0, 0} |
| 47 | }; |
| 48 | |
| 49 | c = getopt_long(argc, argv, "hV", long_options, &option_index); |
| 50 | if (c == -1) |
| 51 | break; |
| 52 | |
| 53 | switch (c) { |
| 54 | case 'h': |
| 55 | print_help(); |
| 56 | exit(0); |
| 57 | break; |
| 58 | case 'V': |
| 59 | printf("osmo-resmim-server version %s\n", VERSION); |
| 60 | exit(0); |
| 61 | break; |
| 62 | default: |
| 63 | /* ignore */ |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (argc > optind) { |
| 69 | fprintf(stderr, "Unsupported extra positional arguments in command line\n"); |
| 70 | exit(2); |
| 71 | } |
| 72 | } |
| 73 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 74 | int main(int argc, char **argv) |
| 75 | { |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 76 | void *talloc_rest_ctx; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 77 | int rc; |
| 78 | |
| 79 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 80 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 81 | talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest"); |
Harald Welte | 92fd734 | 2019-07-18 18:46:48 +0200 | [diff] [blame] | 82 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 83 | |
| 84 | osmo_init_logging2(g_tall_ctx, &log_info); |
| 85 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 86 | handle_options(argc, argv); |
| 87 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 88 | g_rps = rspro_server_create(g_tall_ctx, "0.0.0.0", 9998); |
| 89 | if (!g_rps) |
| 90 | exit(1); |
| 91 | g_rps->slotmaps = slotmap_init(g_rps); |
| 92 | if (!g_rps->slotmaps) |
| 93 | goto out_rspro; |
| 94 | |
| 95 | g_rps->comp_id.type = ComponentType_remsimServer; |
| 96 | OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, "fixme-name"); |
| 97 | OSMO_STRLCPY_ARRAY(g_rps->comp_id.software, "remsim-server"); |
| 98 | OSMO_STRLCPY_ARRAY(g_rps->comp_id.sw_version, PACKAGE_VERSION); |
| 99 | /* FIXME: other members of app_comp_id */ |
| 100 | |
| 101 | rc = eventfd(0, 0); |
| 102 | if (rc < 0) |
| 103 | goto out_rps; |
| 104 | osmo_fd_setup(&g_event_ofd, rc, BSC_FD_READ, event_fd_cb, g_rps, 0); |
| 105 | osmo_fd_register(&g_event_ofd); |
| 106 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 107 | signal(SIGUSR1, handle_sig_usr1); |
| 108 | |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 109 | rc = rest_api_init(talloc_rest_ctx, 9997); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 110 | if (rc < 0) |
| 111 | goto out_eventfd; |
| 112 | |
| 113 | while (1) { |
| 114 | osmo_select_main(0); |
| 115 | } |
| 116 | |
| 117 | rest_api_fini(); |
| 118 | |
| 119 | exit(0); |
| 120 | |
| 121 | out_eventfd: |
| 122 | close(g_event_ofd.fd); |
| 123 | out_rps: |
| 124 | talloc_free(g_rps->slotmaps); |
| 125 | talloc_free(g_rps); |
| 126 | out_rspro: |
| 127 | rspro_server_destroy(g_rps); |
| 128 | |
| 129 | exit(1); |
| 130 | } |