From fd8d5a0c7ad66b3fda2a2a73404878306082e679 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Mon, 9 Sep 2019 17:01:43 +0200 Subject: cli: don't create a NMClient for the 'connection reload' command It is a waste of resources instantiating a NMClient, filling the object cache and then throwing everything away without using it. This can take seconds on slow systems with many objects. Since the ReloadConnections doesn't need anything from the cache, just execute the D-Bus method call directly. --- clients/cli/common.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ clients/cli/common.h | 8 ++++++++ clients/cli/connections.c | 15 +++++++++++---- 3 files changed, 65 insertions(+), 4 deletions(-) diff --git a/clients/cli/common.c b/clients/cli/common.c index eeef9b64c9..9b2d07aacd 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -1421,10 +1421,56 @@ nmc_error_get_simple_message (GError *error) /* Return a clear message instead of the obscure D-Bus policy error */ if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_ACCESS_DENIED)) return _("access denied"); + if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN)) + return _("NetworkManager is not running"); else return error->message; } +GVariant * +nmc_dbus_call_sync (NmCli *nmc, + const char *object_path, + const char *interface_name, + const char *method_name, + GVariant *parameters, + const GVariantType *reply_type, + GError **error) +{ + gs_unref_object GDBusConnection *connection = NULL; + gs_free_error GError *local = NULL; + GVariant *result; + + if (nmc->timeout == -1) + nmc->timeout = 90; + + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &local); + if (!connection) { + g_set_error (error, + NMCLI_ERROR, + NMC_RESULT_ERROR_UNKNOWN, + _("Error: error connecting to system bus: %s"), + local->message); + return NULL; + } + + result = g_dbus_connection_call_sync (connection, + "org.freedesktop.NetworkManager", + object_path, + interface_name, + method_name, + parameters, + reply_type, + G_DBUS_CALL_FLAGS_NONE, + nmc->timeout * 1000, + NULL, + error); + + if (error && *error) + g_dbus_error_strip_remote_error (*error); + + return result; +} + /*****************************************************************************/ NM_UTILS_LOOKUP_STR_DEFINE (nm_connectivity_to_string, NMConnectivityState, diff --git a/clients/cli/common.h b/clients/cli/common.h index 40dee6d0f4..50574a1baf 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -89,4 +89,12 @@ extern const NmcMetaGenericInfo *const metagen_dhcp_config[]; const char *nm_connectivity_to_string (NMConnectivityState connectivity); +GVariant *nmc_dbus_call_sync (NmCli *nmc, + const char *object_path, + const char *interface_name, + const char *method_name, + GVariant *parameters, + const GVariantType *reply_type, + GError **error); + #endif /* NMC_COMMON_H */ diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 8e848ffd19..003400d5fe 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -8845,17 +8845,24 @@ do_connection_monitor (NmCli *nmc, int argc, char **argv) static NMCResultCode do_connection_reload (NmCli *nmc, int argc, char **argv) { - GError *error = NULL; + gs_unref_variant GVariant *result = NULL; + gs_free_error GError *error = NULL; next_arg (nmc, &argc, &argv, NULL); if (nmc->complete) return nmc->return_value; - if (!nm_client_reload_connections (nmc->client, NULL, &error)) { + result = nmc_dbus_call_sync (nmc, + "/org/freedesktop/NetworkManager/Settings", + "org.freedesktop.NetworkManager.Settings", + "ReloadConnections", + g_variant_new ("()"), + G_VARIANT_TYPE("(b)"), + &error); + if (error) { g_string_printf (nmc->return_text, _("Error: failed to reload connections: %s."), nmc_error_get_simple_message (error)); nmc->return_value = NMC_RESULT_ERROR_UNKNOWN; - g_clear_error (&error); } return nmc->return_value; @@ -9245,7 +9252,7 @@ static const NMCCommand connection_cmds[] = { { "add", do_connection_add, usage_connection_add, TRUE, TRUE }, { "edit", do_connection_edit, usage_connection_edit, TRUE, TRUE }, { "delete", do_connection_delete, usage_connection_delete, TRUE, TRUE }, - { "reload", do_connection_reload, usage_connection_reload, TRUE, TRUE }, + { "reload", do_connection_reload, usage_connection_reload, FALSE, FALSE }, { "load", do_connection_load, usage_connection_load, TRUE, TRUE }, { "modify", do_connection_modify, usage_connection_modify, TRUE, TRUE }, { "clone", do_connection_clone, usage_connection_clone, TRUE, TRUE }, -- cgit v1.2.1