[kdbus] Add RequestName and ReleaseName to new API
[platform/upstream/glib.git] / gio / gkdbus.c
index 312b34f..a2cde10 100644 (file)
@@ -78,29 +78,14 @@ typedef enum
   G_BUS_CREDS_SELINUX_CONTEXT  = 4
 } GBusCredentialsFlags;
 
-/* GBusNameOwnerReturnFlags */
-typedef enum
-{
-  G_BUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = 1, /* Caller is now the primary owner of the name, replacing any previous owner */
-  G_BUS_REQUEST_NAME_REPLY_IN_QUEUE = 2,      /* The name already had an owner, the application will be placed in a queue */
-  G_BUS_REQUEST_NAME_REPLY_EXISTS = 3,        /* The name already has an owner */
-  G_BUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4  /* The application trying to request ownership of a name is already the owner of it */
-} GBusNameOwnerReturnFlags;
-
-/* GBusReleaseNameReturnFlags */
-typedef enum
-{
-  G_BUS_RELEASE_NAME_REPLY_RELEASED = 1,     /* The caller has released his claim on the given name */
-  G_BUS_RELEASE_NAME_REPLY_NON_EXISTENT = 2, /* The given name does not exist on this bus*/
-  G_BUS_RELEASE_NAME_REPLY_NOT_OWNER = 3     /* The caller not waiting in the queue to own this name*/
-} GBusReleaseNameReturnFlags;
-
 /* GKdbusPrivate struct */
 struct _GKdbusPrivate
 {
   gint               fd;
 
   gchar             *kdbus_buffer;
+  struct kdbus_msg  *kmsg;
+
   gchar             *unique_name;
   guint64            unique_id;
 
@@ -642,7 +627,7 @@ _g_kdbus_RequestName (GDBusConnection     *connection,
   gssize len, size;
   gint status, ret;
 
-  status = G_BUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
+  status = G_BUS_REQUEST_NAME_FLAGS_PRIMARY_OWNER;
 
   kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
   if (kdbus == NULL)
@@ -687,9 +672,9 @@ _g_kdbus_RequestName (GDBusConnection     *connection,
   if (ret < 0)
     {
       if (errno == EEXIST)
-        status = G_BUS_REQUEST_NAME_REPLY_EXISTS;
+        status = G_BUS_REQUEST_NAME_FLAGS_EXISTS;
       else if (errno == EALREADY)
-        status = G_BUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
+        status = G_BUS_REQUEST_NAME_FLAGS_ALREADY_OWNER;
       else
         {
           g_set_error (error, G_IO_ERROR,
@@ -701,7 +686,7 @@ _g_kdbus_RequestName (GDBusConnection     *connection,
     }
 
   if (kdbus_name->flags & KDBUS_NAME_IN_QUEUE)
-    status = G_BUS_REQUEST_NAME_REPLY_IN_QUEUE;
+    status = G_BUS_REQUEST_NAME_FLAGS_IN_QUEUE;
 
   result = g_variant_new ("(u)", status);
 
@@ -724,7 +709,7 @@ _g_kdbus_ReleaseName (GDBusConnection     *connection,
   gssize len, size;
   gint status, ret;
 
-  status = G_BUS_RELEASE_NAME_REPLY_RELEASED;
+  status = G_BUS_RELEASE_NAME_FLAGS_RELEASED;
 
   kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
   if (kdbus == NULL)
@@ -766,9 +751,9 @@ _g_kdbus_ReleaseName (GDBusConnection     *connection,
   if (ret < 0)
     {
       if (errno == ESRCH)
-        status = G_BUS_RELEASE_NAME_REPLY_NON_EXISTENT;
+        status = G_BUS_RELEASE_NAME_FLAGS_NON_EXISTENT;
       else if (errno == EADDRINUSE)
-        status = G_BUS_RELEASE_NAME_REPLY_NOT_OWNER;
+        status = G_BUS_RELEASE_NAME_FLAGS_NOT_OWNER;
       else
         {
           g_set_error (error, G_IO_ERROR,
@@ -1248,3 +1233,267 @@ _g_kdbus_GetConnectionUnixUser (GDBusConnection  *connection,
                                        G_BUS_CREDS_UID,
                                        error);
 }
+
+
+/*
+ * _g_kdbus_subscribe_name_acquired:
+ *
+ */
+static void
+_g_kdbus_subscribe_name_owner_changed (GDBusConnection  *connection,
+                                       const gchar      *name,
+                                       const gchar      *old_name,
+                                       const gchar      *new_name,
+                                       guint             cookie)
+{
+  GKdbus *kdbus;
+  struct kdbus_item *item;
+  struct kdbus_cmd_match *cmd_match;
+  gssize size, len;
+  gint ret;
+  guint64 old_id;
+  guint64 new_id = KDBUS_MATCH_ID_ANY;
+
+  kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
+
+  len = strlen(name) + 1;
+  size = KDBUS_ALIGN8(G_STRUCT_OFFSET (struct kdbus_cmd_match, items) +
+                      G_STRUCT_OFFSET (struct kdbus_item, name_change) +
+                      G_STRUCT_OFFSET (struct kdbus_notify_name_change, name) + len);
+
+  cmd_match = g_alloca0 (size);
+  cmd_match->size = size;
+  cmd_match->cookie = cookie;
+  item = cmd_match->items;
+
+  if (old_name[0] == 0)
+    {
+      old_id = KDBUS_MATCH_ID_ANY;
+    }
+  else
+    {
+      if (g_dbus_is_unique_name(old_name))
+        old_id = old_id+3;
+      else
+        return;
+    }
+
+  if (new_name[0] == 0)
+    {
+      new_id = KDBUS_MATCH_ID_ANY;
+    }
+  else
+    {
+      if (g_dbus_is_unique_name(new_name))
+        new_id = new_id+3;
+      else
+        return;
+    }
+
+  cmd_match = g_alloca0 (size);
+  cmd_match->size = size;
+  cmd_match->cookie = cookie;
+  item = cmd_match->items;
+
+  /* KDBUS_ITEM_NAME_CHANGE */
+  item->type = KDBUS_ITEM_NAME_CHANGE;
+  item->name_change.old_id.id = old_id;
+  item->name_change.new_id.id = new_id;
+  memcpy(item->name_change.name, name, len);
+  item->size = G_STRUCT_OFFSET (struct kdbus_item, name_change) +
+               G_STRUCT_OFFSET(struct kdbus_notify_name_change, name) + len;
+  item = KDBUS_ITEM_NEXT(item);
+
+  ret = ioctl(kdbus->priv->fd, KDBUS_CMD_MATCH_ADD, cmd_match);
+  if (ret < 0)
+    g_warning ("ERROR - %d\n", (int) errno);
+}
+
+
+/*
+ * _g_kdbus_subscribe_name_acquired:
+ *
+ */
+void
+_g_kdbus_subscribe_name_acquired (GDBusConnection  *connection,
+                                  const gchar      *name)
+{
+  GKdbus *kdbus;
+  struct kdbus_item *item;
+  struct kdbus_cmd_match *cmd_match;
+  gssize size, len;
+  guint64 cookie;
+  gint ret;
+
+  kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
+
+  len = strlen(name) + 1;
+  size = KDBUS_ALIGN8(G_STRUCT_OFFSET (struct kdbus_cmd_match, items) +
+                      G_STRUCT_OFFSET (struct kdbus_item, name_change) +
+                      G_STRUCT_OFFSET (struct kdbus_notify_name_change, name) + len);
+
+  cookie = 0xbeefbeefbeefbeef;
+  cmd_match = g_alloca0 (size);
+  cmd_match->size = size;
+  cmd_match->cookie = cookie;
+  item = cmd_match->items;
+
+  /* KDBUS_ITEM_NAME_ADD */
+  item->type = KDBUS_ITEM_NAME_ADD;
+  item->name_change.old_id.id = KDBUS_MATCH_ID_ANY;
+  item->name_change.new_id.id = kdbus->priv->unique_id;
+  memcpy(item->name_change.name, name, len);
+  item->size = G_STRUCT_OFFSET (struct kdbus_item, name_change) +
+               G_STRUCT_OFFSET(struct kdbus_notify_name_change, name) + len;
+  item = KDBUS_ITEM_NEXT(item);
+
+  ret = ioctl(kdbus->priv->fd, KDBUS_CMD_MATCH_ADD, cmd_match);
+  if (ret < 0)
+    g_warning ("ERROR - %d\n", (int) errno);
+
+  _g_kdbus_subscribe_name_owner_changed (connection, name, "", kdbus->priv->unique_name, cookie);
+}
+
+
+/*
+ * _g_kdbus_subscribe_name_lost:
+ *
+ */
+void
+_g_kdbus_subscribe_name_lost (GDBusConnection  *connection,
+                              const gchar      *name)
+{
+  GKdbus *kdbus;
+  struct kdbus_item *item;
+  struct kdbus_cmd_match *cmd_match;
+  gssize size, len;
+  guint64 cookie;
+  gint ret;
+
+  kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
+
+  len = strlen(name) + 1;
+  size = KDBUS_ALIGN8(G_STRUCT_OFFSET (struct kdbus_cmd_match, items) +
+                      G_STRUCT_OFFSET (struct kdbus_item, name_change) +
+                      G_STRUCT_OFFSET (struct kdbus_notify_name_change, name) + len);
+
+  cookie = 0xdeafdeafdeafdeaf;
+  cmd_match = g_alloca0 (size);
+  cmd_match->size = size;
+  cmd_match->cookie = cookie;
+  item = cmd_match->items;
+
+  /* KDBUS_ITEM_NAME_REMOVE */
+  item->type = KDBUS_ITEM_NAME_REMOVE;
+  item->name_change.old_id.id = kdbus->priv->unique_id;
+  item->name_change.new_id.id = KDBUS_MATCH_ID_ANY;
+  memcpy(item->name_change.name, name, len);
+  item->size = G_STRUCT_OFFSET (struct kdbus_item, name_change) +
+               G_STRUCT_OFFSET(struct kdbus_notify_name_change, name) + len;
+  item = KDBUS_ITEM_NEXT(item);
+
+  ret = ioctl(kdbus->priv->fd, KDBUS_CMD_MATCH_ADD, cmd_match);
+  if (ret < 0)
+    g_warning ("ERROR - %d\n", (int) errno);
+
+  _g_kdbus_subscribe_name_owner_changed (connection, name, kdbus->priv->unique_name, "", cookie);
+}
+
+
+/**
+* g_kdbus_decode_kernel_msg:
+*
+*/
+static gssize
+g_kdbus_decode_kernel_msg (GKdbus  *kdbus)
+{
+  struct kdbus_item *item = NULL;
+  gssize size = 0;
+
+  KDBUS_ITEM_FOREACH(item, kdbus->priv->kmsg, items)
+    {
+     switch (item->type)
+        {
+          case KDBUS_ITEM_ID_ADD:
+          case KDBUS_ITEM_ID_REMOVE:
+          case KDBUS_ITEM_NAME_ADD:
+          case KDBUS_ITEM_NAME_REMOVE:
+          case KDBUS_ITEM_NAME_CHANGE:
+            //size = g_kdbus_NameOwnerChanged_generate (kdbus, item);
+            g_error ("'NameOwnerChanged'");
+            break;
+
+          case KDBUS_ITEM_REPLY_TIMEOUT:
+          case KDBUS_ITEM_REPLY_DEAD:
+            //size = g_kdbus_KernelMethodError_generate (kdbus, item);
+            g_error ("'KernelMethodError'");
+            break;
+
+          default:
+            g_warning ("Unknown field in kernel message - %lld", item->type);
+       }
+    }
+
+#if 0
+  /* Override information from the user header with data from the kernel */
+  g_string_printf (kdbus->priv->msg_sender, "org.freedesktop.DBus");
+
+  /* for destination */
+  if (kdbus->priv->kmsg->dst_id == KDBUS_DST_ID_BROADCAST)
+    /* for broadcast messages we don't have to set destination */
+    ;
+  else if (kdbus->priv->kmsg->dst_id == KDBUS_DST_ID_NAME)
+    g_string_printf (kdbus->priv->msg_destination, ":1.%" G_GUINT64_FORMAT, (guint64) kdbus->priv->unique_id);
+  else
+   g_string_printf (kdbus->priv->msg_destination, ":1.%" G_GUINT64_FORMAT, (guint64) kdbus->priv->kmsg->dst_id);
+#endif
+
+  return size;
+}
+
+
+/**
+ * _g_kdbus_receive:
+ *
+ */
+gssize
+_g_kdbus_receive (GKdbus        *kdbus,
+                  GCancellable  *cancellable,
+                  GError       **error)
+{
+  struct kdbus_cmd_recv recv = {};
+  gssize size = 0;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return -1;
+
+again:
+    if (ioctl(kdbus->priv->fd, KDBUS_CMD_MSG_RECV, &recv) < 0)
+      {
+        if (errno == EINTR || errno == EAGAIN)
+          goto again;
+
+        g_set_error (error, G_IO_ERROR,
+                     g_io_error_from_errno (errno),
+                     _("Error while receiving message: %s"),
+                     g_strerror (errno));
+        return -1;
+      }
+
+   kdbus->priv->kmsg = (struct kdbus_msg *)((guint8 *)kdbus->priv->kdbus_buffer + recv.offset);
+
+   if (kdbus->priv->kmsg->payload_type == KDBUS_PAYLOAD_DBUS)
+     g_error ("Received standard dbus message - not supported yet");
+   else if (kdbus->priv->kmsg->payload_type == KDBUS_PAYLOAD_KERNEL)
+     size = g_kdbus_decode_kernel_msg (kdbus);
+   else
+     {
+       g_set_error (error,
+                    G_DBUS_ERROR,
+                    G_DBUS_ERROR_FAILED,
+                    _("Received unknown payload type"));
+       return -1;
+     }
+
+   return size;
+}