Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
spawn: support sending data to the child process stdin
  • Loading branch information
john-tornblom committed Jan 28, 2014
1 parent 6f1b17e commit 635ce0a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/epggrab/module.c
Expand Up @@ -278,7 +278,7 @@ 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);
outlen = spawn_and_store_stdout(mod->path, NULL, NULL, &outbuf);
if ( outlen < 1 ) {
tvhlog(LOG_ERR, mod->id, "no output detected");
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/epggrab/module/xmltv.c
Expand Up @@ -687,7 +687,7 @@ static void _xmltv_load_grabbers ( void )
char *tmp, *tmp2 = NULL, *path;

/* Load data */
outlen = spawn_and_store_stdout(XMLTV_FIND, NULL, &outbuf);
outlen = spawn_and_store_stdout(XMLTV_FIND, NULL, NULL, &outbuf);

/* Process */
if ( outlen > 0 ) {
Expand Down Expand Up @@ -731,7 +731,7 @@ 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) {
if ((outlen = spawn_and_store_stdout(bin, argv, NULL, &outbuf)) > 0) {
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,
Expand Down
42 changes: 29 additions & 13 deletions src/spawn.c
Expand Up @@ -153,13 +153,19 @@ spawn_enq(const char *name, int pid)
*/

int
spawn_and_store_stdout(const char *prog, char *argv[], char **outp)
spawn_and_store_stdout(const char *prog, char *argv[], char *in, char **outp)
{
pid_t p;
int fd[2], f;
int fd[2][2], f;
char bin[256];
const char *local_argv[2] = { NULL, NULL };

#define PARENT_READ fd[0][0]
#define CHILD_WRITE fd[0][1]

#define CHILD_READ fd[1][0]
#define PARENT_WRITE fd[1][1]

if (*prog != '/' && *prog != '.') {
if (!find_exec(prog, bin, sizeof(bin))) return -1;
prog = bin;
Expand All @@ -170,7 +176,7 @@ spawn_and_store_stdout(const char *prog, char *argv[], char **outp)

pthread_mutex_lock(&fork_lock);

if(pipe(fd) == -1) {
if(pipe(fd[0]) == -1 || pipe(fd[1]) == -1) {
pthread_mutex_unlock(&fork_lock);
return -1;
}
Expand All @@ -184,12 +190,16 @@ spawn_and_store_stdout(const char *prog, char *argv[], char **outp)
return -1;
}

if(p == 0) {
close(0);
close(2);
close(fd[0]);
dup2(fd[1], 1);
close(fd[1]);
if(p == 0) { /* in the child */
close(STDERR_FILENO);
close(PARENT_READ);
close(PARENT_WRITE);

dup2(CHILD_READ, STDIN_FILENO);
close(CHILD_READ);

dup2(CHILD_WRITE, STDOUT_FILENO);
close(CHILD_WRITE);

f = open("/dev/null", O_RDWR);
if(f == -1) {
Expand All @@ -199,8 +209,7 @@ spawn_and_store_stdout(const char *prog, char *argv[], char **outp)
exit(1);
}

dup2(f, 0);
dup2(f, 2);
dup2(f, STDERR_FILENO);
close(f);

execve(prog, argv, environ);
Expand All @@ -213,9 +222,16 @@ spawn_and_store_stdout(const char *prog, char *argv[], char **outp)

spawn_enq(prog, p);

close(fd[1]);
close(CHILD_READ);
close(CHILD_WRITE);

if(in && write(PARENT_WRITE, in, strlen(in)) < 0)
syslog(LOG_ERR,
"spawn: pid %d cannot write %s -- %s",
getpid(), prog, strerror(errno));

return file_readall(fd[0], outp);
close(PARENT_WRITE);
return file_readall(PARENT_READ, outp);
}


Expand Down
3 changes: 2 additions & 1 deletion src/spawn.h
Expand Up @@ -21,7 +21,8 @@

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_store_stdout(const char *prog, char *argv[], char *in,
char **outp);

int spawnv(const char *prog, char *argv[]);

Expand Down

0 comments on commit 635ce0a

Please sign in to comment.