From 3b56f64923a71a90734c5167d549e4eb14002d18 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 30 Nov 2011 15:17:47 +1100 Subject: [PATCH] lib/util: added set_close_on_exec() this was already in tevent_util.c, but library layering prevented us from using it in some other libraries --- lib/util/blocking.c | 18 ++++++++++++++++++ lib/util/samba_util.h | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/lib/util/blocking.c b/lib/util/blocking.c index f5933cc92b7..9dede7aa0dd 100644 --- a/lib/util/blocking.c +++ b/lib/util/blocking.c @@ -60,3 +60,21 @@ _PUBLIC_ int set_blocking(int fd, bool set) return fcntl( fd, F_SETFL, val); #undef FLAG_TO_SET } + + +_PUBLIC_ bool set_close_on_exec(int fd) +{ +#ifdef FD_CLOEXEC + int val; + + val = fcntl(fd, F_GETFD, 0); + if (val >= 0) { + val |= FD_CLOEXEC; + val = fcntl(fd, F_SETFD, val); + if (val != -1) { + return true; + } + } +#endif + return false; +} diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index 9a76fa9f043..3b5169d7446 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -667,6 +667,11 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid, **/ _PUBLIC_ int set_blocking(int fd, bool set); +/** + set close on exec on a file descriptor if available + **/ +_PUBLIC_ bool set_close_on_exec(int fd); + /** Sleep for a specified number of milliseconds. **/ -- 2.34.1