linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Terrence Koeman" <root@mediamonks.net>
To: linux-c-programming@vger.kernel.org
Subject: Assistance requested with 'injecting' data into the IPC of two propetiary binaries
Date: Fri, 11 Mar 2005 01:56:54 +0100	[thread overview]
Message-ID: <auto-000000700076@mail.mediamonks.net> (raw)

[-- Attachment #1: Type: text/plain, Size: 2474 bytes --]

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 #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3709 bytes --]

                 reply	other threads:[~2005-03-11  0:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auto-000000700076@mail.mediamonks.net \
    --to=root@mediamonks.net \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).