system/FileDescriptor: add CheckDuplicate()

This commit is contained in:
Max Kellermann 2017-09-13 17:37:28 +02:00
parent b188ae0e5c
commit a13897cf6f
2 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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