Hello everyone,

I'm trying to 'inject' a command into the IPC of two propetiary binaries, 
Program A (a daemon) and Program C.

Program A executes Program C as a child and sends newline-terminated commands 
to it's stdin, Program C responds by sending newline-terminated responses to 
stdout.

Now, I need Program C to process two commands of mine, before servicing 
Program A's commands. So I need a Program B which sends these commands to 
Program C.

At the moment I have the following source for Program B:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/types.h>

int main(){
  pid_t pid;
  char buffer[256];
  int rv;
  int inpipe[2];
  FILE * stream;
  fd_set rfds;
  int retval;

  if(pipe(inpipe)){
    fprintf(stderr,"pipe error!\n");
    exit(1);
  }
  if((pid=fork()) == -1){
    fprintf(stderr,"fork error!\n");
    exit(1);
  }
  if(pid){
    /* parent */

    close(inpipe[0]);

    stream = fdopen( inpipe[1], "w" );
    setbuf (stream, NULL );
    fprintf(stream, "TEST COMMAND\n");

    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    while(waitpid(-1, &rv, WNOHANG) <= 0){
      if (select(1, &rfds, NULL, NULL, NULL)) {
        fgets (buffer, 256, stdin);
        fputs(buffer, stream);
      }
    }
    wait(&rv);
    fprintf(stderr,"child exited with a %d value\n",rv);
  }
  else{
    /* child */
    close(inpipe[1]);
    dup2(inpipe[0],0);
    if(execl("program","program",NULL) == -1){
      fprintf(stderr,"execl error!");
      exit(1);
    }
  }
  return 0;
}

This essentially works, however, in this setup the parent will sit around 
passing it's stdin to the pipe. And of course the select() still blocks if the 
child terminates and there will be nasty broken pipes.

There must be an easier way to accomplish what I need. For instance, the 
parent connecting it's stdin to the pipe, detach the child and exit. Or, the 
parent restoring the stdin of the child to the original stdin(), then setsid() 
the child and exit. In any case, it would be nice if the parent can be 
eliminated after the commands have been sent.

I've been at it for hours now, and this was the best I could get working, so 
I'd appreciate some input.

Thanks for your time!

-- 
Regards,
Terrence Koeman

MediaMonks B.V. (www.mediamonks.com)
Please quote all replies in correspondence.


"The crucial memorandum will be snared in the out-basket by
the paper clip of the overlying memo and go to file."

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to