blob: 17b23b2b975d7f8c24d8f48234f034d5bf50d206 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Welte1c369472010-01-12 10:46:27 +01003/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther5faf1d32012-05-11 23:28:19 +02004 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte0e3e88e2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte59b04682009-06-10 05:40:52 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte0e3e88e2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte59b04682009-06-10 05:40:52 +080016 *
Harald Welte0e3e88e2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte59b04682009-06-10 05:40:52 +080019 *
20 */
21
22#include <unistd.h>
Harald Welte59b04682009-06-10 05:40:52 +080023#include <time.h>
Harald Welte59b04682009-06-10 05:40:52 +080024#include <errno.h>
25#include <signal.h>
26#include <fcntl.h>
27#include <sys/stat.h>
28
29#define _GNU_SOURCE
30#include <getopt.h>
31
32#include <openbsc/db.h>
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +020033#include <osmocom/core/application.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010034#include <osmocom/core/select.h>
Jacob Erlbeck63fac682015-10-26 16:25:37 +010035#include <osmocom/core/stats.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/debug.h>
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +020037#include <osmocom/abis/abis.h>
38#include <osmocom/abis/e1_input.h>
Pablo Neira Ayusodd5fff42011-03-22 16:47:59 +010039#include <osmocom/core/talloc.h>
Harald Welted21adfe2009-11-30 19:37:18 +010040#include <openbsc/signal.h>
Holger Hans Peter Freytherc7efbe42010-06-30 14:30:35 +080041#include <openbsc/osmo_msc.h>
Neels Hofmeyr38639352017-02-23 21:49:55 +010042#include <openbsc/bsc_msc_data.h>
Harald Welte30f93fb2011-02-19 16:48:17 +010043#include <openbsc/sms_queue.h>
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +080044#include <openbsc/vty.h>
Pablo Neira Ayuso5bd96a72011-05-14 11:32:48 +020045#include <openbsc/bss.h>
Harald Welte28dce912011-05-24 13:25:38 +020046#include <openbsc/mncc.h>
Alexander Huemeree8e7ee2011-09-06 00:09:48 +020047#include <openbsc/token_auth.h>
48#include <openbsc/handover_decision.h>
49#include <openbsc/rrlp.h>
Harald Welte5919b6a2014-08-20 23:47:15 +020050#include <osmocom/ctrl/control_if.h>
Harald Welte6bcd3032014-08-24 17:54:49 +020051#include <osmocom/ctrl/ports.h>
Neels Hofmeyr4d92c432016-02-23 15:10:33 +010052#include <osmocom/ctrl/control_vty.h>
Harald Welte5919b6a2014-08-20 23:47:15 +020053#include <openbsc/ctrl.h>
Holger Hans Peter Freyther5faf1d32012-05-11 23:28:19 +020054#include <openbsc/osmo_bsc_rf.h>
Harald Weltebad8ffc2015-01-01 12:32:03 +010055#include <openbsc/smpp.h>
Harald Welte40152872010-05-16 20:52:23 +020056
Harald Welted9dea592011-03-04 13:53:51 +010057#include "../../bscconfig.h"
Harald Welte40152872010-05-16 20:52:23 +020058
Harald Welte59b04682009-06-10 05:40:52 +080059/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020060struct gsm_network *bsc_gsmnet = 0;
Harald Welte59b04682009-06-10 05:40:52 +080061static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020062static const char *config_file = "openbsc.cfg";
Neels Hofmeyrf428d9a2016-02-23 14:57:22 +010063static const char *rf_ctrl_path = NULL;
Harald Welte40152872010-05-16 20:52:23 +020064extern const char *openbsc_copyright;
Harald Weltec4ae1762010-08-25 19:43:54 +020065static int daemonize = 0;
Neels Hofmeyr54f31c62016-02-23 14:55:17 +010066static const char *mncc_sock_path = NULL;
Harald Weltecfa26852011-11-13 19:02:06 +010067static int use_db_counter = 1;
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +010068
69/* timer to store statistics */
70#define DB_SYNC_INTERVAL 60, 0
Jan Luebbe0f1f95e2012-12-27 00:40:32 +010071#define EXPIRE_INTERVAL 10, 0
72
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +020073static struct osmo_timer_list db_sync_timer;
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +010074
Harald Welte59b04682009-06-10 05:40:52 +080075static void create_pcap_file(char *file)
76{
77 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
78 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
79
80 if (fd < 0) {
81 perror("Failed to open file for pcap");
82 return;
83 }
84
85 e1_set_pcap_fd(fd);
86}
87
88static void print_usage()
89{
Harald Weltefaae5b82013-11-27 01:28:21 +010090 printf("Usage: osmo-nitb\n");
Harald Welte59b04682009-06-10 05:40:52 +080091}
92
93static void print_help()
94{
95 printf(" Some useful help...\n");
Neels Hofmeyre1aed092015-09-15 11:50:50 +020096 printf(" -h --help This text.\n");
97 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
98 printf(" -D --daemonize Fork the process into a background daemon.\n");
99 printf(" -c --config-file filename The config file to use.\n");
Holger Hans Peter Freytherb913c4d2010-03-25 02:41:38 +0100100 printf(" -s --disable-color\n");
Neels Hofmeyre1aed092015-09-15 11:50:50 +0200101 printf(" -l --database db-name The database to use.\n");
102 printf(" -a --authorize-everyone Authorize every new subscriber. Dangerous!\n");
103 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
104 printf(" -V --version Print the version of OpenBSC.\n");
105 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC.\n");
106 printf(" -e --log-level number Set a global loglevel.\n");
Neels Hofmeyr54f31c62016-02-23 14:55:17 +0100107 printf(" -M --mncc-sock-path PATH Disable built-in MNCC handler and offer socket.\n");
108 printf(" -m --mncc-sock Same as `-M /tmp/bsc_mncc' (deprecated).\n");
Neels Hofmeyre1aed092015-09-15 11:50:50 +0200109 printf(" -C --no-dbcounter Disable regular syncing of counters to database.\n");
Neels Hofmeyrf428d9a2016-02-23 14:57:22 +0100110 printf(" -r --rf-ctl PATH A unix domain socket to listen for cmds.\n");
Philippb9abf302016-10-14 18:08:00 +0200111 printf(" -p --pcap PATH Write abis communication to pcap trace file.\n");
Harald Welte59b04682009-06-10 05:40:52 +0800112}
113
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800114static void handle_options(int argc, char **argv)
Harald Welte59b04682009-06-10 05:40:52 +0800115{
116 while (1) {
Harald Weltea8379772009-06-20 22:36:41 +0200117 int option_index = 0, c;
Harald Welte59b04682009-06-10 05:40:52 +0800118 static struct option long_options[] = {
119 {"help", 0, 0, 'h'},
120 {"debug", 1, 0, 'd'},
Harald Weltec4ae1762010-08-25 19:43:54 +0200121 {"daemonize", 0, 0, 'D'},
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200122 {"config-file", 1, 0, 'c'},
Harald Welte59b04682009-06-10 05:40:52 +0800123 {"disable-color", 0, 0, 's'},
Harald Welte59b04682009-06-10 05:40:52 +0800124 {"database", 1, 0, 'l'},
125 {"authorize-everyone", 0, 0, 'a'},
Harald Welte59b04682009-06-10 05:40:52 +0800126 {"pcap", 1, 0, 'p'},
Harald Welte59b04682009-06-10 05:40:52 +0800127 {"timestamp", 0, 0, 'T'},
Harald Weltefa13cad2010-03-23 00:09:32 +0800128 {"version", 0, 0, 'V' },
Harald Welte3c062072009-07-28 18:25:29 +0200129 {"rtp-proxy", 0, 0, 'P'},
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100130 {"log-level", 1, 0, 'e'},
Harald Weltee532fb72010-12-23 01:31:07 +0100131 {"mncc-sock", 0, 0, 'm'},
Neels Hofmeyr54f31c62016-02-23 14:55:17 +0100132 {"mncc-sock-path", 1, 0, 'M'},
Harald Weltecfa26852011-11-13 19:02:06 +0100133 {"no-dbcounter", 0, 0, 'C'},
Holger Hans Peter Freyther5faf1d32012-05-11 23:28:19 +0200134 {"rf-ctl", 1, 0, 'r'},
Harald Welte59b04682009-06-10 05:40:52 +0800135 {0, 0, 0, 0}
136 };
137
Neels Hofmeyr54f31c62016-02-23 14:55:17 +0100138 c = getopt_long(argc, argv, "hd:Dsl:ar:p:TPVc:e:mCr:M:",
Harald Welte59b04682009-06-10 05:40:52 +0800139 long_options, &option_index);
140 if (c == -1)
141 break;
142
143 switch (c) {
144 case 'h':
145 print_usage();
146 print_help();
147 exit(0);
148 case 's':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200149 log_set_use_color(osmo_stderr_target, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800150 break;
151 case 'd':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200152 log_parse_category_mask(osmo_stderr_target, optarg);
Harald Welte59b04682009-06-10 05:40:52 +0800153 break;
Harald Weltec4ae1762010-08-25 19:43:54 +0200154 case 'D':
155 daemonize = 1;
156 break;
Harald Welte59b04682009-06-10 05:40:52 +0800157 case 'l':
Daniel Willmannc415da52014-05-21 15:46:44 +0200158 database_name = optarg;
Harald Welte59b04682009-06-10 05:40:52 +0800159 break;
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200160 case 'c':
Daniel Willmannc415da52014-05-21 15:46:44 +0200161 config_file = optarg;
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200162 break;
Harald Welte59b04682009-06-10 05:40:52 +0800163 case 'p':
164 create_pcap_file(optarg);
165 break;
Harald Welte59b04682009-06-10 05:40:52 +0800166 case 'T':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200167 log_set_print_timestamp(osmo_stderr_target, 1);
Harald Welte59b04682009-06-10 05:40:52 +0800168 break;
Harald Welte3c062072009-07-28 18:25:29 +0200169 case 'P':
170 ipacc_rtp_direct = 0;
171 break;
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100172 case 'e':
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200173 log_set_log_level(osmo_stderr_target, atoi(optarg));
Holger Hans Peter Freyther38e15c02010-03-25 02:39:15 +0100174 break;
Neels Hofmeyr54f31c62016-02-23 14:55:17 +0100175 case 'M':
176 mncc_sock_path = optarg;
177 break;
Harald Weltee532fb72010-12-23 01:31:07 +0100178 case 'm':
Neels Hofmeyr54f31c62016-02-23 14:55:17 +0100179 mncc_sock_path = "/tmp/bsc_mncc";
Harald Weltee532fb72010-12-23 01:31:07 +0100180 break;
Harald Weltecfa26852011-11-13 19:02:06 +0100181 case 'C':
182 use_db_counter = 0;
183 break;
Harald Weltefa13cad2010-03-23 00:09:32 +0800184 case 'V':
Harald Welte10c29f62010-05-16 19:20:24 +0200185 print_version(1);
Harald Weltefa13cad2010-03-23 00:09:32 +0800186 exit(0);
187 break;
Holger Hans Peter Freyther5faf1d32012-05-11 23:28:19 +0200188 case 'r':
Neels Hofmeyrf428d9a2016-02-23 14:57:22 +0100189 rf_ctrl_path = optarg;
Holger Hans Peter Freyther5faf1d32012-05-11 23:28:19 +0200190 break;
Harald Welte59b04682009-06-10 05:40:52 +0800191 default:
Neels Hofmeyrdeb56752016-02-23 14:59:43 +0100192 /* catch unknown options *as well as* missing arguments. */
193 fprintf(stderr, "Error in command line options. Exiting.\n");
194 exit(-1);
Harald Welte59b04682009-06-10 05:40:52 +0800195 break;
196 }
197 }
198}
199
Harald Welte0c836602009-12-24 10:51:56 +0100200extern void *tall_vty_ctx;
Harald Welte59b04682009-06-10 05:40:52 +0800201static void signal_handler(int signal)
202{
203 fprintf(stdout, "signal %u received\n", signal);
204
205 switch (signal) {
Harald Welte3b714502009-08-06 17:43:50 +0200206 case SIGINT:
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200207 bsc_shutdown_net(bsc_gsmnet);
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200208 osmo_signal_dispatch(SS_L_GLOBAL, S_L_GLOBAL_SHUTDOWN, NULL);
Harald Welte3b714502009-08-06 17:43:50 +0200209 sleep(3);
210 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800211 break;
Harald Welte4d5e6d52009-08-07 00:29:44 +0200212 case SIGABRT:
Harald Welte84282112016-08-25 16:35:26 +0900213 osmo_generate_backtrace();
Harald Welte4d5e6d52009-08-07 00:29:44 +0200214 /* in case of abort, we want to obtain a talloc report
215 * and then return to the caller, who will abort the process */
Harald Weltea8379772009-06-20 22:36:41 +0200216 case SIGUSR1:
Harald Welte0c836602009-12-24 10:51:56 +0100217 talloc_report(tall_vty_ctx, stderr);
Harald Weltea8379772009-06-20 22:36:41 +0200218 talloc_report_full(tall_bsc_ctx, stderr);
219 break;
Harald Welte0c836602009-12-24 10:51:56 +0100220 case SIGUSR2:
221 talloc_report_full(tall_vty_ctx, stderr);
222 break;
Harald Welte59b04682009-06-10 05:40:52 +0800223 default:
224 break;
225 }
226}
227
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100228/* timer handling */
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200229static int _db_store_counter(struct osmo_counter *counter, void *data)
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100230{
231 return db_store_counter(counter);
232}
233
234static void db_sync_timer_cb(void *data)
235{
236 /* store counters to database and re-schedule */
Pablo Neira Ayuso1c450742011-05-06 12:13:10 +0200237 osmo_counters_for_each(_db_store_counter, NULL);
Pablo Neira Ayuso840ccf62011-05-06 12:11:06 +0200238 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100239}
240
Jan Luebbe0f1f95e2012-12-27 00:40:32 +0100241static void subscr_expire_cb(void *data)
242{
Jacob Erlbeckda8770b2014-12-03 09:28:24 +0100243 subscr_expire(bsc_gsmnet->subscr_group);
Jan Luebbe0f1f95e2012-12-27 00:40:32 +0100244 osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL);
245}
246
Holger Hans Peter Freyther4ac8cd12015-09-24 15:38:38 +0200247extern int bsc_vty_go_parent(struct vty *vty);
Harald Welte778c4b92010-05-25 23:31:39 +0200248
249static struct vty_app_info vty_info = {
250 .name = "OpenBSC",
251 .version = PACKAGE_VERSION,
252 .go_parent_cb = bsc_vty_go_parent,
Holger Hans Peter Freyther19ce77c2010-08-26 15:38:42 +0800253 .is_config_node = bsc_vty_is_config_node,
Harald Welte778c4b92010-05-25 23:31:39 +0200254};
255
Harald Welte59b04682009-06-10 05:40:52 +0800256int main(int argc, char **argv)
257{
258 int rc;
259
Harald Welte778c4b92010-05-25 23:31:39 +0200260 vty_info.copyright = openbsc_copyright;
261
Harald Weltea8379772009-06-20 22:36:41 +0200262 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Neels Hofmeyra50599e2016-05-11 13:53:47 +0200263 talloc_ctx_init(tall_bsc_ctx);
Harald Welte (local)bd931f12009-08-13 13:52:14 +0200264 on_dso_load_token();
Harald Welte (local)95713782009-08-16 13:18:51 +0200265 on_dso_load_rrlp();
Harald Welteb90d7bd2009-12-17 00:31:10 +0100266 on_dso_load_ho_dec();
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200267
Pablo Neira Ayuso42e41df2011-08-17 22:44:07 +0200268 libosmo_abis_init(tall_bsc_ctx);
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200269 osmo_init_logging(&log_info);
Jacob Erlbeck63fac682015-10-26 16:25:37 +0100270 osmo_stats_init(tall_bsc_ctx);
Pablo Neira Ayuso5bd96a72011-05-14 11:32:48 +0200271 bts_init();
Neels Hofmeyr80108782016-12-28 18:56:19 +0100272 vty_init(&vty_info);
Harald Weltee76bea02011-02-05 13:54:41 +0100273
Neels Hofmeyr7e643942016-10-24 01:53:38 +0200274 /* Parse options */
275 handle_options(argc, argv);
276
277 /* Allocate global gsm_network struct; choose socket/internal MNCC */
278 rc = bsc_network_alloc(mncc_sock_path?
279 mncc_sock_from_cc : int_mncc_recv);
280 if (rc) {
281 fprintf(stderr, "Allocation failed. Exiting.\n");
282 exit(1);
283 }
284
285 /* Initialize VTY */
Max09c26f12017-01-12 19:35:11 +0100286 bsc_vty_init(bsc_gsmnet);
Neels Hofmeyr4d92c432016-02-23 15:10:33 +0100287 ctrl_vty_init(tall_bsc_ctx);
Harald Welte40152872010-05-16 20:52:23 +0200288
Harald Welteb7dd6612012-11-20 22:22:04 +0100289#ifdef BUILD_SMPP
Neels Hofmeyrabc64652016-02-24 19:15:39 +0100290 if (smpp_openbsc_alloc_init(tall_bsc_ctx) < 0)
Harald Welteb7dd6612012-11-20 22:22:04 +0100291 return -1;
292#endif
293
Neels Hofmeyr7e643942016-10-24 01:53:38 +0200294 /* Initialize MNCC socket if appropriate */
Neels Hofmeyr72901be2016-05-09 21:56:42 +0200295 if (mncc_sock_path) {
296 rc = mncc_sock_init(bsc_gsmnet, mncc_sock_path);
297 if (rc) {
298 fprintf(stderr, "MNCC socket initialization failed. exiting.\n");
299 exit(1);
300 }
301 } else
Max3a816e82016-04-18 23:11:18 +0200302 DEBUGP(DMNCC, "Using internal MNCC handler.\n");
Neels Hofmeyr7e643942016-10-24 01:53:38 +0200303
Vadim Yanitskiyc859f1a2017-03-31 23:27:44 +0700304 /*
305 * For osmo-nitb, skip TCH/F for now, because otherwise dyn TS
306 * always imply the possibility to have a mix of TCH/F and
307 * TCH/H channels; if two phones request a TCH/F and a TCH/H,
308 * respectively, they cannot call each other. If we deny TCH/F,
309 * they will both fall back to TCH/H, and dynamic channels are
310 * usable. See OS#1778.
311 *
312 * A third-party MSC may well be able to handle a TCH/H TCH/F
313 * mismatch. Moreover, this option may be overwritten in the
314 * config file or in VTY.
315 */
316 bsc_gsmnet->dyn_ts_allow_tch_f = false;
317
Neels Hofmeyr7e643942016-10-24 01:53:38 +0200318 /* Read the config */
Neels Hofmeyr6318d9f2016-05-11 14:12:00 +0200319 rc = bsc_network_configure(config_file);
320 if (rc < 0) {
321 fprintf(stderr, "Reading config failed. Exiting.\n");
Harald Welte10c29f62010-05-16 19:20:24 +0200322 exit(1);
Neels Hofmeyr6318d9f2016-05-11 14:12:00 +0200323 }
324
Harald Welte784be942013-03-13 15:39:56 +0100325#ifdef BUILD_SMPP
Neels Hofmeyrabc64652016-02-24 19:15:39 +0100326 smpp_openbsc_start(bsc_gsmnet);
Harald Welte784be942013-03-13 15:39:56 +0100327#endif
Holger Hans Peter Freytherc7efbe42010-06-30 14:30:35 +0800328 bsc_api_init(bsc_gsmnet, msc_bsc_api());
Harald Welte10c29f62010-05-16 19:20:24 +0200329
Neels Hofmeyr4d92c432016-02-23 15:10:33 +0100330 /* start control interface after reading config for
331 * ctrl_vty_get_bind_addr() */
Neels Hofmeyr4d92c432016-02-23 15:10:33 +0100332 bsc_gsmnet->ctrl = bsc_controlif_setup(bsc_gsmnet,
333 ctrl_vty_get_bind_addr(),
334 OSMO_CTRL_PORT_NITB_BSC);
Daniel Willmann6eb756f2011-08-05 12:22:35 +0200335 if (!bsc_gsmnet->ctrl) {
336 printf("Failed to initialize control interface. Exiting.\n");
337 return -1;
338 }
339
Holger Hans Peter Freyther1ba8fa62013-01-09 17:03:27 +0100340 if (bsc_base_ctrl_cmds_install() != 0) {
Holger Hans Peter Freytherfba03162014-03-23 12:06:36 +0100341 printf("Failed to initialize the BSC control commands.\n");
342 return -1;
343 }
344
345 if (msc_ctrl_cmds_install() != 0) {
346 printf("Failed to initialize the MSC control commands.\n");
Holger Hans Peter Freyther1ba8fa62013-01-09 17:03:27 +0100347 return -1;
348 }
349
Harald Welte59b04682009-06-10 05:40:52 +0800350 /* seed the PRNG */
351 srand(time(NULL));
352
Neels Hofmeyrf428d9a2016-02-23 14:57:22 +0100353 bsc_gsmnet->bsc_data->rf_ctrl = osmo_bsc_rf_create(rf_ctrl_path, bsc_gsmnet);
Ciaby38abd7b2014-03-06 17:20:55 +0100354 if (!bsc_gsmnet->bsc_data->rf_ctrl) {
Holger Hans Peter Freytherbcfa0b62014-02-07 20:07:51 +0100355 fprintf(stderr, "Failed to create the RF service.\n");
356 exit(1);
Holger Hans Peter Freyther5faf1d32012-05-11 23:28:19 +0200357 }
358
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200359 if (db_init(database_name)) {
360 printf("DB: Failed to init database. Please check the option settings.\n");
361 return -1;
362 }
363 printf("DB: Database initialized.\n");
364
365 if (db_prepare()) {
366 printf("DB: Failed to prepare database.\n");
367 return -1;
368 }
369 printf("DB: Database prepared.\n");
370
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100371 /* setup the timer */
Pablo Neira Ayuso274b3842017-05-08 20:57:52 +0200372 osmo_timer_setup(&db_sync_timer, db_sync_timer_cb, NULL);
Harald Weltecfa26852011-11-13 19:02:06 +0100373 if (use_db_counter)
374 osmo_timer_schedule(&db_sync_timer, DB_SYNC_INTERVAL);
Holger Hans Peter Freyther76037f02009-12-23 05:29:04 +0100375
Pablo Neira Ayuso274b3842017-05-08 20:57:52 +0200376 osmo_timer_setup(&bsc_gsmnet->subscr_expire_timer, subscr_expire_cb,
377 NULL);
Jan Luebbe0f1f95e2012-12-27 00:40:32 +0100378 osmo_timer_schedule(&bsc_gsmnet->subscr_expire_timer, EXPIRE_INTERVAL);
379
Harald Welte3b714502009-08-06 17:43:50 +0200380 signal(SIGINT, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800381 signal(SIGABRT, &signal_handler);
Harald Weltea8379772009-06-20 22:36:41 +0200382 signal(SIGUSR1, &signal_handler);
Harald Welte0c836602009-12-24 10:51:56 +0100383 signal(SIGUSR2, &signal_handler);
Holger Hans Peter Freyther2d59cc62011-05-12 16:02:07 +0200384 osmo_init_ignore_signals();
Harald Welte59b04682009-06-10 05:40:52 +0800385
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100386 /* start the SMS queue */
Holger Hans Peter Freyther48710462010-12-25 17:43:03 +0100387 if (sms_queue_start(bsc_gsmnet, 20) != 0)
Holger Hans Peter Freyther575d2e12010-12-24 13:48:27 +0100388 return -1;
Harald Welteb7dd6612012-11-20 22:22:04 +0100389
Harald Weltec4ae1762010-08-25 19:43:54 +0200390 if (daemonize) {
391 rc = osmo_daemonize();
392 if (rc < 0) {
393 perror("Error during daemonize");
394 exit(1);
395 }
396 }
397
Harald Welte59b04682009-06-10 05:40:52 +0800398 while (1) {
Harald Welte51d2a592010-03-26 21:28:59 +0800399 log_reset_context();
Pablo Neira Ayuso04d24cd2011-05-06 12:11:23 +0200400 osmo_select_main(0);
Harald Welte59b04682009-06-10 05:40:52 +0800401 }
402}