Mark VY
Mark VY

Reputation: 1673

how to detect read() on a pipe or better yet a pseudo-terminal

Suppose I spawn a process and connect its standard input to one end of a pipe I created. (I still write stuff to the other end.) Eventually it will call read() and if the pipe is empty it will then block while waiting for me to put more stuff in there. The OS kernel (mostly care about Linux here) knows exactly when this happens. But it doesn't tell me about it. I'm wondering if any of the following ideas might work to find out when this happens:

  1. Instead of using pipe() to create an anonymous pipe, I could create a named one using mkfifo or similar. Then maybe by using inotify or one of its relatives (dnotify or fanotify) I could get told about every read on this pipe, and then I'll know if/when a read was attempted when the pipe was empty.
  2. If that doesn't work for some reason, I could try to create a named pipe inside a FUSE filesystem, and then all reads to that pipe would be routed to me. (I hope. Is that actually how it works?)

I suspect that at least one of the above tricks works if I want to use a normal pipe. But if the process I'm running is picky, then it might not want to get input from a pipe. Some programs insist on reading at least some inputs from the controlling terminal. This is fine: that's why we have pseudo-terminals. But I think that both of the above tricks break down if I want to know about when a process tries to read from its controlling terminal. There's one more trick I can think of that might work there, but I don't think it does:

  1. Keep putting process into the background. Then reads from its TTY will send it SIGTSTOP, which will suspend it, which is detectable to its parent. But this likely has side effects... for instance, if there was input queued up but not yet read, then I think forcing a process into the background loses that input.

There's also a desperate trick number four:

  1. try to ptrace the spawned child

This idea is annoying because the child may spawn yet more children, so I'd have to trace them too. If any of them were setuid root then the whole thing stops working. And at this point I'm out of ideas.

Upvotes: 0

Views: 19

Answers (0)

Related Questions