char *epggrab_module_grab_spawn ( void *m )
{
- int outlen;
+ int rd = -1, outlen;
char *outbuf;
epggrab_module_int_t *mod = 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;
}
#include "tvheadend.h"
#include "channels.h"
#include "spawn.h"
+#include "file.h"
#include "htsstr.h"
#include "lang_str.h"
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 ) {
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);
totalsize += r;
}
- close(fd);
-
*outp = outbuf;
if (totalsize == outsize) {
n = realloc(outbuf, outsize += 1);
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).
*/
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[]);