lib/util: added set_close_on_exec()
authorAndrew Tridgell <tridge@samba.org>
Wed, 30 Nov 2011 04:17:47 +0000 (15:17 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 15 Dec 2011 22:36:22 +0000 (23:36 +0100)
this was already in tevent_util.c, but library layering prevented us
from using it in some other libraries

lib/util/blocking.c
lib/util/samba_util.h

index f5933cc92b71f0071d2a1213eade489970e798ff..9dede7aa0dd63779f630c2e211f625ceb95f0cf2 100644 (file)
@@ -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;
+}
index 9a76fa9f0430f8527078a75dffa80441834d8812..3b5169d74466d48e0553e3e0eb9b21c1e7d99b14 100644 (file)
@@ -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.
 **/