summary refs log tree commit diff
path: root/src/core/nm-hostname-manager.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2022-08-16 18:24:19 +0200
committerMichael Biebl <biebl@debian.org>2022-08-16 18:24:19 +0200
commit0018d1f3cf71d680d7b6bceda55a5717244d8b26 (patch)
treea058f1d106d172d3354179437ef034c9355cdf9c /src/core/nm-hostname-manager.c
parent6accbd3ec0e42d8633bbde4d47ed7bfe854e7e0b (diff)
New upstream version 1.39.90 upstream/1.39.90
Diffstat (limited to 'src/core/nm-hostname-manager.c')
-rw-r--r--src/core/nm-hostname-manager.c132
1 files changed, 97 insertions, 35 deletions
diff --git a/src/core/nm-hostname-manager.c b/src/core/nm-hostname-manager.c
index 64c2531e..ba0c6c9d 100644
--- a/src/core/nm-hostname-manager.c
+++ b/src/core/nm-hostname-manager.c
@@ -239,7 +239,7 @@ _set_hostname_read_file(NMHostnameManager *self)
 #if defined(HOSTNAME_PERSIST_GENTOO)
     hostname = read_hostname_gentoo(HOSTNAME_FILE);
 #elif defined(HOSTNAME_PERSIST_SLACKWARE)
-    hostname     = read_hostname_slackware(HOSTNAME_FILE);
+    hostname = read_hostname_slackware(HOSTNAME_FILE);
 #else
     if (g_file_get_contents(HOSTNAME_FILE, &hostname, NULL, NULL))
         g_strchomp(hostname);
@@ -321,39 +321,50 @@ nm_hostname_manager_get_transient_hostname(NMHostnameManager *self, char **hostn
     return TRUE;
 }
 
-gboolean
-nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname)
+/*****************************************************************************/
+
+static void
+_write_hostname_dbus_cb(GObject *source, GAsyncResult *result, gpointer user_data)
+{
+    gs_unref_object GTask     *task  = G_TASK(user_data);
+    gs_unref_variant GVariant *res   = NULL;
+    GError                    *error = NULL;
+
+    res = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), result, &error);
+    if (!res) {
+        g_task_return_error(task, error);
+        return;
+    }
+    g_task_return_boolean(task, TRUE);
+}
+
+static void
+_write_hostname_on_idle_cb(gpointer user_data, GCancellable *cancellable)
 {
-    NMHostnameManagerPrivate  *priv;
-    char                      *hostname_eol;
-    gboolean                   ret;
-    gs_free_error GError      *error     = NULL;
-    const char                *file      = HOSTNAME_FILE;
-    gs_free char              *link_path = NULL;
-    gs_unref_variant GVariant *var       = NULL;
-    struct stat                file_stat;
+    gs_unref_object GTask    *task = G_TASK(user_data);
+    NMHostnameManager        *self;
+    NMHostnameManagerPrivate *priv;
+    const char               *hostname;
+    gs_free char             *hostname_eol = NULL;
+    gboolean                  ret;
+    gs_free_error GError     *error     = NULL;
+    const char               *file      = HOSTNAME_FILE;
+    gs_free char             *link_path = NULL;
+    struct stat               file_stat;
 #if HAVE_SELINUX
     gboolean fcon_was_set = FALSE;
     char    *fcon_prev    = NULL;
 #endif
 
-    g_return_val_if_fail(NM_IS_HOSTNAME_MANAGER(self), FALSE);
+    if (g_task_return_error_if_cancelled(task))
+        return;
 
+    self = g_task_get_source_object(task);
     priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
 
-    if (priv->hostnamed_proxy) {
-        var = g_dbus_proxy_call_sync(priv->hostnamed_proxy,
-                                     "SetStaticHostname",
-                                     g_variant_new("(sb)", hostname, FALSE),
-                                     G_DBUS_CALL_FLAGS_NONE,
-                                     -1,
-                                     NULL,
-                                     &error);
-        if (error)
-            _LOGW("could not set hostname: %s", error->message);
-
-        return !error;
-    }
+    nm_assert(!priv->hostnamed_proxy);
+
+    hostname = g_task_get_task_data(task);
 
     /* If the hostname file is a symbolic link, follow it to find where the
      * real file is located, otherwise g_file_set_contents will attempt to
@@ -363,13 +374,15 @@ nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname
         && (link_path = nm_utils_read_link_absolute(file, NULL)))
         file = link_path;
 
+    if (hostname) {
 #if defined(HOSTNAME_PERSIST_GENTOO)
-    hostname_eol = g_strdup_printf("#Generated by NetworkManager\n"
-                                   "hostname=\"%s\"\n",
-                                   hostname);
+        hostname_eol = g_strdup_printf("#Generated by NetworkManager\n"
+                                       "hostname=\"%s\"\n",
+                                       hostname);
 #else
-    hostname_eol = g_strdup_printf("%s\n", hostname);
+        hostname_eol = g_strdup_printf("%s\n", hostname);
 #endif
+    }
 
 #if HAVE_SELINUX
     /* Get default context for hostname file and set it for fscreate */
@@ -396,7 +409,7 @@ nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname
     }
 #endif
 
-    ret = g_file_set_contents(file, hostname_eol, -1, &error);
+    ret = g_file_set_contents(file, hostname_eol ?: "", -1, &error);
 
 #if HAVE_SELINUX
     /* Restore previous context and cleanup */
@@ -406,14 +419,63 @@ nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname
         freecon(fcon_prev);
 #endif
 
-    g_free(hostname_eol);
-
     if (!ret) {
-        _LOGW("could not save hostname to %s: %s", file, error->message);
-        return FALSE;
+        g_task_return_new_error(task,
+                                NM_UTILS_ERROR,
+                                NM_UTILS_ERROR_UNKNOWN,
+                                "could not save hostname to %s: %s",
+                                file,
+                                error->message);
+        return;
     }
 
-    return TRUE;
+    g_task_return_boolean(task, TRUE);
+}
+
+void
+nm_hostname_manager_write_hostname(NMHostnameManager  *self,
+                                   const char         *hostname,
+                                   GCancellable       *cancellable,
+                                   GAsyncReadyCallback callback,
+                                   gpointer            user_data)
+{
+    NMHostnameManagerPrivate *priv;
+    GTask                    *task;
+
+    g_return_if_fail(NM_IS_HOSTNAME_MANAGER(self));
+
+    priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
+
+    task =
+        nm_g_task_new(self, cancellable, nm_hostname_manager_write_hostname, callback, user_data);
+
+    g_task_set_task_data(task, g_strdup(hostname), g_free);
+
+    if (priv->hostnamed_proxy) {
+        g_dbus_proxy_call(priv->hostnamed_proxy,
+                          "SetStaticHostname",
+                          g_variant_new("(sb)", hostname ?: "", FALSE),
+                          G_DBUS_CALL_FLAGS_NONE,
+                          15000,
+                          cancellable,
+                          _write_hostname_dbus_cb,
+                          task);
+        return;
+    }
+
+    nm_utils_invoke_on_idle(cancellable, _write_hostname_on_idle_cb, task);
+}
+
+gboolean
+nm_hostname_manager_write_hostname_finish(NMHostnameManager *self,
+                                          GAsyncResult      *result,
+                                          GError           **error)
+{
+    g_return_val_if_fail(NM_IS_HOSTNAME_MANAGER(self), FALSE);
+    g_return_val_if_fail(nm_g_task_is_valid(result, self, nm_hostname_manager_write_hostname),
+                         FALSE);
+
+    return g_task_propagate_boolean(G_TASK(result), error);
 }
 
 /*****************************************************************************/