]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
spawn: remove spawn_and_store_stdout()
authorJaroslav Kysela <perex@perex.cz>
Mon, 17 Nov 2014 13:50:13 +0000 (14:50 +0100)
committerJaroslav Kysela <perex@perex.cz>
Mon, 17 Nov 2014 13:57:46 +0000 (14:57 +0100)
src/epggrab/module.c
src/epggrab/module/xmltv.c
src/file.c
src/spawn.c
src/spawn.h

index dc16392c714ecfc73f05f88c7995314a137c4f96..9f2f71e8bcaff239d188ad8971975366be2bd75b 100644 (file)
@@ -279,7 +279,7 @@ epggrab_module_int_t *epggrab_module_int_create
 
 char *epggrab_module_grab_spawn ( void *m )
 { 
-  int        outlen;
+  int        rd = -1, outlen;
   char       *outbuf;
   epggrab_module_int_t *mod = m;
 
@@ -287,13 +287,24 @@ char *epggrab_module_grab_spawn ( void *m )
   tvhlog(LOG_INFO, mod->id, "grab %s", mod->path);
 
   /* Grab */
-  outlen = spawn_and_store_stdout(mod->path, NULL, &outbuf);
-  if ( outlen < 1 ) {
-    tvhlog(LOG_ERR, mod->id, "no output detected");
-    return NULL;
-  }
+  outlen = spawn_and_give_stdout(mod->path, NULL, &rd, 1);
+
+  if (outlen < 0)
+    goto error;
+
+  outlen = file_readall(rd, &outbuf);
+  if (outlen < 1)
+    goto error;
+
+  close(rd);
 
   return outbuf;
+
+error:
+  if (rd >= 0)
+    close(rd);
+  tvhlog(LOG_ERR, mod->id, "no output detected");
+  return NULL;
 }
 
 
index a697e5c486d3c370e05227e2ba8434fe985ba6eb..8e42448c863d95dc9d14bcc393988fc885aa5724 100755 (executable)
@@ -32,6 +32,7 @@
 #include "tvheadend.h"
 #include "channels.h"
 #include "spawn.h"
+#include "file.h"
 #include "htsstr.h"
 
 #include "lang_str.h"
@@ -675,14 +676,17 @@ static int _xmltv_parse
 
 static void _xmltv_load_grabbers ( void )
 {
-  int outlen;
+  int outlen = -1, rd = -1;
   size_t i, p, n;
   char *outbuf;
   char name[1000];
   char *tmp, *tmp2 = NULL, *path;
 
   /* Load data */
-  outlen = spawn_and_store_stdout(XMLTV_FIND, NULL, &outbuf);
+  if (spawn_and_give_stdout(XMLTV_FIND, NULL, &rd, 1) >= 0)
+    outlen = file_readall(rd, &outbuf);
+  if (rd >= 0)
+    close(rd);
 
   /* Process */
   if ( outlen > 0 ) {
@@ -726,12 +730,18 @@ static void _xmltv_load_grabbers ( void )
           if (stat(bin, &st)) continue;
           if (!(st.st_mode & S_IEXEC)) continue;
           if (!S_ISREG(st.st_mode)) continue;
-          if ((outlen = spawn_and_store_stdout(bin, argv, &outbuf)) > 0) {
+          rd = -1;
+          if (spawn_and_give_stdout(bin, argv, &rd, 1) >= 0 &&
+              (outlen = file_readall(rd, &outbuf)) > 0) {
+            close(rd);
             if (outbuf[outlen-1] == '\n') outbuf[outlen-1] = '\0';
             snprintf(name, sizeof(name), "XMLTV: %s", outbuf);
             epggrab_module_int_create(NULL, bin, name, 3, bin,
                                       NULL, _xmltv_parse, NULL, NULL);
             free(outbuf);
+          } else {
+            if (rd >= 0)
+              close(rd);
           }
         }
         closedir(dir);
index daf9e06fe9e618c61ec7c356c52b2d254dc2c9b5..5043d9a8bb24db8629ec019073e0e0ca0bdf39ef 100644 (file)
@@ -46,8 +46,6 @@ size_t file_readall ( int fd, char **outp )
     totalsize += r;
   } 
 
-  close(fd);
-
   *outp = outbuf;
   if (totalsize == outsize) {
     n = realloc(outbuf, outsize += 1);
index a770351f65451450c7eb254aa0df2644e51b9e9c..0d9e9c628080f2dd41920064ccc6d872a64261fc 100644 (file)
@@ -290,80 +290,6 @@ spawn_enq(const char *name, int pid)
   return s;
 }
 
-/**
- * Execute the given program and return its output in a malloc()ed buffer
- * 
- * *outp will point to the allocated buffer
- * The function will return the size of the buffer
- */
-
-int
-spawn_and_store_stdout(const char *prog, char *argv[], char **outp)
-{
-  pid_t p;
-  int fd[2], f;
-  char bin[256];
-  const char *local_argv[2] = { NULL, NULL };
-
-  if (*prog != '/' && *prog != '.') {
-    if (!find_exec(prog, bin, sizeof(bin))) return -1;
-    prog = bin;
-  }
-
-  if(!argv) argv = (void *)local_argv;
-  if (!argv[0]) argv[0] = (char*)prog;
-
-  pthread_mutex_lock(&fork_lock);
-
-  if(pipe(fd) == -1) {
-    pthread_mutex_unlock(&fork_lock);
-    return -1;
-  }
-
-  p = fork();
-
-  if(p == -1) {
-    pthread_mutex_unlock(&fork_lock);
-    tvherror("spawn", "Unable to fork() for \"%s\" -- %s",
-             prog, strerror(errno));
-    return -1;
-  }
-
-  if(p == 0) {
-    close(0);
-    close(2);
-    close(fd[0]);
-    dup2(fd[1], 1);
-    close(fd[1]);
-
-    f = open("/dev/null", O_RDWR);
-    if(f == -1) {
-      spawn_error("pid %d cannot open /dev/null for redirect %s -- %s",
-                  getpid(), prog, strerror(errno));
-      exit(1);
-    }
-
-    dup2(f, 0);
-    dup2(f, 2);
-    close(f);
-
-    spawn_info("Executing \"%s\"\n", prog);
-
-    execve(prog, argv, environ);
-    spawn_error("pid %d cannot execute %s -- %s\n",
-                getpid(), prog, strerror(errno));
-    exit(1);
-  }
-
-  pthread_mutex_unlock(&fork_lock);
-
-  spawn_enq(prog, p);
-
-  close(fd[1]);
-
-  return file_readall(fd[0], outp);
-}
-
 /**
  * Execute the given program and return its standard output as file-descriptor (pipe).
  */
index b81a1151d7cbd67b599f1e80f32de29d432a4b98..882d4c6940f98f12d8fbaf0ca30731a38ca024e7 100644 (file)
@@ -25,8 +25,6 @@ void spawn_error ( const char *fmt, ... );
 
 int find_exec ( const char *name, char *out, size_t len );
 
-int spawn_and_store_stdout(const char *prog, char *argv[], char **outp);
-
 int spawn_and_give_stdout(const char *prog, char *argv[], int *rd, int redir_stderr);
 
 int spawnv(const char *prog, char *argv[]);