diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2019-09-09 17:01:43 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2019-09-09 17:42:27 +0200 |
commit | 83cc5308ee9d44249c6dd6fb1a090494616a8c38 (patch) | |
tree | 1be22154229ded7d05794c192dd542cf3b83ed43 | |
parent | f2e1c831b3a73d41823d8fe19d469803dc7bd621 (diff) | |
download | NetworkManager-83cc5308ee9d44249c6dd6fb1a090494616a8c38.tar.gz |
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.
-rw-r--r-- | clients/cli/common.c | 46 | ||||
-rw-r--r-- | clients/cli/common.h | 8 | ||||
-rw-r--r-- | 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 2fc8aaf643..aaa8dd116c 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -1434,10 +1434,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 688f2819f6..bab0e9662f 100644 --- a/clients/cli/common.h +++ b/clients/cli/common.h @@ -102,4 +102,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 0e42831c7a..7786114113 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -8858,17 +8858,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; @@ -9258,7 +9265,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 }, |