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;
kdbus->priv->kdbus_buffer = NULL;
- kdbus->priv->hello_flags = 0; /* KDBUS_HELLO_ACCEPT_FD | KDBUS_HELLO_ACCEPT_MEMFD */
+ kdbus->priv->hello_flags = 0; /* KDBUS_HELLO_ACCEPT_FD */
kdbus->priv->attach_flags = KDBUS_ATTACH_NAMES;
}
/**
- * _g_kdbus_free_data:
+ * g_kdbus_free_data:
*
*/
static gboolean
/**
+ * g_kdbus_translate_nameowner_flags:
+ *
+ */
+static void
+g_kdbus_translate_nameowner_flags (GBusNameOwnerFlags flags,
+ guint64 *kdbus_flags)
+{
+ guint64 new_flags;
+
+ new_flags = 0;
+
+ if (flags & G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT)
+ new_flags |= KDBUS_NAME_ALLOW_REPLACEMENT;
+
+ if (flags & G_BUS_NAME_OWNER_FLAGS_REPLACE)
+ new_flags |= KDBUS_NAME_REPLACE_EXISTING;
+
+ if (!(flags & G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE))
+ new_flags |= KDBUS_NAME_QUEUE;
+
+ *kdbus_flags = new_flags;
+}
+
+
+/**
* _g_kdbus_close:
*
*/
return NULL;
}
- if (hello->bus_flags > 0xFFFFFFFFULL || hello->conn_flags > 0xFFFFFFFFULL)
+ if (hello->bus_flags > 0xFFFFFFFFULL)
{
g_set_error_literal (error,
G_IO_ERROR,
}
+/*
+ * _g_kdbus_RequestName:
+ *
+ */
+GVariant *
+_g_kdbus_RequestName (GDBusConnection *connection,
+ const gchar *name,
+ GBusNameOwnerFlags flags,
+ GError **error)
+{
+ GKdbus *kdbus;
+ GVariant *result;
+ struct kdbus_cmd_name *kdbus_name;
+ guint64 kdbus_flags;
+ gssize len, size;
+ gint status, ret;
+
+ status = G_BUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
+
+ kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
+ if (kdbus == NULL)
+ {
+ g_set_error_literal (error,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_IO_ERROR,
+ _("The connection is closed"));
+ return NULL;
+ }
+
+ if (!g_dbus_is_name (name))
+ {
+ g_set_error (error,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_INVALID_ARGS,
+ "Given bus name \"%s\" is not valid", name);
+ return NULL;
+ }
+
+ if (*name == ':')
+ {
+ g_set_error (error,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_INVALID_ARGS,
+ "Cannot acquire a service starting with ':' such as \"%s\"", name);
+ return NULL;
+ }
+
+ g_kdbus_translate_nameowner_flags (flags, &kdbus_flags);
+
+ len = strlen(name) + 1;
+ size = G_STRUCT_OFFSET (struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(len);
+ kdbus_name = g_alloca0 (size);
+ kdbus_name->size = size;
+ kdbus_name->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
+ kdbus_name->items[0].type = KDBUS_ITEM_NAME;
+ kdbus_name->flags = kdbus_flags;
+ memcpy (kdbus_name->items[0].str, name, len);
+
+ ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_ACQUIRE, kdbus_name);
+ if (ret < 0)
+ {
+ if (errno == EEXIST)
+ status = G_BUS_REQUEST_NAME_REPLY_EXISTS;
+ else if (errno == EALREADY)
+ status = G_BUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
+ else
+ {
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errno),
+ _("Error while acquiring name: %s"),
+ g_strerror (errno));
+ return NULL;
+ }
+ }
+
+ if (kdbus_name->flags & KDBUS_NAME_IN_QUEUE)
+ status = G_BUS_REQUEST_NAME_REPLY_IN_QUEUE;
+
+ result = g_variant_new ("(u)", status);
+
+ return result;
+}
+
+
+/*
+ * _g_kdbus_ReleaseName:
+ *
+ */
+GVariant *
+_g_kdbus_ReleaseName (GDBusConnection *connection,
+ const gchar *name,
+ GError **error)
+{
+ GKdbus *kdbus;
+ GVariant *result;
+ struct kdbus_cmd_name *kdbus_name;
+ gssize len, size;
+ gint status, ret;
+
+ status = G_BUS_RELEASE_NAME_REPLY_RELEASED;
+
+ kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
+ if (kdbus == NULL)
+ {
+ g_set_error_literal (error,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_IO_ERROR,
+ _("The connection is closed"));
+ return NULL;
+ }
+
+ if (!g_dbus_is_name (name))
+ {
+ g_set_error (error,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_INVALID_ARGS,
+ "Given bus name \"%s\" is not valid", name);
+ return NULL;
+ }
+
+ if (*name == ':')
+ {
+ g_set_error (error,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_INVALID_ARGS,
+ "Cannot release a service starting with ':' such as \"%s\"", name);
+ return NULL;
+ }
+
+ len = strlen(name) + 1;
+ size = G_STRUCT_OFFSET (struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(len);
+ kdbus_name = g_alloca0 (size);
+ kdbus_name->size = size;
+ kdbus_name->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
+ kdbus_name->items[0].type = KDBUS_ITEM_NAME;
+ memcpy (kdbus_name->items[0].str, name, len);
+
+ ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_RELEASE, kdbus_name);
+ if (ret < 0)
+ {
+ if (errno == ESRCH)
+ status = G_BUS_RELEASE_NAME_REPLY_NON_EXISTENT;
+ else if (errno == EADDRINUSE)
+ status = G_BUS_RELEASE_NAME_REPLY_NOT_OWNER;
+ else
+ {
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errno),
+ _("Error while releasing name: %s"),
+ g_strerror (errno));
+ return NULL;
+ }
+ }
+
+ result = g_variant_new ("(u)", status);
+
+ return result;
+}
+
+
/**
* _g_kdbus_GetBusId:
*
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;
+}