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);
706 * _g_kdbus_ReleaseName:
710 _g_kdbus_ReleaseName (GDBusConnection *connection,
716 struct kdbus_cmd_name *kdbus_name;
720 status = G_BUS_RELEASE_NAME_REPLY_RELEASED;
722 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
725 g_set_error_literal (error,
727 G_DBUS_ERROR_IO_ERROR,
728 _("The connection is closed"));
732 if (!g_dbus_is_name (name))
736 G_DBUS_ERROR_INVALID_ARGS,
737 "Given bus name \"%s\" is not valid", name);
745 G_DBUS_ERROR_INVALID_ARGS,
746 "Cannot release a service starting with ':' such as \"%s\"", name);
750 len = strlen(name) + 1;
751 size = G_STRUCT_OFFSET (struct kdbus_cmd_name, items) + KDBUS_ITEM_SIZE(len);
752 kdbus_name = g_alloca0 (size);
753 kdbus_name->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
754 kdbus_name->items[0].type = KDBUS_ITEM_NAME;
755 memcpy (kdbus_name->items[0].str, name, len);
757 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_RELEASE, kdbus_name);
761 status = G_BUS_RELEASE_NAME_REPLY_NON_EXISTENT;
762 else if (errno == EADDRINUSE)
763 status = G_BUS_RELEASE_NAME_REPLY_NOT_OWNER;
768 result = g_variant_new ("(u)", status);
779 _g_kdbus_GetBusId (GDBusConnection *connection,
787 result_str = g_string_new (NULL);
788 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
791 g_set_error_literal (error,
793 G_DBUS_ERROR_IO_ERROR,
794 _("The connection is closed"));
795 g_string_free (result_str, TRUE);
799 for (cnt=0; cnt<16; cnt++)
800 g_string_append_printf (result_str, "%02x", kdbus->priv->bus_id[cnt]);
802 result = g_variant_new ("(s)", result_str->str);
803 g_string_free (result_str, TRUE);
810 * _g_kdbus_GetListNames:
814 _g_kdbus_GetListNames (GDBusConnection *connection,
815 guint list_name_type,
820 GVariantBuilder *builder;
822 struct kdbus_cmd_name_list cmd = {};
823 struct kdbus_name_list *name_list;
824 struct kdbus_cmd_name *name;
830 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
833 g_set_error_literal (error,
835 G_DBUS_ERROR_IO_ERROR,
836 _("The connection is closed"));
841 cmd.flags = KDBUS_NAME_LIST_ACTIVATORS; /* ListActivatableNames */
843 cmd.flags = KDBUS_NAME_LIST_UNIQUE | KDBUS_NAME_LIST_NAMES; /* ListNames */
845 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_LIST, &cmd);
851 _("Error listing names"));
855 name_list = (struct kdbus_name_list *) ((guint8 *) kdbus->priv->kdbus_buffer + cmd.offset);
856 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
858 KDBUS_ITEM_FOREACH(name, name_list, names)
860 struct kdbus_item *item;
861 const gchar *item_name = "";
863 if ((cmd.flags & KDBUS_NAME_LIST_UNIQUE) && name->owner_id != prev_id)
865 GString *unique_name;
867 unique_name = g_string_new (NULL);
868 g_string_printf (unique_name, ":1.%llu", name->owner_id);
869 g_variant_builder_add (builder, "s", unique_name->str);
870 g_string_free (unique_name,TRUE);
871 prev_id = name->owner_id;
874 KDBUS_ITEM_FOREACH(item, name, items)
875 if (item->type == KDBUS_ITEM_NAME)
876 item_name = item->str;
878 if (g_dbus_is_name (item_name))
879 g_variant_builder_add (builder, "s", item_name);
882 result = g_variant_new ("(as)", builder);
883 g_variant_builder_unref (builder);
885 g_kdbus_free_data (kdbus, cmd.offset);
891 * _g_kdbus_NameHasOwner_internal:
895 g_kdbus_NameHasOwner_internal (GKdbus *kdbus,
899 struct kdbus_cmd_conn_info *cmd;
903 if (g_dbus_is_unique_name(name))
905 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items);
906 cmd = g_alloca0 (size);
907 cmd->id = g_ascii_strtoull (name+3, NULL, 10);
911 len = strlen(name) + 1;
912 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items) + KDBUS_ITEM_SIZE(len);
913 cmd = g_alloca0 (size);
914 cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
915 cmd->items[0].type = KDBUS_ITEM_NAME;
916 memcpy (cmd->items[0].str, name, len);
920 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_CONN_INFO, cmd);
921 g_kdbus_free_data (kdbus, cmd->offset);
931 * _g_kdbus_GetListQueuedOwners:
935 _g_kdbus_GetListQueuedOwners (GDBusConnection *connection,
941 GVariantBuilder *builder;
942 GString *unique_name;
945 struct kdbus_cmd_name_list cmd = {};
946 struct kdbus_name_list *name_list;
947 struct kdbus_cmd_name *kname;
949 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
952 g_set_error_literal (error,
954 G_DBUS_ERROR_IO_ERROR,
955 _("The connection is closed"));
959 if (!g_dbus_is_name (name))
963 G_DBUS_ERROR_INVALID_ARGS,
964 "Given bus name \"%s\" is not valid", name);
968 if (!g_kdbus_NameHasOwner_internal (kdbus, name, error))
972 G_DBUS_ERROR_NAME_HAS_NO_OWNER,
973 "Could not get owner of name '%s': no such name", name);
977 cmd.flags = KDBUS_NAME_LIST_QUEUED;
978 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_NAME_LIST, &cmd);
984 _("Error listing names"));
988 name_list = (struct kdbus_name_list *) ((guint8 *) kdbus->priv->kdbus_buffer + cmd.offset);
990 unique_name = g_string_new (NULL);
991 builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
992 KDBUS_ITEM_FOREACH(kname, name_list, names)
994 struct kdbus_item *item;
995 const char *item_name = "";
997 KDBUS_ITEM_FOREACH(item, kname, items)
998 if (item->type == KDBUS_ITEM_NAME)
999 item_name = item->str;
1001 if (strcmp(item_name, name))
1004 g_string_printf (unique_name, ":1.%llu", kname->owner_id);
1005 g_variant_builder_add (builder, "s", item_name);
1008 result = g_variant_new ("(as)", builder);
1009 g_variant_builder_unref (builder);
1010 g_string_free (unique_name,TRUE);
1012 g_kdbus_free_data (kdbus, cmd.offset);
1018 * _g_kdbus_NameHasOwner:
1022 _g_kdbus_NameHasOwner (GDBusConnection *connection,
1029 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
1032 g_set_error_literal (error,
1034 G_DBUS_ERROR_IO_ERROR,
1035 _("The connection is closed"));
1039 if (!g_dbus_is_name (name))
1043 G_DBUS_ERROR_INVALID_ARGS,
1044 "Given bus name \"%s\" is not valid", name);
1048 if (!g_kdbus_NameHasOwner_internal (kdbus, name, error))
1049 result = g_variant_new ("(b)", FALSE);
1051 result = g_variant_new ("(b)", TRUE);
1058 * g_kdbus_GetConnInfo_internal:
1062 g_kdbus_GetConnInfo_internal (GDBusConnection *connection,
1070 struct kdbus_cmd_conn_info *cmd;
1071 struct kdbus_conn_info *conn_info;
1072 struct kdbus_item *item;
1077 kdbus = _g_kdbus_connection_get_kdbus (G_KDBUS_CONNECTION (g_dbus_connection_get_stream (connection)));
1080 g_set_error_literal (error,
1082 G_DBUS_ERROR_IO_ERROR,
1083 _("The connection is closed"));
1087 if (!g_dbus_is_name (name))
1091 G_DBUS_ERROR_INVALID_ARGS,
1092 "Given bus name \"%s\" is not valid", name);
1096 if (!g_kdbus_NameHasOwner_internal (kdbus, name, error))
1100 G_DBUS_ERROR_NAME_HAS_NO_OWNER,
1101 "Could not get owner of name '%s': no such name", name);
1105 if (g_dbus_is_unique_name(name))
1107 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items);
1108 cmd = g_alloca0 (size);
1109 cmd->id = g_ascii_strtoull (name+3, NULL, 10);
1113 len = strlen(name) + 1;
1114 size = G_STRUCT_OFFSET (struct kdbus_cmd_conn_info, items) + KDBUS_ITEM_SIZE(len);
1115 cmd = g_alloca0 (size);
1116 cmd->items[0].size = KDBUS_ITEM_HEADER_SIZE + len;
1117 cmd->items[0].type = KDBUS_ITEM_NAME;
1118 memcpy (cmd->items[0].str, name, len);
1121 cmd->flags = KDBUS_ATTACH_NAMES;
1124 ret = ioctl(kdbus->priv->fd, KDBUS_CMD_CONN_INFO, cmd);
1129 G_DBUS_ERROR_FAILED,
1130 _("Could not get connection info"));
1134 conn_info = (struct kdbus_conn_info *) ((guint8 *) kdbus->priv->kdbus_buffer + cmd->offset);
1137 if (conn_info->flags & KDBUS_HELLO_ACTIVATOR)
1141 if (flag == G_BUS_CREDS_UNIQUE_NAME)
1143 GString *unique_name;
1145 unique_name = g_string_new (NULL);
1146 g_string_printf (unique_name, ":1.%llu", (unsigned long long) conn_info->id);
1147 result = g_variant_new ("(s)", unique_name->str);
1148 g_string_free (unique_name,TRUE);
1152 KDBUS_ITEM_FOREACH(item, conn_info, items)
1156 case KDBUS_ITEM_CREDS:
1158 if (flag == G_BUS_CREDS_PID)
1160 guint pid = item->creds.pid;
1161 result = g_variant_new ("(u)", pid);
1165 if (flag == G_BUS_CREDS_UID)
1167 guint uid = item->creds.uid;
1168 result = g_variant_new ("(u)", uid);
1172 case KDBUS_ITEM_SECLABEL:
1173 case KDBUS_ITEM_PID_COMM:
1174 case KDBUS_ITEM_TID_COMM:
1175 case KDBUS_ITEM_EXE:
1176 case KDBUS_ITEM_CMDLINE:
1177 case KDBUS_ITEM_CGROUP:
1178 case KDBUS_ITEM_CAPS:
1179 case KDBUS_ITEM_NAME:
1180 case KDBUS_ITEM_AUDIT:
1186 g_kdbus_free_data (kdbus, cmd->offset);
1192 * _g_kdbus_GetNameOwner:
1196 _g_kdbus_GetNameOwner (GDBusConnection *connection,
1200 return g_kdbus_GetConnInfo_internal (connection,
1202 G_BUS_CREDS_UNIQUE_NAME,
1208 * _g_kdbus_GetConnectionUnixProcessID:
1212 _g_kdbus_GetConnectionUnixProcessID (GDBusConnection *connection,
1216 return g_kdbus_GetConnInfo_internal (connection,
1224 * _g_kdbus_GetConnectionUnixUser:
1228 _g_kdbus_GetConnectionUnixUser (GDBusConnection *connection,
1232 return g_kdbus_GetConnInfo_internal (connection,