-2

I want to make a proxy process which opens the real one.

Like if I rename linux's espeak to espeak_real and my app to espeak. espeak opens espeak_real and I get the output.

I want to make possible to:

  • Prints it's STDIN to the console
  • Prints it's STDIN to another process's STDIN
  • Prints the second process's STDOUT

I'm trying to do it in C (I guesss it's possible with raw bash too).

blez
  • 4,939
  • 5
  • 50
  • 82
  • maybe this question will help you http://stackoverflow.com/questions/7383803/writing-to-stdin-and-reading-from-stdout-unix-linux-c-programming – Baget May 06 '12 at 19:25

1 Answers1

1

I don't exactly understand what you do, but it seems like a combination of fork, exec, pipe and dup2 should do it.

app can use pipe to get a pair of file descriptors, connected with a pipe (what's written into one is read from the other).
Then it can fork, and the child can exec app_real.
But between fork and exec, dup2 can be used to change any file descriptor you want to 0,1 and 2 (but close the real 0,1,2) first.

Short code example:

int pipe_fds[2];
pipe(pipe_fds);
if (fork()==0) {
    // Child
    close(fds[1]);    // This side is for the parent only
    close(0);         // Close original stdin before dup2
    dup2(fds[0],0);   // Now one side of the pipe is the child's stdin
    close(fds[0]);    // No need to have it open twice
    exec(...);
} else {
    // Parent
    close(fds[0]);            // This side is for the child only
    write(fds[1],data,len);   // This data goes to the child
}
ugoren
  • 16,023
  • 3
  • 35
  • 65
  • linux doesn't have exec(), also I want to write all of STDIN to the child. – blez May 07 '12 at 10:15
  • 1
    Linux has multiple varieties of `exec`, the simplest is `execl`. If you do nothing with `stdin`, the child can just read it (the parent should close it). You can play the `dup2` games with other fds. – ugoren May 07 '12 at 15:41