DaanDeMeyer / reproc

A cross-platform (C99/C++11) process library
MIT License
552 stars 65 forks source link

use dup tu duplicate the file descriptor #26

Closed Milerius closed 4 years ago

Milerius commented 4 years ago

instead of take the whole ownership : https://linux.die.net/man/2/dup2

I will check for the equivalent on windows

Milerius commented 4 years ago

https://docs.microsoft.com/fr-fr/windows/win32/api/handleapi/nf-handleapi-duplicatehandle?redirectedfrom=MSDN

the equivalent on windows ^

But I don't have a windows to try it.

DaanDeMeyer commented 4 years ago

I'm not sure why we need to dup though. Can you test the inherit example build/reproc/examples/inherit cmake --help to see if that works?

Milerius commented 4 years ago

It's work but, you are not printing anything in your program.

Let's say you run a program:

You print on STDOUT_FILENO

With the version of reproc::inherit You cannot write anymore on STDOUT_FILENO after the process is launched because it's owned by the child process, that's why we need to duplicate the file descriptor

Milerius commented 4 years ago

The difference is showed in screenshot here: https://github.com/DaanDeMeyer/reproc/issues/20#issuecomment-553273946

DaanDeMeyer commented 4 years ago

Ahah, I found it, I close the fd's inherited by the child after starting the child process but when using stdin/stdout/stderr that obviously shouldn't happen.

Instead of dupping I'm just not going to close in the parent process when redirect is used.

Milerius commented 4 years ago

Ok thank's @DaanDeMeyer Sorry I was just trying to fix it

DaanDeMeyer commented 4 years ago

Actually, using dup is cleaner since we can keep the guarantee that any child fd/handle returned by redirect can (has to) be closed. I'll merge this and add the Windows implementation myself.

Milerius commented 4 years ago

ok thank's !