bluetooth, vpn-manager: use GDBusProxies to monitor services

Create a GDBusProxy for the service to be monitored and use that to
tell whether it is running, rather than using NMDBusManager and the
global NameOwnerChanged signal.
This commit is contained in:
Dan Winship
2015-04-20 10:48:57 -04:00
parent 94078a139a
commit 1bcf42f37b
3 changed files with 81 additions and 106 deletions

View File

@@ -33,13 +33,9 @@
#include "nm-bluez-device.h" #include "nm-bluez-device.h"
#include "nm-bluez-common.h" #include "nm-bluez-common.h"
#include "nm-dbus-manager.h"
#include "nm-core-internal.h" #include "nm-core-internal.h"
typedef struct { typedef struct {
NMDBusManager *dbus_mgr;
gulong name_owner_changed_id;
NMConnectionProvider *provider; NMConnectionProvider *provider;
GDBusProxy *proxy; GDBusProxy *proxy;
@@ -224,6 +220,8 @@ get_managed_objects_cb (GDBusProxy *proxy,
g_variant_unref (variant); g_variant_unref (variant);
} }
static void name_owner_changed_cb (GObject *object, GParamSpec *pspec, gpointer user_data);
static void static void
on_proxy_acquired (GObject *object, on_proxy_acquired (GObject *object,
GAsyncResult *res, GAsyncResult *res,
@@ -241,6 +239,9 @@ on_proxy_acquired (GObject *object,
return; return;
} }
g_signal_connect (priv->proxy, "notify::g-name-owner",
G_CALLBACK (name_owner_changed_cb), self);
/* Get already managed devices. */ /* Get already managed devices. */
g_dbus_proxy_call (priv->proxy, "GetManagedObjects", g_dbus_proxy_call (priv->proxy, "GetManagedObjects",
NULL, NULL,
@@ -275,24 +276,19 @@ bluez_connect (NMBluez5Manager *self)
} }
static void static void
name_owner_changed_cb (NMDBusManager *dbus_mgr, name_owner_changed_cb (GObject *object,
const char *name, GParamSpec *pspec,
const char *old_owner,
const char *new_owner,
gpointer user_data) gpointer user_data)
{ {
NMBluez5Manager *self = NM_BLUEZ5_MANAGER (user_data); NMBluez5Manager *self = NM_BLUEZ5_MANAGER (user_data);
NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self); NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self);
gboolean old_owner_good = (old_owner && strlen (old_owner)); char *owner;
gboolean new_owner_good = (new_owner && strlen (new_owner));
/* Can't handle the signal if its not from the Bluez */ if (priv->devices) {
if (strcmp (BLUEZ_SERVICE, name)) owner = g_dbus_proxy_get_name_owner (priv->proxy);
return; if (!owner)
if (old_owner_good && !new_owner_good) {
if (priv->devices)
remove_all_devices (self); remove_all_devices (self);
g_free (owner);
} }
} }
@@ -302,8 +298,8 @@ bluez_cleanup (NMBluez5Manager *self, gboolean do_signal)
NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self); NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self);
if (priv->proxy) { if (priv->proxy) {
g_object_unref (priv->proxy); g_signal_handlers_disconnect_by_func (priv->proxy, G_CALLBACK (name_owner_changed_cb), self);
priv->proxy = NULL; g_clear_object (&priv->proxy);
} }
if (do_signal) if (do_signal)
@@ -312,19 +308,6 @@ bluez_cleanup (NMBluez5Manager *self, gboolean do_signal)
g_hash_table_remove_all (priv->devices); g_hash_table_remove_all (priv->devices);
} }
static void
dbus_connection_changed_cb (NMDBusManager *dbus_mgr,
DBusGConnection *connection,
gpointer user_data)
{
NMBluez5Manager *self = NM_BLUEZ5_MANAGER (user_data);
if (!connection)
bluez_cleanup (self, TRUE);
else
bluez_connect (self);
}
/****************************************************************/ /****************************************************************/
NMBluez5Manager * NMBluez5Manager *
@@ -342,19 +325,6 @@ nm_bluez5_manager_init (NMBluez5Manager *self)
{ {
NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self); NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self);
priv->dbus_mgr = nm_dbus_manager_get ();
g_assert (priv->dbus_mgr);
g_signal_connect (priv->dbus_mgr,
NM_DBUS_MANAGER_NAME_OWNER_CHANGED,
G_CALLBACK (name_owner_changed_cb),
self);
g_signal_connect (priv->dbus_mgr,
NM_DBUS_MANAGER_DBUS_CONNECTION_CHANGED,
G_CALLBACK (dbus_connection_changed_cb),
self);
bluez_connect (self); bluez_connect (self);
priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal,
@@ -365,16 +335,9 @@ static void
dispose (GObject *object) dispose (GObject *object)
{ {
NMBluez5Manager *self = NM_BLUEZ5_MANAGER (object); NMBluez5Manager *self = NM_BLUEZ5_MANAGER (object);
NMBluez5ManagerPrivate *priv = NM_BLUEZ5_MANAGER_GET_PRIVATE (self);
bluez_cleanup (self, FALSE); bluez_cleanup (self, FALSE);
if (priv->dbus_mgr) {
g_signal_handlers_disconnect_by_func (priv->dbus_mgr, name_owner_changed_cb, self);
g_signal_handlers_disconnect_by_func (priv->dbus_mgr, dbus_connection_changed_cb, self);
priv->dbus_mgr = NULL;
}
G_OBJECT_CLASS (nm_bluez5_manager_parent_class)->dispose (object); G_OBJECT_CLASS (nm_bluez5_manager_parent_class)->dispose (object);
} }

View File

@@ -28,7 +28,6 @@
#include "nm-glib.h" #include "nm-glib.h"
#include "nm-bluez-common.h" #include "nm-bluez-common.h"
#include "nm-bluez-device.h" #include "nm-bluez-device.h"
#include "nm-dbus-manager.h"
#include "nm-device-bt.h" #include "nm-device-bt.h"
#include "nm-device-private.h" #include "nm-device-private.h"
#include "nm-logging.h" #include "nm-logging.h"
@@ -46,7 +45,9 @@
#include "nm-bt-error.h" #include "nm-bt-error.h"
#include "nm-bt-enum-types.h" #include "nm-bt-enum-types.h"
#define MM_DBUS_SERVICE "org.freedesktop.ModemManager1" #define MM_DBUS_SERVICE "org.freedesktop.ModemManager1"
#define MM_DBUS_PATH "/org/freedesktop/ModemManager1"
#define MM_DBUS_INTERFACE "org.freedesktop.ModemManager1"
#include "nm-device-logging.h" #include "nm-device-logging.h"
_LOG_DECLARE_SELF(NMDeviceBt); _LOG_DECLARE_SELF(NMDeviceBt);
@@ -58,8 +59,7 @@ G_DEFINE_TYPE (NMDeviceBt, nm_device_bt, NM_TYPE_DEVICE)
static gboolean modem_stage1 (NMDeviceBt *self, NMModem *modem, NMDeviceStateReason *reason); static gboolean modem_stage1 (NMDeviceBt *self, NMModem *modem, NMDeviceStateReason *reason);
typedef struct { typedef struct {
NMDBusManager *dbus_mgr; GDBusProxy *mm_proxy;
guint mm_watch_id;
gboolean mm_running; gboolean mm_running;
NMBluezDevice *bt_device; NMBluezDevice *bt_device;
@@ -966,25 +966,17 @@ set_mm_running (NMDeviceBt *self, gboolean running)
} }
static void static void
mm_name_owner_changed (NMDBusManager *dbus_mgr, mm_name_owner_changed (GObject *object,
const char *name, GParamSpec *pspec,
const char *old_owner,
const char *new_owner,
NMDeviceBt *self) NMDeviceBt *self)
{ {
gboolean old_owner_good; char *owner;
gboolean new_owner_good;
/* Can't handle the signal if its not from the modem service */ owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (object));
if (strcmp (MM_DBUS_SERVICE, name) != 0) if (owner) {
return;
old_owner_good = (old_owner && strlen (old_owner));
new_owner_good = (new_owner && strlen (new_owner));
if (!old_owner_good && new_owner_good)
set_mm_running (self, TRUE); set_mm_running (self, TRUE);
else if (old_owner_good && !new_owner_good) g_free (owner);
} else
set_mm_running (self, FALSE); set_mm_running (self, FALSE);
} }
@@ -1020,18 +1012,27 @@ static void
nm_device_bt_init (NMDeviceBt *self) nm_device_bt_init (NMDeviceBt *self)
{ {
NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self); NMDeviceBtPrivate *priv = NM_DEVICE_BT_GET_PRIVATE (self);
gboolean mm_running; GError *error = NULL;
priv->dbus_mgr = nm_dbus_manager_get (); priv->mm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
priv->mm_watch_id = g_signal_connect (priv->dbus_mgr, G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
NM_DBUS_MANAGER_NAME_OWNER_CHANGED, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
G_CALLBACK (mm_name_owner_changed), NULL,
self); MM_DBUS_SERVICE,
MM_DBUS_PATH,
/* Initial check to see if ModemManager is running */ MM_DBUS_INTERFACE,
mm_running = nm_dbus_manager_name_has_owner (priv->dbus_mgr, MM_DBUS_SERVICE); NULL, &error);
set_mm_running (self, mm_running); if (priv->mm_proxy) {
g_signal_connect (priv->mm_proxy, "notify::g-name-owner",
G_CALLBACK (mm_name_owner_changed),
self);
mm_name_owner_changed (G_OBJECT (priv->mm_proxy), NULL, self);
} else {
_LOGW (LOGD_MB, "Could not create proxy for '%s': %s",
MM_DBUS_SERVICE, error->message);
g_clear_error (&error);
}
} }
static void static void
@@ -1112,11 +1113,10 @@ dispose (GObject *object)
g_signal_handlers_disconnect_matched (priv->bt_device, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, object); g_signal_handlers_disconnect_matched (priv->bt_device, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, object);
if (priv->dbus_mgr && priv->mm_watch_id) { if (priv->mm_proxy) {
g_signal_handler_disconnect (priv->dbus_mgr, priv->mm_watch_id); g_signal_handlers_disconnect_by_func (priv->mm_proxy, G_CALLBACK (mm_name_owner_changed), object);
priv->mm_watch_id = 0; g_clear_object (&priv->mm_proxy);
} }
priv->dbus_mgr = NULL;
modem_cleanup (NM_DEVICE_BT (object)); modem_cleanup (NM_DEVICE_BT (object));
g_clear_object (&priv->bt_device); g_clear_object (&priv->bt_device);

View File

@@ -29,7 +29,6 @@
#include "nm-glib.h" #include "nm-glib.h"
#include "nm-vpn-service.h" #include "nm-vpn-service.h"
#include "nm-dbus-manager.h"
#include "nm-logging.h" #include "nm-logging.h"
#include "nm-vpn-manager.h" #include "nm-vpn-manager.h"
@@ -45,6 +44,7 @@ typedef struct {
GSList *pending; GSList *pending;
guint start_timeout; guint start_timeout;
GDBusProxy *proxy;
gboolean service_running; gboolean service_running;
} NMVpnServicePrivate; } NMVpnServicePrivate;
@@ -54,6 +54,8 @@ typedef struct {
static gboolean start_pending_vpn (NMVpnService *self, GError **error); static gboolean start_pending_vpn (NMVpnService *self, GError **error);
static void _name_owner_changed (GObject *object, GParamSpec *pspec, gpointer user_data);
NMVpnService * NMVpnService *
nm_vpn_service_new (const char *namefile, GError **error) nm_vpn_service_new (const char *namefile, GError **error)
{ {
@@ -86,7 +88,21 @@ nm_vpn_service_new (const char *namefile, GError **error)
if (!priv->name) if (!priv->name)
goto error; goto error;
priv->service_running = nm_dbus_manager_name_has_owner (nm_dbus_manager_get (), priv->dbus_service); priv->proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
NULL,
priv->dbus_service,
NM_VPN_DBUS_PLUGIN_PATH,
NM_VPN_DBUS_PLUGIN_INTERFACE,
NULL, error);
if (!priv->proxy)
goto error;
g_signal_connect (priv->proxy, "notify::g-name-owner",
G_CALLBACK (_name_owner_changed), self);
_name_owner_changed (G_OBJECT (priv->proxy), NULL, self);
g_key_file_free (kf); g_key_file_free (kf);
return self; return self;
@@ -288,18 +304,16 @@ nm_vpn_service_activate (NMVpnService *service,
} }
static void static void
_name_owner_changed (NMDBusManager *mgr, _name_owner_changed (GObject *object,
const char *name, GParamSpec *pspec,
const char *old,
const char *new,
gpointer user_data) gpointer user_data)
{ {
NMVpnService *service = NM_VPN_SERVICE (user_data); NMVpnService *service = NM_VPN_SERVICE (user_data);
NMVpnServicePrivate *priv = NM_VPN_SERVICE_GET_PRIVATE (service); NMVpnServicePrivate *priv = NM_VPN_SERVICE_GET_PRIVATE (service);
gboolean old_owner_good, new_owner_good, success; gboolean success;
char *owner;
if (strcmp (name, priv->dbus_service)) owner = g_dbus_proxy_get_name_owner (G_DBUS_PROXY (object));
return;
/* Service changed, no need to wait for the timeout any longer */ /* Service changed, no need to wait for the timeout any longer */
if (priv->start_timeout) { if (priv->start_timeout) {
@@ -307,22 +321,21 @@ _name_owner_changed (NMDBusManager *mgr,
priv->start_timeout = 0; priv->start_timeout = 0;
} }
old_owner_good = (old && old[0]); if (owner && !priv->service_running) {
new_owner_good = (new && new[0]);
if (!old_owner_good && new_owner_good) {
/* service appeared */ /* service appeared */
priv->service_running = TRUE; priv->service_running = TRUE;
nm_log_info (LOGD_VPN, "VPN service '%s' appeared; activating connections", priv->name); nm_log_info (LOGD_VPN, "VPN service '%s' appeared; activating connections", priv->name);
/* Expect success because the VPN service has already appeared */ /* Expect success because the VPN service has already appeared */
success = start_active_vpn (service, NULL); success = start_active_vpn (service, NULL);
g_warn_if_fail (success); g_warn_if_fail (success);
} else if (old_owner_good && !new_owner_good) { } else if (!owner && priv->service_running) {
/* service went away */ /* service went away */
priv->service_running = FALSE; priv->service_running = FALSE;
nm_log_info (LOGD_VPN, "VPN service '%s' disappeared", priv->name); nm_log_info (LOGD_VPN, "VPN service '%s' disappeared", priv->name);
nm_vpn_service_stop_connections (service, FALSE, NM_VPN_CONNECTION_STATE_REASON_SERVICE_STOPPED); nm_vpn_service_stop_connections (service, FALSE, NM_VPN_CONNECTION_STATE_REASON_SERVICE_STOPPED);
} }
g_free (owner);
} }
/******************************************************************************/ /******************************************************************************/
@@ -330,10 +343,6 @@ _name_owner_changed (NMDBusManager *mgr,
static void static void
nm_vpn_service_init (NMVpnService *self) nm_vpn_service_init (NMVpnService *self)
{ {
g_signal_connect (nm_dbus_manager_get (),
NM_DBUS_MANAGER_NAME_OWNER_CHANGED,
G_CALLBACK (_name_owner_changed),
self);
} }
static void static void
@@ -351,9 +360,12 @@ dispose (GObject *object)
g_assert (priv->active == NULL); g_assert (priv->active == NULL);
g_assert (priv->pending == NULL); g_assert (priv->pending == NULL);
g_signal_handlers_disconnect_by_func (nm_dbus_manager_get (), if (priv->proxy) {
G_CALLBACK (_name_owner_changed), g_signal_handlers_disconnect_by_func (priv->proxy,
self); G_CALLBACK (_name_owner_changed),
self);
g_clear_object (&priv->proxy);
}
G_OBJECT_CLASS (nm_vpn_service_parent_class)->dispose (object); G_OBJECT_CLASS (nm_vpn_service_parent_class)->dispose (object);
} }