{
struct addrinfo *addr_cur = conn->addr_cur;
char host_addr[NI_MAXHOST];
+ int sock_type;
/*
* Advance to next possible host, if we've tried all of
conn->connip = strdup(host_addr);
/* Try to create the socket */
- conn->sock = socket(addr_cur->ai_family, SOCK_STREAM, 0);
+ sock_type = SOCK_STREAM;
+#ifdef SOCK_CLOEXEC
+
+ /*
+ * Atomically mark close-on-exec, if possible on this
+ * platform, so that there isn't a window where a
+ * subprogram executed by another thread inherits the
+ * socket. See fallback code below.
+ */
+ sock_type |= SOCK_CLOEXEC;
+#endif
+#ifdef SOCK_NONBLOCK
+
+ /*
+ * We might as well skip a system call for nonblocking
+ * mode too, if we can.
+ */
+ sock_type |= SOCK_NONBLOCK;
+#endif
+ conn->sock = socket(addr_cur->ai_family, sock_type, 0);
if (conn->sock == PGINVALID_SOCKET)
{
int errorno = SOCK_ERRNO;
goto keep_going;
}
}
+#ifndef SOCK_NONBLOCK
if (!pg_set_noblock(conn->sock))
{
libpq_append_conn_error(conn, "could not set socket to nonblocking mode: %s",
conn->try_next_addr = true;
goto keep_going;
}
+#endif
+#ifndef SOCK_CLOEXEC
#ifdef F_SETFD
if (fcntl(conn->sock, F_SETFD, FD_CLOEXEC) == -1)
{
goto keep_going;
}
#endif /* F_SETFD */
+#endif
if (addr_cur->ai_family != AF_UNIX)
{