| 1 |
/* |
| 2 |
* Copyright (C) 2018 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vojtech Trefny <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <gio/gio.h> |
| 22 |
|
| 23 |
#include "dbus.h" |
| 24 |
|
| 25 |
#define DBUS_TOP_IFACE "org.freedesktop.DBus" |
| 26 |
#define DBUS_TOP_OBJ "/org/freedesktop/DBus" |
| 27 |
#define DBUS_PROPS_IFACE "org.freedesktop.DBus.Properties" |
| 28 |
#define DBUS_INTRO_IFACE "org.freedesktop.DBus.Introspectable" |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* bd_utils_dbus_error_quark: (skip) |
| 33 |
*/ |
| 34 |
GQuark bd_utils_dbus_error_quark (void) |
| 35 |
{ |
| 36 |
return g_quark_from_static_string ("g-bd-utils-dbus-error-quark"); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* bd_utils_dbus_service_available: |
| 41 |
* @connection: (nullable): existing GDBusConnection or %NULL |
| 42 |
* @bus_type: bus type (system or session), ignored if @connection is specified |
| 43 |
* @bus_name: name of the service to check (e.g. "com.redhat.lvmdbus1") |
| 44 |
* @obj_prefix: object path prefix for the service (e.g. "/com/redhat/lvmdbus1") |
| 45 |
* @error: (out) (optional): place to store error (if any) |
| 46 |
* |
| 47 |
* Returns: whether the service was found in the system |
| 48 |
*/ |
| 49 |
gboolean bd_utils_dbus_service_available (GDBusConnection *connection, GBusType bus_type, const gchar *bus_name, const gchar *obj_prefix, GError **error) { |
| 50 |
GVariant *ret = NULL; |
| 51 |
GVariant *real_ret = NULL; |
| 52 |
GVariantIter iter; |
| 53 |
GVariant *service = NULL; |
| 54 |
gboolean found = FALSE; |
| 55 |
GDBusConnection *bus = NULL; |
| 56 |
|
| 57 |
if (connection) |
| 58 |
bus = g_object_ref (connection); |
| 59 |
else { |
| 60 |
bus = g_bus_get_sync (bus_type, NULL, error); |
| 61 |
if (!bus) { |
| 62 |
g_prefix_error (error, "Failed to get system bus: "); |
| 63 |
return FALSE; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
ret = g_dbus_connection_call_sync (bus, DBUS_TOP_IFACE, DBUS_TOP_OBJ, DBUS_TOP_IFACE, |
| 68 |
"ListNames", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, |
| 69 |
-1, NULL, error); |
| 70 |
if (!ret) { |
| 71 |
g_object_unref (bus); |
| 72 |
return FALSE; |
| 73 |
} |
| 74 |
|
| 75 |
real_ret = g_variant_get_child_value (ret, 0); |
| 76 |
g_variant_unref (ret); |
| 77 |
|
| 78 |
g_variant_iter_init (&iter, real_ret); |
| 79 |
while (!found && (service = g_variant_iter_next_value (&iter))) { |
| 80 |
found = (g_strcmp0 (g_variant_get_string (service, NULL), bus_name) == 0); |
| 81 |
g_variant_unref (service); |
| 82 |
} |
| 83 |
g_variant_unref (real_ret); |
| 84 |
|
| 85 |
ret = g_dbus_connection_call_sync (bus, DBUS_TOP_IFACE, DBUS_TOP_OBJ, DBUS_TOP_IFACE, |
| 86 |
"ListActivatableNames", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, |
| 87 |
-1, NULL, error); |
| 88 |
if (!ret) { |
| 89 |
g_object_unref (bus); |
| 90 |
return FALSE; |
| 91 |
} |
| 92 |
|
| 93 |
real_ret = g_variant_get_child_value (ret, 0); |
| 94 |
g_variant_unref (ret); |
| 95 |
|
| 96 |
g_variant_iter_init (&iter, real_ret); |
| 97 |
while (!found && (service = g_variant_iter_next_value (&iter))) { |
| 98 |
found = (g_strcmp0 (g_variant_get_string (service, NULL), bus_name) == 0); |
| 99 |
g_variant_unref (service); |
| 100 |
} |
| 101 |
g_variant_unref (real_ret); |
| 102 |
|
| 103 |
if (!found) { |
| 104 |
g_object_unref (bus); |
| 105 |
return FALSE; |
| 106 |
} |
| 107 |
|
| 108 |
/* try to introspect the root node - i.e. check we can access it and possibly |
| 109 |
autostart the service */ |
| 110 |
ret = g_dbus_connection_call_sync (bus, bus_name, obj_prefix, DBUS_INTRO_IFACE, |
| 111 |
"Introspect", NULL, NULL, G_DBUS_CALL_FLAGS_NONE, |
| 112 |
-1, NULL, error); |
| 113 |
if (!ret) { |
| 114 |
g_object_unref (bus); |
| 115 |
return FALSE; |
| 116 |
} |
| 117 |
|
| 118 |
g_variant_unref (ret); |
| 119 |
g_object_unref (bus); |
| 120 |
return TRUE; |
| 121 |
} |