From ea0d1d4b6830877c2fc2109952b035dc9fd1a1e2 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Tue, 9 Feb 2016 11:17:59 +0100 Subject: [PATCH] introduce system-wide defaults file --- Makefile | 6 +++--- config.mk.in | 3 +++ etc/centos/config.mk.in | 1 + etc/centos/fpm-make.sh | 3 ++- etc/debian/config.mk.in | 1 + etc/debian/fpm-make.sh | 3 ++- etc/ot-recorder.default | 6 ++++++ misc.c | 31 +++++++++++++++++++++++++++++++ misc.h | 1 + ocat.c | 3 +++ recorder.c | 8 +++++--- 11 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 etc/ot-recorder.default diff --git a/Makefile b/Makefile index 54ca705..831317d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ include config.mk CFLAGS =-Wall -Werror $(MOSQUITTO_INC) LIBS = $(MORELIBS) $(MOSQUITTO_LIB) -lmosquitto -lm -LIBS += -lcurl +LIBS += -lcurl -lconfig TARGETS= OTR_OBJS = json.o \ @@ -64,8 +64,7 @@ else endif CFLAGS += -DSTORAGEDEFAULT=\"$(STORAGEDEFAULT)\" -DDOCROOT=\"$(DOCROOT)\" - - +CFLAGS += -DCONFIGFILE=\"$(CONFIGFILE)\" TARGETS += ot-recorder ocat @@ -111,6 +110,7 @@ install: ot-recorder ocat cp -R docroot/* $(DESTDIR)$(DOCROOT)/ install -m 0755 ot-recorder $(DESTDIR)$(INSTALLDIR)/sbin install -m 0755 ocat $(DESTDIR)$(INSTALLDIR)/bin + test -r $(DESTDIR)/$(CONFIGFILE) || install -D etc/ot-recorder.default $(DESTDIR)/$(CONFIGFILE) ifndef DESTDIR $(INSTALLDIR)/sbin/ot-recorder --initialize endif diff --git a/config.mk.in b/config.mk.in index 1cc69dd..bfeb64f 100644 --- a/config.mk.in +++ b/config.mk.in @@ -56,6 +56,9 @@ GHASHPREC = 7 # yes or no JSON_INDENT ?= no +# Location of optional default configuration file +CONFIGFILE = /etc/defaults/ot-recorder + # Optionally specify the path to the Mosquitto libs, include here MOSQUITTO_INC = -I/usr/include MOSQUITTO_LIB = -L/usr/lib diff --git a/etc/centos/config.mk.in b/etc/centos/config.mk.in index cebe73e..5120b06 100644 --- a/etc/centos/config.mk.in +++ b/etc/centos/config.mk.in @@ -1,6 +1,7 @@ #(@)config.mk for Centos 7 (x86_64) INSTALLDIR = /usr/local +CONFIGFILE = /etc/default/ot-recorder WITH_HTTP ?= yes WITH_LMDB ?= yes diff --git a/etc/centos/fpm-make.sh b/etc/centos/fpm-make.sh index 7b21014..6fa40dd 100755 --- a/etc/centos/fpm-make.sh +++ b/etc/centos/fpm-make.sh @@ -28,8 +28,9 @@ fpm -s dir \ -d "libcurl" \ -d "libmosquitto1" \ -d "lua" \ + -d "libconfig" \ --post-install etc/centos/postinst \ - usr var + usr var etc echo "${rpmfile}" > package.name rm -rf "${tempdir}" diff --git a/etc/debian/config.mk.in b/etc/debian/config.mk.in index 2c87e05..6c45c6e 100644 --- a/etc/debian/config.mk.in +++ b/etc/debian/config.mk.in @@ -1,6 +1,7 @@ #(@)config.mk for Debian 8 (x86_64) INSTALLDIR = /usr/local +CONFIGFILE = /etc/default/ot-recorder WITH_HTTP ?= yes WITH_LMDB ?= yes diff --git a/etc/debian/fpm-make.sh b/etc/debian/fpm-make.sh index f5e23f9..4bf8b1b 100755 --- a/etc/debian/fpm-make.sh +++ b/etc/debian/fpm-make.sh @@ -33,8 +33,9 @@ fpm -s dir \ -d "libcurl3" \ -d "libmosquitto1" \ -d "liblua5.2-0" \ + -d "libconfig9" \ --post-install etc/debian/postinst \ - usr var + usr var etc echo "${debfile}" > package.name rm -rf "${tempdir}" diff --git a/etc/ot-recorder.default b/etc/ot-recorder.default new file mode 100644 index 0000000..cfc9337 --- /dev/null +++ b/etc/ot-recorder.default @@ -0,0 +1,6 @@ +#(@)ot-recorder.default +# +# Specify global configuration options for the OwnTracks Recorder +# and its associated utilities to override compiled-in defaults. + +# OTR_STORAGEDIR = "/var/spool/owntracks/recorder/store" diff --git a/misc.c b/misc.c index 60dc926..1bd8326 100644 --- a/misc.c +++ b/misc.c @@ -22,6 +22,8 @@ #include #include +#include +#include #include "utstring.h" #include "ctype.h" #include "udata.h" @@ -93,3 +95,32 @@ char *monitor_get() return (ret); } + +/* + * Fill in some defaults + */ + +void get_defaults(char *filename, struct udata *ud) +{ + config_t cfg, *cf; + const char *value; + + if (access(filename, R_OK) == -1) + return; + + config_init(cf = &cfg); + + if (!config_read_file(cf, filename)) { + olog(LOG_ERR, "%s:%d - %s", + config_error_file(cf), + config_error_line(cf), + config_error_text(cf)); + config_destroy(cf); + exit(2); + } + + if (config_lookup_string(cf, "OTR_STORAGEDIR", &value) != CONFIG_FALSE) + strcpy(STORAGEDIR, value); + + config_destroy(cf); +} diff --git a/misc.h b/misc.h index 5989c13..28f2700 100644 --- a/misc.h +++ b/misc.h @@ -17,5 +17,6 @@ char *bindump(char *buf, long buflen); // void monitor_update(struct udata *ud, time_t now, char *topic); void monitorhook(struct udata *userdata, time_t now, char *topic); char *monitor_get(); +void get_defaults(char *filename, struct udata *userdata); #endif diff --git a/ocat.c b/ocat.c index 5d1f5c1..8a9a461 100644 --- a/ocat.c +++ b/ocat.c @@ -114,6 +114,7 @@ void print_versioninfo() printf("\tWITH_RONLY = yes\n"); #endif printf("\tSTORAGEDEFAULT = \"%s\"\n", STORAGEDEFAULT); + printf("\tSTORAGEDIR = \"%s\"\n", STORAGEDIR); printf("\tDOCROOT = \"%s\"\n", DOCROOT); printf("\tGHASHPREC = %d\n", GHASHPREC); printf("\tDEFAULT_HISTORY_HOURS = %d\n", DEFAULT_HISTORY_HOURS); @@ -152,6 +153,8 @@ int main(int argc, char **argv) JsonNode *fields = NULL; FILE *xmlp = stdout; + get_defaults(CONFIGFILE, NULL); + if ((p = getenv("OCAT_USERNAME")) != NULL) { username = strdup(p); } diff --git a/recorder.c b/recorder.c index ba79e9d..9367f8c 100644 --- a/recorder.c +++ b/recorder.c @@ -1321,6 +1321,10 @@ int main(int argc, char **argv) udata.geokey = NULL; /* default: no API key */ udata.debug = FALSE; + openlog("ot-recorder", LOG_PID | LOG_PERROR, syslog_facility_code(logfacility)); + + get_defaults(CONFIGFILE, &udata); + if ((p = getenv("OTR_HOST")) != NULL) { hostname = strdup(p); } @@ -1568,8 +1572,6 @@ int main(int argc, char **argv) password = getenv("OTR_PASS"); } - openlog("ot-recorder", LOG_PID | LOG_PERROR, syslog_facility_code(logfacility)); - #ifdef WITH_HTTP if (http_port) { if (!is_directory(doc_root)) { @@ -1580,7 +1582,7 @@ int main(int argc, char **argv) udata.mgserver = mg_create_server(ud, ev_handler); } #endif - olog(LOG_DEBUG, "starting"); + olog(LOG_DEBUG, "starting with STORAGEDIR=%s", STORAGEDIR); if (ud->revgeo == TRUE) { #ifdef WITH_LMDB