]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
spawn: close pipe on fork() error path
authorJaroslav Kysela <perex@perex.cz>
Sat, 3 Nov 2018 09:15:41 +0000 (10:15 +0100)
committerJaroslav Kysela <perex@perex.cz>
Sat, 3 Nov 2018 09:15:41 +0000 (10:15 +0100)
src/spawn.c

index 78fa2cba266a20e28153c60d9ec5572d59c3ae5a..41b8e1ae4902b2dc1673cf0c9a2c5188cae28017 100644 (file)
@@ -520,6 +520,8 @@ spawn_and_give_stdout(const char *prog, char *argv[], char *envp[],
 
   if(p == -1) {
     pthread_mutex_unlock(&fork_lock);
+    close(fd[0]);
+    close(fd[1]);
     tvherror(LS_SPAWN, "Unable to fork() for \"%s\" -- %s",
              prog, strerror(errno));
     return -1;
@@ -647,6 +649,8 @@ spawn_with_passthrough(const char *prog, char *argv[], char *envp[],
 
   if(p == -1) {
     pthread_mutex_unlock(&fork_lock);
+    close(fd[0]);
+    close(fd[1]);
     tvherror(LS_SPAWN, "Unable to fork() for \"%s\" -- %s",
              prog, strerror(errno));
     // do not pass the local variable outside
@@ -781,9 +785,15 @@ spawnv(const char *prog, char *argv[], pid_t *pid, int redir_stdout, int redir_s
 
   spawn_enq(prog, p);
 
-  if (pid)
+  if (pid) {
     *pid = p;
 
+    // make the spawned process a session leader so killing the
+    // process group recursively kills any child process that
+    // might have been spawned
+    setpgid(p, p);
+  }
+
   // do not pass the local variable outside
   if (argv[0] == bin)
     argv[0] = NULL;