console-server: refactor pollfd management

Instead of the previous memory management of pollfd array,
with a fixed and flexible part and hardcoded indices,
provide two functions to request and release pollfds.

- console_server_request_pollfd
- console_server_release_pollfd

The pollfds are still in the same array but can be requested and
released by these functions now. struct console_server and
struct console now contain indices into that array of pollfds.

The benefit of this contribution is that the new interface provides a
clean allocator-like abstraction for requesting and releasing pollfds,
which will scale to multiple consoles and can be refactored or
unit-tested more easily in the future.

The previous implementation was tightly coupled to the single-console
use-case and the pollfds stored at hardcoded indices.

Change-Id: I93226699618130b175bffbeb4f71c20c91a7083a
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
diff --git a/console-dbus.c b/console-dbus.c
index 220d3e0..ba8356d 100644
--- a/console-dbus.c
+++ b/console-dbus.c
@@ -166,7 +166,6 @@
 {
 	char obj_name[dbus_obj_path_len];
 	char dbus_name[dbus_obj_path_len];
-	int dbus_poller = 0;
 	int fd;
 	int r;
 	size_t bytes;
@@ -234,10 +233,13 @@
 		return -1;
 	}
 
-	dbus_poller = POLLFD_DBUS;
+	const ssize_t index =
+		console_server_request_pollfd(console->server, fd, POLLIN);
+	if (index < 0) {
+		fprintf(stderr, "Error: failed to allocate pollfd\n");
+		return -1;
+	}
 
-	console->pollfds[dbus_poller].fd = fd;
-	console->pollfds[dbus_poller].events = POLLIN;
-
+	console->dbus_pollfd_index = index;
 	return 0;
 }