summary refs log tree commit diff
path: root/cli/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'cli/src/utils.c')
-rw-r--r--cli/src/utils.c57
1 files changed, 56 insertions, 1 deletions
diff --git a/cli/src/utils.c b/cli/src/utils.c
index 0096a0ae..bcc51bbd 100644
--- a/cli/src/utils.c
+++ b/cli/src/utils.c
@@ -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, "%s", 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;
+}
+