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> |
Harald Welte | 73bbd54 | 2021-12-08 20:39:55 +0100 | [diff] [blame] | 11 | #include <osmocom/core/logging.h> |
| 12 | #include <osmocom/core/fsm.h> |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 13 | #include <osmocom/core/application.h> |
| 14 | #include <osmocom/core/select.h> |
| 15 | |
| 16 | #include "debug.h" |
| 17 | #include "slotmap.h" |
| 18 | #include "rest_api.h" |
| 19 | #include "rspro_server.h" |
| 20 | |
| 21 | struct rspro_server *g_rps; |
| 22 | void *g_tall_ctx; |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 23 | __thread void *talloc_asn1_ctx; |
| 24 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 25 | struct osmo_fd g_event_ofd; |
| 26 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 27 | static void handle_sig_usr1(int signal) |
| 28 | { |
| 29 | OSMO_ASSERT(signal == SIGUSR1); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 30 | talloc_report_full(g_tall_ctx, stderr); |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 33 | static void print_help() |
| 34 | { |
| 35 | printf( " Some useful help...\n" |
James Tavares | e206e6a | 2022-11-15 08:04:03 -0500 | [diff] [blame] | 36 | " -h --help This text\n" |
| 37 | " -V --version Print version of the program\n" |
| 38 | " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n" |
| 39 | " -L --disable-color Disable colors for logging to stderr\n" |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 40 | ); |
| 41 | } |
| 42 | |
| 43 | static void handle_options(int argc, char **argv) |
| 44 | { |
| 45 | while (1) { |
| 46 | int option_index = 0, c; |
| 47 | static struct option long_options[] = { |
| 48 | { "help", 0, 0, 'h' }, |
| 49 | { "version", 0, 0, 'V' }, |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 50 | { "debug", 1, 0, 'd' }, |
James Tavares | e206e6a | 2022-11-15 08:04:03 -0500 | [diff] [blame] | 51 | { "disable-color", 0, 0, 'L' }, |
| 52 | { 0, 0, 0, 0 } |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
James Tavares | e206e6a | 2022-11-15 08:04:03 -0500 | [diff] [blame] | 55 | c = getopt_long(argc, argv, "hVd:L", long_options, &option_index); |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 56 | if (c == -1) |
| 57 | break; |
| 58 | |
| 59 | switch (c) { |
| 60 | case 'h': |
| 61 | print_help(); |
| 62 | exit(0); |
| 63 | break; |
Harald Welte | 80a0110 | 2020-05-25 14:55:12 +0200 | [diff] [blame] | 64 | case 'd': |
| 65 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 66 | break; |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 67 | case 'V': |
| 68 | printf("osmo-resmim-server version %s\n", VERSION); |
| 69 | exit(0); |
| 70 | break; |
James Tavares | e206e6a | 2022-11-15 08:04:03 -0500 | [diff] [blame] | 71 | case 'L': |
| 72 | log_set_use_color(osmo_stderr_target, 0); |
| 73 | break; |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 74 | default: |
| 75 | /* ignore */ |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (argc > optind) { |
| 81 | fprintf(stderr, "Unsupported extra positional arguments in command line\n"); |
| 82 | exit(2); |
| 83 | } |
| 84 | } |
| 85 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 86 | int main(int argc, char **argv) |
| 87 | { |
Harald Welte | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 88 | char hostname[256]; |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 89 | void *talloc_rest_ctx; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 90 | int rc; |
| 91 | |
Harald Welte | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 92 | if (gethostname(hostname, sizeof(hostname)) < 0) |
| 93 | OSMO_STRLCPY_ARRAY(hostname, "unknown"); |
| 94 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 95 | g_tall_ctx = talloc_named_const(NULL, 0, "global"); |
Harald Welte | b54a51e | 2019-03-31 15:57:59 +0200 | [diff] [blame] | 96 | talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1"); |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 97 | talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest"); |
Harald Welte | 92fd734 | 2019-07-18 18:46:48 +0200 | [diff] [blame] | 98 | msgb_talloc_ctx_init(g_tall_ctx, 0); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 99 | |
| 100 | osmo_init_logging2(g_tall_ctx, &log_info); |
Harald Welte | 73bbd54 | 2021-12-08 20:39:55 +0100 | [diff] [blame] | 101 | log_set_print_level(osmo_stderr_target, 1); |
| 102 | log_set_print_category(osmo_stderr_target, 1); |
| 103 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 104 | osmo_fsm_log_addr(0); |
Harald Welte | a9d7ad1 | 2021-12-08 21:09:12 +0100 | [diff] [blame] | 105 | log_set_print_tid(osmo_stderr_target, 1); |
| 106 | log_enable_multithread(); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 107 | |
Harald Welte | cd7fcd7 | 2019-12-03 21:29:07 +0100 | [diff] [blame] | 108 | handle_options(argc, argv); |
| 109 | |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 110 | g_rps = rspro_server_create(g_tall_ctx, "0.0.0.0", 9998); |
| 111 | if (!g_rps) |
| 112 | exit(1); |
| 113 | g_rps->slotmaps = slotmap_init(g_rps); |
| 114 | if (!g_rps->slotmaps) |
| 115 | goto out_rspro; |
| 116 | |
| 117 | g_rps->comp_id.type = ComponentType_remsimServer; |
Harald Welte | eea631b | 2022-05-03 15:14:19 +0200 | [diff] [blame] | 118 | OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, hostname); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 119 | OSMO_STRLCPY_ARRAY(g_rps->comp_id.software, "remsim-server"); |
| 120 | OSMO_STRLCPY_ARRAY(g_rps->comp_id.sw_version, PACKAGE_VERSION); |
| 121 | /* FIXME: other members of app_comp_id */ |
| 122 | |
| 123 | rc = eventfd(0, 0); |
| 124 | if (rc < 0) |
| 125 | goto out_rps; |
Harald Welte | a5daec4 | 2020-10-18 22:40:28 +0200 | [diff] [blame] | 126 | osmo_fd_setup(&g_event_ofd, rc, OSMO_FD_READ, event_fd_cb, g_rps, 0); |
Harald Welte | ae0d9d2 | 2023-02-03 20:25:56 +0100 | [diff] [blame] | 127 | rc = osmo_fd_register(&g_event_ofd); |
| 128 | if (rc < 0) |
| 129 | goto out_eventfd; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 130 | |
Harald Welte | ce638d8 | 2019-03-17 09:36:04 +0100 | [diff] [blame] | 131 | signal(SIGUSR1, handle_sig_usr1); |
| 132 | |
Harald Welte | 0c50c34 | 2019-07-21 20:16:29 +0200 | [diff] [blame] | 133 | rc = rest_api_init(talloc_rest_ctx, 9997); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 134 | if (rc < 0) |
Harald Welte | ae0d9d2 | 2023-02-03 20:25:56 +0100 | [diff] [blame] | 135 | goto out_unregister; |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 136 | |
| 137 | while (1) { |
| 138 | osmo_select_main(0); |
| 139 | } |
| 140 | |
| 141 | rest_api_fini(); |
| 142 | |
| 143 | exit(0); |
| 144 | |
Harald Welte | ae0d9d2 | 2023-02-03 20:25:56 +0100 | [diff] [blame] | 145 | out_unregister: |
| 146 | osmo_fd_unregister(&g_event_ofd); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 147 | out_eventfd: |
| 148 | close(g_event_ofd.fd); |
| 149 | out_rps: |
| 150 | talloc_free(g_rps->slotmaps); |
Harald Welte | f5a0fa3 | 2019-03-03 15:44:18 +0100 | [diff] [blame] | 151 | out_rspro: |
| 152 | rspro_server_destroy(g_rps); |
| 153 | |
| 154 | exit(1); |
| 155 | } |