2007-10-09 07:51:03 +00:00
|
|
|
/* -*- Mode: C; tab-width: 5; indent-tabs-mode: t; c-basic-offset: 5 -*- */
|
2006-10-13 19:41:47 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2006 Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Written by Dan Williams <dcbw@redhat.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
2008-06-26 18:31:52 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-10-13 19:41:47 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "NetworkManager.h"
|
|
|
|
|
#include "nm-dbus-manager.h"
|
|
|
|
|
#include "nm-marshal.h"
|
|
|
|
|
|
|
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
#include <dbus/dbus-glib.h>
|
|
|
|
|
#include <dbus/dbus-glib-lowlevel.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "nm-utils.h"
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
DBUS_CONNECTION_CHANGED = 0,
|
|
|
|
|
NAME_OWNER_CHANGED,
|
|
|
|
|
NUMBER_OF_SIGNALS
|
|
|
|
|
};
|
|
|
|
|
|
2007-10-10 14:36:31 +00:00
|
|
|
static guint nm_dbus_manager_signals[NUMBER_OF_SIGNALS];
|
2006-10-13 19:41:47 +00:00
|
|
|
|
|
|
|
|
G_DEFINE_TYPE(NMDBusManager, nm_dbus_manager, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
|
|
#define NM_DBUS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
|
2007-10-10 14:36:31 +00:00
|
|
|
NM_TYPE_DBUS_MANAGER, \
|
|
|
|
|
NMDBusManagerPrivate))
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
typedef struct {
|
2006-11-25 07:09:11 +00:00
|
|
|
DBusConnection * connection;
|
2007-02-02 08:50:56 +00:00
|
|
|
DBusGConnection *g_connection;
|
|
|
|
|
DBusGProxy * proxy;
|
2006-11-25 07:09:11 +00:00
|
|
|
gboolean started;
|
2007-02-02 08:50:56 +00:00
|
|
|
} NMDBusManagerPrivate;
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2006-11-25 07:09:11 +00:00
|
|
|
static gboolean nm_dbus_manager_init_bus (NMDBusManager *self);
|
|
|
|
|
static void nm_dbus_manager_cleanup (NMDBusManager *self);
|
|
|
|
|
static void start_reconnection_timeout (NMDBusManager *self);
|
|
|
|
|
|
2006-10-13 19:41:47 +00:00
|
|
|
NMDBusManager *
|
2007-01-04 12:06:26 +00:00
|
|
|
nm_dbus_manager_get (void)
|
2006-10-13 19:41:47 +00:00
|
|
|
{
|
|
|
|
|
static NMDBusManager *singleton = NULL;
|
|
|
|
|
|
|
|
|
|
if (!singleton) {
|
2007-01-04 12:06:26 +00:00
|
|
|
singleton = NM_DBUS_MANAGER (g_object_new (NM_TYPE_DBUS_MANAGER, NULL));
|
2006-10-13 19:41:47 +00:00
|
|
|
if (!nm_dbus_manager_init_bus (singleton))
|
|
|
|
|
start_reconnection_timeout (singleton);
|
|
|
|
|
} else {
|
|
|
|
|
g_object_ref (singleton);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_assert (singleton);
|
|
|
|
|
return singleton;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_dbus_manager_init (NMDBusManager *self)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_dbus_manager_finalize (GObject *object)
|
|
|
|
|
{
|
2007-10-10 14:36:31 +00:00
|
|
|
nm_dbus_manager_cleanup (NM_DBUS_MANAGER (object));
|
2006-12-05 04:14:43 +00:00
|
|
|
|
2006-10-13 19:41:47 +00:00
|
|
|
G_OBJECT_CLASS (nm_dbus_manager_parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_dbus_manager_class_init (NMDBusManagerClass *klass)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
|
|
object_class->finalize = nm_dbus_manager_finalize;
|
|
|
|
|
|
|
|
|
|
nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED] =
|
|
|
|
|
g_signal_new ("dbus-connection-changed",
|
|
|
|
|
G_OBJECT_CLASS_TYPE (object_class),
|
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
|
G_STRUCT_OFFSET (NMDBusManagerClass, dbus_connection_changed),
|
|
|
|
|
NULL, NULL, nm_marshal_VOID__POINTER,
|
|
|
|
|
G_TYPE_NONE, 1, G_TYPE_POINTER);
|
|
|
|
|
|
|
|
|
|
nm_dbus_manager_signals[NAME_OWNER_CHANGED] =
|
|
|
|
|
g_signal_new ("name-owner-changed",
|
|
|
|
|
G_OBJECT_CLASS_TYPE (object_class),
|
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
|
G_STRUCT_OFFSET (NMDBusManagerClass, name_owner_changed),
|
2007-05-07 14:33:51 +00:00
|
|
|
NULL, NULL, nm_marshal_VOID__STRING_STRING_STRING,
|
|
|
|
|
G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
|
2006-10-13 19:41:47 +00:00
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (NMDBusManagerPrivate));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Only cleanup a specific dbus connection, not all our private data */
|
|
|
|
|
static void
|
|
|
|
|
nm_dbus_manager_cleanup (NMDBusManager *self)
|
|
|
|
|
{
|
2007-02-02 08:50:56 +00:00
|
|
|
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
|
2007-10-09 07:51:03 +00:00
|
|
|
if (priv->proxy) {
|
|
|
|
|
g_object_unref (priv->proxy);
|
|
|
|
|
priv->proxy = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
if (priv->g_connection) {
|
|
|
|
|
dbus_g_connection_unref (priv->g_connection);
|
|
|
|
|
priv->g_connection = NULL;
|
|
|
|
|
priv->connection = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
priv->started = FALSE;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
nm_dbus_manager_reconnect (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMDBusManager *self = NM_DBUS_MANAGER (user_data);
|
|
|
|
|
|
|
|
|
|
g_assert (self != NULL);
|
|
|
|
|
|
|
|
|
|
if (nm_dbus_manager_init_bus (self)) {
|
|
|
|
|
if (nm_dbus_manager_start_service (self)) {
|
|
|
|
|
nm_info ("reconnected to the system bus.");
|
2007-02-02 08:50:56 +00:00
|
|
|
g_signal_emit (self,
|
2007-10-10 14:36:31 +00:00
|
|
|
nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED],
|
|
|
|
|
0,
|
|
|
|
|
NM_DBUS_MANAGER_GET_PRIVATE (self)->connection);
|
2007-02-02 08:50:56 +00:00
|
|
|
return TRUE;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
nm_dbus_manager_cleanup (self);
|
|
|
|
|
return FALSE;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
start_reconnection_timeout (NMDBusManager *self)
|
|
|
|
|
{
|
|
|
|
|
/* Schedule timeout for reconnection attempts */
|
2007-01-04 12:06:26 +00:00
|
|
|
g_timeout_add (3000, nm_dbus_manager_reconnect, self);
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
nm_dbus_manager_get_name_owner (NMDBusManager *self,
|
|
|
|
|
const char *name)
|
|
|
|
|
{
|
2007-02-02 08:50:56 +00:00
|
|
|
char *owner = NULL;
|
|
|
|
|
GError *err = NULL;
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), NULL);
|
2006-10-13 19:41:47 +00:00
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
if (!dbus_g_proxy_call (NM_DBUS_MANAGER_GET_PRIVATE (self)->proxy,
|
2007-10-10 14:36:31 +00:00
|
|
|
"GetNameOwner", &err,
|
|
|
|
|
G_TYPE_STRING, name,
|
|
|
|
|
G_TYPE_INVALID,
|
|
|
|
|
G_TYPE_STRING, &owner,
|
|
|
|
|
G_TYPE_INVALID)) {
|
2007-02-02 08:50:56 +00:00
|
|
|
nm_warning ("Error on GetNameOwner DBUS call: %s", err->message);
|
|
|
|
|
g_error_free (err);
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return owner;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_dbus_manager_name_has_owner (NMDBusManager *self,
|
|
|
|
|
const char *name)
|
|
|
|
|
{
|
2007-02-02 08:50:56 +00:00
|
|
|
gboolean has_owner = FALSE;
|
|
|
|
|
GError *err = NULL;
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), FALSE);
|
2006-10-13 19:41:47 +00:00
|
|
|
g_return_val_if_fail (name != NULL, FALSE);
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
if (!dbus_g_proxy_call (NM_DBUS_MANAGER_GET_PRIVATE (self)->proxy,
|
2007-10-10 14:36:31 +00:00
|
|
|
"NameHasOwner", &err,
|
|
|
|
|
G_TYPE_STRING, name,
|
|
|
|
|
G_TYPE_INVALID,
|
|
|
|
|
G_TYPE_BOOLEAN, &has_owner,
|
|
|
|
|
G_TYPE_INVALID)) {
|
2007-02-02 08:50:56 +00:00
|
|
|
nm_warning ("Error on NameHasOwner DBUS call: %s", err->message);
|
|
|
|
|
g_error_free (err);
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
return has_owner;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
static void
|
|
|
|
|
proxy_name_owner_changed (DBusGProxy *proxy,
|
2007-10-10 14:36:31 +00:00
|
|
|
const char *name,
|
|
|
|
|
const char *old_owner,
|
|
|
|
|
const char *new_owner,
|
|
|
|
|
gpointer user_data)
|
2007-02-02 08:50:56 +00:00
|
|
|
{
|
2007-10-10 14:36:31 +00:00
|
|
|
g_signal_emit (NM_DBUS_MANAGER (user_data),
|
|
|
|
|
nm_dbus_manager_signals[NAME_OWNER_CHANGED],
|
|
|
|
|
0,
|
|
|
|
|
name, old_owner, new_owner);
|
2007-02-02 08:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
destroy_cb (DBusGProxy *proxy, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
NMDBusManager *self = NM_DBUS_MANAGER (user_data);
|
|
|
|
|
|
|
|
|
|
/* Clean up existing connection */
|
|
|
|
|
nm_info ("disconnected by the system bus.");
|
2007-10-09 07:51:03 +00:00
|
|
|
NM_DBUS_MANAGER_GET_PRIVATE (self)->proxy = NULL;
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
nm_dbus_manager_cleanup (self);
|
|
|
|
|
|
|
|
|
|
g_signal_emit (G_OBJECT (self),
|
2007-10-10 14:36:31 +00:00
|
|
|
nm_dbus_manager_signals[DBUS_CONNECTION_CHANGED],
|
|
|
|
|
0, NULL);
|
2007-02-02 08:50:56 +00:00
|
|
|
|
|
|
|
|
start_reconnection_timeout (self);
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-13 19:41:47 +00:00
|
|
|
static gboolean
|
|
|
|
|
nm_dbus_manager_init_bus (NMDBusManager *self)
|
|
|
|
|
{
|
2007-02-02 08:50:56 +00:00
|
|
|
NMDBusManagerPrivate *priv = NM_DBUS_MANAGER_GET_PRIVATE (self);
|
|
|
|
|
GError *err = NULL;
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
if (priv->connection) {
|
2006-10-13 19:41:47 +00:00
|
|
|
nm_warning ("DBus Manager already has a valid connection.");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_connection_set_change_sigpipe (TRUE);
|
2007-02-02 08:50:56 +00:00
|
|
|
|
|
|
|
|
priv->g_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
|
|
|
|
|
if (!priv->g_connection) {
|
2006-10-13 19:41:47 +00:00
|
|
|
nm_warning ("Could not get the system bus. Make sure "
|
|
|
|
|
"the message bus daemon is running! Message: %s",
|
2007-02-02 08:50:56 +00:00
|
|
|
err->message);
|
|
|
|
|
g_error_free (err);
|
2007-10-10 14:36:31 +00:00
|
|
|
return FALSE;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
priv->connection = dbus_g_connection_get_connection (priv->g_connection);
|
|
|
|
|
dbus_connection_set_exit_on_disconnect (priv->connection, FALSE);
|
|
|
|
|
|
|
|
|
|
priv->proxy = dbus_g_proxy_new_for_name (priv->g_connection,
|
2007-10-10 14:36:31 +00:00
|
|
|
"org.freedesktop.DBus",
|
|
|
|
|
"/org/freedesktop/DBus",
|
|
|
|
|
"org.freedesktop.DBus");
|
2007-02-02 08:50:56 +00:00
|
|
|
|
|
|
|
|
g_signal_connect (priv->proxy, "destroy",
|
2007-10-10 14:36:31 +00:00
|
|
|
G_CALLBACK (destroy_cb),
|
|
|
|
|
self);
|
2007-02-02 08:50:56 +00:00
|
|
|
|
|
|
|
|
dbus_g_proxy_add_signal (priv->proxy, "NameOwnerChanged",
|
2007-10-10 14:36:31 +00:00
|
|
|
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
|
|
|
|
|
G_TYPE_INVALID);
|
2007-02-02 08:50:56 +00:00
|
|
|
dbus_g_proxy_connect_signal (priv->proxy,
|
2007-10-10 14:36:31 +00:00
|
|
|
"NameOwnerChanged",
|
|
|
|
|
G_CALLBACK (proxy_name_owner_changed),
|
|
|
|
|
self, NULL);
|
|
|
|
|
return TRUE;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Register our service on the bus; shouldn't be called until
|
|
|
|
|
* all necessary message handlers have been registered, because
|
|
|
|
|
* when we register on the bus, clients may start to call.
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
nm_dbus_manager_start_service (NMDBusManager *self)
|
|
|
|
|
{
|
2007-02-02 08:50:56 +00:00
|
|
|
NMDBusManagerPrivate *priv;
|
|
|
|
|
int flags;
|
|
|
|
|
int request_name_result;
|
|
|
|
|
GError *err = NULL;
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), FALSE);
|
|
|
|
|
|
|
|
|
|
priv = NM_DBUS_MANAGER_GET_PRIVATE (self);
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
if (priv->started) {
|
2006-10-13 19:41:47 +00:00
|
|
|
nm_warning ("Service has already started.");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 11:00:22 +00:00
|
|
|
#if (DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 60)
|
2006-10-13 19:41:47 +00:00
|
|
|
flags = DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT;
|
2006-11-29 11:00:22 +00:00
|
|
|
#else
|
|
|
|
|
flags = DBUS_NAME_FLAG_DO_NOT_QUEUE;
|
2006-10-13 19:41:47 +00:00
|
|
|
#endif
|
2007-02-02 08:50:56 +00:00
|
|
|
|
|
|
|
|
if (!dbus_g_proxy_call (priv->proxy, "RequestName", &err,
|
2007-10-10 14:36:31 +00:00
|
|
|
G_TYPE_STRING, NM_DBUS_SERVICE,
|
|
|
|
|
G_TYPE_UINT, flags,
|
|
|
|
|
G_TYPE_INVALID,
|
|
|
|
|
G_TYPE_UINT, &request_name_result,
|
|
|
|
|
G_TYPE_INVALID)) {
|
2006-10-13 19:41:47 +00:00
|
|
|
nm_warning ("Could not acquire the NetworkManager service.\n"
|
2007-02-02 08:50:56 +00:00
|
|
|
" Message: '%s'", err->message);
|
|
|
|
|
g_error_free (err);
|
2006-10-13 19:41:47 +00:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
if (request_name_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
|
2007-10-09 08:57:35 +00:00
|
|
|
nm_warning ("Could not acquire the NetworkManager service as it is already taken.");
|
2006-10-13 19:41:47 +00:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
priv->started = TRUE;
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-10-10 14:36:31 +00:00
|
|
|
out:
|
2007-02-02 08:50:56 +00:00
|
|
|
if (!priv->started)
|
2006-10-13 19:41:47 +00:00
|
|
|
nm_dbus_manager_cleanup (self);
|
2007-02-02 08:50:56 +00:00
|
|
|
|
|
|
|
|
return priv->started;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DBusConnection *
|
|
|
|
|
nm_dbus_manager_get_dbus_connection (NMDBusManager *self)
|
|
|
|
|
{
|
2007-02-02 08:50:56 +00:00
|
|
|
g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), NULL);
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
return NM_DBUS_MANAGER_GET_PRIVATE (self)->connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DBusGConnection *
|
|
|
|
|
nm_dbus_manager_get_connection (NMDBusManager *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (NM_IS_DBUS_MANAGER (self), NULL);
|
2006-10-13 19:41:47 +00:00
|
|
|
|
2007-02-02 08:50:56 +00:00
|
|
|
return NM_DBUS_MANAGER_GET_PRIVATE (self)->g_connection;
|
2006-10-13 19:41:47 +00:00
|
|
|
}
|