tsocket: Use common code in tsocket_bsd_common_prepare_fd
authorVolker Lendecke <vl@samba.org>
Fri, 5 Jun 2015 09:02:45 +0000 (11:02 +0200)
committerVolker Lendecke <vl@samba.org>
Fri, 5 Jun 2015 12:33:19 +0000 (14:33 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: "Stefan (metze) Metzmacher" <metze@samba.org>
lib/tsocket/tsocket_bsd.c
lib/tsocket/wscript_build

index 79235c6e4034fdc474b81e198fa4d1cfc6391592..5d8f80cc223496a159c415a848fd86982eda07e1 100644 (file)
@@ -27,6 +27,7 @@
 #include "tsocket.h"
 #include "tsocket_internal.h"
 #include "lib/util/iov_buf.h"
+#include "lib/util/blocking.h"
 
 static int tsocket_bsd_error_from_errno(int ret,
                                        int sys_errno,
@@ -84,7 +85,8 @@ static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
        int fds[3];
        int num_fds = 0;
 
-       int result, flags;
+       int result;
+       bool ok;
 
        if (fd == -1) {
                return -1;
@@ -109,40 +111,16 @@ static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
                }
        }
 
-       /* fd should be nonblocking. */
-
-#ifdef O_NONBLOCK
-#define FLAG_TO_SET O_NONBLOCK
-#else
-#ifdef SYSV
-#define FLAG_TO_SET O_NDELAY
-#else /* BSD */
-#define FLAG_TO_SET FNDELAY
-#endif
-#endif
-
-       if ((flags = fcntl(fd, F_GETFL)) == -1) {
+       result = set_blocking(fd, false);
+       if (result == -1) {
                goto fail;
        }
 
-       flags |= FLAG_TO_SET;
-       if (fcntl(fd, F_SETFL, flags) == -1) {
+       ok = smb_set_close_on_exec(fd);
+       if (!ok) {
                goto fail;
        }
 
-#undef FLAG_TO_SET
-
-       /* fd should be closed on exec() */
-#ifdef FD_CLOEXEC
-       result = flags = fcntl(fd, F_GETFD, 0);
-       if (flags >= 0) {
-               flags |= FD_CLOEXEC;
-               result = fcntl(fd, F_SETFD, flags);
-       }
-       if (result < 0) {
-               goto fail;
-       }
-#endif
        return fd;
 
  fail:
index 31ef14ec3d1c9f41d24454016de37adf42159eff..420daff817404774e510e818dc5aeb43803d932c 100644 (file)
@@ -3,7 +3,7 @@
 
 bld.SAMBA_SUBSYSTEM('LIBTSOCKET',
        source='tsocket.c tsocket_helpers.c tsocket_bsd.c',
-       public_deps='talloc tevent iov_buf',
+       public_deps='talloc tevent iov_buf socket-blocking',
        public_headers='tsocket.h tsocket_internal.h',
        )