aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Freyther <zecke@selfish.org>2008-12-28 22:51:39 +0000
committerHolger Freyther <zecke@selfish.org>2008-12-28 22:51:39 +0000
commitbde361064ae41d35b10ee44abf38ce867c858725 (patch)
tree31d48d9965668e618d9ad3cd99f97f82d8ff0282
parentaa0fb362c0d44d43320e15529e84920bf2d38771 (diff)
make it optional where the hlr database is stored
Add --database to define where the database is stored. The default was changed to not store the file in /tmp anymore.
-rw-r--r--include/openbsc/db.h2
-rw-r--r--src/bsc_hack.c8
-rw-r--r--src/db.c19
-rw-r--r--src/db_test.c2
4 files changed, 25 insertions, 6 deletions
diff --git a/include/openbsc/db.h b/include/openbsc/db.h
index e02bb6246..df703e04e 100644
--- a/include/openbsc/db.h
+++ b/include/openbsc/db.h
@@ -24,7 +24,7 @@
#include <openbsc/gsm_subscriber.h>
-int db_init();
+int db_init(const char *name);
int db_prepare();
int db_fini();
diff --git a/src/bsc_hack.c b/src/bsc_hack.c
index 274a9cf12..3076758ef 100644
--- a/src/bsc_hack.c
+++ b/src/bsc_hack.c
@@ -46,6 +46,7 @@ static struct gsm_network *gsmnet;
/* MCC and MNC for the Location Area Identifier */
static int MCC = 1;
static int MNC = 1;
+static const char *database_name = "hlr.sqlite3";
/* The following definitions are for OM and NM packets that we cannot yet
@@ -653,6 +654,7 @@ static void print_help()
printf(" -s --disable-color\n");
printf(" -n --network-code number(MNC) \n");
printf(" -c --country-code number (MCC) \n");
+ printf(" -l --database db-name The database to use\n");
printf(" -h --help this text\n");
}
@@ -666,6 +668,7 @@ static void handle_options(int argc, char** argv)
{"disable-color", 0, 0, 's'},
{"network-code", 1, 0, 'n'},
{"country-code", 1, 0, 'c'},
+ {"database", 1, 0, 'l'},
{0, 0, 0, 0}
};
@@ -691,6 +694,9 @@ static void handle_options(int argc, char** argv)
case 'c':
MCC = atoi(optarg);
break;
+ case 'l':
+ database_name = strdup(optarg);
+ break;
default:
/* ignore */
break;
@@ -771,7 +777,7 @@ int main(int argc, char **argv)
/* parse options */
handle_options(argc, argv);
- if (db_init()) {
+ if (db_init(database_name)) {
printf("DB: Failed to init database. Please check the option settings.\n");
return 1;
}
diff --git a/src/db.c b/src/db.c
index bc0b51246..a3bf6ce87 100644
--- a/src/db.c
+++ b/src/db.c
@@ -19,11 +19,14 @@
#include <openbsc/db.h>
+#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbi/dbi.h>
+static char *db_basename = NULL;
+static char *db_dirname = NULL;
dbi_conn conn;
void db__error_func(dbi_conn conn, void* data) {
@@ -32,7 +35,7 @@ void db__error_func(dbi_conn conn, void* data) {
printf("DBI: %s\n", msg);
}
-int db_init() {
+int db_init(const char *name) {
dbi_initialize(NULL);
conn = dbi_conn_new("sqlite3");
if (conn==NULL) {
@@ -51,10 +54,15 @@ int db_init() {
*/
/* SqLite 3 */
- dbi_conn_set_option(conn, "sqlite3_dbdir", "/tmp");
- dbi_conn_set_option(conn, "dbname", "hlr.sqlite3");
+ char *db_basename = strdup(name);
+ char *db_dirname = strdup(name);
+ dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
+ dbi_conn_set_option(conn, "dbname", basename(db_basename));
if (dbi_conn_connect(conn) < 0) {
+ free(db_dirname);
+ free(db_basename);
+ db_dirname = db_basename = NULL;
return 1;
}
@@ -138,6 +146,11 @@ int db_prepare() {
int db_fini() {
dbi_conn_close(conn);
dbi_shutdown();
+
+ if (db_dirname)
+ free(db_dirname);
+ if (db_basename)
+ free(db_basename);
return 0;
}
diff --git a/src/db_test.c b/src/db_test.c
index 0367ce227..760d7ab7a 100644
--- a/src/db_test.c
+++ b/src/db_test.c
@@ -25,7 +25,7 @@
int main() {
- if (db_init()) {
+ if (db_init("hlr.sqlite3")) {
printf("DB: Failed to init database. Please check the option settings.\n");
return 1;
}