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

This commit is contained in:
Max Kellermann 2017-08-10 11:54:26 +02:00
parent 33bd9e80bb
commit 91ce76af9d
2 changed files with 32 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* 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

View File

@ -1,5 +1,5 @@
/*
* 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
@ -114,6 +114,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.
*/