Skip to content

Commit fe49e55

Browse files
author
John Salem
authored
set CLOEXEC on diagnostic server FDs (#55850)
* set CLOEXEC on diagnostic server FDs * Add assert and comment * fix compiler warning * simplify asserts to appease all the different builds
1 parent 6b6310b commit fe49e55

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/native/eventpipe/ds-ipc-pal-socket.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,22 @@ ipc_socket_create_uds (DiagnosticsIpc *ipc)
316316
EP_ASSERT (ipc->server_address_family == AF_UNIX);
317317

318318
ds_ipc_socket_t new_socket = DS_IPC_INVALID_SOCKET;
319+
int socket_type = SOCK_STREAM;
320+
#ifdef SOCK_CLOEXEC
321+
socket_type |= SOCK_CLOEXEC;
322+
#endif // SOCK_CLOEXEC
319323
DS_ENTER_BLOCKING_PAL_SECTION;
320-
new_socket = socket (ipc->server_address_family, SOCK_STREAM, 0);
324+
new_socket = socket (ipc->server_address_family, socket_type, 0);
325+
#ifndef SOCK_CLOEXEC
326+
if (new_socket != DS_IPC_INVALID_SOCKET) {
327+
if (fcntl (new_socket, F_SETFD, FD_CLOEXEC) == -1) {
328+
EP_ASSERT (!"Failed to set CLOEXEC");
329+
}
330+
}
331+
#endif // SOCK_CLOEXEC
321332
DS_EXIT_BLOCKING_PAL_SECTION;
322333
return new_socket;
323-
#endif
334+
#endif // DS_IPC_PAL_AF_UNIX
324335
return DS_IPC_INVALID_SOCKET;
325336
}
326337

@@ -337,9 +348,18 @@ ipc_socket_create_tcp (DiagnosticsIpc *ipc)
337348
#endif
338349

339350
ds_ipc_socket_t new_socket = DS_IPC_INVALID_SOCKET;
351+
int socket_type = SOCK_STREAM;
352+
#ifdef SOCK_CLOEXEC
353+
socket_type |= SOCK_CLOEXEC;
354+
#endif // SOCK_CLOEXEC
340355
DS_ENTER_BLOCKING_PAL_SECTION;
341-
new_socket = socket (ipc->server_address_family, SOCK_STREAM, IPPROTO_TCP);
356+
new_socket = socket (ipc->server_address_family, socket_type, IPPROTO_TCP);
342357
if (new_socket != DS_IPC_INVALID_SOCKET) {
358+
#ifndef SOCK_CLOEXEC
359+
if (fcntl (new_socket, F_SETFD, FD_CLOEXEC) == -1) {
360+
EP_ASSERT (!"Failed to set CLOEXEC");
361+
}
362+
#endif // SOCK_CLOEXEC
343363
int option_value = 1;
344364
setsockopt (new_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&option_value, sizeof (option_value));
345365
if (ipc->mode == DS_IPC_CONNECTION_MODE_LISTEN) {

0 commit comments

Comments
 (0)