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/tty-handler.c b/tty-handler.c
index 3976aec..734f81e 100644
--- a/tty-handler.c
+++ b/tty-handler.c
@@ -236,10 +236,12 @@
 	return 0;
 }
 
-static int tty_init(struct handler *handler, struct console *console,
-		    struct config *config __attribute__((unused)))
+static struct handler *tty_init(const struct handler_type *type
+				__attribute__((unused)),
+				struct console *console,
+				struct config *config __attribute__((unused)))
 {
-	struct tty_handler *th = to_tty_handler(handler);
+	struct tty_handler *th;
 	speed_t desired_speed;
 	const char *tty_name;
 	const char *tty_baud;
@@ -248,19 +250,25 @@
 
 	tty_name = config_get_value(config, "local-tty");
 	if (!tty_name) {
-		return -1;
+		return NULL;
 	}
 
 	rc = asprintf(&tty_path, "/dev/%s", tty_name);
 	if (!rc) {
-		return -1;
+		return NULL;
+	}
+
+	th = malloc(sizeof(*th));
+	if (!th) {
+		return NULL;
 	}
 
 	th->fd = open(tty_path, O_RDWR | O_NONBLOCK);
 	if (th->fd < 0) {
 		warn("Can't open %s; disabling local tty", tty_name);
 		free(tty_path);
-		return -1;
+		free(th);
+		return NULL;
 	}
 
 	free(tty_path);
@@ -286,13 +294,13 @@
 		fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name);
 	}
 
-	th->poller = console_poller_register(console, handler, tty_poll, NULL,
-					     th->fd, POLLIN, NULL);
+	th->poller = console_poller_register(console, &th->handler, tty_poll,
+					     NULL, th->fd, POLLIN, NULL);
 	th->console = console;
 	th->rbc = console_ringbuffer_consumer_register(console,
 						       tty_ringbuffer_poll, th);
 
-	return 0;
+	return &th->handler;
 }
 
 static void tty_fini(struct handler *handler)
@@ -302,6 +310,7 @@
 		console_poller_unregister(th->console, th->poller);
 	}
 	close(th->fd);
+	free(th);
 }
 
 static int tty_baudrate(struct handler *handler, speed_t baudrate)
@@ -321,13 +330,11 @@
 	return 0;
 }
 
-static struct tty_handler tty_handler = {
-	.handler = {
-		.name		= "tty",
-		.init		= tty_init,
-		.fini		= tty_fini,
-		.baudrate	= tty_baudrate,
-	},
+static const struct handler_type tty_handler = {
+	.name = "tty",
+	.init = tty_init,
+	.fini = tty_fini,
+	.baudrate = tty_baudrate,
 };
 
-console_handler_register(&tty_handler.handler);
+console_handler_register(&tty_handler);