Skip to contentSkip to conversation

tap: Use g_spawn_sync() and g_spawn_check_wait_status()

PATCH In review Akihiko Odaki 2 participants 5 messages Jan 15, 2025 – Jan 17, 2025 Networking

lore
Share & tools
lore
#0 lore raw

v4 cover tap: Use g_spawn_sync() and g_spawn_check_wait_status()

g_spawn_sync() gives an informative message if it fails to execute the script instead of reporting exiting status 1.

g_spawn_check_wait_status() also gives an message easier to understand than the raw value returned by waitpid().

---
Changes in v4:
- Changed to use g_spawn_async_with_pipes_and_fds() when available. (Daniel P. Berrangé)
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Added GLIB_CHECK_VERSION(2, 70, 0) for patch "glib-compat: Define g_spawn_check_wait_status()". (Daniel P. Berrangé)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebased.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (2):
glib-compat: Define g_spawn_check_wait_status()
tap: Use g_spawn_sync() and g_spawn_check_wait_status()

 include/glib-compat.h |  11 +++
 net/tap.c             | 220 +++++++++++++++++++++++++++-----------------------
 2 files changed, 128 insertions(+), 103 deletions(-)

---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20231219-glib-034a34bb05d8

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v4 1/2 glib-compat: Define g_spawn_check_wait_status()

g_spawn_check_exit_status() is renamed to g_spawn_check_wait_status() in 2.70.

---
 include/glib-compat.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/glib-compat.h +11 -0
index 86be439ba0ef..a553ba13a36e 100644
@@ -68,6 +68,17 @@
  * without generating warnings.
  */
 
+static inline gboolean g_spawn_check_wait_status_qemu(gint wait_status,
+                                                      GError **error)
+{
+#if GLIB_CHECK_VERSION(2, 70, 0)
+    return g_spawn_check_wait_status(wait_status, error);
+#else
+    return g_spawn_check_exit_status(wait_status, error);
+#endif
+}
+#define g_spawn_check_wait_status(w, e) g_spawn_check_wait_status_qemu(w, e)
+
 /*
  * g_memdup2_qemu:
  * @mem: (nullable): the memory to copy.
Signature · 1 line
2.47.1
Reviewed-by on v4 1/2
#2 lore raw

Re: [PATCH v4 1/2] glib-compat: Define g_spawn_check_wait_status()

Quoted 8 lines · Akihiko Odaki

g_spawn_check_exit_status() is renamed to g_spawn_check_wait_status() in 2.70.

---
 include/glib-compat.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

PATCH v4 2/2 tap: Use g_spawn_sync() and g_spawn_check_wait_status()

In reply to Akihiko Odaki

g_spawn_sync() (for GLib < 2.68) or g_spawn_async_with_pipes_and_fds() (for the newer) gives an informative message if it fails to execute the script instead of reporting exiting status 1.

g_spawn_check_wait_status() also gives an message easier to understand than the raw value returned by waitpid().

---
 net/tap.c | 220 +++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 117 insertions(+), 103 deletions(-)
net/tap.c +117 -103
index ae1c7e398321..f1e00de4181c 100644
@@ -385,56 +385,59 @@ static TAPState *net_tap_fd_init(NetClientState *peer,
     return s;
 }
 
-static void close_all_fds_after_fork(int excluded_fd)
+#if !GLIB_CHECK_VERSION(2, 68, 0)
+static void unset_cloexec(gpointer data)
 {
-    const int skip_fd[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO,
-                           excluded_fd};
-    unsigned int nskip = ARRAY_SIZE(skip_fd);
-
-    /*
-     * skip_fd must be an ordered array of distinct fds, exclude
-     * excluded_fd if already included in the [STDIN_FILENO - STDERR_FILENO]
-     * range
-     */
-    if (excluded_fd <= STDERR_FILENO) {
-        nskip--;
-    }
-
-    qemu_close_all_open_fd(skip_fd, nskip);
+    g_assert(!fcntl(GPOINTER_TO_INT(data), F_SETFD, 0));
 }
+#endif
 
 static void launch_script(const char *setup_script, const char *ifname,
                           int fd, Error **errp)
 {
-    int pid, status;
-    char *args[3];
-    char **parg;
+    int status;
+    const gchar *args[] = { setup_script, ifname, NULL };
+    g_autoptr(GError) error = NULL;
+    bool spawned;
 
     /* try to launch network script */
-    pid = fork();
-    if (pid < 0) {
-        error_setg_errno(errp, errno, "could not launch network script %s",
-                         setup_script);
-        return;
-    }
-    if (pid == 0) {
-        close_all_fds_after_fork(fd);
-        parg = args;
-        *parg++ = (char *)setup_script;
-        *parg++ = (char *)ifname;
-        *parg = NULL;
-        execv(setup_script, args);
-        _exit(1);
-    } else {
+#if GLIB_CHECK_VERSION(2, 68, 0)
+    pid_t pid;
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+    spawned = g_spawn_async_with_pipes_and_fds(NULL, args, NULL,
+                                               G_SPAWN_DO_NOT_REAP_CHILD |
+                                               G_SPAWN_CHILD_INHERITS_STDIN,
+                                               NULL, NULL, -1, -1, -1,
+                                               &fd, &fd, 1, &pid,
+                                               NULL, NULL, NULL, &error);
+#pragma GCC diagnostic pop
+    if (spawned) {
         while (waitpid(pid, &status, 0) != pid) {
             /* loop */
         }
+    }
+#else
+    gchar *mutable_args[sizeof(args)];
 
-        if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
-            return;
-        }
-        error_setg(errp, "network script %s failed with status %d",
-                   setup_script, status);
+    for (size_t i = 0; i < ARRAY_SIZE(args); i++) {
+        mutable_args[i] = (gchar *)args[i];
+    }
+
+    spawned = g_spawn_sync(NULL, mutable_args, NULL,
+                           G_SPAWN_CHILD_INHERITS_STDIN,
+                           unset_cloexec, GINT_TO_POINTER(fd),
+                           NULL, NULL, &status, &error);
+#endif
+    if (!spawned) {
+        error_setg(errp, "could not launch network script %s: %s",
+                   setup_script, error->message);
+        return;
+    }
+
+    if (!g_spawn_check_wait_status(status, &error)) {
+        error_setg(errp, "network script %s failed: %s",
+                   setup_script, error->message);
     }
 }
 
@@ -477,10 +480,17 @@ static int net_bridge_run_helper(const char *helper, const char *bridge,
 {
     sigset_t oldmask, mask;
     g_autofree char *default_helper = NULL;
+    g_autofree char *fd_buf = NULL;
+    g_autofree char *br_buf = NULL;
+    g_autofree char *helper_cmd = NULL;
+    g_autoptr(GError) error = NULL;
+    int fd;
+    int saved_errno;
     int pid, status;
-    char *args[5];
-    char **parg;
+    const char *args[5];
+    const char **parg;
     int sv[2];
+    bool spawned;
 
     sigemptyset(&mask);
     sigaddset(&mask, SIGCHLD);
@@ -495,82 +505,86 @@ static int net_bridge_run_helper(const char *helper, const char *bridge,
         return -1;
     }
 
-    /* try to launch bridge helper */
-    pid = fork();
-    if (pid < 0) {
-        error_setg_errno(errp, errno, "Can't fork bridge helper");
-        return -1;
-    }
-    if (pid == 0) {
-        char *fd_buf = NULL;
-        char *br_buf = NULL;
-        char *helper_cmd = NULL;
-
-        close_all_fds_after_fork(sv[1]);
-        fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
+    fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
 
-        if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
-            /* assume helper is a command */
+    if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
+        /* assume helper is a command */
 
-            if (strstr(helper, "--br=") == NULL) {
-                br_buf = g_strdup_printf("%s%s", "--br=", bridge);
-            }
-
-            helper_cmd = g_strdup_printf("%s %s %s %s", helper,
-                            "--use-vnet", fd_buf, br_buf ? br_buf : "");
+        if (strstr(helper, "--br=") == NULL) {
+            br_buf = g_strdup_printf("%s%s", "--br=", bridge);
+        }
 
-            parg = args;
-            *parg++ = (char *)"sh";
-            *parg++ = (char *)"-c";
-            *parg++ = helper_cmd;
-            *parg++ = NULL;
+        helper_cmd = g_strdup_printf("%s %s %s %s", helper,
+                        "--use-vnet", fd_buf, br_buf ? br_buf : "");
 
-            execv("/bin/sh", args);
-            g_free(helper_cmd);
-        } else {
-            /* assume helper is just the executable path name */
+        parg = args;
+        *parg++ = "sh";
+        *parg++ = "-c";
+        *parg++ = helper_cmd;
+        *parg++ = NULL;
+    } else {
+        /* assume helper is just the executable path name */
 
-            br_buf = g_strdup_printf("%s%s", "--br=", bridge);
+        br_buf = g_strdup_printf("%s%s", "--br=", bridge);
 
-            parg = args;
-            *parg++ = (char *)helper;
-            *parg++ = (char *)"--use-vnet";
-            *parg++ = fd_buf;
-            *parg++ = br_buf;
-            *parg++ = NULL;
+        parg = args;
+        *parg++ = helper;
+        *parg++ = "--use-vnet";
+        *parg++ = fd_buf;
+        *parg++ = br_buf;
+        *parg++ = NULL;
+    }
 
-            execv(helper, args);
-        }
-        g_free(fd_buf);
-        g_free(br_buf);
-        _exit(1);
+    /* try to launch bridge helper */
+#if GLIB_CHECK_VERSION(2, 68, 0)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+    spawned = g_spawn_async_with_pipes_and_fds(NULL, args, NULL,
+                                               G_SPAWN_DO_NOT_REAP_CHILD |
+                                               G_SPAWN_CHILD_INHERITS_STDIN,
+                                               NULL, NULL, -1, -1, -1,
+                                               &fd, &fd, 1, &pid,
+                                               NULL, NULL, NULL, &error);
+#pragma GCC diagnostic pop
+#else
+    gchar *mutable_args[sizeof(args)];
+
+    for (size_t i = 0; i < ARRAY_SIZE(args); i++) {
+        mutable_args[i] = (gchar *)args[i];
+    }
 
-    } else {
-        int fd;
-        int saved_errno;
+    spawned = g_spawn_async(NULL, mutable_args, NULL,
+                            G_SPAWN_DO_NOT_REAP_CHILD |
+                            G_SPAWN_CHILD_INHERITS_STDIN,
+                            unset_cloexec, GINT_TO_POINTER(sv[1]),
+                            &pid, &error);
+#endif
+    if (!spawned) {
+        error_setg(errp, "could not launch bridge helper: %s", error->message);
+        return -1;
+    }
 
-        close(sv[1]);
+    close(sv[1]);
 
-        fd = RETRY_ON_EINTR(recv_fd(sv[0]));
-        saved_errno = errno;
+    fd = RETRY_ON_EINTR(recv_fd(sv[0]));
+    saved_errno = errno;
 
-        close(sv[0]);
+    close(sv[0]);
 
-        while (waitpid(pid, &status, 0) != pid) {
-            /* loop */
-        }
-        sigprocmask(SIG_SETMASK, &oldmask, NULL);
-        if (fd < 0) {
-            error_setg_errno(errp, saved_errno,
-                             "failed to recv file descriptor");
-            return -1;
-        }
-        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-            error_setg(errp, "bridge helper failed");
-            return -1;
-        }
-        return fd;
+    while (waitpid(pid, &status, 0) != pid) {
+        /* loop */
     }
+    sigprocmask(SIG_SETMASK, &oldmask, NULL);
+    if (fd < 0) {
+        error_setg_errno(errp, saved_errno,
+                            "failed to recv file descriptor");
+        return -1;
+    }
+    if (!g_spawn_check_wait_status(status, &error)) {
+        error_setg(errp, "bridge helper failed: %s", error->message);
+        return -1;
+    }
+    return fd;
 }
 
 int net_init_bridge(const Netdev *netdev, const char *name,
Signature · 1 line
2.47.1
Reviewed-by on v4 2/2
#4 lore raw
Quoted 4 lines · Akihiko Odaki

g_spawn_sync() (for GLib < 2.68) or g_spawn_async_with_pipes_and_fds() (for the newer) gives an informative message if it fails to execute the script instead of reporting exiting status 1.

Quoted 8 lines

g_spawn_check_wait_status() also gives an message easier to understand than the raw value returned by waitpid().

---
 net/tap.c | 220 +++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 117 insertions(+), 103 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|