blob: 3687f15b0ac0238bf34cc8074b4bf4e440001217 [file] [log] [blame]
Harald Weltef5a0fa32019-03-03 15:44:18 +01001#include <unistd.h>
Harald Weltece638d82019-03-17 09:36:04 +01002#include <signal.h>
Harald Weltef5a0fa32019-03-03 15:44:18 +01003
Harald Weltecd7fcd72019-12-03 21:29:07 +01004#define _GNU_SOURCE
5#include <getopt.h>
6
Harald Weltef5a0fa32019-03-03 15:44:18 +01007#include <sys/eventfd.h>
8
9#include <osmocom/core/utils.h>
10#include <osmocom/core/talloc.h>
Harald Welte73bbd542021-12-08 20:39:55 +010011#include <osmocom/core/logging.h>
12#include <osmocom/core/fsm.h>
Harald Weltef5a0fa32019-03-03 15:44:18 +010013#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
21struct rspro_server *g_rps;
22void *g_tall_ctx;
Harald Welteb54a51e2019-03-31 15:57:59 +020023__thread void *talloc_asn1_ctx;
24
Harald Weltef5a0fa32019-03-03 15:44:18 +010025struct osmo_fd g_event_ofd;
26
Harald Weltece638d82019-03-17 09:36:04 +010027static void handle_sig_usr1(int signal)
28{
29 OSMO_ASSERT(signal == SIGUSR1);
Harald Welteb54a51e2019-03-31 15:57:59 +020030 talloc_report_full(g_tall_ctx, stderr);
Harald Weltece638d82019-03-17 09:36:04 +010031}
32
Harald Weltecd7fcd72019-12-03 21:29:07 +010033static void print_help()
34{
35 printf( " Some useful help...\n"
James Tavarese206e6a2022-11-15 08:04:03 -050036 " -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 Weltecd7fcd72019-12-03 21:29:07 +010040 );
41}
42
43static 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 Welte80a01102020-05-25 14:55:12 +020050 { "debug", 1, 0, 'd' },
James Tavarese206e6a2022-11-15 08:04:03 -050051 { "disable-color", 0, 0, 'L' },
52 { 0, 0, 0, 0 }
Harald Weltecd7fcd72019-12-03 21:29:07 +010053 };
54
James Tavarese206e6a2022-11-15 08:04:03 -050055 c = getopt_long(argc, argv, "hVd:L", long_options, &option_index);
Harald Weltecd7fcd72019-12-03 21:29:07 +010056 if (c == -1)
57 break;
58
59 switch (c) {
60 case 'h':
61 print_help();
62 exit(0);
63 break;
Harald Welte80a01102020-05-25 14:55:12 +020064 case 'd':
65 log_parse_category_mask(osmo_stderr_target, optarg);
66 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +010067 case 'V':
68 printf("osmo-resmim-server version %s\n", VERSION);
69 exit(0);
70 break;
James Tavarese206e6a2022-11-15 08:04:03 -050071 case 'L':
72 log_set_use_color(osmo_stderr_target, 0);
73 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +010074 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 Weltef5a0fa32019-03-03 15:44:18 +010086int main(int argc, char **argv)
87{
Harald Welteeea631b2022-05-03 15:14:19 +020088 char hostname[256];
Harald Welte0c50c342019-07-21 20:16:29 +020089 void *talloc_rest_ctx;
Harald Weltef5a0fa32019-03-03 15:44:18 +010090 int rc;
91
Harald Welteeea631b2022-05-03 15:14:19 +020092 if (gethostname(hostname, sizeof(hostname)) < 0)
93 OSMO_STRLCPY_ARRAY(hostname, "unknown");
94
Harald Weltef5a0fa32019-03-03 15:44:18 +010095 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +020096 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Welte0c50c342019-07-21 20:16:29 +020097 talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest");
Harald Welte92fd7342019-07-18 18:46:48 +020098 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +010099
100 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +0100101 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 Weltea9d7ad12021-12-08 21:09:12 +0100105 log_set_print_tid(osmo_stderr_target, 1);
106 log_enable_multithread();
Harald Weltef5a0fa32019-03-03 15:44:18 +0100107
Harald Weltecd7fcd72019-12-03 21:29:07 +0100108 handle_options(argc, argv);
109
Harald Weltef5a0fa32019-03-03 15:44:18 +0100110 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 Welteeea631b2022-05-03 15:14:19 +0200118 OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, hostname);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100119 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 Weltea5daec42020-10-18 22:40:28 +0200126 osmo_fd_setup(&g_event_ofd, rc, OSMO_FD_READ, event_fd_cb, g_rps, 0);
Harald Welteae0d9d22023-02-03 20:25:56 +0100127 rc = osmo_fd_register(&g_event_ofd);
128 if (rc < 0)
129 goto out_eventfd;
Harald Weltef5a0fa32019-03-03 15:44:18 +0100130
Harald Weltece638d82019-03-17 09:36:04 +0100131 signal(SIGUSR1, handle_sig_usr1);
132
Harald Welte0c50c342019-07-21 20:16:29 +0200133 rc = rest_api_init(talloc_rest_ctx, 9997);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100134 if (rc < 0)
Harald Welteae0d9d22023-02-03 20:25:56 +0100135 goto out_unregister;
Harald Weltef5a0fa32019-03-03 15:44:18 +0100136
137 while (1) {
138 osmo_select_main(0);
139 }
140
141 rest_api_fini();
142
143 exit(0);
144
Harald Welteae0d9d22023-02-03 20:25:56 +0100145out_unregister:
146 osmo_fd_unregister(&g_event_ofd);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100147out_eventfd:
148 close(g_event_ofd.fd);
149out_rps:
150 talloc_free(g_rps->slotmaps);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100151out_rspro:
152 rspro_server_destroy(g_rps);
153
154 exit(1);
155}