Commit 91ce76af authored by Max Kellermann's avatar Max Kellermann

system/FileDescriptor: add methods EnableCloseOnExec(), DisableCloseOnExec()

parent 33bd9e80
/*
* Copyright (C) 2012-2015 Max Kellermann <max.kellermann@gmail.com>
* Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -126,6 +126,24 @@ FileDescriptor::SetBlocking() noexcept
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
}
void
FileDescriptor::EnableCloseOnExec() noexcept
{
assert(IsDefined());
const int old_flags = fcntl(fd, F_GETFD, 0);
fcntl(fd, F_SETFD, old_flags | FD_CLOEXEC);
}
void
FileDescriptor::DisableCloseOnExec() noexcept
{
assert(IsDefined());
const int old_flags = fcntl(fd, F_GETFD, 0);
fcntl(fd, F_SETFD, old_flags & ~FD_CLOEXEC);
}
#endif
#ifdef USE_EVENTFD
......
/*
* Copyright (C) 2012-2015 Max Kellermann <max.kellermann@gmail.com>
* Copyright (C) 2012-2017 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
......@@ -115,6 +115,18 @@ public:
void SetBlocking() noexcept;
/**
* Auto-close this file descriptor when a new program is
* executed.
*/
void EnableCloseOnExec() noexcept;
/**
* Do not auto-close this file descriptor when a new program
* is executed.
*/
void DisableCloseOnExec() noexcept;
/**
* Duplicate the file descriptor onto the given file descriptor.
*/
bool Duplicate(int new_fd) const noexcept {
......
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