Patchwork [v2] Don't leak file descriptors

login
register
mail settings
Submitter Kevin Wolf
Date Dec. 2, 2009, 10:34 a.m.
Message ID <4B1642A0.5030701@redhat.com>
Download mbox | patch
Permalink /patch/39981/
State New
Headers show

Comments

Kevin Wolf - Dec. 2, 2009, 10:34 a.m.
Am 01.12.2009 16:55, schrieb Alexander Graf:
> Kevin Wolf wrote:
>> We're leaking file descriptors to child processes. Set FD_CLOEXEC on file
>> descriptors that don't need to be passed to children to stop this misbehaviour.
>>
>> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
>
> On Anthony's staging tree:
> 
> cc1: warnings being treated as errors
> osdep.c: In function ‘qemu_accept’:
> osdep.c:304: error: implicit declaration of function ‘accept4’

Does this one on top work for you?

Kevin

Patch

diff --git a/configure b/configure
index dca5a43..0a1e347 100755
--- a/configure
+++ b/configure
@@ -1550,6 +1550,23 @@  if compile_prog "" "" ; then
   pipe2=yes
 fi
 
+# check if accept4 is there
+accept4=no
+cat > $TMPC << EOF
+#define _GNU_SOURCE
+#include <sys/socket.h>
+#include <stddef.h>
+
+int main(void)
+{
+    accept4(0, NULL, NULL, SOCK_CLOEXEC);
+    return 0;
+}
+EOF
+if compile_prog "" "" ; then
+  accept4=yes
+fi
+
 # check if tee/splice is there. vmsplice was added same time.
 splice=no
 cat > $TMPC << EOF
@@ -2013,6 +2030,9 @@  fi
 if test "$pipe2" = "yes" ; then
   echo "CONFIG_PIPE2=y" >> $config_host_mak
 fi
+if test "$accept4" = "yes" ; then
+  echo "CONFIG_ACCEPT4=y" >> $config_host_mak
+fi
 if test "$splice" = "yes" ; then
   echo "CONFIG_SPLICE=y" >> $config_host_mak
 fi
diff --git a/osdep.c b/osdep.c
index 039065e..7509c5b 100644
--- a/osdep.c
+++ b/osdep.c
@@ -260,7 +260,7 @@  int qemu_pipe(int pipefd[2])
 {
     int ret;
 
-#ifdef O_CLOEXEC
+#ifdef CONFIG_PIPE2
     ret = pipe2(pipefd, O_CLOEXEC);
 #else
     ret = pipe(pipefd);
@@ -300,7 +300,7 @@  int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
 {
     int ret;
 
-#ifdef SOCK_CLOEXEC
+#ifdef CONFIG_ACCEPT4
     ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
 #else
     ret = accept(s, addr, addrlen);