cli: improve/add NM running checks

Check whether NetworkManager is running and return new error
NMC_RESULT_ERROR_NM_NOT_RUNNING when appropriate.
This commit is contained in:
Jiří Klimeš
2011-02-10 14:21:18 +01:00
parent 4242f18fb2
commit f29b709590
6 changed files with 167 additions and 92 deletions

View File

@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2010 Red Hat, Inc.
* (C) Copyright 2010 - 2011 Red Hat, Inc.
*/
#include <stdio.h>
@@ -22,6 +22,7 @@
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib-bindings.h>
#include "utils.h"
@@ -296,3 +297,57 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
g_string_free (str, TRUE);
}
/*
* Find out whether NetworkManager is running (via D-Bus NameHasOwner), assuring
* NetworkManager won't be autostart (by D-Bus) if not running.
* We can't use NMClient (nm_client_get_manager_running()) because NMClient
* constructor calls GetPermissions of NM_DBUS_SERVICE, which would autostart
* NetworkManger if it is configured as D-Bus launchable service.
*/
gboolean
nmc_is_nm_running (NmCli *nmc, GError **error)
{
DBusGConnection *connection = NULL;
DBusGProxy *proxy = NULL;
GError *err = NULL;
gboolean has_owner = FALSE;
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &err);
if (!connection) {
g_string_printf (nmc->return_text, _("Error: Couldn't connect to system bus: %s"), err->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_propagate_error (error, err);
goto done;
}
proxy = dbus_g_proxy_new_for_name (connection,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus");
if (!proxy) {
g_string_printf (nmc->return_text, _("Error: Couldn't create D-Bus object proxy for org.freedesktop.DBus"));
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
if (error)
g_set_error (error, 0, 0, nmc->return_text->str);
goto done;
}
if (!org_freedesktop_DBus_name_has_owner (proxy, NM_DBUS_SERVICE, &has_owner, &err)) {
g_string_printf (nmc->return_text, _("Error: NameHasOwner request failed: %s"),
(err && err->message) ? err->message : _("(unknown)"));
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_propagate_error (error, err);
goto done;
}
done:
if (connection)
dbus_g_connection_unref (connection);
if (proxy)
g_object_unref (proxy);
return has_owner;
}