Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
spawn: remove spawn_and_store_stdout()
  • Loading branch information
perexg committed Nov 17, 2014
1 parent c4f30d1 commit 7c1ff47
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 87 deletions.
23 changes: 17 additions & 6 deletions src/epggrab/module.c
Expand Up @@ -279,21 +279,32 @@ 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;

/* Debug */
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;
}


Expand Down
16 changes: 13 additions & 3 deletions src/epggrab/module/xmltv.c
Expand Up @@ -32,6 +32,7 @@
#include "tvheadend.h"
#include "channels.h"
#include "spawn.h"
#include "file.h"
#include "htsstr.h"

#include "lang_str.h"
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/file.c
Expand Up @@ -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);
Expand Down
74 changes: 0 additions & 74 deletions src/spawn.c
Expand Up @@ -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).
*/
Expand Down
2 changes: 0 additions & 2 deletions src/spawn.h
Expand Up @@ -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[]);
Expand Down

0 comments on commit 7c1ff47

Please sign in to comment.