Fixed broken dbus interface for multiple consoles

There is a console server instance per console. So if there are two
consoles then two console server instances will be started. The issue
is that each instance is sharing same bus name and object path. The
issue is fixed by keeping the interface name same as before
(xyz.openbmc_project.console) but changing the bus name and object path
name by appending socket-id which makes them unique. So the bus name
becomes xyz.openbmc_project.Console.<socket-id> and the object name
becomes /xyz/openbmc_project/Console/<socket-id>. Each console server
has its own socket-id hence there should not be any conflict between two
consoles.

Testing:
    -> Ran busctl to check object changes
    $ busctl get-property xyz.openbmc_project.Console.console0 /xyz/openbmc_project/console/console0 xyz.openbmc_project.console baudrate
    u 0
    $ busctl introspect xyz.openbmc_project.Console.console0 /xyz/openbmc_project/console/console0    ....
    xyz.openbmc_project.Console         interface -         -            -
    .setBaudRate                        method    u         x            -
    .baudrate                           property  u         0            -

Change-Id: Ic1aacd8f284a867416081b4965b55ba7c3cb34e3
Signed-off-by: Ninad Palsule <ninadpalsule@us.ibm.com>
diff --git a/console-server.c b/console-server.c
index 9da26b6..169d8f9 100644
--- a/console-server.c
+++ b/console-server.c
@@ -38,14 +38,19 @@
 
 #include "console-server.h"
 
+/* size of the dbus object path length */
+const size_t dbus_obj_path_len = 1024;
+
 #define DBUS_ERR  "org.openbmc.error"
-#define DBUS_NAME "xyz.openbmc_project.console"
-#define OBJ_NAME  "/xyz/openbmc_project/console"
+#define INTF_NAME "xyz.openbmc_project.console"
+#define DBUS_NAME "xyz.openbmc_project.Console.%s"
+#define OBJ_NAME  "/xyz/openbmc_project/console/%s"
 
 struct console {
 	const char *tty_kname;
 	char *tty_sysfs_devnode;
 	char *tty_dev;
+	const char *console_id;
 	int tty_sirq;
 	uint16_t tty_lpc_addr;
 	speed_t tty_baud;
@@ -428,9 +433,12 @@
 static void dbus_init(struct console *console,
 		      struct config *config __attribute__((unused)))
 {
+	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;
 
 	if (!console) {
 		warnx("Couldn't get valid console");
@@ -443,14 +451,31 @@
 		return;
 	}
 
-	r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
+	/* Register support console interface */
+	bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME,
+			 console->console_id);
+	if (bytes >= dbus_obj_path_len) {
+		warnx("Console id '%s' is too long. There is no enough space in the buffer.",
+		      console->console_id);
+		return;
+	}
+
+	r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, INTF_NAME,
 				     console_vtable, console);
 	if (r < 0) {
 		warnx("Failed to issue method call: %s", strerror(-r));
 		return;
 	}
 
-	r = sd_bus_request_name(console->bus, DBUS_NAME,
+	bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME,
+			 console->console_id);
+	if (bytes >= dbus_obj_path_len) {
+		warnx("Console id '%s' is too long. There is no enough space in the buffer.",
+		      console->console_id);
+		return;
+	}
+
+	r = sd_bus_request_name(console->bus, dbus_name,
 				SD_BUS_NAME_ALLOW_REPLACEMENT |
 					SD_BUS_NAME_REPLACE_EXISTING);
 	if (r < 0) {
@@ -902,6 +927,12 @@
 
 	console->tty_kname = config_tty_kname;
 
+	console->console_id = config_get_value(config, "socket-id");
+	if (!console->console_id) {
+		warnx("Error: The socket-id is not set in the config file");
+		return EXIT_FAILURE;
+	}
+
 	rc = tty_init(console, config);
 	if (rc) {
 		goto out_config_fini;