blob: 419dcad96a3eb134658cc263df67afd728a36c88 [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"
36 " -h --help This text\n"
37 " -V --version Print version of the program\n"
Harald Welte80a01102020-05-25 14:55:12 +020038 " -d --debug option Enable debug logging (e.g. DMAIN:DST2)\n"
Harald Weltecd7fcd72019-12-03 21:29:07 +010039 );
40}
41
42static void handle_options(int argc, char **argv)
43{
44 while (1) {
45 int option_index = 0, c;
46 static struct option long_options[] = {
47 { "help", 0, 0, 'h' },
48 { "version", 0, 0, 'V' },
Harald Welte80a01102020-05-25 14:55:12 +020049 { "debug", 1, 0, 'd' },
Harald Weltecd7fcd72019-12-03 21:29:07 +010050 {0, 0, 0, 0}
51 };
52
Harald Welte80a01102020-05-25 14:55:12 +020053 c = getopt_long(argc, argv, "hVd:", long_options, &option_index);
Harald Weltecd7fcd72019-12-03 21:29:07 +010054 if (c == -1)
55 break;
56
57 switch (c) {
58 case 'h':
59 print_help();
60 exit(0);
61 break;
Harald Welte80a01102020-05-25 14:55:12 +020062 case 'd':
63 log_parse_category_mask(osmo_stderr_target, optarg);
64 break;
Harald Weltecd7fcd72019-12-03 21:29:07 +010065 case 'V':
66 printf("osmo-resmim-server version %s\n", VERSION);
67 exit(0);
68 break;
69 default:
70 /* ignore */
71 break;
72 }
73 }
74
75 if (argc > optind) {
76 fprintf(stderr, "Unsupported extra positional arguments in command line\n");
77 exit(2);
78 }
79}
80
Harald Weltef5a0fa32019-03-03 15:44:18 +010081int main(int argc, char **argv)
82{
Harald Welteeea631b2022-05-03 15:14:19 +020083 char hostname[256];
Harald Welte0c50c342019-07-21 20:16:29 +020084 void *talloc_rest_ctx;
Harald Weltef5a0fa32019-03-03 15:44:18 +010085 int rc;
86
Harald Welteeea631b2022-05-03 15:14:19 +020087 if (gethostname(hostname, sizeof(hostname)) < 0)
88 OSMO_STRLCPY_ARRAY(hostname, "unknown");
89
Harald Weltef5a0fa32019-03-03 15:44:18 +010090 g_tall_ctx = talloc_named_const(NULL, 0, "global");
Harald Welteb54a51e2019-03-31 15:57:59 +020091 talloc_asn1_ctx = talloc_named_const(g_tall_ctx, 0, "asn1");
Harald Welte0c50c342019-07-21 20:16:29 +020092 talloc_rest_ctx = talloc_named_const(g_tall_ctx, 0, "rest");
Harald Welte92fd7342019-07-18 18:46:48 +020093 msgb_talloc_ctx_init(g_tall_ctx, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +010094
95 osmo_init_logging2(g_tall_ctx, &log_info);
Harald Welte73bbd542021-12-08 20:39:55 +010096 log_set_print_level(osmo_stderr_target, 1);
97 log_set_print_category(osmo_stderr_target, 1);
98 log_set_print_category_hex(osmo_stderr_target, 0);
99 osmo_fsm_log_addr(0);
Harald Weltea9d7ad12021-12-08 21:09:12 +0100100 log_set_print_tid(osmo_stderr_target, 1);
101 log_enable_multithread();
Harald Weltef5a0fa32019-03-03 15:44:18 +0100102
Harald Weltecd7fcd72019-12-03 21:29:07 +0100103 handle_options(argc, argv);
104
Harald Weltef5a0fa32019-03-03 15:44:18 +0100105 g_rps = rspro_server_create(g_tall_ctx, "0.0.0.0", 9998);
106 if (!g_rps)
107 exit(1);
108 g_rps->slotmaps = slotmap_init(g_rps);
109 if (!g_rps->slotmaps)
110 goto out_rspro;
111
112 g_rps->comp_id.type = ComponentType_remsimServer;
Harald Welteeea631b2022-05-03 15:14:19 +0200113 OSMO_STRLCPY_ARRAY(g_rps->comp_id.name, hostname);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100114 OSMO_STRLCPY_ARRAY(g_rps->comp_id.software, "remsim-server");
115 OSMO_STRLCPY_ARRAY(g_rps->comp_id.sw_version, PACKAGE_VERSION);
116 /* FIXME: other members of app_comp_id */
117
118 rc = eventfd(0, 0);
119 if (rc < 0)
120 goto out_rps;
Harald Weltea5daec42020-10-18 22:40:28 +0200121 osmo_fd_setup(&g_event_ofd, rc, OSMO_FD_READ, event_fd_cb, g_rps, 0);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100122 osmo_fd_register(&g_event_ofd);
123
Harald Weltece638d82019-03-17 09:36:04 +0100124 signal(SIGUSR1, handle_sig_usr1);
125
Harald Welte0c50c342019-07-21 20:16:29 +0200126 rc = rest_api_init(talloc_rest_ctx, 9997);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100127 if (rc < 0)
128 goto out_eventfd;
129
130 while (1) {
131 osmo_select_main(0);
132 }
133
134 rest_api_fini();
135
136 exit(0);
137
138out_eventfd:
139 close(g_event_ofd.fd);
140out_rps:
141 talloc_free(g_rps->slotmaps);
Harald Weltef5a0fa32019-03-03 15:44:18 +0100142out_rspro:
143 rspro_server_destroy(g_rps);
144
145 exit(1);
146}