summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-03-14 08:49:55 +0100
committerThomas Haller <thaller@redhat.com>2020-03-23 09:37:53 +0100
commitc6c15c2c2500961f0561785cad1c5dbf5970a198 (patch)
tree123bc6760191da5f1df0e904b30cf043d4310d7b
parentd99d1dc26525d86a6f0000505fbfd256908cf7a7 (diff)
cli: use async method D-Bus for `nmcli networking on|off`
Previously, we would call the synchronous nm_client_networking_set_enabled() method. There were 3 problems: 1) nmcli ignored the return value, that means, if the request failed with access denied it would just silently pretend that it succeeded. 2) nmcli first called nmc_start_polkit_agent_start_try(), but when invoking the synchronous method, the main context is busy and a polkit request cannot possibly be handled. 3) nm_client_networking_set_enabled() is deprecated. Fix all of these, by calling the D-Bus method directly. Policykit authentication requests are only handled partly. There seems to be an unrelated race/bug. Now it works sometimes.
-rw-r--r--clients/cli/general.c36
1 files changed, 34 insertions, 2 deletions
diff --git a/clients/cli/general.c b/clients/cli/general.c
index c083386a65..bd6bc5d671 100644
--- a/clients/cli/general.c
+++ b/clients/cli/general.c
@@ -933,16 +933,48 @@ nmc_switch_parse_on_off (NmCli *nmc, const char *arg1, const char *arg2, gboolea
return TRUE;
}
+static void
+_do_networking_on_off_cb (GObject *object, GAsyncResult *result, gpointer user_data)
+{
+ NmCli *nmc = user_data;
+ gs_unref_variant GVariant *ret = NULL;
+ gs_free_error GError *error = NULL;
+
+ ret = nm_client_dbus_call_finish (NM_CLIENT (object), result, &error);
+ if (!ret) {
+ if (g_error_matches (error,
+ NM_MANAGER_ERROR,
+ NM_MANAGER_ERROR_ALREADY_ENABLED_OR_DISABLED)) {
+ /* This is fine. Be quiet about it. */
+ } else {
+ g_dbus_error_strip_remote_error (error);
+ g_string_printf (nmc->return_text, _("Error: failed to set networking: %s"),
+ nmc_error_get_simple_message (error));
+ nmc->return_value = NMC_RESULT_ERROR_UNKNOWN;
+ }
+ }
+ quit ();
+}
+
static NMCResultCode
do_networking_on_off (NmCli *nmc, int argc, char **argv, gboolean enable)
{
if (nmc->complete)
return nmc->return_value;
- /* Register polkit agent */
nmc_start_polkit_agent_start_try (nmc);
- nm_client_networking_set_enabled (nmc->client, enable, NULL);
+ nmc->should_wait++;
+ nm_client_dbus_call (nmc->client,
+ NM_DBUS_PATH,
+ NM_DBUS_INTERFACE,
+ "Enable",
+ g_variant_new ("(b)", enable),
+ G_VARIANT_TYPE ("()"),
+ -1,
+ NULL,
+ _do_networking_on_off_cb,
+ nmc);
return nmc->return_value;
}