Skip to content

Commit a40814d

Browse files
committed
Handle invalid libpq sockets in more places
Also, make error messages consistent. From: Michael Paquier <michael.paquier@gmail.com>
1 parent a2fd62d commit a40814d

File tree

7 files changed

+32
-9
lines changed

7 files changed

+32
-9
lines changed

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ libpq_select(int timeout_ms)
331331
if (PQsocket(streamConn) < 0)
332332
ereport(ERROR,
333333
(errcode_for_socket_access(),
334-
errmsg("socket not open")));
334+
errmsg("invalid socket: %s", PQerrorMessage(streamConn))));
335335

336336
/* We use poll(2) if available, otherwise select(2) */
337337
{

src/bin/pg_basebackup/pg_recvlogical.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,14 @@ StreamLogicalLog(void)
360360
struct timeval timeout;
361361
struct timeval *timeoutptr = NULL;
362362

363+
if (PQsocket(conn) < 0)
364+
{
365+
fprintf(stderr,
366+
_("%s: invalid socket: %s"),
367+
progname, PQerrorMessage(conn));
368+
goto error;
369+
}
370+
363371
FD_ZERO(&input_mask);
364372
FD_SET(PQsocket(conn), &input_mask);
365373

src/bin/pg_basebackup/receivelog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@ CopyStreamPoll(PGconn *conn, long timeout_ms)
956956

957957
if (PQsocket(conn) < 0)
958958
{
959-
fprintf(stderr, _("%s: socket not open"), progname);
959+
fprintf(stderr, _("%s: invalid socket: %s"), progname,
960+
PQerrorMessage(conn));
960961
return -1;
961962
}
962963

src/bin/pgbench/pgbench.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3797,7 +3797,7 @@ threadRun(void *arg)
37973797
sock = PQsocket(st->con);
37983798
if (sock < 0)
37993799
{
3800-
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
3800+
fprintf(stderr, "invalid socket: %s", PQerrorMessage(st->con));
38013801
goto done;
38023802
}
38033803

@@ -3867,7 +3867,8 @@ threadRun(void *arg)
38673867

38683868
if (sock < 0)
38693869
{
3870-
fprintf(stderr, "bad socket: %s", PQerrorMessage(st->con));
3870+
fprintf(stderr, "invalid socket: %s",
3871+
PQerrorMessage(st->con));
38713872
goto done;
38723873
}
38733874
if (FD_ISSET(sock, &input_mask) ||

src/bin/scripts/vacuumdb.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void DisconnectDatabase(ParallelSlot *slot);
7070

7171
static int select_loop(int maxFd, fd_set *workerset, bool *aborting);
7272

73-
static void init_slot(ParallelSlot *slot, PGconn *conn);
73+
static void init_slot(ParallelSlot *slot, PGconn *conn, const char *progname);
7474

7575
static void help(const char *progname);
7676

@@ -421,14 +421,14 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
421421
* array contains the connection.
422422
*/
423423
slots = (ParallelSlot *) pg_malloc(sizeof(ParallelSlot) * concurrentCons);
424-
init_slot(slots, conn);
424+
init_slot(slots, conn, progname);
425425
if (parallel)
426426
{
427427
for (i = 1; i < concurrentCons; i++)
428428
{
429429
conn = connectDatabase(dbname, host, port, username, prompt_password,
430430
progname, false, true);
431-
init_slot(slots + i, conn);
431+
init_slot(slots + i, conn, progname);
432432
}
433433
}
434434

@@ -917,11 +917,18 @@ select_loop(int maxFd, fd_set *workerset, bool *aborting)
917917
}
918918

919919
static void
920-
init_slot(ParallelSlot *slot, PGconn *conn)
920+
init_slot(ParallelSlot *slot, PGconn *conn, const char *progname)
921921
{
922922
slot->connection = conn;
923923
slot->isFree = true;
924924
slot->sock = PQsocket(conn);
925+
926+
if (slot->sock < 0)
927+
{
928+
fprintf(stderr, _("%s: invalid socket: %s"), progname,
929+
PQerrorMessage(conn));
930+
exit(1);
931+
}
925932
}
926933

927934
static void

src/interfaces/libpq/fe-misc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
10581058
if (conn->sock == PGINVALID_SOCKET)
10591059
{
10601060
printfPQExpBuffer(&conn->errorMessage,
1061-
libpq_gettext("socket not open\n"));
1061+
libpq_gettext("invalid socket\n"));
10621062
return -1;
10631063
}
10641064

src/test/isolation/isolationtester.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,12 @@ try_complete_step(Step *step, int flags)
705705
PGresult *res;
706706
bool canceled = false;
707707

708+
if (sock < 0)
709+
{
710+
fprintf(stderr, "invalid socket: %s", PQerrorMessage(conn));
711+
exit_nicely();
712+
}
713+
708714
gettimeofday(&start_time, NULL);
709715
FD_ZERO(&read_set);
710716

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy