diff --git a/src/epggrab/module.c b/src/epggrab/module.c index dc16392c..9f2f71e8 100644 --- a/src/epggrab/module.c +++ b/src/epggrab/module.c @@ -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; } diff --git a/src/epggrab/module/xmltv.c b/src/epggrab/module/xmltv.c index a697e5c4..8e42448c 100755 --- a/src/epggrab/module/xmltv.c +++ b/src/epggrab/module/xmltv.c @@ -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); diff --git a/src/file.c b/src/file.c index daf9e06f..5043d9a8 100644 --- a/src/file.c +++ b/src/file.c @@ -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); diff --git a/src/spawn.c b/src/spawn.c index a770351f..0d9e9c62 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -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). */ diff --git a/src/spawn.h b/src/spawn.h index b81a1151..882d4c69 100644 --- a/src/spawn.h +++ b/src/spawn.h @@ -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[]);