60f91a5
commit 3cf2e4b4f1912d18772a0fa476d4671c25ca2ea4
60f91a5
Author: coolo <coolo@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>
60f91a5
Date:   Mon Mar 2 09:47:26 2009 +0000
60f91a5
60f91a5
    more fixes from Michal Schmidt:
60f91a5
    - don't leak file descriptor to create-env
60f91a5
    - don't use the shell to call simple commands
60f91a5
    
60f91a5
    
60f91a5
    git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/icecream@934044 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
60f91a5
60f91a5
diff --git a/daemon/environment.cpp b/daemon/environment.cpp
60f91a5
index fd38f8e..9dc2831 100644
60f91a5
--- a/daemon/environment.cpp
60f91a5
+++ b/daemon/environment.cpp
60f91a5
@@ -142,40 +142,48 @@ static void list_target_dirs( const string &current_target, const string &target
60f91a5
     closedir( envdir );
60f91a5
 }
60f91a5
 
60f91a5
-bool cleanup_cache( const string &basedir )
60f91a5
+/* Returns true if the child exited with success */
60f91a5
+static bool exec_and_wait( const char *const argv[] )
60f91a5
 {
60f91a5
-    flush_debug();
60f91a5
     pid_t pid = fork();
60f91a5
-    if ( pid )
60f91a5
-    {
60f91a5
-        int status = 0;
60f91a5
+    if ( pid == -1 ) {
60f91a5
+        log_perror("fork");
60f91a5
+        return false;
60f91a5
+    }
60f91a5
+    if ( pid ) {
60f91a5
+        // parent
60f91a5
+        int status;
60f91a5
         while ( waitpid( pid, &status, 0 ) < 0 && errno == EINTR )
60f91a5
             ;
60f91a5
+        return WIFEXITED(status) && WEXITSTATUS(status) == 0;
60f91a5
+    }
60f91a5
+    // child
60f91a5
+    _exit(execv(argv[0], const_cast<char *const *>(argv)));
60f91a5
+}
60f91a5
 
60f91a5
-        if ( mkdir( basedir.c_str(), 0755 ) && errno != EEXIST ) {
60f91a5
-            if ( errno == EPERM )
60f91a5
-                log_error() << "permission denied on mkdir " << basedir << endl;
60f91a5
-            else
60f91a5
-                log_perror( "mkdir in cleanup_cache() failed" );
60f91a5
-            return false;
60f91a5
-        }
60f91a5
-        chown( basedir.c_str(), 0, 0 );
60f91a5
-        chmod( basedir.c_str(), 0755 );
60f91a5
+bool cleanup_cache( const string &basedir )
60f91a5
+{
60f91a5
+    flush_debug();
60f91a5
 
60f91a5
-        return WIFEXITED(status);
60f91a5
-    }
60f91a5
-    // else
60f91a5
-    char **argv;
60f91a5
-    argv = new char*[5];
60f91a5
-    argv[0] = strdup( "/bin/rm" );
60f91a5
-    argv[1] = strdup( "-rf" );
60f91a5
-    argv[2] = strdup( "--" );
60f91a5
     // make sure it ends with '/' to not fall into symlink traps
60f91a5
     string bdir = basedir + '/';
60f91a5
-    argv[3] = strdup( bdir.c_str()  );
60f91a5
-    argv[4] = NULL;
60f91a5
+    const char *const argv[] = {
60f91a5
+        "/bin/rm", "-rf", "--", bdir.c_str(), NULL
60f91a5
+    };
60f91a5
 
60f91a5
-    _exit(execv(argv[0], argv));
60f91a5
+    bool ret = exec_and_wait( argv );
60f91a5
+
60f91a5
+    if ( mkdir( basedir.c_str(), 0755 ) && errno != EEXIST ) {
60f91a5
+        if ( errno == EPERM )
60f91a5
+            log_error() << "permission denied on mkdir " << basedir << endl;
60f91a5
+        else
60f91a5
+            log_perror( "mkdir in cleanup_cache() failed" );
60f91a5
+        return false;
60f91a5
+    }
60f91a5
+    chown( basedir.c_str(), 0, 0 );
60f91a5
+    chmod( basedir.c_str(), 0755 );
60f91a5
+
60f91a5
+    return ret;
60f91a5
 }
60f91a5
 
60f91a5
 Environments available_environmnents(const string &basedir)
60f91a5
@@ -259,7 +267,10 @@ size_t setup_env_cache(const string &basedir, string &native_environment, uid_t
60f91a5
          _exit(1);
60f91a5
     }
60f91a5
 
60f91a5
-    if ( system( BINDIR "/icecc --build-native" ) ) {
60f91a5
+    const char *const argv[] = {
60f91a5
+        BINDIR "/icecc", "--build-native", NULL
60f91a5
+    };
60f91a5
+    if ( !exec_and_wait( argv ) ) {
60f91a5
         log_error() << BINDIR "/icecc --build-native failed\n";
60f91a5
         _exit(1);
60f91a5
     }
60f91a5
diff --git a/services/comm.cpp b/services/comm.cpp
60f91a5
index 47e7304..5ffb790 100644
60f91a5
--- a/services/comm.cpp
60f91a5
+++ b/services/comm.cpp
60f91a5
@@ -987,6 +987,12 @@ open_send_broadcast (void)
60f91a5
       return -1;
60f91a5
     }
60f91a5
 
60f91a5
+  if (fcntl (ask_fd, F_SETFD, FD_CLOEXEC) < 0)
60f91a5
+    {
60f91a5
+      log_perror("open_send_broadcast fcntl");
60f91a5
+      close (ask_fd);
60f91a5
+      return -1;
60f91a5
+    }
60f91a5
   int optval = 1;
60f91a5
   if (setsockopt (ask_fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) < 0)
60f91a5
     {