Search code examples
pipetraceinject

How to inject logging of communication over pipes


I have a program that execs another program and then communicates with that process over stdin/stdout. I want to inject a small logger that captures the communication, which (I think) is synchronous line-by-line text, propagates the two-way communication but also logs it to a file.

Are there any trivial ways to do this? I've tried to use strace, but it just gives to much noise, and I rather want to capture the whole sequence so I want to start it when the execing takes place.

I was think of creating a small program that just sits in between the caller and the callee and after it has execed the real callee reads from stdin, logs, puts that on the callees stdin, and then waits for something to get back on the callees stdout, log that and then write that back to the caller.

Sounds simple but I was slightly intimidated by setting up the pipe connections and the difficulty to debug such a program. So I thought that I ask here for a simpler solution before putting that effort into it.

I've googled a bit but have a hard time to formulate a good query. Mostly ending up with things like this which is still code and only capturing one-way communications.


Solution

  • Ok, so I bit the bullet and wrote the program...

    We need three child processes, two to monitor, propagate and log the stdin and stdout respectively, and one to exec the real target.

    The tricky part is how to connect the pipes. I learned some basics from a video by Ramesh Yerraballi which enabled me to assemble the pieces.

    Here's the repo for for my implementation of injecting a pipe-spy between two processes communicating over pipes.