seahorse r2310 - in seahorse-plugins/trunk: . agent



Author: sadam
Date: Sun Jul 27 16:48:21 2008
New Revision: 2310
URL: http://svn.gnome.org/viewvc/seahorse?rev=2310&view=rev

Log:
2008-07-27  Adam Schreiber  <sadam clemson edu>
    
    * agent/seahorse-agent-main.c:
    * agent/seahorse-agent-io.c: Close file descriptors after fork
    Patch from Josselin Mouette.  Fixes bug #544672


Modified:
   seahorse-plugins/trunk/ChangeLog
   seahorse-plugins/trunk/agent/seahorse-agent-io.c
   seahorse-plugins/trunk/agent/seahorse-agent-main.c

Modified: seahorse-plugins/trunk/agent/seahorse-agent-io.c
==============================================================================
--- seahorse-plugins/trunk/agent/seahorse-agent-io.c	(original)
+++ seahorse-plugins/trunk/agent/seahorse-agent-io.c	Sun Jul 27 16:48:21 2008
@@ -29,6 +29,7 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <err.h>
 #include <errno.h>
@@ -107,6 +108,23 @@
 
 /* -------------------------------------------------------------------------- */
 
+static int
+set_cloexec_flag (int fd)
+{
+    int oldflags;
+   
+    oldflags = fcntl (fd, F_GETFD, 0);
+    /* If reading the flags failed, return error indication now.*/
+    if (oldflags < 0)
+    return oldflags;
+    /* Set just the flag we want to set. */
+    oldflags |= FD_CLOEXEC;
+
+    /* Store modified flag word in the descriptor. */
+    return fcntl (fd, F_SETFD, oldflags);
+}
+
+
 /* Create the socket and fill in sockname with its path */
 int
 seahorse_agent_io_socket ()
@@ -151,6 +169,9 @@
         return -1;
     }
 
+    if (set_cloexec_flag (g_socket) < 0)
+        g_warning ("can't set close-on-exec flag: %s", strerror (errno));
+
     /* Bind it to the address */
     if (bind (g_socket, (struct sockaddr *) &addr, len) == -1) {
         g_critical ("couldn't bind to socket: %s: %s", addr.sun_path,

Modified: seahorse-plugins/trunk/agent/seahorse-agent-main.c
==============================================================================
--- seahorse-plugins/trunk/agent/seahorse-agent-main.c	(original)
+++ seahorse-plugins/trunk/agent/seahorse-agent-main.c	Sun Jul 27 16:48:21 2008
@@ -30,6 +30,7 @@
 #include <unistd.h>
 #include <syslog.h>
 #include <fcntl.h>
+#include <string.h>
 #include <errno.h>
   
 #include <glib/gi18n.h>
@@ -71,6 +72,22 @@
     { NULL }
 };
 
+static int
+set_cloexec_flag (int fd)
+{
+    int oldflags;
+    
+    oldflags = fcntl (fd, F_GETFD, 0);
+    /* If reading the flags failed, return error indication now.*/
+    if (oldflags < 0)
+    return oldflags;
+    /* Set just the flag we want to set. */
+    oldflags |= FD_CLOEXEC;
+
+    /* Store modified flag word in the descriptor. */
+    return fcntl (fd, F_SETFD, oldflags);
+}
+
 static void
 daemonize (gchar **exec)
 {
@@ -80,7 +97,7 @@
      */
 
     pid_t pid;
-    int i;
+    int i, fd;
 
     if (agent_no_daemonize) {
         pid = getpid ();
@@ -101,10 +118,18 @@
                 close (i);
             
             /* Open stdin, stdout and stderr. GPGME doesn't work without this */
-            open ("/dev/null", O_RDONLY, 0666);
-            open ("/dev/null", O_WRONLY, 0666);
-            open ("/dev/null", O_WRONLY, 0666);
-
+            fd = open ("/dev/null", O_RDONLY, 0666);
+            if (set_cloexec_flag (fd) < 0)
+                g_warning ("can't set close-on-exec flag: %s", strerror (errno));
+                
+            fd = open ("/dev/null", O_WRONLY, 0666);
+            if (set_cloexec_flag (fd) < 0)
+                g_warning ("can't set close-on-exec flag: %s", strerror (errno));
+                
+            fd = open ("/dev/null", O_WRONLY, 0666);
+            if (set_cloexec_flag (fd) < 0)
+                g_warning ("can't set close-on-exec flag: %s", strerror (errno));
+            
             chdir ("/tmp");
             return; /* Child process returns */
         };



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]