nbd-proxy: add configuration facility

Add a little configuration facility. We'll later add a method to
integrate this with UI-side configuration, so it's implmented as json.

We add a (temporarily required) argument to specify which configuration
to use.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/Makefile.am b/Makefile.am
index 6ee7fe1..8e0d140 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,3 +1,9 @@
 sbin_PROGRAMS = nbd-proxy
 
-nbd_proxy_CPPFLAGS = -DRUNSTATEDIR=\"$(runstatedir)\"
+nbd_proxy_CPPFLAGS = \
+	$(JSON_CFLAGS) \
+	-DRUNSTATEDIR=\"$(runstatedir)\" \
+	-DSYSCONFDIR=\"$(sysconfdir)\"
+
+nbd_proxy_LDADD = \
+	$(JSON_LIBS)
diff --git a/README b/README
index 9401251..d5ab82e 100644
--- a/README
+++ b/README
@@ -25,7 +25,9 @@
     git clone https://github.com/joewalnes/websocketd
     (cd websocketd && make)
 
-    sudo websocketd/websocketd --port=8000 --staticdir=web --binary ./nbd-proxy
+    sudo websocketd/websocketd --port=8000 --staticdir=web --binary ./nbd-proxy <config>
+
+- where <config> is a name of a configuration in the config.json file.
 
 Note that this type of invocation is very insecure, and intended just for
 experimentation. See the Security section below.
diff --git a/config.sample.json b/config.sample.json
new file mode 100644
index 0000000..dc8e0f2
--- /dev/null
+++ b/config.sample.json
@@ -0,0 +1,8 @@
+{
+    "timeout": 30,
+    "configurations": {
+        "0": {
+            "nbd-device": "/dev/nbd0"
+        }
+    }
+}
diff --git a/configure.ac b/configure.ac
index 9bd1841..ace06b4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -14,5 +14,7 @@
 
 AC_CHECK_FUNCS(splice)
 
+PKG_CHECK_MODULES(JSON, [json-c])
+
 AC_CONFIG_FILES([Makefile])
 AC_OUTPUT
diff --git a/m4/pkg.m4 b/m4/pkg.m4
new file mode 100644
index 0000000..82bea96
--- /dev/null
+++ b/m4/pkg.m4
@@ -0,0 +1,275 @@
+dnl pkg.m4 - Macros to locate and utilise pkg-config.   -*- Autoconf -*-
+dnl serial 11 (pkg-config-0.29.1)
+dnl
+dnl Copyright © 2004 Scott James Remnant <scott@netsplit.com>.
+dnl Copyright © 2012-2015 Dan Nicholson <dbn.lists@gmail.com>
+dnl
+dnl This program is free software; you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl This program is distributed in the hope that it will be useful, but
+dnl WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+dnl General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program; if not, write to the Free Software
+dnl Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+dnl 02111-1307, USA.
+dnl
+dnl As a special exception to the GNU General Public License, if you
+dnl distribute this file as part of a program that contains a
+dnl configuration script generated by Autoconf, you may include it under
+dnl the same distribution terms that you use for the rest of that
+dnl program.
+
+dnl PKG_PREREQ(MIN-VERSION)
+dnl -----------------------
+dnl Since: 0.29
+dnl
+dnl Verify that the version of the pkg-config macros are at least
+dnl MIN-VERSION. Unlike PKG_PROG_PKG_CONFIG, which checks the user's
+dnl installed version of pkg-config, this checks the developer's version
+dnl of pkg.m4 when generating configure.
+dnl
+dnl To ensure that this macro is defined, also add:
+dnl m4_ifndef([PKG_PREREQ],
+dnl     [m4_fatal([must install pkg-config 0.29 or later before running autoconf/autogen])])
+dnl
+dnl See the "Since" comment for each macro you use to see what version
+dnl of the macros you require.
+m4_defun([PKG_PREREQ],
+[m4_define([PKG_MACROS_VERSION], [0.29.1])
+m4_if(m4_version_compare(PKG_MACROS_VERSION, [$1]), -1,
+    [m4_fatal([pkg.m4 version $1 or higher is required but ]PKG_MACROS_VERSION[ found])])
+])dnl PKG_PREREQ
+
+dnl PKG_PROG_PKG_CONFIG([MIN-VERSION])
+dnl ----------------------------------
+dnl Since: 0.16
+dnl
+dnl Search for the pkg-config tool and set the PKG_CONFIG variable to
+dnl first found in the path. Checks that the version of pkg-config found
+dnl is at least MIN-VERSION. If MIN-VERSION is not specified, 0.9.0 is
+dnl used since that's the first version where most current features of
+dnl pkg-config existed.
+AC_DEFUN([PKG_PROG_PKG_CONFIG],
+[m4_pattern_forbid([^_?PKG_[A-Z_]+$])
+m4_pattern_allow([^PKG_CONFIG(_(PATH|LIBDIR|SYSROOT_DIR|ALLOW_SYSTEM_(CFLAGS|LIBS)))?$])
+m4_pattern_allow([^PKG_CONFIG_(DISABLE_UNINSTALLED|TOP_BUILD_DIR|DEBUG_SPEW)$])
+AC_ARG_VAR([PKG_CONFIG], [path to pkg-config utility])
+AC_ARG_VAR([PKG_CONFIG_PATH], [directories to add to pkg-config's search path])
+AC_ARG_VAR([PKG_CONFIG_LIBDIR], [path overriding pkg-config's built-in search path])
+
+if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
+	AC_PATH_TOOL([PKG_CONFIG], [pkg-config])
+fi
+if test -n "$PKG_CONFIG"; then
+	_pkg_min_version=m4_default([$1], [0.9.0])
+	AC_MSG_CHECKING([pkg-config is at least version $_pkg_min_version])
+	if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then
+		AC_MSG_RESULT([yes])
+	else
+		AC_MSG_RESULT([no])
+		PKG_CONFIG=""
+	fi
+fi[]dnl
+])dnl PKG_PROG_PKG_CONFIG
+
+dnl PKG_CHECK_EXISTS(MODULES, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
+dnl -------------------------------------------------------------------
+dnl Since: 0.18
+dnl
+dnl Check to see whether a particular set of modules exists. Similar to
+dnl PKG_CHECK_MODULES(), but does not set variables or print errors.
+dnl
+dnl Please remember that m4 expands AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+dnl only at the first occurence in configure.ac, so if the first place
+dnl it's called might be skipped (such as if it is within an "if", you
+dnl have to call PKG_CHECK_EXISTS manually
+AC_DEFUN([PKG_CHECK_EXISTS],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+if test -n "$PKG_CONFIG" && \
+    AC_RUN_LOG([$PKG_CONFIG --exists --print-errors "$1"]); then
+  m4_default([$2], [:])
+m4_ifvaln([$3], [else
+  $3])dnl
+fi])
+
+dnl _PKG_CONFIG([VARIABLE], [COMMAND], [MODULES])
+dnl ---------------------------------------------
+dnl Internal wrapper calling pkg-config via PKG_CONFIG and setting
+dnl pkg_failed based on the result.
+m4_define([_PKG_CONFIG],
+[if test -n "$$1"; then
+    pkg_cv_[]$1="$$1"
+ elif test -n "$PKG_CONFIG"; then
+    PKG_CHECK_EXISTS([$3],
+                     [pkg_cv_[]$1=`$PKG_CONFIG --[]$2 "$3" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes ],
+		     [pkg_failed=yes])
+ else
+    pkg_failed=untried
+fi[]dnl
+])dnl _PKG_CONFIG
+
+dnl _PKG_SHORT_ERRORS_SUPPORTED
+dnl ---------------------------
+dnl Internal check to see if pkg-config supports short errors.
+AC_DEFUN([_PKG_SHORT_ERRORS_SUPPORTED],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
+fi[]dnl
+])dnl _PKG_SHORT_ERRORS_SUPPORTED
+
+
+dnl PKG_CHECK_MODULES(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
+dnl   [ACTION-IF-NOT-FOUND])
+dnl --------------------------------------------------------------
+dnl Since: 0.4.0
+dnl
+dnl Note that if there is a possibility the first call to
+dnl PKG_CHECK_MODULES might not happen, you should be sure to include an
+dnl explicit call to PKG_PROG_PKG_CONFIG in your configure.ac
+AC_DEFUN([PKG_CHECK_MODULES],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+AC_ARG_VAR([$1][_CFLAGS], [C compiler flags for $1, overriding pkg-config])dnl
+AC_ARG_VAR([$1][_LIBS], [linker flags for $1, overriding pkg-config])dnl
+
+pkg_failed=no
+AC_MSG_CHECKING([for $1])
+
+_PKG_CONFIG([$1][_CFLAGS], [cflags], [$2])
+_PKG_CONFIG([$1][_LIBS], [libs], [$2])
+
+m4_define([_PKG_TEXT], [Alternatively, you may set the environment variables $1[]_CFLAGS
+and $1[]_LIBS to avoid the need to call pkg-config.
+See the pkg-config man page for more details.])
+
+if test $pkg_failed = yes; then
+   	AC_MSG_RESULT([no])
+        _PKG_SHORT_ERRORS_SUPPORTED
+        if test $_pkg_short_errors_supported = yes; then
+	        $1[]_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "$2" 2>&1`
+        else 
+	        $1[]_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "$2" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$$1[]_PKG_ERRORS" >&AS_MESSAGE_LOG_FD
+
+	m4_default([$4], [AC_MSG_ERROR(
+[Package requirements ($2) were not met:
+
+$$1_PKG_ERRORS
+
+Consider adjusting the PKG_CONFIG_PATH environment variable if you
+installed software in a non-standard prefix.
+
+_PKG_TEXT])[]dnl
+        ])
+elif test $pkg_failed = untried; then
+     	AC_MSG_RESULT([no])
+	m4_default([$4], [AC_MSG_FAILURE(
+[The pkg-config script could not be found or is too old.  Make sure it
+is in your PATH or set the PKG_CONFIG environment variable to the full
+path to pkg-config.
+
+_PKG_TEXT
+
+To get pkg-config, see <http://pkg-config.freedesktop.org/>.])[]dnl
+        ])
+else
+	$1[]_CFLAGS=$pkg_cv_[]$1[]_CFLAGS
+	$1[]_LIBS=$pkg_cv_[]$1[]_LIBS
+        AC_MSG_RESULT([yes])
+	$3
+fi[]dnl
+])dnl PKG_CHECK_MODULES
+
+
+dnl PKG_CHECK_MODULES_STATIC(VARIABLE-PREFIX, MODULES, [ACTION-IF-FOUND],
+dnl   [ACTION-IF-NOT-FOUND])
+dnl ---------------------------------------------------------------------
+dnl Since: 0.29
+dnl
+dnl Checks for existence of MODULES and gathers its build flags with
+dnl static libraries enabled. Sets VARIABLE-PREFIX_CFLAGS from --cflags
+dnl and VARIABLE-PREFIX_LIBS from --libs.
+dnl
+dnl Note that if there is a possibility the first call to
+dnl PKG_CHECK_MODULES_STATIC might not happen, you should be sure to
+dnl include an explicit call to PKG_PROG_PKG_CONFIG in your
+dnl configure.ac.
+AC_DEFUN([PKG_CHECK_MODULES_STATIC],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+_save_PKG_CONFIG=$PKG_CONFIG
+PKG_CONFIG="$PKG_CONFIG --static"
+PKG_CHECK_MODULES($@)
+PKG_CONFIG=$_save_PKG_CONFIG[]dnl
+])dnl PKG_CHECK_MODULES_STATIC
+
+
+dnl PKG_INSTALLDIR([DIRECTORY])
+dnl -------------------------
+dnl Since: 0.27
+dnl
+dnl Substitutes the variable pkgconfigdir as the location where a module
+dnl should install pkg-config .pc files. By default the directory is
+dnl $libdir/pkgconfig, but the default can be changed by passing
+dnl DIRECTORY. The user can override through the --with-pkgconfigdir
+dnl parameter.
+AC_DEFUN([PKG_INSTALLDIR],
+[m4_pushdef([pkg_default], [m4_default([$1], ['${libdir}/pkgconfig'])])
+m4_pushdef([pkg_description],
+    [pkg-config installation directory @<:@]pkg_default[@:>@])
+AC_ARG_WITH([pkgconfigdir],
+    [AS_HELP_STRING([--with-pkgconfigdir], pkg_description)],,
+    [with_pkgconfigdir=]pkg_default)
+AC_SUBST([pkgconfigdir], [$with_pkgconfigdir])
+m4_popdef([pkg_default])
+m4_popdef([pkg_description])
+])dnl PKG_INSTALLDIR
+
+
+dnl PKG_NOARCH_INSTALLDIR([DIRECTORY])
+dnl --------------------------------
+dnl Since: 0.27
+dnl
+dnl Substitutes the variable noarch_pkgconfigdir as the location where a
+dnl module should install arch-independent pkg-config .pc files. By
+dnl default the directory is $datadir/pkgconfig, but the default can be
+dnl changed by passing DIRECTORY. The user can override through the
+dnl --with-noarch-pkgconfigdir parameter.
+AC_DEFUN([PKG_NOARCH_INSTALLDIR],
+[m4_pushdef([pkg_default], [m4_default([$1], ['${datadir}/pkgconfig'])])
+m4_pushdef([pkg_description],
+    [pkg-config arch-independent installation directory @<:@]pkg_default[@:>@])
+AC_ARG_WITH([noarch-pkgconfigdir],
+    [AS_HELP_STRING([--with-noarch-pkgconfigdir], pkg_description)],,
+    [with_noarch_pkgconfigdir=]pkg_default)
+AC_SUBST([noarch_pkgconfigdir], [$with_noarch_pkgconfigdir])
+m4_popdef([pkg_default])
+m4_popdef([pkg_description])
+])dnl PKG_NOARCH_INSTALLDIR
+
+
+dnl PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE,
+dnl [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
+dnl -------------------------------------------
+dnl Since: 0.28
+dnl
+dnl Retrieves the value of the pkg-config variable for the given module.
+AC_DEFUN([PKG_CHECK_VAR],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+AC_ARG_VAR([$1], [value of $3 for $2, overriding pkg-config])dnl
+
+_PKG_CONFIG([$1], [variable="][$3]["], [$2])
+AS_VAR_COPY([$1], [pkg_cv_][$1])
+
+AS_VAR_IF([$1], [""], [$5], [$4])dnl
+])dnl PKG_CHECK_VAR
diff --git a/nbd-proxy.c b/nbd-proxy.c
index 13a1366..cb3c33c 100644
--- a/nbd-proxy.c
+++ b/nbd-proxy.c
@@ -34,22 +34,34 @@
 #include <sys/un.h>
 #include <sys/wait.h>
 
+#include <json.h>
+
 #include "config.h"
 
+struct config {
+	char			*name;
+	char			*nbd_device;
+	struct json_object	*metadata;
+};
+
 struct ctx {
 	int		sock;
 	int		sock_client;
 	int		signal_pipe[2];
 	char		*sock_path;
+	char		*dev_path;
 	pid_t		nbd_client_pid;
+	int		nbd_timeout;
 	uint8_t		*buf;
 	size_t		bufsize;
+	struct config	*configs;
+	int		n_configs;
 };
 
+static const char *conf_path = SYSCONFDIR "/nbd-proxy/config.json";
 static const char *sockpath_tmpl = RUNSTATEDIR "/nbd.%d.sock";
-static const char *dev_path = "/dev/nbd0";
 static const size_t bufsize = 0x20000;
-static const int nbd_timeout = 30;
+static const int nbd_timeout_default = 30;
 
 static int open_nbd_socket(struct ctx *ctx)
 {
@@ -116,7 +128,8 @@
 		char timeout_str[10];
 		int fd;
 
-		snprintf(timeout_str, sizeof(timeout_str), "%d", nbd_timeout);
+		snprintf(timeout_str, sizeof(timeout_str),
+				"%d", ctx->nbd_timeout);
 
 		fd = open("/dev/null", O_RDWR);
 		if (fd < 0)
@@ -132,7 +145,7 @@
 				"-u", ctx->sock_path,
 				"-n",
 				"-t", timeout_str,
-				dev_path,
+				ctx->dev_path,
 				NULL);
 		err(EXIT_FAILURE, "can't start ndb client");
 	}
@@ -373,16 +386,163 @@
 	return rc ? -1 : 0;
 }
 
-int main(void)
+static void config_free_one(struct config *config)
 {
+	if (config->metadata)
+		json_object_put(config->metadata);
+	free(config->nbd_device);
+	free(config->name);
+}
+
+static int config_parse_one(struct config *config, const char *name,
+		json_object *obj)
+{
+	struct json_object *tmp, *meta;
+	json_bool jrc;
+
+	jrc = json_object_object_get_ex(obj, "nbd-device", &tmp);
+	if (!jrc) {
+		warnx("config %s doesn't specify a nbd-device", name);
+		return -1;
+	}
+
+	if (!json_object_is_type(tmp, json_type_string)) {
+		warnx("config %s has invalid nbd-device", name);
+		return -1;
+	}
+
+	config->nbd_device = strdup(json_object_get_string(tmp));
+	config->name = strdup(name);
+
+	jrc = json_object_object_get_ex(obj, "metadata", &meta);
+	if (jrc && json_object_is_type(meta, json_type_object))
+		config->metadata = json_object_get(meta);
+	else
+		config->metadata = NULL;
+
+	return 0;
+}
+
+static void config_free(struct ctx *ctx)
+{
+	int i;
+
+	for (i = 0; i < ctx->n_configs; i++)
+		config_free_one(&ctx->configs[i]);
+
+	free(ctx->configs);
+	ctx->n_configs = 0;
+}
+
+static int config_init(struct ctx *ctx)
+{
+	struct json_object *obj, *tmp;
+	json_bool jrc;
+	int i, rc;
+
+	/* apply defaults */
+	ctx->nbd_timeout = nbd_timeout_default;
+
+	obj = json_object_from_file(conf_path);
+	if (!obj) {
+		warnx("can't read configuration from %s\n", conf_path);
+		return -1;
+	}
+
+	/* global configuration */
+	jrc = json_object_object_get_ex(obj, "timeout", &tmp);
+	if (jrc) {
+		errno = 0;
+		ctx->nbd_timeout = json_object_get_int(tmp);
+		if (ctx->nbd_timeout == 0 && errno) {
+			warnx("can't parse timeout value");
+			goto err_free;
+		}
+	}
+
+	/* per-config configuration */
+	jrc = json_object_object_get_ex(obj, "configurations", &tmp);
+	if (!jrc) {
+		warnx("no configurations specified");
+		goto err_free;
+	}
+
+	if (!json_object_is_type(tmp, json_type_object)) {
+		warnx("invalid configurations format");
+		goto err_free;
+	}
+
+	ctx->n_configs = json_object_object_length(tmp);
+	ctx->configs = calloc(ctx->n_configs, sizeof(*ctx->configs));
+
+	i = 0;
+	json_object_object_foreach(tmp, name, config) {
+		rc = config_parse_one(&ctx->configs[i], name, config);
+		if (rc)
+			goto err_free;
+		i++;
+	}
+
+	json_object_put(obj);
+
+	return 0;
+
+err_free:
+	warnx("failed to load config from %s", conf_path);
+	json_object_put(obj);
+	return -1;
+}
+
+static int config_select(struct ctx *ctx, const char *name)
+{
+	struct config *config;
+	int i;
+
+	config = NULL;
+
+	/* find a matching config... */
+	for (i = 0; i < ctx->n_configs; i++) {
+		if (!strcmp(ctx->configs[i].name, name)) {
+			config = &ctx->configs[i];
+			break;
+		}
+	}
+
+	if (!config) {
+		warnx("no such configuration '%s'", name);
+		return -1;
+	}
+
+	/* ... and apply it */
+	ctx->dev_path = config->nbd_device;
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	const char *config_name;
 	struct ctx _ctx, *ctx;
 	int rc;
 
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s <configuration>\n", argv[0]);
+		return EXIT_FAILURE;
+	}
+
+	config_name = argv[1];
+
 	ctx = &_ctx;
+	memset(ctx, 0, sizeof(*ctx));
 	ctx->bufsize = bufsize;
 	ctx->buf = malloc(ctx->bufsize);
-	ctx->sock_path = NULL;
-	ctx->nbd_client_pid = 0;
+
+	rc = config_init(ctx);
+	if (rc)
+		goto out_free;
+
+	rc = config_select(ctx, config_name);
+	if (rc)
+		goto out_free;
 
 	rc = open_nbd_socket(ctx);
 	if (rc)
@@ -419,6 +579,7 @@
 	}
 	close(ctx->sock);
 out_free:
+	config_free(ctx);
 	free(ctx->buf);
 	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
 }