0
votes

I've made a daemon that launch commands.

If the execvp function waiting for standard input (with cat, grep, wc, by example), I have to redirect stdin to a named pipe.

Currently, my daemon always open the named pipe and redirect stdin before execvp, I have to manually CTRL+D so that the execution of commands can continue.

I would like to use this pipe only if the command asking for standard input.

Example:

cat | wc -l : I'm opening my named pipe and redirect stdin to it before execvp

ls | wc -l : I don't open the named pipe, execvp pass successfully

Here is how I want my program to work: enter image description here

enter image description here

1
So you want to predict in advance whether the command is going to read standard input or not? - Ian Abbott
Exactly. Or any trick that would permit me to open the pipe (and redirect stdin to it) only if command is going to read standard input. - Romuald Leroux
You could ptrace() the forked process to detect when it reads standard input, but by then it's too late to switch it to read from a pipe instead. - Ian Abbott
I've added details on my post, do you think there is a cleaner/easier solution ? - Romuald Leroux
Does the client tell the daemon which named pipe (if any) to use for standard input? If no pipe is specified, the daemon could open /dev/null and use that as standard input for the command. - Ian Abbott

1 Answers

0
votes

The only way I can see it working is if the client opens its "read" pipe (pipe2 in the picture) in non-blocking mode (and then possibly sets it back to blocking mode after opening) before it sends the request to the daemon and then opens its "write" (pipe1) pipe in normal, blocking mode. Likewise, the daemon opens its "read" pipe (pipe1 in the picture) in non-blocking mode (and then sets it back to blocking mode before running the command) and its "write" pipe (pipe2) in blocking mode. The opens of the "write" pipes will not block if the other end of the same pipe is already open. – Ian Abbott