Commit a13897cf authored by Max Kellermann's avatar Max Kellermann

system/FileDescriptor: add CheckDuplicate()

parent b188ae0e
......@@ -188,6 +188,16 @@ FileDescriptor::DisableCloseOnExec() noexcept
fcntl(fd, F_SETFD, old_flags & ~FD_CLOEXEC);
}
bool
FileDescriptor::CheckDuplicate(int new_fd) noexcept
{
if (fd == new_fd) {
DisableCloseOnExec();
return true;
} else
return Duplicate(new_fd);
}
#endif
#ifdef USE_EVENTFD
......
......@@ -146,6 +146,14 @@ public:
bool Duplicate(int new_fd) const noexcept {
return ::dup2(Get(), new_fd) == 0;
}
/**
* Similar to Duplicate(), but if destination and source file
* descriptor are equal, clear the close-on-exec flag. Use
* this method to inject file descriptors into a new child
* process, to be used by a newly executed program.
*/
bool CheckDuplicate(int new_fd) noexcept;
#endif
#ifdef USE_EVENTFD
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment