From ad145bc9cb1103e1c3dc75610ab221a8cbbacc07 Mon Sep 17 00:00:00 2001
From: Craig Tiller <craig.tiller@gmail.com>
Date: Wed, 21 Jan 2015 21:28:27 -0800
Subject: [PATCH] Use the right parameters to syscalls

---
 src/core/iomgr/socket_utils_posix.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/core/iomgr/socket_utils_posix.c b/src/core/iomgr/socket_utils_posix.c
index e8c8071037..06c5033d45 100644
--- a/src/core/iomgr/socket_utils_posix.c
+++ b/src/core/iomgr/socket_utils_posix.c
@@ -50,12 +50,22 @@ int grpc_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
 
   fd = accept(sockfd, addr, addrlen);
   if (fd >= 0) {
-    flags = fcntl(fd, F_GETFL, 0);
-    flags |= nonblock ? O_NONBLOCK : 0;
-    flags |= cloexec ? FD_CLOEXEC : 0;
-    GPR_ASSERT(fcntl(fd, F_SETFL, flags) == 0);
+    if (nonblock) {
+      flags = fcntl(fd, F_GETFL, 0);
+      if (flags < 0) goto close_and_error;
+      if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != 0) goto close_and_error;
+    }
+    if (cloexec) {
+      flags = fcntl(fd, F_GETFD, 0);
+      if (flags < 0) goto close_and_error;
+      if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != 0) goto close_and_error;
+    }
   }
   return fd;
+
+close_and_error:
+  close(fd);
+  return -1;
 }
 
 #endif /* GPR_POSIX_SOCKETUTILS */
-- 
GitLab