0

I am porting a code from windows to linux. I have this code

// We want a non-inherited read handle. DuplicateHandle with a
    // NULL target fixes the read side to be non-inheritable
    ::DuplicateHandle(::GetCurrentProcess(),    // in this process
               hStdOutR,           // child read handle
               ::GetCurrentProcess(),    // to this process
               NULL,                     // modify existing handle
               0,                        // flags
               FALSE,                    // not inheritable
               DUPLICATE_SAME_ACCESS);   // same handle access*/

If I want the same functionality to be achieved in Linux, what could be done?

GSL
  • 11
  • 1
  • What does it do exactly? – Kevin C Nov 16 '22 at 06:40
  • `dup` followed by [`fcntl(... FD_CLOEXEC)`](https://stackoverflow.com/questions/6125068/what-does-the-fd-cloexec-fcntl-flag-do). There may be a variant that avoids the small window between `dup` and `fcntl` where the file descriptor is inheritable. – j6t Nov 16 '22 at 06:47
  • @j6t `dup3()` takes flags in third argument. – dimich Nov 16 '22 at 07:32
  • Reading through the [docs of DuplicateHandle](https://learn.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle) makes me wonder whether the code does what the comments suggest it does. I.e., I think the code just leaks a handle, but does not make the original non-inheritable. – j6t Nov 16 '22 at 08:40

0 Answers0