1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2013 Samsung Electronics
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Michal Eljasiewicz <m.eljasiewic@samsung.com>
21 * Author: Lukasz Skalski <l.skalski@samsung.com>
26 #include "glib-unix.h"
29 #include "gkdbusconnection.h"
35 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_FILIO_H
39 # include <sys/filio.h>
46 #define KDBUS_POOL_SIZE (16 * 1024LU * 1024LU)
47 #define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
48 #define KDBUS_ALIGN8_PTR(p) ((void*) (uintptr_t)(p))
50 #define KDBUS_ITEM_HEADER_SIZE G_STRUCT_OFFSET(struct kdbus_item, data)
51 #define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_ITEM_HEADER_SIZE)
53 #define KDBUS_ITEM_NEXT(item) \
54 (typeof(item))(((guint8 *)item) + KDBUS_ALIGN8((item)->size))
55 #define KDBUS_ITEM_FOREACH(item, head, first) \
56 for (item = (head)->first; \
57 (guint8 *)(item) < (guint8 *)(head) + (head)->size; \
58 item = KDBUS_ITEM_NEXT(item))
60 #define g_alloca0(x) memset(g_alloca(x), '\0', (x))
62 static void g_kdbus_initable_iface_init (GInitableIface *iface);
63 static gboolean g_kdbus_initable_init (GInitable *initable,
64 GCancellable *cancellable,
67 #define g_kdbus_get_type _g_kdbus_get_type
68 G_DEFINE_TYPE_WITH_CODE (GKdbus, g_kdbus, G_TYPE_OBJECT,
69 G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
70 g_kdbus_initable_iface_init));
72 /* GBusCredentialsFlags */
77 G_BUS_CREDS_UNIQUE_NAME = 3,
78 G_BUS_CREDS_SELINUX_CONTEXT = 4
79 } GBusCredentialsFlags;
81 /* GBusNameOwnerReturnFlags */
84 G_BUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = 1, /* Caller is now the primary owner of the name, replacing any previous owner */
85 G_BUS_REQUEST_NAME_REPLY_IN_QUEUE = 2, /* The name already had an owner, the application will be placed in a queue */
86 G_BUS_REQUEST_NAME_REPLY_EXISTS = 3, /* The name already has an owner */
87 G_BUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4 /* The application trying to request ownership of a name is already the owner of it */
88 } GBusNameOwnerReturnFlags;
90 /* GBusReleaseNameReturnFlags */
93 G_BUS_RELEASE_NAME_REPLY_RELEASED = 1, /* The caller has released his claim on the given name */
94 G_BUS_RELEASE_NAME_REPLY_NON_EXISTENT = 2, /* The given name does not exist on this bus*/
95 G_BUS_RELEASE_NAME_REPLY_NOT_OWNER = 3 /* The caller not waiting in the queue to own this name*/
96 } GBusReleaseNameReturnFlags;
98 /* GKdbusPrivate struct */
108 guint64 attach_flags;
118 /* GKdbusSource struct */
123 GIOCondition condition;
124 GCancellable *cancellable;
125 GPollFD cancel_pollfd;
130 typedef gboolean (*GKdbusSourceFunc) (GKdbus *kdbus,
131 GIOCondition condition,
139 g_kdbus_finalize (GObject *object)
141 GKdbus *kdbus = G_KDBUS (object);
143 if (kdbus->priv->kdbus_buffer != NULL)
144 munmap (kdbus->priv->kdbus_buffer, KDBUS_POOL_SIZE);
146 kdbus->priv->kdbus_buffer = NULL;
148 if (kdbus->priv->fd != -1 && !kdbus->priv->closed)
149 _g_kdbus_close (kdbus, NULL);
151 if (G_OBJECT_CLASS (g_kdbus_parent_class)->finalize)
152 (*G_OBJECT_CLASS (g_kdbus_parent_class)->finalize) (object);
157 * g_kdbus_class_init:
161 g_kdbus_class_init (GKdbusClass *klass)
163 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
165 g_type_class_add_private (klass, sizeof (GKdbusPrivate));
166 gobject_class->finalize = g_kdbus_finalize;
171 * g_kdbus_initable_iface_init:
175 g_kdbus_initable_iface_init (GInitableIface *iface)
177 iface->init = g_kdbus_initable_init;
186 g_kdbus_init (GKdbus *kdbus)
188 kdbus->priv = G_TYPE_INSTANCE_GET_PRIVATE (kdbus, G_TYPE_KDBUS, GKdbusPrivate);
190 kdbus->priv->fd = -1;
192 kdbus->priv->unique_id = -1;
193 kdbus->priv->unique_name = NULL;
195 kdbus->priv->kdbus_buffer = NULL;
197 kdbus->priv->hello_flags = 0; /* KDBUS_HELLO_ACCEPT_FD */
198 kdbus->priv->attach_flags = KDBUS_ATTACH_NAMES;
203 * g_kdbus_initable_init:
207 g_kdbus_initable_init (GInitable *initable,
208 GCancellable *cancellable,
213 g_return_val_if_fail (G_IS_KDBUS (initable), FALSE);
215 kdbus = G_KDBUS (initable);
217 if (cancellable != NULL)
219 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
220 _("Cancellable initialization not supported"));
224 kdbus->priv->inited = TRUE;
231 * kdbus_source_prepare:
235 kdbus_source_prepare (GSource *source,
238 GKdbusSource *kdbus_source = (GKdbusSource *)source;
240 if (g_cancellable_is_cancelled (kdbus_source->cancellable))
243 if (kdbus_source->timeout_time)
247 now = g_source_get_time (source);
249 *timeout = (kdbus_source->timeout_time - now + 999) / 1000;
252 kdbus_source->kdbus->priv->timed_out = TRUE;
260 if ((kdbus_source->condition & kdbus_source->pollfd.revents) != 0)
268 * kdbus_source_check:
272 kdbus_source_check (GSource *source)
276 return kdbus_source_prepare (source, &timeout);
281 * kdbus_source_dispatch
285 kdbus_source_dispatch (GSource *source,
286 GSourceFunc callback,
289 GKdbusSourceFunc func = (GKdbusSourceFunc)callback;
290 GKdbusSource *kdbus_source = (GKdbusSource *)source;
291 GKdbus *kdbus = kdbus_source->kdbus;
294 if (kdbus_source->kdbus->priv->timed_out)
295 kdbus_source->pollfd.revents |= kdbus_source->condition & (G_IO_IN | G_IO_OUT);
297 ret = (*func) (kdbus,
298 kdbus_source->pollfd.revents & kdbus_source->condition,
301 if (kdbus->priv->timeout)
302 kdbus_source->timeout_time = g_get_monotonic_time ()
303 + kdbus->priv->timeout * 1000000;
306 kdbus_source->timeout_time = 0;
313 * kdbus_source_finalize
317 kdbus_source_finalize (GSource *source)
319 GKdbusSource *kdbus_source = (GKdbusSource *)source;
322 kdbus = kdbus_source->kdbus;
324 g_object_unref (kdbus);
326 if (kdbus_source->cancellable)
328 g_cancellable_release_fd (kdbus_source->cancellable);
329 g_object_unref (kdbus_source->cancellable);
335 * kdbus_source_closure_callback:
339 kdbus_source_closure_callback (GKdbus *kdbus,
340 GIOCondition condition,
343 GClosure *closure = data;
344 GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
345 GValue result_value = G_VALUE_INIT;
348 g_value_init (&result_value, G_TYPE_BOOLEAN);
350 g_value_init (¶ms[0], G_TYPE_KDBUS);
351 g_value_set_object (¶ms[0], kdbus);
352 g_value_init (¶ms[1], G_TYPE_IO_CONDITION);
353 g_value_set_flags (¶ms[1], condition);
355 g_closure_invoke (closure, &result_value, 2, params, NULL);
357 result = g_value_get_boolean (&result_value);
358 g_value_unset (&result_value);
359 g_value_unset (¶ms[0]);
360 g_value_unset (¶ms[1]);
366 static GSourceFuncs kdbus_source_funcs =
368 kdbus_source_prepare,
370 kdbus_source_dispatch,
371 kdbus_source_finalize,
372 (GSourceFunc)kdbus_source_closure_callback,
381 kdbus_source_new (GKdbus *kdbus,
382 GIOCondition condition,
383 GCancellable *cancellable)
386 GKdbusSource *kdbus_source;
388 source = g_source_new (&kdbus_source_funcs, sizeof (GKdbusSource));
389 g_source_set_name (source, "GKdbus");
390 kdbus_source = (GKdbusSource *)source;
392 kdbus_source->kdbus = g_object_ref (kdbus);
393 kdbus_source->condition = condition;
395 if (g_cancellable_make_pollfd (cancellable,
396 &kdbus_source->cancel_pollfd))
398 kdbus_source->cancellable = g_object_ref (cancellable);
399 g_source_add_poll (source, &kdbus_source->cancel_pollfd);
402 kdbus_source->pollfd.fd = kdbus->priv->fd;
403 kdbus_source->pollfd.events = condition;
404 kdbus_source->pollfd.revents = 0;
405 g_source_add_poll (source, &kdbus_source->pollfd);
407 if (kdbus->priv->timeout)
408 kdbus_source->timeout_time = g_get_monotonic_time ()
409 + kdbus->priv->timeout * 1000000;
411 kdbus_source->timeout_time = 0;
418 * _g_kdbus_create_source:
422 _g_kdbus_create_source (GKdbus *kdbus,
423 GIOCondition condition,
424 GCancellable *cancellable)
426 g_return_val_if_fail (G_IS_KDBUS (kdbus) && (cancellable == NULL || G_IS_CANCELLABLE (cancellable)), NULL);
428 return kdbus_source_new (kdbus, condition, cancellable);
437 _g_kdbus_open (GKdbus *kdbus,
438 const gchar *address,
441 g_return_val_if_fail (G_IS_KDBUS (kdbus), FALSE);
443 kdbus->priv->fd = open(address, O_RDWR|O_NOCTTY|O_CLOEXEC);
444 if (kdbus->priv->fd<0)
446 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't open kdbus endpoint"));
450 kdbus->priv->closed = FALSE;
461 g_kdbus_free_data (GKdbus *kdbus,
464 struct kdbus_cmd_free cmd;
470 ret = ioctl (kdbus->priv->fd, KDBUS_CMD_FREE, &cmd);
479 * g_kdbus_translate_nameowner_flags:
483 g_kdbus_translate_nameowner_flags (GBusNameOwnerFlags flags,
484 guint64 *kdbus_flags)
490 if (flags & G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT)
491 new_flags |= KDBUS_NAME_ALLOW_REPLACEMENT;
493 if (flags & G_BUS_NAME_OWNER_FLAGS_REPLACE)
494 new_flags |= KDBUS_NAME_REPLACE_EXISTING;
496 if (!(flags & G_BUS_NAME_OWNER_FLAGS_DO_NOT_QUEUE))
497 new_flags |= KDBUS_NAME_QUEUE;
499 *kdbus_flags = new_flags;
508 _g_kdbus_close (GKdbus *kdbus,
513 g_return_val_if_fail (G_IS_KDBUS (kdbus), FALSE);
515 if (kdbus->priv->closed)
520 res = close (kdbus->priv->fd);
527 g_set_error (error, G_IO_ERROR,
528 g_io_error_from_errno (errno),
529 _("Error closing kdbus fd: %s"),
536 kdbus->priv->closed = TRUE;
537 kdbus->priv->fd = -1;
544 * _g_kdbus_is_closed:
548 _g_kdbus_is_closed (GKdbus *kdbus)
550 g_return_val_if_fail (G_IS_KDBUS (kdbus), FALSE);
552 return kdbus->priv->closed;
561 _g_kdbus_Hello (GIOStream *stream,
565 struct kdbus_cmd_hello *hello;
566 struct kdbus_item *item;
569 size_t size, conn_name_size;
571 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (stream));
573 conn_name = "gdbus-kdbus";
574 conn_name_size = strlen (conn_name);
576 size = KDBUS_ALIGN8 (G_STRUCT_OFFSET (struct kdbus_cmd_hello, items)) +
577 KDBUS_ALIGN8 (G_STRUCT_OFFSET (struct kdbus_item, str) + conn_name_size + 1);
579 hello = g_alloca0 (size);
580 hello->conn_flags = kdbus->priv->hello_flags;
581 hello->attach_flags = kdbus->priv->attach_flags;
583 hello->pool_size = KDBUS_POOL_SIZE;
586 item->size = G_STRUCT_OFFSET (struct kdbus_item, str) + conn_name_size + 1;
587 item->type = KDBUS_ITEM_CONN_NAME;
588 memcpy (item->str, conn_name, conn_name_size+1);
589 item = KDBUS_ITEM_NEXT (item);
591 if (ioctl(kdbus->priv->fd, KDBUS_CMD_HELLO, hello))
593 g_set_error (error, G_IO_ERROR,
594 g_io_error_from_errno (errno),
595 _("Failed to send HELLO: %s"),
600 kdbus->priv->kdbus_buffer = mmap(NULL, KDBUS_POOL_SIZE, PROT_READ, MAP_SHARED, kdbus->priv->fd, 0);
601 if (kdbus->priv->kdbus_buffer == MAP_FAILED)
603 g_set_error (error, G_IO_ERROR,
604 g_io_error_from_errno (errno),
610 if (hello->bus_flags > 0xFFFFFFFFULL)
612 g_set_error_literal (error,
615 _("Incompatible HELLO flags"));
619 memcpy (kdbus->priv->bus_id, hello->id128, 16);
621 kdbus->priv->unique_id = hello->id;
622 asprintf(&kdbus->priv->unique_name, ":1.%llu", (unsigned long long) hello->id);
624 return g_variant_new ("(s)", kdbus->priv->unique_name);
629 * _g_kdbus_RequestName:
633 _g_kdbus_RequestName (GDBusConnection *connection,
635 GBusNameOwnerFlags flags,
640 struct kdbus_cmd_name *kdbus_name;
645 status = G_BUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
647 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
650 g_set_error_literal (error,
652 G_DBUS_ERROR_IO_ERROR,
653 _("The connection is closed"));
657 if (!g_dbus_is_name (name))
661 G_DBUS_ERROR_INVALID_ARGS,
662 "Given bus name \"%s\" is not valid", name);
670 G_DBUS_ERROR_INVALID_ARGS,
671 "Cannot acquire a service starting with ':' such as \"%s\"", name);
675 g_kdbus_translate_nameowner_flags (flags, &kdbus_flags);
677 len = strlen(name) + 1;
678 size = G_STRUCT_OFFSET (struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(len);
679 kdbus_name = g_alloca0 (size);
680 kdbus_name->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
681 kdbus_name->items[0].type = KDBUS_ITEM_NAME;
682 kdbus_name->flags = kdbus_flags;
683 memcpy (kdbus_name->items[0].str, name, len);
685 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_ACQUIRE, kdbus_name);
689 status = G_BUS_REQUEST_NAME_REPLY_EXISTS;
690 else if (errno == EALREADY)
691 status = G_BUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
696 if (kdbus_name->flags & KDBUS_NAME_IN_QUEUE)
697 status = G_BUS_REQUEST_NAME_REPLY_IN_QUEUE;
699 result = g_variant_new ("(u)", status);
710 _g_kdbus_GetBusId (GDBusConnection *connection,
718 result_str = g_string_new (NULL);
719 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
722 g_set_error_literal (error,
724 G_DBUS_ERROR_IO_ERROR,
725 _("The connection is closed"));
726 g_string_free (result_str, TRUE);
730 for (cnt=0; cnt<16; cnt++)
731 g_string_append_printf (result_str, "%02x", kdbus->priv->bus_id[cnt]);
733 result = g_variant_new ("(s)", result_str->str);
734 g_string_free (result_str, TRUE);
741 * _g_kdbus_GetListNames:
745 _g_kdbus_GetListNames (GDBusConnection *connection,
746 guint list_name_type,
751 GVariantBuilder *builder;
753 struct kdbus_cmd_name_list cmd = {};
754 struct kdbus_name_list *name_list;
755 struct kdbus_cmd_name *name;
761 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
764 g_set_error_literal (error,
766 G_DBUS_ERROR_IO_ERROR,
767 _("The connection is closed"));
772 cmd.flags = KDBUS_NAME_LIST_ACTIVATORS; /* ListActivatableNames */
774 cmd.flags = KDBUS_NAME_LIST_UNIQUE | KDBUS_NAME_LIST_NAMES; /* ListNames */
776 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_LIST, &cmd);
782 _("Error listing names"));
786 name_list = (struct kdbus_name_list *) ((guint8 *) kdbus->priv->kdbus_buffer + cmd.offset);
787 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
789 KDBUS_ITEM_FOREACH(name, name_list, names)
791 struct kdbus_item *item;
792 const gchar *item_name = "";
794 if ((cmd.flags & KDBUS_NAME_LIST_UNIQUE) && name->owner_id != prev_id)
796 GString *unique_name;
798 unique_name = g_string_new (NULL);
799 g_string_printf (unique_name, ":1.%llu", name->owner_id);
800 g_variant_builder_add (builder, "s", unique_name->str);
801 g_string_free (unique_name,TRUE);
802 prev_id = name->owner_id;
805 KDBUS_ITEM_FOREACH(item, name, items)
806 if (item->type == KDBUS_ITEM_NAME)
807 item_name = item->str;
809 if (g_dbus_is_name (item_name))
810 g_variant_builder_add (builder, "s", item_name);
813 result = g_variant_new ("(as)", builder);
814 g_variant_builder_unref (builder);
816 g_kdbus_free_data (kdbus, cmd.offset);
822 * _g_kdbus_NameHasOwner_internal:
826 g_kdbus_NameHasOwner_internal (GKdbus *kdbus,
830 struct kdbus_cmd_conn_info *cmd;
834 if (g_dbus_is_unique_name(name))
836 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items);
837 cmd = g_alloca0 (size);
838 cmd->id = g_ascii_strtoull (name+3, NULL, 10);
842 len = strlen(name) + 1;
843 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items) + KDBUS_ITEM_SIZE(len);
844 cmd = g_alloca0 (size);
845 cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
846 cmd->items[0].type = KDBUS_ITEM_NAME;
847 memcpy (cmd->items[0].str, name, len);
851 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_CONN_INFO, cmd);
852 g_kdbus_free_data (kdbus, cmd->offset);
862 * _g_kdbus_GetListQueuedOwners:
866 _g_kdbus_GetListQueuedOwners (GDBusConnection *connection,
872 GVariantBuilder *builder;
873 GString *unique_name;
876 struct kdbus_cmd_name_list cmd = {};
877 struct kdbus_name_list *name_list;
878 struct kdbus_cmd_name *kname;
880 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
883 g_set_error_literal (error,
885 G_DBUS_ERROR_IO_ERROR,
886 _("The connection is closed"));
890 if (!g_dbus_is_name (name))
894 G_DBUS_ERROR_INVALID_ARGS,
895 "Given bus name \"%s\" is not valid", name);
899 if (!g_kdbus_NameHasOwner_internal (kdbus, name, error))
903 G_DBUS_ERROR_NAME_HAS_NO_OWNER,
904 "Could not get owner of name '%s': no such name", name);
908 cmd.flags = KDBUS_NAME_LIST_QUEUED;
909 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_LIST, &cmd);
915 _("Error listing names"));
919 name_list = (struct kdbus_name_list *) ((guint8 *) kdbus->priv->kdbus_buffer + cmd.offset);
921 unique_name = g_string_new (NULL);
922 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
923 KDBUS_ITEM_FOREACH(kname, name_list, names)
925 struct kdbus_item *item;
926 const char *item_name = "";
928 KDBUS_ITEM_FOREACH(item, kname, items)
929 if (item->type == KDBUS_ITEM_NAME)
930 item_name = item->str;
932 if (strcmp(item_name, name))
935 g_string_printf (unique_name, ":1.%llu", kname->owner_id);
936 g_variant_builder_add (builder, "s", item_name);
939 result = g_variant_new ("(as)", builder);
940 g_variant_builder_unref (builder);
941 g_string_free (unique_name,TRUE);
943 g_kdbus_free_data (kdbus, cmd.offset);
949 * _g_kdbus_NameHasOwner:
953 _g_kdbus_NameHasOwner (GDBusConnection *connection,
960 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
963 g_set_error_literal (error,
965 G_DBUS_ERROR_IO_ERROR,
966 _("The connection is closed"));
970 if (!g_dbus_is_name (name))
974 G_DBUS_ERROR_INVALID_ARGS,
975 "Given bus name \"%s\" is not valid", name);
979 if (!g_kdbus_NameHasOwner_internal (kdbus, name, error))
980 result = g_variant_new ("(b)", FALSE);
982 result = g_variant_new ("(b)", TRUE);
989 * g_kdbus_GetConnInfo_internal:
993 g_kdbus_GetConnInfo_internal (GDBusConnection *connection,
1001 struct kdbus_cmd_conn_info *cmd;
1002 struct kdbus_conn_info *conn_info;
1003 struct kdbus_item *item;
1008 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
1011 g_set_error_literal (error,
1013 G_DBUS_ERROR_IO_ERROR,
1014 _("The connection is closed"));
1018 if (!g_dbus_is_name (name))
1022 G_DBUS_ERROR_INVALID_ARGS,
1023 "Given bus name \"%s\" is not valid", name);
1027 if (!g_kdbus_NameHasOwner_internal (kdbus, name, error))
1031 G_DBUS_ERROR_NAME_HAS_NO_OWNER,
1032 "Could not get owner of name '%s': no such name", name);
1036 if (g_dbus_is_unique_name(name))
1038 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items);
1039 cmd = g_alloca0 (size);
1040 cmd->id = g_ascii_strtoull (name+3, NULL, 10);
1044 len = strlen(name) + 1;
1045 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items) + KDBUS_ITEM_SIZE(len);
1046 cmd = g_alloca0 (size);
1047 cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
1048 cmd->items[0].type = KDBUS_ITEM_NAME;
1049 memcpy (cmd->items[0].str, name, len);
1052 cmd->flags = KDBUS_ATTACH_NAMES;
1055 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_CONN_INFO, cmd);
1060 G_DBUS_ERROR_FAILED,
1061 _("Could not get connection info"));
1065 conn_info = (struct kdbus_conn_info *) ((guint8 *) kdbus->priv->kdbus_buffer + cmd->offset);
1068 if (conn_info->flags & KDBUS_HELLO_ACTIVATOR)
1072 if (flag == G_BUS_CREDS_UNIQUE_NAME)
1074 GString *unique_name;
1076 unique_name = g_string_new (NULL);
1077 g_string_printf (unique_name, ":1.%llu", (unsigned long long) conn_info->id);
1078 result = g_variant_new ("(s)", unique_name->str);
1079 g_string_free (unique_name,TRUE);
1083 KDBUS_ITEM_FOREACH(item, conn_info, items)
1087 case KDBUS_ITEM_CREDS:
1089 if (flag == G_BUS_CREDS_PID)
1091 guint pid = item->creds.pid;
1092 result = g_variant_new ("(u)", pid);
1096 if (flag == G_BUS_CREDS_UID)
1098 guint uid = item->creds.uid;
1099 result = g_variant_new ("(u)", uid);
1103 case KDBUS_ITEM_SECLABEL:
1104 case KDBUS_ITEM_PID_COMM:
1105 case KDBUS_ITEM_TID_COMM:
1106 case KDBUS_ITEM_EXE:
1107 case KDBUS_ITEM_CMDLINE:
1108 case KDBUS_ITEM_CGROUP:
1109 case KDBUS_ITEM_CAPS:
1110 case KDBUS_ITEM_NAME:
1111 case KDBUS_ITEM_AUDIT:
1117 g_kdbus_free_data (kdbus, cmd->offset);
1123 * _g_kdbus_GetNameOwner:
1127 _g_kdbus_GetNameOwner (GDBusConnection *connection,
1131 return g_kdbus_GetConnInfo_internal (connection,
1133 G_BUS_CREDS_UNIQUE_NAME,
1139 * _g_kdbus_GetConnectionUnixProcessID:
1143 _g_kdbus_GetConnectionUnixProcessID (GDBusConnection *connection,
1147 return g_kdbus_GetConnInfo_internal (connection,
1155 * _g_kdbus_GetConnectionUnixUser:
1159 _g_kdbus_GetConnectionUnixUser (GDBusConnection *connection,
1163 return g_kdbus_GetConnInfo_internal (connection,