Handle invalid libpq sockets in more places
authorPeter Eisentraut
Wed, 9 Mar 2016 02:10:33 +0000 (21:10 -0500)
committerPeter Eisentraut
Wed, 9 Mar 2016 02:10:33 +0000 (21:10 -0500)
Also, make error messages consistent.

From: Michael Paquier 

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
src/bin/pg_basebackup/pg_recvlogical.c
src/bin/pg_basebackup/receivelog.c
src/bin/pgbench/pgbench.c
src/bin/scripts/vacuumdb.c
src/interfaces/libpq/fe-misc.c
src/test/isolation/isolationtester.c

index f6709575dee45144c0aac59de07f6a9cc543e320..4ee4d7106da91b4764f3a41300dab0e1d303efce 100644 (file)
@@ -331,7 +331,7 @@ libpq_select(int timeout_ms)
    if (PQsocket(streamConn) < 0)
        ereport(ERROR,
                (errcode_for_socket_access(),
-                errmsg("socket not open")));
+                errmsg("invalid socket: %s", PQerrorMessage(streamConn))));
 
    /* We use poll(2) if available, otherwise select(2) */
    {
index 832f9f9141e729b99a32bf2b046be75036b4c16f..6d1270508d5b9b1169b121fe931ae08b70f0d2af 100644 (file)
@@ -360,6 +360,14 @@ StreamLogicalLog(void)
            struct timeval timeout;
            struct timeval *timeoutptr = NULL;
 
+           if (PQsocket(conn) < 0)
+           {
+               fprintf(stderr,
+                       _("%s: invalid socket: %s"),
+                       progname, PQerrorMessage(conn));
+               goto error;
+           }
+
            FD_ZERO(&input_mask);
            FD_SET(PQsocket(conn), &input_mask);
 
index 6d7e635b0e41e383b59ab1f20c7cf4f9ed5a4e29..01c42fc063914cae520fb3ca72f06d7522cee669 100644 (file)
@@ -956,7 +956,8 @@ CopyStreamPoll(PGconn *conn, long timeout_ms)
 
    if (PQsocket(conn) < 0)
    {
-       fprintf(stderr, _("%s: socket not open"), progname);
+       fprintf(stderr, _("%s: invalid socket: %s"), progname,
+               PQerrorMessage(conn));
        return -1;
    }
 
index 8b0b17a74ce48cad82f7be0431eb1c9af5e05284..92df7504ad57e5aafe0cff3c424d220db4d70647 100644 (file)
@@ -3797,7 +3797,7 @@ threadRun(void *arg)
            sock = PQsocket(st->con);
            if (sock < 0)
            {
-               fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
+               fprintf(stderr, "invalid socket: %s", PQerrorMessage(st->con));
                goto done;
            }
 
@@ -3867,7 +3867,8 @@ threadRun(void *arg)
 
                if (sock < 0)
                {
-                   fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
+                   fprintf(stderr, "invalid socket: %s",
+                           PQerrorMessage(st->con));
                    goto done;
                }
                if (FD_ISSET(sock, &input_mask) ||
index c6afcd515cbb9c941fc9b08c01541fa4162953d3..b673be83ff16830f170136e81a856b2bd6118866 100644 (file)
@@ -70,7 +70,7 @@ static void DisconnectDatabase(ParallelSlot *slot);
 
 static int select_loop(int maxFd, fd_set *workerset, bool *aborting);
 
-static void init_slot(ParallelSlot *slot, PGconn *conn);
+static void init_slot(ParallelSlot *slot, PGconn *conn, const char *progname);
 
 static void help(const char *progname);
 
@@ -421,14 +421,14 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
     * array contains the connection.
     */
    slots = (ParallelSlot *) pg_malloc(sizeof(ParallelSlot) * concurrentCons);
-   init_slot(slots, conn);
+   init_slot(slots, conn, progname);
    if (parallel)
    {
        for (i = 1; i < concurrentCons; i++)
        {
            conn = connectDatabase(dbname, host, port, username, prompt_password,
                                   progname, false, true);
-           init_slot(slots + i, conn);
+           init_slot(slots + i, conn, progname);
        }
    }
 
@@ -917,11 +917,18 @@ select_loop(int maxFd, fd_set *workerset, bool *aborting)
 }
 
 static void
-init_slot(ParallelSlot *slot, PGconn *conn)
+init_slot(ParallelSlot *slot, PGconn *conn, const char *progname)
 {
    slot->connection = conn;
    slot->isFree = true;
    slot->sock = PQsocket(conn);
+
+   if (slot->sock < 0)
+   {
+       fprintf(stderr, _("%s: invalid socket: %s"), progname,
+               PQerrorMessage(conn));
+       exit(1);
+   }
 }
 
 static void
index 30cee7f053cec281d1c7740fe920f44367851eef..32da8ca4616b55dc7a8494b04a28afc8eddbeac7 100644 (file)
@@ -1058,7 +1058,7 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
    if (conn->sock == PGINVALID_SOCKET)
    {
        printfPQExpBuffer(&conn->errorMessage,
-                         libpq_gettext("socket not open\n"));
+                         libpq_gettext("invalid socket\n"));
        return -1;
    }
 
index 6461ae8f81534fb00ef2d93593367af13c614bf0..2969ce9470d64c4e41b1612de0eceaa27f99041d 100644 (file)
@@ -705,6 +705,12 @@ try_complete_step(Step *step, int flags)
    PGresult   *res;
    bool        canceled = false;
 
+   if (sock < 0)
+   {
+       fprintf(stderr, "invalid socket: %s", PQerrorMessage(conn));
+       exit_nicely();
+   }
+
    gettimeofday(&start_time, NULL);
    FD_ZERO(&read_set);