Add Connect() method to console DBUS object

Added new poller and consumer for the console DBUS data.

Note: We initially developed SocketName attribute but it is now
      deprecated/removed.

The tree of default object:
$ busctl tree xyz.openbmc_project.Console.default
`-/xyz
  `-/xyz/openbmc_project
    `-/xyz/openbmc_project/console
      `-/xyz/openbmc_project/console/default

The introspect of default console:
$ busctl introspect xyz.openbmc_project.Console.default /xyz/openbmc_project/console/default
NAME                                TYPE      SIGNATURE RESULT/VALUE FLAGS
org.freedesktop.DBus.Introspectable interface -         -            -
.Introspect                         method    -         s            -
org.freedesktop.DBus.Peer           interface -         -            -
.GetMachineId                       method    -         s            -
.Ping                               method    -         -            -
org.freedesktop.DBus.Properties     interface -         -            -
.Get                                method    ss        v            -
.GetAll                             method    s         a{sv}        -
.Set                                method    ssv       -            -
.PropertiesChanged                  signal    sa{sv}as  -            -
xyz.openbmc_project.Console.Access  interface -         -            -
.Connect                            method    -         h            -
xyz.openbmc_project.console         interface -         -            -
.setBaudRate                        method    u         x            -
.baudrate                           property  u         0            -

Tested:
  Performed integration testing with bmcweb.

Change-Id: I2444b1083cf26536f43c6f6b4b0857a2921c4f78
Signed-off-by: Ninad Palsule <ninadpalsule@us.ibm.com>
diff --git a/console-dbus.c b/console-dbus.c
index d1dd674..d4477c1 100644
--- a/console-dbus.c
+++ b/console-dbus.c
@@ -17,6 +17,8 @@
 #include <assert.h>
 #include <errno.h>
 #include <err.h>
+#include <string.h>
+#include <sys/socket.h>
 
 #include "console-server.h"
 
@@ -105,20 +107,35 @@
 	return r;
 }
 
-static int get_socket_name(sd_bus *bus __attribute__((unused)),
-			   const char *path __attribute__((unused)),
-			   const char *interface __attribute__((unused)),
-			   const char *property __attribute__((unused)),
-			   sd_bus_message *reply, void *userdata,
-			   sd_bus_error *error __attribute__((unused)))
+static int method_connect(sd_bus_message *msg, void *userdata,
+			  sd_bus_error *err)
 {
 	struct console *console = userdata;
+	int rc;
+	int socket_fd = -1;
 
-	/* The abstract socket name starts with null character hence we need to
-	 * send it as a byte stream instead of regular string.
-	 */
-	return sd_bus_message_append_array(reply, 'y', console->socket_name,
-					   console->socket_name_len);
+	if (!console) {
+		warnx("Internal error: Console pointer is null");
+		sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
+		return sd_bus_reply_method_error(msg, err);
+	}
+
+	/* Register the consumer. */
+	socket_fd = dbus_create_socket_consumer(console);
+	if (socket_fd < 0) {
+		rc = socket_fd;
+		warnx("Failed to create socket consumer: %s", strerror(rc));
+		sd_bus_error_set_const(err, DBUS_ERR,
+				       "Failed to create socket consumer");
+		return sd_bus_reply_method_error(msg, err);
+	}
+
+	rc = sd_bus_reply_method_return(msg, "h", socket_fd);
+
+	/* Close the our end */
+	close(socket_fd);
+
+	return rc;
 }
 
 static const sd_bus_vtable console_tty_vtable[] = {
@@ -131,7 +148,8 @@
 
 static const sd_bus_vtable console_access_vtable[] = {
 	SD_BUS_VTABLE_START(0),
-	SD_BUS_PROPERTY("SocketName", "ay", get_socket_name, 0, 0),
+	SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect,
+		      SD_BUS_VTABLE_UNPRIVILEGED),
 	SD_BUS_VTABLE_END,
 };