Commit a829c043 authored by Dan Williams's avatar Dan Williams

cli: use nm_client_get_manager_running() instead of nmc_is_nm_running()

We now guarantee that NMClient won't dbus-activate NetworkManager so
there is no need for a custom function to prevent this.
parent 110a4035
......@@ -888,25 +888,19 @@ static NMCResultCode
do_connections_status (NmCli *nmc, int argc, char **argv)
{
const GPtrArray *active_cons;
GError *err = NULL;
GError *err1 = NULL;
nmc->should_wait = FALSE;
if (!nmc_is_nm_running (nmc, &err)) {
if (err) {
g_string_printf (nmc->return_text, _("Error: Can't find out if NetworkManager is running: %s."), err->message);
nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
g_error_free (err);
} else {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
}
/* Get active connections */
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
/* Get active connections */
nmc->get_client (nmc);
active_cons = nm_client_get_active_connections (nmc->client);
if (argc == 0) {
......@@ -1555,21 +1549,15 @@ do_connection_up (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;
}
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
s_con = nm_connection_get_setting_connection (connection);
g_assert (s_con);
con_type = nm_setting_connection_get_connection_type (s_con);
......@@ -1624,7 +1612,6 @@ 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;
......@@ -1668,21 +1655,15 @@ do_connection_down (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;
}
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
con_path = nm_connection_get_path (connection);
active_cons = nm_client_get_active_connections (nmc->client);
......@@ -1734,7 +1715,6 @@ do_connection_delete (NmCli *nmc, int argc, char **argv)
NMConnection *connection = NULL;
const char *selector = NULL;
const char *id = NULL;
GError *error = NULL;
while (argc > 0) {
if (strcmp (*argv, "id") == 0 || strcmp (*argv, "uuid") == 0) {
......@@ -1759,15 +1739,12 @@ do_connection_delete (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;
}
/* create NMClient */
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
......
......@@ -998,15 +998,11 @@ 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;
}
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
......@@ -1018,7 +1014,6 @@ do_devices_status (NmCli *nmc, int argc, char **argv)
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);
......@@ -1035,7 +1030,6 @@ 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;
......@@ -1060,22 +1054,17 @@ do_devices_list (NmCli *nmc, int argc, char **argv)
argv++;
}
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;
}
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
if (iface_specified) {
......@@ -1174,7 +1163,6 @@ 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;
......@@ -1226,22 +1214,17 @@ do_device_disconnect (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;
}
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
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);
......@@ -1359,15 +1342,11 @@ 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;
}
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
......@@ -1378,7 +1357,6 @@ do_device_wifi_list (NmCli *nmc, int argc, char **argv)
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 */
......@@ -1667,7 +1645,6 @@ do_device_wifi_connect_network (NmCli *nmc, int argc, char **argv)
GByteArray *bssid2_arr = NULL;
const GPtrArray *devices;
int devices_idx;
GError *error = NULL;
/* Default timeout for waiting for operation completion */
nmc->timeout = 90;
......@@ -1778,22 +1755,17 @@ do_device_wifi_connect_network (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;
}
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
if (!nmc_versions_match (nmc))
goto error;
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
/* Find a device to activate the connection on */
......@@ -2006,15 +1978,11 @@ do_device_wimax_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;
}
nmc->get_client (nmc);
if (!nm_client_get_manager_running (nmc->client)) {
g_string_printf (nmc->return_text, _("Error: NetworkManager is not running."));
nmc->return_value = NMC_RESULT_ERROR_NM_NOT_RUNNING;
goto error;
}
......@@ -2025,7 +1993,6 @@ do_device_wimax_list (NmCli *nmc, int argc, char **argv)
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 = _("WiMAX NSP list");
nmc->get_client (nmc);
devices = nm_client_get_devices (nmc->client);
if (iface) {
/* Device specified - list only NSPs of this interface */
......
......@@ -163,12 +163,13 @@ show_nm_status (NmCli *nmc)
return nmc->return_value;
}
nm_running = nmc_is_nm_running (nmc, NULL);
nmc->get_client (nmc); /* create NMClient */
nm_running = nm_client_get_manager_running (nmc->client);
if (nm_running) {
if (!nmc_versions_match (nmc))
goto error;
nmc->get_client (nmc); /* create NMClient */
state = nm_client_get_state (nmc->client);
net_enabled_str = nm_client_networking_get_enabled (nmc->client) ? _("enabled") : _("disabled");
wireless_hw_enabled_str = nm_client_wireless_hardware_get_enabled (nmc->client) ? _("enabled") : _("disabled");
......@@ -306,20 +307,14 @@ show_nm_permissions (NmCli *nmc)
return nmc->return_value;
}
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;
}
nmc->get_client (nmc); /* create NMClient */
if (!nm_client_get_manager_running (nmc->client)) {
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); /* create NMClient */
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 = _("NetworkManager permissions");
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print header */
......@@ -424,10 +419,10 @@ 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 (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */
nmc->get_client (nmc); /* create NMClient */
if (nm_client_get_manager_running (nmc->client))
nmc->allowed_fields[3].value = nm_client_networking_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
else
nmc->allowed_fields[3].value = _("unknown");
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
......@@ -479,10 +474,10 @@ 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 (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */
nmc->get_client (nmc); /* create NMClient */
if (nm_client_get_manager_running (nmc->client))
nmc->allowed_fields[5].value = nm_client_wireless_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
else
nmc->allowed_fields[5].value = _("unknown");
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
......@@ -517,10 +512,10 @@ 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 (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */
nmc->get_client (nmc); /* create NMClient */
if (nm_client_get_manager_running (nmc->client))
nmc->allowed_fields[7].value = nm_client_wwan_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
else
nmc->allowed_fields[7].value = _("unknown");
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
......@@ -556,10 +551,10 @@ 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 (nmc_is_nm_running (nmc, NULL)) {
nmc->get_client (nmc); /* create NMClient */
nmc->get_client (nmc); /* create NMClient */
if (nm_client_get_manager_running (nmc->client))
nmc->allowed_fields[9].value = nm_client_wimax_get_enabled (nmc->client) ? _("enabled") : _("disabled");
} else
else
nmc->allowed_fields[9].value = _("unknown");
nmc->print_fields.flags = multiline_flag | mode_flag | escape_flag;
print_fields (nmc->print_fields, nmc->allowed_fields); /* Print values */
......
......@@ -453,60 +453,6 @@ 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_literal (error, NMCLI_ERROR, 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;
}
/*
* Compare versions of nmcli and NM daemon.
* Return: TRUE - the versions match (when only major and minor match, print a warning)
......
......@@ -39,7 +39,6 @@ void nmc_free_output_field_values (NmcOutputField fields_array[]);
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);
gboolean nmc_versions_match (NmCli *nmc);
#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