nbd-proxy: Add metadata facility

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
diff --git a/config.sample.json b/config.sample.json
index dc8e0f2..461046a 100644
--- a/config.sample.json
+++ b/config.sample.json
@@ -2,7 +2,10 @@
     "timeout": 30,
     "configurations": {
         "0": {
-            "nbd-device": "/dev/nbd0"
+            "nbd-device": "/dev/nbd0",
+            "metadata": {
+                "description": "Virtual media device"
+            }
         }
     }
 }
diff --git a/nbd-proxy.c b/nbd-proxy.c
index 3ce8879..6b23d6a 100644
--- a/nbd-proxy.c
+++ b/nbd-proxy.c
@@ -389,6 +389,24 @@
 	return rc ? -1 : 0;
 }
 
+static void print_metadata(struct ctx *ctx)
+{
+	struct json_object *md;
+	int i;
+
+	md = json_object_new_object();
+
+	for (i = 0; i < ctx->n_configs; i++) {
+		struct config *config = &ctx->configs[i];
+		json_object_object_add(md, config->name,
+				config->metadata);
+	}
+
+	puts(json_object_get_string(md));
+
+	json_object_put(md);
+}
+
 static void config_free_one(struct config *config)
 {
 	if (config->metadata)
@@ -548,17 +566,26 @@
 }
 
 static const struct option options[] = {
-	{ .name = "help", .val = 'h' },
+	{ .name = "help",	.val = 'h' },
+	{ .name = "metadata",	.val = 'm' },
 	{ 0 },
 };
 
+enum action {
+	ACTION_PROXY,
+	ACTION_METADATA,
+};
+
 static void print_usage(const char *progname)
 {
-	fprintf(stderr, "usage: %s [configuration]\n", progname);
+	fprintf(stderr, "usage:\n");
+	fprintf(stderr, "\t%s [configuration]\n", progname);
+	fprintf(stderr, "\t%s --metadata\n", progname);
 }
 
 int main(int argc, char **argv)
 {
+	enum action action = ACTION_PROXY;
 	const char *config_name;
 	struct ctx _ctx, *ctx;
 	int rc;
@@ -571,6 +598,9 @@
 			break;
 
 		switch (c) {
+		case 'm':
+			action = ACTION_METADATA;
+			break;
 		case 'h':
 		case '?':
 			print_usage(argv[0]);
@@ -590,6 +620,11 @@
 	if (rc)
 		goto out_free;
 
+	if (action == ACTION_METADATA) {
+		print_metadata(ctx);
+		goto out_free;
+	}
+
 	rc = config_select(ctx, config_name);
 	if (rc)
 		goto out_free;