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 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 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>
......@@ -647,12 +647,6 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
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)
fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
......@@ -673,10 +667,25 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
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.header_name = _("Active connections");
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) {
info = g_malloc0 (sizeof (StatusInfo));
info->nmc = nmc;
......@@ -688,7 +697,6 @@ do_connections_status (NmCli *nmc, int argc, char **argv)
}
error:
return nmc->return_value;
}
......@@ -1555,9 +1563,20 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
goto error;
}
/* create NMClient */
if (!nmc->get_client (nmc))
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;
}
/* create NMClient */
nmc->get_client (nmc);
is_system = (nm_connection_get_scope (connection) == NM_CONNECTION_SCOPE_SYSTEM) ? TRUE : FALSE;
con_path = nm_connection_get_path (connection);
......@@ -1574,6 +1593,7 @@ do_connection_up (NmCli *nmc, int argc, char **argv)
else
g_string_printf (nmc->return_text, _("Error: No suitable device found."));
nmc->return_value = NMC_RESULT_ERROR_CON_ACTIVATION;
g_clear_error (&error);
goto error;
}
......@@ -1600,6 +1620,7 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
{
NMConnection *connection = NULL;
NMActiveConnection *active = NULL;
GError *error = NULL;
const GPtrArray *active_cons;
const char *con_path;
const char *active_path;
......@@ -1645,9 +1666,20 @@ do_connection_down (NmCli *nmc, int argc, char **argv)
goto error;
}
/* create NMClient */
if (!nmc->get_client (nmc))
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;
}
/* create NMClient */
nmc->get_client (nmc);
con_path = nm_connection_get_path (connection);
con_scope = nm_connection_get_scope (connection);
......
......@@ -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>
......@@ -28,7 +28,6 @@
#include <glib.h>
#include <glib/gi18n.h>
#include <nm-client.h>
#include <nm-client.h>
#include <nm-device.h>
......@@ -942,12 +941,6 @@ do_devices_status (NmCli *nmc, int argc, char **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)
fields_str = fields_common;
else if (!nmc->required_fields || strcasecmp (nmc->required_fields, "all") == 0)
......@@ -968,10 +961,25 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
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.header_name = _("Status of devices");
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++) {
NMDevice *device = g_ptr_array_index (devices, i);
show_device_status (device, nmc);
......@@ -987,6 +995,7 @@ static NMCResultCode
do_devices_list (NmCli *nmc, int argc, char **argv)
{
const GPtrArray *devices;
GError *error = NULL;
NMDevice *device = NULL;
const char *iface = NULL;
gboolean iface_specified = FALSE;
......@@ -1011,10 +1020,19 @@ do_devices_list (NmCli *nmc, int argc, char **argv)
argv++;
}
/* create NMClient */
if (!nmc->get_client (nmc))
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;
}
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
if (iface_specified) {
......@@ -1100,6 +1118,7 @@ static NMCResultCode
do_device_disconnect (NmCli *nmc, int argc, char **argv)
{
const GPtrArray *devices;
GError *error = NULL;
NMDevice *device = NULL;
const char *iface = NULL;
gboolean iface_specified = FALSE;
......@@ -1151,10 +1170,19 @@ do_device_disconnect (NmCli *nmc, int argc, char **argv)
goto error;
}
/* create NMClient */
if (!nmc->get_client (nmc))
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;
}
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
for (i = 0; devices && (i < devices->len); i++) {
NMDevice *candidate = g_ptr_array_index (devices, i);
......@@ -1246,11 +1274,6 @@ do_device_wifi_list (NmCli *nmc, int argc, char **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)
fields_str = fields_common;
......@@ -1272,9 +1295,24 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
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.header_name = _("WiFi scan list");
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
if (iface) {
/* Device specified - list only APs of this interface */
for (i = 0; devices && (i < devices->len); i++) {
......@@ -1596,10 +1634,6 @@ do_devices (NmCli *nmc, int argc, char **argv)
{
GError *error = NULL;
/* create NMClient */
if (!nmc->get_client (nmc))
goto end;
if (argc == 0) {
if (!nmc_terse_option_check (nmc->print_output, nmc->required_fields, &error))
goto opt_error;
......@@ -1639,7 +1673,6 @@ do_devices (NmCli *nmc, int argc, char **argv)
}
}
end:
return nmc->return_value;
opt_error:
......
......@@ -23,7 +23,6 @@
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib-bindings.h>
#include <nm-client.h>
#include <nm-setting-connection.h>
......@@ -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
show_nm_status (NmCli *nmc)
{
......@@ -189,7 +140,7 @@ show_nm_status (NmCli *nmc)
nmc->print_fields.header_name = _("NetworkManager status");
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) {
nmc->get_client (nmc); /* create NMClient */
state = nm_client_get_state (nmc->client);
......@@ -300,7 +251,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("Networking enabled");
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->allowed_fields[2].value = nm_client_networking_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
......@@ -355,7 +306,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("WiFi enabled");
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->allowed_fields[4].value = nm_client_wireless_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
......@@ -393,7 +344,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("WWAN enabled");
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->allowed_fields[6].value = nm_client_wwan_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
......@@ -431,7 +382,7 @@ do_network_manager (NmCli *nmc, int argc, char **argv)
nmc->print_fields.header_name = _("WiMAX enabled");
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->allowed_fields[8].value = nm_client_wimax_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
......
......@@ -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.
*/
#ifndef NMC_NMCLI_H
......@@ -47,7 +47,10 @@ typedef enum {
NMC_RESULT_ERROR_CON_DEACTIVATION = 5,
/* 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;
typedef enum {
......
......@@ -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;
}
......@@ -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.
*/
#ifndef NMC_UTILS_H
......@@ -32,5 +32,6 @@ int nmc_string_screen_width (const char *start, const char *end);
GArray *parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error);
gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error);
void print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]);
gboolean nmc_is_nm_running (NmCli *nmc, GError **error);
#endif /* NMC_UTILS_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment