From: Francois Gouget Subject: [2/6] testbot/testagentd: Child processes should not inherit the server's sockets (and other file descriptors). Message-Id: Date: Mon, 14 Apr 2014 15:45:24 +0200 (CEST) --- This is a resubmit of last saturday's patch 2/3. This patch is independent from all other patches in the series, except 4/6 which depends on it. testbot/src/testagentd/platform_windows.c | 2 +- testbot/src/testagentd/testagentd.c | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/testbot/src/testagentd/platform_windows.c b/testbot/src/testagentd/platform_windows.c index 9dda56a..89e435d 100644 --- a/testbot/src/testagentd/platform_windows.c +++ b/testbot/src/testagentd/platform_windows.c @@ -126,7 +126,7 @@ uint64_t platform_run(char** argv, uint32_t flags, char** redirects) si.hStdInput = fhs[0]; si.hStdOutput = fhs[1]; si.hStdError = fhs[2]; - if (!CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, NORMAL_PRIORITY_CLASS, + if (!CreateProcessA(NULL, cmdline, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi)) { set_status(ST_ERROR, "could not run '%s': %lu", cmdline, GetLastError()); diff --git a/testbot/src/testagentd/testagentd.c b/testbot/src/testagentd/testagentd.c index b8f9c57..230683a 100644 --- a/testbot/src/testagentd/testagentd.c +++ b/testbot/src/testagentd/testagentd.c @@ -1067,7 +1067,7 @@ int main(int argc, char** argv) char* opt_port = NULL; char* opt_srchost = NULL; struct addrinfo *addresses, *addrp; - int rc, addrlen; + int rc, sockflags, addrlen; int opt_usage = 0; SOCKET master; int on = 1; @@ -1171,6 +1171,11 @@ int main(int argc, char** argv) } /* Bind to the host in a protocol neutral way */ +#ifdef SOCK_CLOEXEC + sockflags = SOCK_CLOEXEC; +#else + sockflags = 0; +#endif rc = ta_getaddrinfo(NULL, opt_port, &addresses); if (rc) { @@ -1182,7 +1187,8 @@ int main(int argc, char** argv) debug("trying family=%d\n", addrp->ai_family); if (addrp->ai_family != PF_INET) continue; - master = socket(addrp->ai_family, addrp->ai_socktype, addrp->ai_protocol); + master = socket(addrp->ai_family, addrp->ai_socktype | sockflags, + addrp->ai_protocol); if (master < 0) continue; setsockopt(master, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on)); @@ -1212,6 +1218,9 @@ int main(int argc, char** argv) SOCKET client; debug("Waiting in accept()\n"); client = accept(master, NULL, NULL); +#ifdef O_CLOEXEC + fcntl(client, F_SETFL, fcntl(client, F_GETFL, 0) | O_CLOEXEC); +#endif if (client >= 0) { if (is_host_allowed(client, opt_srchost, addrlen)) -- 1.9.1