This function is going to be used on FDs inherited from parent. Parent can't set O_CLOEXEC obviously, so we have to set it ourselves. Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com> --- util.c | 17 +++++++++++++++++ util.h | 1 + 2 files changed, 18 insertions(+) diff --git a/util.c b/util.c index 1d00404..9817c74 100644 --- a/util.c +++ b/util.c @@ -526,6 +526,23 @@ int write_file(const char *path, const char *buf) return len == 0 ? 0 : -1; } +/** + * set_cloexec() - Set Close-on-exec flag on given FD + * @fd: FD to set the flag on + * + * Return: 0 on success, -1 on any error + */ +int set_cloexec(int fd) +{ + int fflags; + if ((fflags = fcntl(fd, F_GETFD)) < 0) + return -1; + fflags |= FD_CLOEXEC; + if ((fcntl(fd, F_SETFD, fflags)) < 0) + return -1; + return 0; +} + #ifdef __ia64__ /* Needed by do_clone() below: glibc doesn't export the prototype of __clone2(), * use the description from clone(2). diff --git a/util.h b/util.h index 26892aa..3db901d 100644 --- a/util.h +++ b/util.h @@ -222,5 +222,6 @@ void write_pidfile(int fd, pid_t pid); int __daemon(int pidfile_fd, int devnull_fd); int fls(unsigned long x); int write_file(const char *path, const char *buf); +int set_cloexec(int fd); #endif /* UTIL_H */ -- 2.39.3