Skip to content

Commit 25dfb54

Browse files
rmacnak-googleCommit Queue
authored andcommitted
[dart:io] Always use O_CLOEXEC instead of FD_CLOEXEC on Linux.
TEST=ci Bug: #60252 Change-Id: Ide6d156d8a7c569984d4fa702dc32ac08f9ee9e1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426580 Reviewed-by: Brian Quinlan <[email protected]> Reviewed-by: Siva Annamalai <[email protected]> Commit-Queue: Ryan Macnak <[email protected]>
1 parent 5d0d9e4 commit 25dfb54

File tree

3 files changed

+6
-34
lines changed

3 files changed

+6
-34
lines changed

runtime/bin/eventhandler_linux.cc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,20 @@ static void AddToEpollInstance(intptr_t epoll_fd_, DescriptorInfo* di) {
7373
EventHandlerImplementation::EventHandlerImplementation()
7474
: socket_map_(&SimpleHashMap::SamePointerValue, 16) {
7575
intptr_t result;
76-
result = NO_RETRY_EXPECTED(pipe(interrupt_fds_));
76+
result = NO_RETRY_EXPECTED(pipe2(interrupt_fds_, O_CLOEXEC));
7777
if (result != 0) {
7878
FATAL("Pipe creation failed");
7979
}
8080
if (!FDUtils::SetNonBlocking(interrupt_fds_[0])) {
8181
FATAL("Failed to set pipe fd non blocking\n");
8282
}
83-
if (!FDUtils::SetCloseOnExec(interrupt_fds_[0])) {
84-
FATAL("Failed to set pipe fd close on exec\n");
85-
}
86-
if (!FDUtils::SetCloseOnExec(interrupt_fds_[1])) {
87-
FATAL("Failed to set pipe fd close on exec\n");
88-
}
8983
shutdown_ = false;
9084
// The initial size passed to epoll_create is ignore on newer (>=
9185
// 2.6.8) Linux versions
92-
const int kEpollInitialSize = 64;
93-
epoll_fd_ = NO_RETRY_EXPECTED(epoll_create(kEpollInitialSize));
86+
epoll_fd_ = NO_RETRY_EXPECTED(epoll_create1(O_CLOEXEC));
9487
if (epoll_fd_ == -1) {
9588
FATAL("Failed creating epoll file descriptor: %i", errno);
9689
}
97-
if (!FDUtils::SetCloseOnExec(epoll_fd_)) {
98-
FATAL("Failed to set epoll fd close on exec\n");
99-
}
10090
// Register the interrupt_fd with the epoll instance.
10191
struct epoll_event event;
10292
event.events = EPOLLIN;

runtime/bin/fdutils_linux.cc

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,8 @@ namespace dart {
1818
namespace bin {
1919

2020
bool FDUtils::SetCloseOnExec(intptr_t fd) {
21-
intptr_t status;
22-
status = NO_RETRY_EXPECTED(fcntl(fd, F_GETFD));
23-
if (status < 0) {
24-
perror("fcntl(F_GETFD) failed");
25-
return false;
26-
}
27-
status |= FD_CLOEXEC;
28-
if (NO_RETRY_EXPECTED(fcntl(fd, F_SETFD, status)) < 0) {
29-
perror("fcntl(F_SETFD, FD_CLOEXEC) failed");
30-
return false;
31-
}
32-
return true;
21+
FATAL("Use O_CLOEXEC instead");
22+
return false;
3323
}
3424

3525
static bool SetBlockingHelper(intptr_t fd, bool blocking) {

runtime/bin/socket_linux.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ intptr_t ServerSocket::Accept(intptr_t fd) {
252252
intptr_t socket;
253253
struct sockaddr clientaddr;
254254
socklen_t addrlen = sizeof(clientaddr);
255-
socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen));
255+
socket = TEMP_FAILURE_RETRY(
256+
accept4(fd, &clientaddr, &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC));
256257
if (socket == -1) {
257258
if (IsTemporaryAcceptError(errno)) {
258259
// We need to signal to the caller that this is actually not an
@@ -261,15 +262,6 @@ intptr_t ServerSocket::Accept(intptr_t fd) {
261262
ASSERT(kTemporaryFailure != -1);
262263
socket = kTemporaryFailure;
263264
}
264-
} else {
265-
if (!FDUtils::SetCloseOnExec(socket)) {
266-
FDUtils::SaveErrorAndClose(socket);
267-
return -1;
268-
}
269-
if (!FDUtils::SetNonBlocking(socket)) {
270-
FDUtils::SaveErrorAndClose(socket);
271-
return -1;
272-
}
273265
}
274266
return socket;
275267
}

0 commit comments

Comments
 (0)