console-server: allow separate handler instances

Currently, each handler (socket-handler, tty-handler and log-handler)
provides a statically-allocated instance of a handler, which gets
initialized for a console through the ->init callback.

We have upcoming changes that may create more than one console object,
in which case means we will need multiple instances of each handler
type.

This change splits the handler type from the handler instance; the
former is now struct handler_type, with struct handler being the
instance. Handler modules define a (const) struct handler_type, and
->init() now returns a newly-allocated instance of a handler of that
type.

This allows multiple handlers of each type.

Because the handler instances are allocated by type->init, we now
require both ->init and ->fini to be present on registered handlers.

We no longer need the `bool active` member of the handler, as instances
are always active.

Change-Id: Id97f15bd6445e17786f5883b849de8559c5ea434
Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
diff --git a/log-handler.c b/log-handler.c
index d93b679..4109d8b 100644
--- a/log-handler.c
+++ b/log-handler.c
@@ -149,15 +149,21 @@
 	return 0;
 }
 
-static int log_init(struct handler *handler, struct console *console,
-		    struct config *config)
+static struct handler *log_init(const struct handler_type *type
+				__attribute__((unused)),
+				struct console *console, struct config *config)
 {
-	struct log_handler *lh = to_log_handler(handler);
+	struct log_handler *lh;
 	const char *filename;
 	const char *logsize_str;
 	size_t logsize = default_logsize;
 	int rc;
 
+	lh = malloc(sizeof(*lh));
+	if (!lh) {
+		return NULL;
+	}
+
 	lh->console = console;
 	lh->pagesize = 4096;
 	lh->size = 0;
@@ -183,17 +189,22 @@
 	rc = asprintf(&lh->rotate_filename, "%s.1", filename);
 	if (rc < 0) {
 		warn("Failed to construct rotate filename");
-		return -1;
+		goto err_free;
 	}
 
 	rc = log_create(lh);
 	if (rc < 0) {
-		return -1;
+		goto err_free;
 	}
 	lh->rbc = console_ringbuffer_consumer_register(console,
 						       log_ringbuffer_poll, lh);
 
-	return 0;
+	return &lh->handler;
+
+err_free:
+	free(lh->log_filename);
+	free(lh);
+	return NULL;
 }
 
 static void log_fini(struct handler *handler)
@@ -203,14 +214,13 @@
 	close(lh->fd);
 	free(lh->log_filename);
 	free(lh->rotate_filename);
+	free(lh);
 }
 
-static struct log_handler log_handler = {
-	.handler = {
-		.name		= "log",
-		.init		= log_init,
-		.fini		= log_fini,
-	},
+static const struct handler_type log_handler = {
+	.name = "log",
+	.init = log_init,
+	.fini = log_fini,
 };
 
-console_handler_register(&log_handler.handler);
+console_handler_register(&log_handler);