From 99b2b958f9f7aab23f5ce2e221ceba824116e5b2 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Thu, 7 Jul 2022 11:34:44 +0300 Subject: [PATCH] shared: introduce os_fd_clear_cloexec() This function will be used between fork() and exec() to remove the close-on-exec flag. The first user will be compositor/xwayland.c. Signed-off-by: Pekka Paalanen --- shared/os-compatibility.c | 15 +++++++++++++++ shared/os-compatibility.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/shared/os-compatibility.c b/shared/os-compatibility.c index 78409d15..a9d91c52 100644 --- a/shared/os-compatibility.c +++ b/shared/os-compatibility.c @@ -40,6 +40,21 @@ #define READONLY_SEALS (F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE) +int +os_fd_clear_cloexec(int fd) +{ + int flags; + + flags = fcntl(fd, F_GETFD); + if (flags == -1) + return -1; + + if (fcntl(fd, F_SETFD, flags & ~(int)FD_CLOEXEC) == -1) + return -1; + + return 0; +} + int os_fd_set_cloexec(int fd) { diff --git a/shared/os-compatibility.h b/shared/os-compatibility.h index 6aaa2688..85f65854 100644 --- a/shared/os-compatibility.h +++ b/shared/os-compatibility.h @@ -30,6 +30,9 @@ #include +int +os_fd_clear_cloexec(int fd); + int os_fd_set_cloexec(int fd);