Commit f29b7095 authored by Jiří Klimeš's avatar Jiří Klimeš

cli: improve/add NM running checks

Check whether NetworkManager is running and return new error
NMC_RESULT_ERROR_NM_NOT_RUNNING when appropriate.
parent 4242f18f
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* (C) Copyright 2011 Red Hat, Inc. * (C) Copyright 2010 - 2011 Red Hat, Inc.
*/ */
#include <glib.h> #include <glib.h>
...@@ -647,12 +647,6 @@ do_connections_status (NmCli *nmc, int argc, char **argv) ...@@ -647,12 +647,6 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
nmc->should_wait = FALSE; nmc->should_wait = FALSE;
/* create NMClient */
if (!nmc->get_client (nmc))
return nmc->return_value;
active_cons = nm_client_get_active_connections (nmc->client);
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common; fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0) else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
...@@ -673,10 +667,25 @@ do_connections_status (NmCli *nmc, int argc, char **argv) ...@@ -673,10 +667,25 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
if (!nmc_is_nm_running (nmc, &error)) {
if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error;
}
/* Print headers */
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES; nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("Active connections"); nmc->print_fields.header_name = _("Active connections");
print_fields (nmc->print_fields, nmc->allowed_fields); print_fields (nmc->print_fields, nmc->allowed_fields);
nmc->get_client (nmc);
active_cons = nm_client_get_active_connections (nmc->client);
if (active_cons && active_cons->len) { if (active_cons && active_cons->len) {
info = g_malloc0 (sizeof (StatusInfo)); info = g_malloc0 (sizeof (StatusInfo));
info->nmc = nmc; info->nmc = nmc;
...@@ -688,7 +697,6 @@ do_connections_status (NmCli *nmc, int argc, char **argv) ...@@ -688,7 +697,6 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
} }
error: error:
return nmc->return_value; return nmc->return_value;
} }
...@@ -1555,9 +1563,20 @@ do_connection_up (NmCli *nmc, int argc, char **argv) ...@@ -1555,9 +1563,20 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
/* create NMClient */ if (!nmc_is_nm_running (nmc, &error)) {
if (!nmc->get_client (nmc)) if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error; goto error;
}
/* create NMClient */
nmc->get_client (nmc);
is_system = (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_SYSTEM) ? TRUE : FALSE; is_system = (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_SYSTEM) ? TRUE : FALSE;
con_path = nm_connection_get_path (connection); con_path = nm_connection_get_path (connection);
...@@ -1574,6 +1593,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv) ...@@ -1574,6 +1593,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
else else
g_string_printf (nmc->return_text, _("Error: No suitable device found.")); g_string_printf (nmc->return_text, _("Error: No suitable device found."));
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION; nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
g_clear_error (&error);
goto error; goto error;
} }
...@@ -1600,6 +1620,7 @@ do_connection_down (NmCli *nmc, int argc, char **argv) ...@@ -1600,6 +1620,7 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
{ {
NMConnection *connection = NULL; NMConnection *connection = NULL;
NMActiveConnection *active = NULL; NMActiveConnection *active = NULL;
GError *error = NULL;
const GPtrArray *active_cons; const GPtrArray *active_cons;
const char *con_path; const char *con_path;
const char *active_path; const char *active_path;
...@@ -1645,9 +1666,20 @@ do_connection_down (NmCli *nmc, int argc, char **argv) ...@@ -1645,9 +1666,20 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
/* create NMClient */ if (!nmc_is_nm_running (nmc, &error)) {
if (!nmc->get_client (nmc)) if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error; goto error;
}
/* create NMClient */
nmc->get_client (nmc);
con_path = nm_connection_get_path (connection); con_path = nm_connection_get_path (connection);
con_scope = nm_connection_get_scope (connection); con_scope = nm_connection_get_scope (connection);
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 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> #include <stdio.h>
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <nm-client.h>
#include <nm-client.h> #include <nm-client.h>
#include <nm-device.h> #include <nm-device.h>
...@@ -942,12 +941,6 @@ do_devices_status (NmCli *nmc, int argc, char **argv) ...@@ -942,12 +941,6 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
argv++; argv++;
} }
/* create NMClient */
if (!nmc->get_client (nmc))
goto error;
devices = nm_client_get_devices (nmc->client);
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common; fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0) else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
...@@ -968,10 +961,25 @@ do_devices_status (NmCli *nmc, int argc, char **argv) ...@@ -968,10 +961,25 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
if (!nmc_is_nm_running (nmc, &error)) {
if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error;
}
/* Print headers */
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES; nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("Status of devices"); nmc->print_fields.header_name = _("Status of devices");
print_fields (nmc->print_fields, nmc->allowed_fields); print_fields (nmc->print_fields, nmc->allowed_fields);
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
for (i = 0; devices && (i < devices->len); i++) { for (i = 0; devices && (i < devices->len); i++) {
NMDevice *device = g_ptr_array_index (devices, i); NMDevice *device = g_ptr_array_index (devices, i);
show_device_status (device, nmc); show_device_status (device, nmc);
...@@ -987,6 +995,7 @@ static NMCResultCode ...@@ -987,6 +995,7 @@ static NMCResultCode
do_devices_list (NmCli *nmc, int argc, char **argv) do_devices_list (NmCli *nmc, int argc, char **argv)
{ {
const GPtrArray *devices; const GPtrArray *devices;
GError *error = NULL;
NMDevice *device = NULL; NMDevice *device = NULL;
const char *iface = NULL; const char *iface = NULL;
gboolean iface_specified = FALSE; gboolean iface_specified = FALSE;
...@@ -1011,10 +1020,19 @@ do_devices_list (NmCli *nmc, int argc, char **argv) ...@@ -1011,10 +1020,19 @@ do_devices_list (NmCli *nmc, int argc, char **argv)
argv++; argv++;
} }
/* create NMClient */ if (!nmc_is_nm_running (nmc, &error)) {
if (!nmc->get_client (nmc)) if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error; goto error;
}
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client); devices = nm_client_get_devices (nmc->client);
if (iface_specified) { if (iface_specified) {
...@@ -1100,6 +1118,7 @@ static NMCResultCode ...@@ -1100,6 +1118,7 @@ static NMCResultCode
do_device_disconnect (NmCli *nmc, int argc, char **argv) do_device_disconnect (NmCli *nmc, int argc, char **argv)
{ {
const GPtrArray *devices; const GPtrArray *devices;
GError *error = NULL;
NMDevice *device = NULL; NMDevice *device = NULL;
const char *iface = NULL; const char *iface = NULL;
gboolean iface_specified = FALSE; gboolean iface_specified = FALSE;
...@@ -1151,10 +1170,19 @@ do_device_disconnect (NmCli *nmc, int argc, char **argv) ...@@ -1151,10 +1170,19 @@ do_device_disconnect (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
/* create NMClient */ if (!nmc_is_nm_running (nmc, &error)) {
if (!nmc->get_client (nmc)) if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error; goto error;
}
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client); devices = nm_client_get_devices (nmc->client);
for (i = 0; devices && (i < devices->len); i++) { for (i = 0; devices && (i < devices->len); i++) {
NMDevice *candidate = g_ptr_array_index (devices, i); NMDevice *candidate = g_ptr_array_index (devices, i);
...@@ -1246,11 +1274,6 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) ...@@ -1246,11 +1274,6 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
argv++; argv++;
} }
/* create NMClient */
if (!nmc->get_client (nmc))
goto error;
devices = nm_client_get_devices (nmc->client);
if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0)
fields_str = fields_common; fields_str = fields_common;
...@@ -1272,9 +1295,24 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv) ...@@ -1272,9 +1295,24 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
goto error; goto error;
} }
if (!nmc_is_nm_running (nmc, &error)) {
if (error) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), error->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (error);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
goto error;
}
/* Print headers */
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES; nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag | NMC_PF_FLAG_MAIN_HEADER_ADD | NMC_PF_FLAG_FIELD_NAMES;
nmc->print_fields.header_name = _("WiFi scan list"); nmc->print_fields.header_name = _("WiFi scan list");
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
if (iface) { if (iface) {
/* Device specified - list only APs of this interface */ /* Device specified - list only APs of this interface */
for (i = 0; devices && (i < devices->len); i++) { for (i = 0; devices && (i < devices->len); i++) {
...@@ -1596,10 +1634,6 @@ do_devices (NmCli *nmc, int argc, char **argv) ...@@ -1596,10 +1634,6 @@ do_devices (NmCli *nmc, int argc, char **argv)
{ {
GError *error = NULL; GError *error = NULL;
/* create NMClient */
if (!nmc->get_client (nmc))
goto end;
if (argc == 0) { if (argc == 0) {
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error)) if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error))
goto opt_error; goto opt_error;
...@@ -1639,7 +1673,6 @@ do_devices (NmCli *nmc, int argc, char **argv) ...@@ -1639,7 +1673,6 @@ do_devices (NmCli *nmc, int argc, char **argv)
} }
} }
end:
return nmc->return_value; return nmc->return_value;
opt_error: opt_error:
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <dbus/dbus-glib-bindings.h>
#include <nm-client.h> #include <nm-client.h>
#include <nm-setting-connection.h> #include <nm-setting-connection.h>
...@@ -100,54 +99,6 @@ nm_state_to_string (NMState state) ...@@ -100,54 +99,6 @@ nm_state_to_string (NMState state)
} }
} }
/* 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()) here because NMClient
* constructor calls GetPermissions of NM_DBUS_SERVICE, which would autostart
* NetworkManger if it is configured as D-Bus launchable service. */
static gboolean
is_nm_running (NmCli *nmc)
{
DBusGConnection *connection = NULL;
DBusGProxy *proxy = NULL;
GError *err = NULL;
gboolean has_owner = 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_clear_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;
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_clear_error (&err);
goto done;
}
done:
if (connection)
dbus_g_connection_unref (connection);
if (proxy)
g_object_unref (proxy);
return has_owner;
}
static NMCResultCode static NMCResultCode
show_nm_status (NmCli *nmc) show_nm_status (NmCli *nmc)
{ {
...@@ -189,7 +140,7 @@ show_nm_status (NmCli *nmc) ...@@ -189,7 +140,7 @@ show_nm_status (NmCli *nmc)
nmc->print_fields.header_name = _("NetworkManager status"); nmc->print_fields.header_name = _("NetworkManager status");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */ print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
nm_running = is_nm_running (nmc); nm_running = nmc_is_nm_running (nmc, NULL);
if (nm_running) { if (nm_running) {
nmc->get_client (nmc); /* create NMClient */ nmc->get_client (nmc); /* create NMClient */
state = nm_client_get_state (nmc->client); state = nm_client_get_state (nmc->client);
...@@ -300,7 +251,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv) ...@@ -300,7 +251,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("Networking enabled"); nmc->print_fields.header_name = _("Networking enabled");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */ print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
if (is_nm_running (nmc)) { if (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */ nmc->get_client (nmc); /* create NMClient */
nmc->allowed_fields[2].value = nm_client_networking_get_enabled (nmc->client) ? _("enabled") : _("disabled"); nmc->allowed_fields[2].value = nm_client_networking_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else } else
...@@ -355,7 +306,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv) ...@@ -355,7 +306,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("WiFi enabled"); nmc->print_fields.header_name = _("WiFi enabled");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */ print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
if (is_nm_running (nmc)) { if (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */ nmc->get_client (nmc); /* create NMClient */
nmc->allowed_fields[4].value = nm_client_wireless_get_enabled (nmc->client) ? _("enabled") : _("disabled"); nmc->allowed_fields[4].value = nm_client_wireless_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else } else
...@@ -393,7 +344,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv) ...@@ -393,7 +344,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("WWAN enabled"); nmc->print_fields.header_name = _("WWAN enabled");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */ print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
if (is_nm_running (nmc)) { if (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */ nmc->get_client (nmc); /* create NMClient */
nmc->allowed_fields[6].value = nm_client_wwan_get_enabled (nmc->client) ? _("enabled") : _("disabled"); nmc->allowed_fields[6].value = nm_client_wwan_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else } else
...@@ -431,7 +382,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv) ...@@ -431,7 +382,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("WiMAX enabled"); nmc->print_fields.header_name = _("WiMAX enabled");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */ print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
if (is_nm_running (nmc)) { if (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */ nmc->get_client (nmc); /* create NMClient */
nmc->allowed_fields[8].value = nm_client_wimax_get_enabled (nmc->client) ? _("enabled") : _("disabled"); nmc->allowed_fields[8].value = nm_client_wimax_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else } else
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* *
* (C) Copyright 2010 Red Hat, Inc. * (C) Copyright 2010 - 2011 Red Hat, Inc.
*/ */
#ifndef NMC_NMCLI_H #ifndef NMC_NMCLI_H
...@@ -47,7 +47,10 @@ typedef enum { ...@@ -47,7 +47,10 @@ typedef enum {
NMC_RESULT_ERROR_CON_DEACTIVATION = 5, NMC_RESULT_ERROR_CON_DEACTIVATION = 5,
/* Error in device disconnect */ /* Error in device disconnect */
NMC_RESULT_ERROR_DEV_DISCONNECT = 6 NMC_RESULT_ERROR_DEV_DISCONNECT = 6,
/* NetworkManager is not running */
NMC_RESULT_ERROR_NM_NOT_RUNNING = 7
} NMCResultCode; } NMCResultCode;
typedef enum { typedef enum {
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* with this program; if not, write to the Free Software Foundation, Inc., * with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 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> #include <stdio.h>
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <glib.h> #include <glib.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <dbus/dbus-glib-bindings.h>
#include "utils.h" #include "utils.h"
...@@ -296,3 +297,57 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]) ...@@ -296,3 +297,57 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
g_string_free (str, TRUE); 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;