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/console-server.c b/console-server.c
index d2c2419..4ff3a4e 100644
--- a/console-server.c
+++ b/console-server.c
@@ -533,32 +533,46 @@
 static void handlers_init(struct console *console, struct config *config)
 {
 	/* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
-	extern struct handler *__start_handlers;
-	extern struct handler *__stop_handlers;
+	extern const struct handler_type *const __start_handlers;
+	extern const struct handler_type *const __stop_handlers;
 	/* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
-	struct handler *handler;
-	int i;
-	int rc;
+	size_t n_types;
+	int j = 0;
+	size_t i;
 
-	console->n_handlers = &__stop_handlers - &__start_handlers;
-	console->handlers = &__start_handlers;
+	n_types = &__stop_handlers - &__start_handlers;
+	console->handlers = calloc(n_types, sizeof(struct handler *));
+	if (!console->handlers) {
+		err(EXIT_FAILURE, "malloc(handlers)");
+	}
 
-	printf("%ld handler%s\n", console->n_handlers,
-	       console->n_handlers == 1 ? "" : "s");
+	printf("%ld handler type%s\n", n_types, n_types == 1 ? "" : "s");
 
-	for (i = 0; i < console->n_handlers; i++) {
-		handler = console->handlers[i];
+	for (i = 0; i < n_types; i++) {
+		const struct handler_type *type = &__start_handlers[i];
+		struct handler *handler;
 
-		rc = 0;
-		if (handler->init) {
-			rc = handler->init(handler, console, config);
+		/* Should be picked up at build time by
+		 * console_handler_register, but check anyway
+		 */
+		if (!type->init || !type->fini) {
+			errx(EXIT_FAILURE,
+			     "invalid handler type %s: no init() / fini()",
+			     type->name);
 		}
 
-		handler->active = rc == 0;
+		handler = type->init(type, console, config);
 
-		printf("  %s [%sactive]\n", handler->name,
-		       handler->active ? "" : "in");
+		printf("  console '%s': handler %s [%sactive]\n",
+		       console->console_id, type->name, handler ? "" : "in");
+
+		if (handler) {
+			handler->type = type;
+			console->handlers[j++] = handler;
+		}
 	}
+
+	console->n_handlers = j;
 }
 
 static void handlers_fini(struct console *console)
@@ -568,10 +582,12 @@
 
 	for (i = 0; i < console->n_handlers; i++) {
 		handler = console->handlers[i];
-		if (handler->fini && handler->active) {
-			handler->fini(handler);
-		}
+		handler->type->fini(handler);
 	}
+
+	free(console->handlers);
+	console->handlers = NULL;
+	console->n_handlers = 0;
 }
 
 static int get_current_time(struct timeval *tv)