[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 44/65] slirp: simplify fork_exec()
From: |
Samuel Thibault |
Subject: |
[Qemu-devel] [PULL 44/65] slirp: simplify fork_exec() |
Date: |
Mon, 14 Jan 2019 01:03:05 +0100 |
From: Marc-André Lureau <address@hidden>
Use g_spawn_async_with_fds() to setup the child.
GSpawn handles reaping the child, and closing parent file descriptors.
Signed-off-by: Marc-André Lureau <address@hidden>
Signed-off-by: Samuel Thibault <address@hidden>
---
slirp/misc.c | 75 +++++++++++++++++++++++++---------------------------
1 file changed, 36 insertions(+), 39 deletions(-)
diff --git a/slirp/misc.c b/slirp/misc.c
index ee3492a2ae..753b3da25b 100644
--- a/slirp/misc.c
+++ b/slirp/misc.c
@@ -129,56 +129,53 @@ err:
return -1;
}
+static void
+fork_exec_child_setup(gpointer data)
+{
+ setsid();
+}
+
int
fork_exec(struct socket *so, const char *ex)
{
- char **argv;
- int opt, c, sp[2];
- pid_t pid;
+ GError *err = NULL;
+ char **argv;
+ int opt, sp[2];
- DEBUG_CALL("fork_exec");
- DEBUG_ARG("so = %p", so);
- DEBUG_ARG("ex = %p", ex);
+ DEBUG_CALL("fork_exec");
+ DEBUG_ARG("so = %p", so);
+ DEBUG_ARG("ex = %p", ex);
if (slirp_socketpair_with_oob(sp) < 0) {
return 0;
}
- pid = fork();
- switch(pid) {
- case -1:
- error_report("Error: fork failed: %s", strerror(errno));
- closesocket(sp[0]);
- closesocket(sp[1]);
- return 0;
-
- case 0:
- setsid();
- dup2(sp[1], 0);
- dup2(sp[1], 1);
- dup2(sp[1], 2);
- for (c = getdtablesize() - 1; c >= 3; c--)
- close(c);
+ argv = g_strsplit(ex, " ", -1);
+ g_spawn_async_with_fds(NULL /* cwd */,
+ argv,
+ NULL /* env */,
+ G_SPAWN_SEARCH_PATH,
+ fork_exec_child_setup, NULL /* data */,
+ NULL /* child_pid */,
+ sp[1], sp[1], sp[1],
+ &err);
+ g_strfreev(argv);
- argv = g_strsplit(ex, " ", -1);
- execvp(argv[0], (char **)argv);
-
- /* Ooops, failed, let's tell the user why */
- fprintf(stderr, "Error: execvp of %s failed: %s\n",
- argv[0], strerror(errno));
- close(0); close(1); close(2); /* XXX */
- exit(1);
+ if (err) {
+ error_report("%s", err->message);
+ g_error_free(err);
+ closesocket(sp[0]);
+ closesocket(sp[1]);
+ return 0;
+ }
- default:
- so->s = sp[0];
- closesocket(sp[1]);
- qemu_add_child_watch(pid);
- socket_set_fast_reuse(so->s);
- opt = 1;
- qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt,
sizeof(int));
- qemu_set_nonblock(so->s);
- return 1;
- }
+ so->s = sp[0];
+ closesocket(sp[1]);
+ socket_set_fast_reuse(so->s);
+ opt = 1;
+ qemu_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
+ qemu_set_nonblock(so->s);
+ return 1;
}
#endif
--
2.20.1
- [Qemu-devel] [PULL 35/65] slirp: improve a bit the debug macros, (continued)
- [Qemu-devel] [PULL 35/65] slirp: improve a bit the debug macros, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 46/65] slirp: Enable fork_exec support on Windows, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 40/65] slirp: remove dead TCP_ACK_HACK code, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 26/65] slirp: remove HAVE_SYS_IOCTL_H, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 23/65] slirp: remove unused HAVE_ARPA_INET_H, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 25/65] slirp: remove unused HAVE_SYS_SELECT_H, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 38/65] slirp: remove unused sbflush(), Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 16/65] slirp: remove PROBE_CONN dead-code, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 29/65] slirp: remove unused HAVE_INET_ATON, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 37/65] slirp: remove #if notdef dead code, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 44/65] slirp: simplify fork_exec(),
Samuel Thibault <=
- [Qemu-devel] [PULL 47/65] slirp: drop <Vista compatibility, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 59/65] slirp: factor out guestfwd addition checks, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 60/65] slirp: add clock_get_ns() callback, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 49/65] slirp: use virtual time for packet expiration, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 53/65] slirp: no need to make DPRINTF conditional on DEBUG, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 62/65] slirp: set G_LOG_DOMAIN, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 50/65] slirp: replace a fprintf with g_critical(), Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 58/65] slirp: replace DEBUG_ARGS with DEBUG_ARG, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 63/65] slirp: call into g_debug() for DEBUG macros, Samuel Thibault, 2019/01/13
- [Qemu-devel] [PULL 55/65] slirp: introduce SLIRP_DEBUG environment variable, Samuel Thibault, 2019/01/13