console-server: Add --console-id option

Allow specification of the console-id on the command-line. Specification
on the command line takes precedence over the value in the configuration
file.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Ia143e997b9f5386493564aa92e44e2813173e238
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 78cd16e..3c3cb21 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@
 ### Added
 
 1. console-server: Add PTY support for testing purposes
+2. console-server: Add --console-id option
 
 ### Changed
 
diff --git a/console-server.c b/console-server.c
index 3440fb2..99cf1c7 100644
--- a/console-server.c
+++ b/console-server.c
@@ -53,7 +53,8 @@
 		"usage: %s [options] <DEVICE>\n"
 		"\n"
 		"Options:\n"
-		"  --config <FILE>  Use FILE for configuration\n"
+		"  --config <FILE>\tUse FILE for configuration\n"
+		"  --console-id <NAME>\tUse NAME in the UNIX domain socket address\n"
 		"",
 		progname);
 }
@@ -391,24 +392,32 @@
 }
 
 /* Read console if from config and prepare a socket name */
-static int set_socket_info(struct console *console, struct config *config)
+static int set_socket_info(struct console *console, struct config *config,
+			   const char *console_id)
 {
+	const char *resolved_id;
 	ssize_t len;
 
-	console->console_id = config_get_value(config, "console-id");
+	if (console_id) {
+		resolved_id = console_id;
+	} else {
+		resolved_id = config_get_value(config, "console-id");
 
-	/* socket-id is deprecated */
-	if (!console->console_id) {
-		console->console_id = config_get_value(config, "socket-id");
+		/* socket-id is deprecated */
+		if (!resolved_id) {
+			resolved_id = config_get_value(config, "socket-id");
+		}
 	}
 
-	if (!console->console_id) {
-		warnx("Error: The console-id is not set in the config file");
+	if (!resolved_id) {
+		warnx("console-id was not specified");
 		return EXIT_FAILURE;
 	}
 
+	console->console_id = resolved_id;
+
 	/* Get the socket name/path */
-	len = console_socket_path(console->socket_name, console->console_id);
+	len = console_socket_path(console->socket_name, resolved_id);
 	if (len < 0) {
 		warn("Failed to set socket path: %s", strerror(errno));
 		return EXIT_FAILURE;
@@ -791,6 +800,7 @@
 }
 static const struct option options[] = {
 	{ "config", required_argument, 0, 'c' },
+	{ "console-id", required_argument, 0, 'i' },
 	{ 0, 0, 0, 0 },
 };
 
@@ -798,6 +808,7 @@
 {
 	const char *config_filename = NULL;
 	const char *config_tty_kname = NULL;
+	const char *console_id = NULL;
 	struct console *console;
 	struct config *config;
 	int rc;
@@ -806,7 +817,7 @@
 		int c;
 		int idx;
 
-		c = getopt_long(argc, argv, "c:", options, &idx);
+		c = getopt_long(argc, argv, "c:i:", options, &idx);
 		if (c == -1) {
 			break;
 		}
@@ -815,6 +826,9 @@
 		case 'c':
 			config_filename = optarg;
 			break;
+		case 'i':
+			console_id = optarg;
+			break;
 		case 'h':
 		case '?':
 			usage(argv[0]);
@@ -839,7 +853,7 @@
 		goto out_free;
 	}
 
-	if (set_socket_info(console, config)) {
+	if (set_socket_info(console, config, console_id)) {
 		rc = -1;
 		goto out_config_fini;
 	}
diff --git a/socket-handler.c b/socket-handler.c
index 16aed33..a0ec53e 100644
--- a/socket-handler.c
+++ b/socket-handler.c
@@ -342,7 +342,7 @@
 }
 
 static int socket_init(struct handler *handler, struct console *console,
-		       struct config *config)
+		       struct config *config __attribute__((unused)))
 {
 	struct socket_handler *sh = to_socket_handler(handler);
 	struct sockaddr_un addr;
@@ -356,8 +356,7 @@
 
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_UNIX;
-	len = console_socket_path(addr.sun_path,
-				  config_get_value(config, "console-id"));
+	len = console_socket_path(addr.sun_path, console->console_id);
 	if (len < 0) {
 		if (errno) {
 			warn("Failed to configure socket: %s", strerror(errno));