Make cancellable pipe fds close-on-exec
authorAlexander Larsson <alexl@redhat.com>
Fri, 15 May 2009 08:42:28 +0000 (10:42 +0200)
committerAlexander Larsson <alexl@redhat.com>
Fri, 15 May 2009 08:42:28 +0000 (10:42 +0200)
GCancellable is purely an in-process thing, so ensure that no cancellable
fds accidentally leak to child processes.

gio/gcancellable.c

index 39f8778f7a22f2aab7bbcd65c04d9162efc2099d..589b83a7f09998b44889af81793e3d4f1e2c7ea3 100644 (file)
@@ -188,6 +188,20 @@ set_fd_nonblocking (int fd)
 #endif
 }
 
+static void
+set_fd_close_exec (int fd)
+{
+  int flags;
+
+  flags = fcntl (fd, F_GETFD, 0);
+  if (flags != -1 && (flags & FD_CLOEXEC) == 0)
+    {
+      flags |= FD_CLOEXEC;
+      fcntl (fd, F_SETFD, flags);
+    }
+}
+
+
 static void
 g_cancellable_open_pipe (GCancellable *cancellable)
 {
@@ -198,6 +212,8 @@ g_cancellable_open_pipe (GCancellable *cancellable)
        */
       set_fd_nonblocking (cancellable->cancel_pipe[0]);
       set_fd_nonblocking (cancellable->cancel_pipe[1]);
+      set_fd_close_exec (cancellable->cancel_pipe[0]);
+      set_fd_close_exec (cancellable->cancel_pipe[1]);
     }
   else
     g_warning ("Failed to create pipe for GCancellable. Out of file descriptors?");