Skip to content

Commit 7627b91

Browse files
committed
Set the close-on-exec flag for libpq's socket to the backend, to avoid
any possible problems from child programs executed by the client app. Per suggestion from Elliot Lee of Red Hat.
1 parent 3fdd33a commit 7627b91

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.285 2004/10/16 22:52:49 tgl Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.286 2004/10/21 20:23:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -18,9 +18,9 @@
1818
#include <sys/types.h>
1919
#include <sys/stat.h>
2020
#include <fcntl.h>
21-
#include <errno.h>
2221
#include <ctype.h>
2322
#include <time.h>
23+
#include <unistd.h>
2424

2525
#ifndef HAVE_STRDUP
2626
#include "strdup.h"
@@ -51,6 +51,11 @@
5151
#include "libpq/ip.h"
5252
#include "mb/pg_wchar.h"
5353

54+
#ifndef FD_CLOEXEC
55+
#define FD_CLOEXEC 1
56+
#endif
57+
58+
5459
#define PGPASSFILE ".pgpass"
5560

5661
/* fall back options if they are not specified by arguments or defined
@@ -766,28 +771,6 @@ update_db_info(PGconn *conn)
766771
#endif /* NOT_USED */
767772

768773

769-
/* ----------
770-
* connectMakeNonblocking -
771-
* Make a connection non-blocking.
772-
* Returns 1 if successful, 0 if not.
773-
* ----------
774-
*/
775-
static int
776-
connectMakeNonblocking(PGconn *conn)
777-
{
778-
if (!set_noblock(conn->sock))
779-
{
780-
char sebuf[256];
781-
782-
printfPQExpBuffer(&conn->errorMessage,
783-
libpq_gettext("could not set socket to non-blocking mode: %s\n"),
784-
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
785-
return 0;
786-
}
787-
788-
return 1;
789-
}
790-
791774
/* ----------
792775
* connectNoDelay -
793776
* Sets the TCP_NODELAY socket option.
@@ -1201,8 +1184,8 @@ PQconnectPoll(PGconn *conn)
12011184

12021185
/*
12031186
* Select socket options: no delay of outgoing data
1204-
* for TCP sockets, and nonblock mode. Fail if this
1205-
* fails.
1187+
* for TCP sockets, nonblock mode, close-on-exec.
1188+
* Fail if any of this fails.
12061189
*/
12071190
if (!IS_AF_UNIX(addr_cur->ai_family))
12081191
{
@@ -1214,13 +1197,29 @@ PQconnectPoll(PGconn *conn)
12141197
continue;
12151198
}
12161199
}
1217-
if (connectMakeNonblocking(conn) == 0)
1200+
if (!set_noblock(conn->sock))
12181201
{
1202+
printfPQExpBuffer(&conn->errorMessage,
1203+
libpq_gettext("could not set socket to non-blocking mode: %s\n"),
1204+
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1205+
closesocket(conn->sock);
1206+
conn->sock = -1;
1207+
conn->addr_cur = addr_cur->ai_next;
1208+
continue;
1209+
}
1210+
1211+
#ifdef F_SETFD
1212+
if (fcntl(conn->sock, F_SETFD, FD_CLOEXEC) == -1)
1213+
{
1214+
printfPQExpBuffer(&conn->errorMessage,
1215+
libpq_gettext("could not set socket to close-on-exec mode: %s\n"),
1216+
SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
12191217
closesocket(conn->sock);
12201218
conn->sock = -1;
12211219
conn->addr_cur = addr_cur->ai_next;
12221220
continue;
12231221
}
1222+
#endif /* F_SETFD */
12241223

12251224
/*
12261225
* Start/make connection. This should not block,

0 commit comments

Comments
 (0)