PostgreSQL Source Code  git master
fe-misc.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include "libpq-fe.h"
#include "libpq-int.h"
#include "mb/pg_wchar.h"
#include "pg_config_paths.h"
#include "port/pg_bswap.h"
Include dependency graph for fe-misc.c:

Go to the source code of this file.

Functions

static int pqPutMsgBytes (const void *buf, size_t len, PGconn *conn)
 
static int pqSendSome (PGconn *conn, int len)
 
static int pqSocketCheck (PGconn *conn, int forRead, int forWrite, time_t end_time)
 
static int pqSocketPoll (int sock, int forRead, int forWrite, time_t end_time)
 
int PQlibVersion (void)
 
int pqGetc (char *result, PGconn *conn)
 
int pqPutc (char c, PGconn *conn)
 
static int pqGets_internal (PQExpBuffer buf, PGconn *conn, bool resetbuffer)
 
int pqGets (PQExpBuffer buf, PGconn *conn)
 
int pqGets_append (PQExpBuffer buf, PGconn *conn)
 
int pqPuts (const char *s, PGconn *conn)
 
int pqGetnchar (char *s, size_t len, PGconn *conn)
 
int pqSkipnchar (size_t len, PGconn *conn)
 
int pqPutnchar (const char *s, size_t len, PGconn *conn)
 
int pqGetInt (int *result, size_t bytes, PGconn *conn)
 
int pqPutInt (int value, size_t bytes, PGconn *conn)
 
int pqCheckOutBufferSpace (size_t bytes_needed, PGconn *conn)
 
int pqCheckInBufferSpace (size_t bytes_needed, PGconn *conn)
 
int pqPutMsgStart (char msg_type, bool force_len, PGconn *conn)
 
int pqPutMsgEnd (PGconn *conn)
 
int pqReadData (PGconn *conn)
 
int pqFlush (PGconn *conn)
 
int pqWait (int forRead, int forWrite, PGconn *conn)
 
int pqWaitTimed (int forRead, int forWrite, PGconn *conn, time_t finish_time)
 
int pqReadReady (PGconn *conn)
 
int pqWriteReady (PGconn *conn)
 
int PQmblen (const char *s, int encoding)
 
int PQdsplen (const char *s, int encoding)
 
int PQenv2encoding (void)
 

Function Documentation

◆ pqCheckInBufferSpace()

int pqCheckInBufferSpace ( size_t  bytes_needed,
PGconn conn 
)

Definition at line 394 of file fe-misc.c.

References pg_conn::errorMessage, pg_conn::inBuffer, pg_conn::inBufSize, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, printfPQExpBuffer(), and realloc.

Referenced by getCopyDataMessage(), PQconnectPoll(), pqFunctionCall3(), pqParseInput3(), and pqReadData().

395 {
396  int newsize = conn->inBufSize;
397  char *newbuf;
398 
399  /* Quick exit if we have enough space */
400  if (bytes_needed <= (size_t) newsize)
401  return 0;
402 
403  /*
404  * Before concluding that we need to enlarge the buffer, left-justify
405  * whatever is in it and recheck. The caller's value of bytes_needed
406  * includes any data to the left of inStart, but we can delete that in
407  * preference to enlarging the buffer. It's slightly ugly to have this
408  * function do this, but it's better than making callers worry about it.
409  */
410  bytes_needed -= conn->inStart;
411 
412  if (conn->inStart < conn->inEnd)
413  {
414  if (conn->inStart > 0)
415  {
416  memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
417  conn->inEnd - conn->inStart);
418  conn->inEnd -= conn->inStart;
419  conn->inCursor -= conn->inStart;
420  conn->inStart = 0;
421  }
422  }
423  else
424  {
425  /* buffer is logically empty, reset it */
426  conn->inStart = conn->inCursor = conn->inEnd = 0;
427  }
428 
429  /* Recheck whether we have enough space */
430  if (bytes_needed <= (size_t) newsize)
431  return 0;
432 
433  /*
434  * If we need to enlarge the buffer, we first try to double it in size; if
435  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
436  * the malloc pool by repeated small enlargements.
437  *
438  * Note: tests for newsize > 0 are to catch integer overflow.
439  */
440  do
441  {
442  newsize *= 2;
443  } while (newsize > 0 && bytes_needed > (size_t) newsize);
444 
445  if (newsize > 0 && bytes_needed <= (size_t) newsize)
446  {
447  newbuf = realloc(conn->inBuffer, newsize);
448  if (newbuf)
449  {
450  /* realloc succeeded */
451  conn->inBuffer = newbuf;
452  conn->inBufSize = newsize;
453  return 0;
454  }
455  }
456 
457  newsize = conn->inBufSize;
458  do
459  {
460  newsize += 8192;
461  } while (newsize > 0 && bytes_needed > (size_t) newsize);
462 
463  if (newsize > 0 && bytes_needed <= (size_t) newsize)
464  {
465  newbuf = realloc(conn->inBuffer, newsize);
466  if (newbuf)
467  {
468  /* realloc succeeded */
469  conn->inBuffer = newbuf;
470  conn->inBufSize = newsize;
471  return 0;
472  }
473  }
474 
475  /* realloc failed. Probably out of memory */
477  "cannot allocate memory for input buffer\n");
478  return EOF;
479 }
int inEnd
Definition: libpq-int.h:448
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
int inStart
Definition: libpq-int.h:446
int inBufSize
Definition: libpq-int.h:445
char * inBuffer
Definition: libpq-int.h:444
PQExpBufferData errorMessage
Definition: libpq-int.h:526
#define realloc(a, b)
Definition: header.h:60
int inCursor
Definition: libpq-int.h:447

◆ pqCheckOutBufferSpace()

int pqCheckOutBufferSpace ( size_t  bytes_needed,
PGconn conn 
)

Definition at line 330 of file fe-misc.c.

References pg_conn::errorMessage, pg_conn::outBuffer, pg_conn::outBufSize, printfPQExpBuffer(), and realloc.

Referenced by PQputCopyData(), pqPutMsgBytes(), and pqPutMsgStart().

331 {
332  int newsize = conn->outBufSize;
333  char *newbuf;
334 
335  /* Quick exit if we have enough space */
336  if (bytes_needed <= (size_t) newsize)
337  return 0;
338 
339  /*
340  * If we need to enlarge the buffer, we first try to double it in size; if
341  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
342  * the malloc pool by repeated small enlargements.
343  *
344  * Note: tests for newsize > 0 are to catch integer overflow.
345  */
346  do
347  {
348  newsize *= 2;
349  } while (newsize > 0 && bytes_needed > (size_t) newsize);
350 
351  if (newsize > 0 && bytes_needed <= (size_t) newsize)
352  {
353  newbuf = realloc(conn->outBuffer, newsize);
354  if (newbuf)
355  {
356  /* realloc succeeded */
357  conn->outBuffer = newbuf;
358  conn->outBufSize = newsize;
359  return 0;
360  }
361  }
362 
363  newsize = conn->outBufSize;
364  do
365  {
366  newsize += 8192;
367  } while (newsize > 0 && bytes_needed > (size_t) newsize);
368 
369  if (newsize > 0 && bytes_needed <= (size_t) newsize)
370  {
371  newbuf = realloc(conn->outBuffer, newsize);
372  if (newbuf)
373  {
374  /* realloc succeeded */
375  conn->outBuffer = newbuf;
376  conn->outBufSize = newsize;
377  return 0;
378  }
379  }
380 
381  /* realloc failed. Probably out of memory */
383  "cannot allocate memory for output buffer\n");
384  return EOF;
385 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
PQExpBufferData errorMessage
Definition: libpq-int.h:526
int outBufSize
Definition: libpq-int.h:452
#define realloc(a, b)
Definition: header.h:60
char * outBuffer
Definition: libpq-int.h:451

◆ PQdsplen()

int PQdsplen ( const char *  s,
int  encoding 
)

Definition at line 1241 of file fe-misc.c.

References pg_encoding_dsplen().

Referenced by get_prompt(), pg_wcsformat(), pg_wcssize(), pg_wcswidth(), and strlen_max_width().

1242 {
1243  return pg_encoding_dsplen(encoding, s);
1244 }
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:1565
int32 encoding
Definition: pg_database.h:41

◆ PQenv2encoding()

int PQenv2encoding ( void  )

Definition at line 1250 of file fe-misc.c.

References dgettext, dngettext, encoding, libpq_gettext, libpq_ngettext, pg_char_to_encoding(), PG_SQL_ASCII, PG_TEXTDOMAIN, and generate_unaccent_rules::str.

Referenced by main().

1251 {
1252  char *str;
1253  int encoding = PG_SQL_ASCII;
1254 
1255  str = getenv("PGCLIENTENCODING");
1256  if (str && *str != '\0')
1257  {
1258  encoding = pg_char_to_encoding(str);
1259  if (encoding < 0)
1260  encoding = PG_SQL_ASCII;
1261  }
1262  return encoding;
1263 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:550
int32 encoding
Definition: pg_database.h:41

◆ pqFlush()

int pqFlush ( PGconn conn)

Definition at line 1012 of file fe-misc.c.

References pg_conn::outCount, pg_conn::Pfdebug, and pqSendSome().

Referenced by pg_SASL_init(), PQconnectPoll(), PQconsumeInput(), pqEndcopy2(), pqEndcopy3(), PQflush(), pqFunctionCall2(), pqFunctionCall3(), PQgetResult(), pqPacketSend(), PQputCopyData(), PQputCopyEnd(), PQsendDescribe(), PQsendPrepare(), PQsendQuery(), PQsendQueryGuts(), PQsetnonblocking(), and sendTerminateConn().

1013 {
1014  if (conn->Pfdebug)
1015  fflush(conn->Pfdebug);
1016 
1017  if (conn->outCount > 0)
1018  return pqSendSome(conn, conn->outCount);
1019 
1020  return 0;
1021 }
int outCount
Definition: libpq-int.h:453
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqSendSome(PGconn *conn, int len)
Definition: fe-misc.c:835

◆ pqGetc()

int pqGetc ( char *  result,
PGconn conn 
)

Definition at line 80 of file fe-misc.c.

References fprintf, pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, and pg_conn::Pfdebug.

Referenced by getCopyDataMessage(), getCopyStart(), getReadyForQuery(), PQconnectPoll(), pqFunctionCall2(), pqFunctionCall3(), pqGetErrorNotice3(), pqParseInput2(), and pqParseInput3().

81 {
82  if (conn->inCursor >= conn->inEnd)
83  return EOF;
84 
85  *result = conn->inBuffer[conn->inCursor++];
86 
87  if (conn->Pfdebug)
88  fprintf(conn->Pfdebug, "From backend> %c\n", *result);
89 
90  return 0;
91 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
char * inBuffer
Definition: libpq-int.h:444
int inCursor
Definition: libpq-int.h:447

◆ pqGetInt()

int pqGetInt ( int *  result,
size_t  bytes,
PGconn conn 
)

Definition at line 253 of file fe-misc.c.

References fprintf, pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, pg_conn::noticeHooks, pg_conn::Pfdebug, pg_ntoh16, pg_ntoh32, and pqInternalNotice().

Referenced by getAnotherTuple(), getCopyDataMessage(), getCopyStart(), getNotify(), getParamDescriptions(), getRowDescriptions(), PQconnectPoll(), pqFunctionCall2(), pqFunctionCall3(), pqParseInput2(), and pqParseInput3().

254 {
255  uint16 tmp2;
256  uint32 tmp4;
257 
258  switch (bytes)
259  {
260  case 2:
261  if (conn->inCursor + 2 > conn->inEnd)
262  return EOF;
263  memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
264  conn->inCursor += 2;
265  *result = (int) pg_ntoh16(tmp2);
266  break;
267  case 4:
268  if (conn->inCursor + 4 > conn->inEnd)
269  return EOF;
270  memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
271  conn->inCursor += 4;
272  *result = (int) pg_ntoh32(tmp4);
273  break;
274  default:
276  "integer of size %lu not supported by pqGetInt",
277  (unsigned long) bytes);
278  return EOF;
279  }
280 
281  if (conn->Pfdebug)
282  fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
283 
284  return 0;
285 }
int inEnd
Definition: libpq-int.h:448
def bytes(source, encoding='ascii', errors='strict')
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
unsigned short uint16
Definition: c.h:374
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
PGNoticeHooks noticeHooks
Definition: libpq-int.h:380
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
Definition: fe-exec.c:871
unsigned int uint32
Definition: c.h:375
char * inBuffer
Definition: libpq-int.h:444
int inCursor
Definition: libpq-int.h:447

◆ pqGetnchar()

int pqGetnchar ( char *  s,
size_t  len,
PGconn conn 
)

Definition at line 181 of file fe-misc.c.

References fprintf, pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, and pg_conn::Pfdebug.

Referenced by getAnotherTuple(), pg_password_sendauth(), pg_SASL_continue(), pqFunctionCall2(), and pqFunctionCall3().

182 {
183  if (len > (size_t) (conn->inEnd - conn->inCursor))
184  return EOF;
185 
186  memcpy(s, conn->inBuffer + conn->inCursor, len);
187  /* no terminating null */
188 
189  conn->inCursor += len;
190 
191  if (conn->Pfdebug)
192  {
193  fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
194  fwrite(s, 1, len, conn->Pfdebug);
195  fprintf(conn->Pfdebug, "\n");
196  }
197 
198  return 0;
199 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
char * inBuffer
Definition: libpq-int.h:444
int inCursor
Definition: libpq-int.h:447

◆ pqGets()

int pqGets ( PQExpBuffer  buf,
PGconn conn 
)

Definition at line 149 of file fe-misc.c.

References pqGets_internal().

Referenced by getNotify(), getParameterStatus(), getRowDescriptions(), pg_SASL_init(), pqGetErrorNotice2(), pqGetErrorNotice3(), pqParseInput2(), and pqParseInput3().

150 {
151  return pqGets_internal(buf, conn, true);
152 }
static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
Definition: fe-misc.c:118

◆ pqGets_append()

int pqGets_append ( PQExpBuffer  buf,
PGconn conn 
)

Definition at line 155 of file fe-misc.c.

References pqGets_internal().

Referenced by PQconnectPoll().

156 {
157  return pqGets_internal(buf, conn, false);
158 }
static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
Definition: fe-misc.c:118

◆ pqGets_internal()

static int pqGets_internal ( PQExpBuffer  buf,
PGconn conn,
bool  resetbuffer 
)
static

Definition at line 118 of file fe-misc.c.

References appendBinaryPQExpBuffer(), PQExpBufferData::data, fprintf, pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, pg_conn::Pfdebug, and resetPQExpBuffer().

Referenced by pqGets(), and pqGets_append().

119 {
120  /* Copy conn data to locals for faster search loop */
121  char *inBuffer = conn->inBuffer;
122  int inCursor = conn->inCursor;
123  int inEnd = conn->inEnd;
124  int slen;
125 
126  while (inCursor < inEnd && inBuffer[inCursor])
127  inCursor++;
128 
129  if (inCursor >= inEnd)
130  return EOF;
131 
132  slen = inCursor - conn->inCursor;
133 
134  if (resetbuffer)
135  resetPQExpBuffer(buf);
136 
137  appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
138 
139  conn->inCursor = ++inCursor;
140 
141  if (conn->Pfdebug)
142  fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
143  buf->data);
144 
145  return 0;
146 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
char * inBuffer
Definition: libpq-int.h:444
int inCursor
Definition: libpq-int.h:447
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:399

◆ PQlibVersion()

int PQlibVersion ( void  )

Definition at line 66 of file fe-misc.c.

67 {
68  return PG_VERSION_NUM;
69 }

◆ PQmblen()

int PQmblen ( const char *  s,
int  encoding 
)

◆ pqPutc()

int pqPutc ( char  c,
PGconn conn 
)

Definition at line 98 of file fe-misc.c.

References fprintf, pg_conn::Pfdebug, and pqPutMsgBytes().

Referenced by PQsendDescribe(), and PQsendQueryGuts().

99 {
100  if (pqPutMsgBytes(&c, 1, conn))
101  return EOF;
102 
103  if (conn->Pfdebug)
104  fprintf(conn->Pfdebug, "To backend> %c\n", c);
105 
106  return 0;
107 }
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:550
char * c

◆ pqPutInt()

int pqPutInt ( int  value,
size_t  bytes,
PGconn conn 
)

Definition at line 293 of file fe-misc.c.

References fprintf, pg_conn::noticeHooks, pg_conn::Pfdebug, pg_hton16, pg_hton32, pqInternalNotice(), and pqPutMsgBytes().

Referenced by pg_SASL_init(), pqFunctionCall2(), pqFunctionCall3(), PQsendPrepare(), and PQsendQueryGuts().

294 {
295  uint16 tmp2;
296  uint32 tmp4;
297 
298  switch (bytes)
299  {
300  case 2:
301  tmp2 = pg_hton16((uint16) value);
302  if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
303  return EOF;
304  break;
305  case 4:
306  tmp4 = pg_hton32((uint32) value);
307  if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
308  return EOF;
309  break;
310  default:
312  "integer of size %lu not supported by pqPutInt",
313  (unsigned long) bytes);
314  return EOF;
315  }
316 
317  if (conn->Pfdebug)
318  fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
319 
320  return 0;
321 }
def bytes(source, encoding='ascii', errors='strict')
#define pg_hton16(x)
Definition: pg_bswap.h:120
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
unsigned short uint16
Definition: c.h:374
#define pg_hton32(x)
Definition: pg_bswap.h:121
PGNoticeHooks noticeHooks
Definition: libpq-int.h:380
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:550
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
Definition: fe-exec.c:871
unsigned int uint32
Definition: c.h:375
static struct @143 value

◆ pqPutMsgBytes()

static int pqPutMsgBytes ( const void *  buf,
size_t  len,
PGconn conn 
)
static

Definition at line 550 of file fe-misc.c.

References pg_conn::outBuffer, pg_conn::outMsgEnd, and pqCheckOutBufferSpace().

Referenced by pqPutc(), pqPutInt(), pqPutnchar(), and pqPuts().

551 {
552  /* make sure there is room for it */
553  if (pqCheckOutBufferSpace(conn->outMsgEnd + len, conn))
554  return EOF;
555  /* okay, save the data */
556  memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
557  conn->outMsgEnd += len;
558  /* no Pfdebug call here, caller should do it */
559  return 0;
560 }
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:330
int outMsgEnd
Definition: libpq-int.h:458
static char * buf
Definition: pg_test_fsync.c:68
char * outBuffer
Definition: libpq-int.h:451

◆ pqPutMsgEnd()

int pqPutMsgEnd ( PGconn conn)

Definition at line 573 of file fe-misc.c.

References fprintf, pg_conn::outBuffer, pg_conn::outCount, pg_conn::outMsgEnd, pg_conn::outMsgStart, pg_conn::Pfdebug, pg_hton32, and pqSendSome().

Referenced by pg_SASL_init(), pqEndcopy3(), pqFunctionCall2(), pqFunctionCall3(), pqPacketSend(), PQputCopyData(), PQputCopyEnd(), PQsendDescribe(), PQsendPrepare(), PQsendQuery(), PQsendQueryGuts(), and sendTerminateConn().

574 {
575  if (conn->Pfdebug)
576  fprintf(conn->Pfdebug, "To backend> Msg complete, length %u\n",
577  conn->outMsgEnd - conn->outCount);
578 
579  /* Fill in length word if needed */
580  if (conn->outMsgStart >= 0)
581  {
582  uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
583 
584  msgLen = pg_hton32(msgLen);
585  memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
586  }
587 
588  /* Make message eligible to send */
589  conn->outCount = conn->outMsgEnd;
590 
591  if (conn->outCount >= 8192)
592  {
593  int toSend = conn->outCount - (conn->outCount % 8192);
594 
595  if (pqSendSome(conn, toSend) < 0)
596  return EOF;
597  /* in nonblock mode, don't complain if unable to send it all */
598  }
599 
600  return 0;
601 }
int outCount
Definition: libpq-int.h:453
int outMsgEnd
Definition: libpq-int.h:458
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
#define pg_hton32(x)
Definition: pg_bswap.h:121
unsigned int uint32
Definition: c.h:375
int outMsgStart
Definition: libpq-int.h:456
char * outBuffer
Definition: libpq-int.h:451
static int pqSendSome(PGconn *conn, int len)
Definition: fe-misc.c:835

◆ pqPutMsgStart()

int pqPutMsgStart ( char  msg_type,
bool  force_len,
PGconn conn 
)

Definition at line 505 of file fe-misc.c.

References fprintf, pg_conn::outBuffer, pg_conn::outCount, pg_conn::outMsgEnd, pg_conn::outMsgStart, pg_conn::Pfdebug, PG_PROTOCOL_MAJOR, pqCheckOutBufferSpace(), and pg_conn::pversion.

Referenced by pg_SASL_init(), pqEndcopy3(), pqFunctionCall2(), pqFunctionCall3(), pqPacketSend(), PQputCopyData(), PQputCopyEnd(), PQsendDescribe(), PQsendPrepare(), PQsendQuery(), PQsendQueryGuts(), and sendTerminateConn().

506 {
507  int lenPos;
508  int endPos;
509 
510  /* allow room for message type byte */
511  if (msg_type)
512  endPos = conn->outCount + 1;
513  else
514  endPos = conn->outCount;
515 
516  /* do we want a length word? */
517  if (force_len || PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
518  {
519  lenPos = endPos;
520  /* allow room for message length */
521  endPos += 4;
522  }
523  else
524  lenPos = -1;
525 
526  /* make sure there is room for message header */
527  if (pqCheckOutBufferSpace(endPos, conn))
528  return EOF;
529  /* okay, save the message type byte if any */
530  if (msg_type)
531  conn->outBuffer[conn->outCount] = msg_type;
532  /* set up the message pointers */
533  conn->outMsgStart = lenPos;
534  conn->outMsgEnd = endPos;
535  /* length word, if needed, will be filled in by pqPutMsgEnd */
536 
537  if (conn->Pfdebug)
538  fprintf(conn->Pfdebug, "To backend> Msg %c\n",
539  msg_type ? msg_type : ' ');
540 
541  return 0;
542 }
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:330
int outCount
Definition: libpq-int.h:453
int outMsgEnd
Definition: libpq-int.h:458
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
int outMsgStart
Definition: libpq-int.h:456
ProtocolVersion pversion
Definition: libpq-int.h:414
char * outBuffer
Definition: libpq-int.h:451

◆ pqPutnchar()

int pqPutnchar ( const char *  s,
size_t  len,
PGconn conn 
)

Definition at line 232 of file fe-misc.c.

References fprintf, pg_conn::Pfdebug, and pqPutMsgBytes().

Referenced by pg_SASL_init(), pqFunctionCall2(), pqFunctionCall3(), pqPacketSend(), PQputCopyData(), PQputCopyEnd(), and PQsendQueryGuts().

233 {
234  if (pqPutMsgBytes(s, len, conn))
235  return EOF;
236 
237  if (conn->Pfdebug)
238  {
239  fprintf(conn->Pfdebug, "To backend> ");
240  fwrite(s, 1, len, conn->Pfdebug);
241  fprintf(conn->Pfdebug, "\n");
242  }
243 
244  return 0;
245 }
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:550

◆ pqPuts()

int pqPuts ( const char *  s,
PGconn conn 
)

Definition at line 165 of file fe-misc.c.

References fprintf, pg_conn::Pfdebug, and pqPutMsgBytes().

Referenced by pg_SASL_init(), pqFunctionCall2(), PQputCopyEnd(), PQsendDescribe(), PQsendPrepare(), PQsendQuery(), and PQsendQueryGuts().

166 {
167  if (pqPutMsgBytes(s, strlen(s) + 1, conn))
168  return EOF;
169 
170  if (conn->Pfdebug)
171  fprintf(conn->Pfdebug, "To backend> \"%s\"\n", s);
172 
173  return 0;
174 }
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:550

◆ pqReadData()

int pqReadData ( PGconn conn)

Definition at line 615 of file fe-misc.c.

References ALL_CONNECTION_FAILURE_ERRNOS, CONNECTION_BAD, EAGAIN, EINTR, pg_conn::errorMessage, EWOULDBLOCK, pg_conn::inBuffer, pg_conn::inBufSize, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, libpq_gettext, PGINVALID_SOCKET, pqCheckInBufferSpace(), pqDropConnection(), pqReadReady(), pqsecure_read(), printfPQExpBuffer(), pg_conn::sock, SOCK_ERRNO, pg_conn::ssl_in_use, and pg_conn::status.

Referenced by PQconnectPoll(), PQconsumeInput(), pqFunctionCall2(), pqFunctionCall3(), pqGetCopyData2(), pqGetCopyData3(), pqGetline2(), pqGetline3(), PQgetResult(), pqSendSome(), and pqSetenvPoll().

616 {
617  int someread = 0;
618  int nread;
619 
620  if (conn->sock == PGINVALID_SOCKET)
621  {
623  libpq_gettext("connection not open\n"));
624  return -1;
625  }
626 
627  /* Left-justify any data in the buffer to make room */
628  if (conn->inStart < conn->inEnd)
629  {
630  if (conn->inStart > 0)
631  {
632  memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
633  conn->inEnd - conn->inStart);
634  conn->inEnd -= conn->inStart;
635  conn->inCursor -= conn->inStart;
636  conn->inStart = 0;
637  }
638  }
639  else
640  {
641  /* buffer is logically empty, reset it */
642  conn->inStart = conn->inCursor = conn->inEnd = 0;
643  }
644 
645  /*
646  * If the buffer is fairly full, enlarge it. We need to be able to enlarge
647  * the buffer in case a single message exceeds the initial buffer size. We
648  * enlarge before filling the buffer entirely so as to avoid asking the
649  * kernel for a partial packet. The magic constant here should be large
650  * enough for a TCP packet or Unix pipe bufferload. 8K is the usual pipe
651  * buffer size, so...
652  */
653  if (conn->inBufSize - conn->inEnd < 8192)
654  {
655  if (pqCheckInBufferSpace(conn->inEnd + (size_t) 8192, conn))
656  {
657  /*
658  * We don't insist that the enlarge worked, but we need some room
659  */
660  if (conn->inBufSize - conn->inEnd < 100)
661  return -1; /* errorMessage already set */
662  }
663  }
664 
665  /* OK, try to read some data */
666 retry3:
667  nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
668  conn->inBufSize - conn->inEnd);
669  if (nread < 0)
670  {
671  switch (SOCK_ERRNO)
672  {
673  case EINTR:
674  goto retry3;
675 
676  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
677 #ifdef EAGAIN
678  case EAGAIN:
679  return someread;
680 #endif
681 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
682  case EWOULDBLOCK:
683  return someread;
684 #endif
685 
686  /* We might get ECONNRESET etc here if connection failed */
688  goto definitelyFailed;
689 
690  default:
691  /* pqsecure_read set the error message for us */
692  return -1;
693  }
694  }
695  if (nread > 0)
696  {
697  conn->inEnd += nread;
698 
699  /*
700  * Hack to deal with the fact that some kernels will only give us back
701  * 1 packet per recv() call, even if we asked for more and there is
702  * more available. If it looks like we are reading a long message,
703  * loop back to recv() again immediately, until we run out of data or
704  * buffer space. Without this, the block-and-restart behavior of
705  * libpq's higher levels leads to O(N^2) performance on long messages.
706  *
707  * Since we left-justified the data above, conn->inEnd gives the
708  * amount of data already read in the current message. We consider
709  * the message "long" once we have acquired 32k ...
710  */
711  if (conn->inEnd > 32768 &&
712  (conn->inBufSize - conn->inEnd) >= 8192)
713  {
714  someread = 1;
715  goto retry3;
716  }
717  return 1;
718  }
719 
720  if (someread)
721  return 1; /* got a zero read after successful tries */
722 
723  /*
724  * A return value of 0 could mean just that no data is now available, or
725  * it could mean EOF --- that is, the server has closed the connection.
726  * Since we have the socket in nonblock mode, the only way to tell the
727  * difference is to see if select() is saying that the file is ready.
728  * Grumble. Fortunately, we don't expect this path to be taken much,
729  * since in normal practice we should not be trying to read data unless
730  * the file selected for reading already.
731  *
732  * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
733  * data could arrive before we make the pqReadReady() test, but the second
734  * SSL_read() could still say WANT_READ because the data received was not
735  * a complete SSL record. So we must play dumb and assume there is more
736  * data, relying on the SSL layer to detect true EOF.
737  */
738 
739 #ifdef USE_SSL
740  if (conn->ssl_in_use)
741  return 0;
742 #endif
743 
744  switch (pqReadReady(conn))
745  {
746  case 0:
747  /* definitely no data available */
748  return 0;
749  case 1:
750  /* ready for read */
751  break;
752  default:
753  /* we override pqReadReady's message with something more useful */
754  goto definitelyEOF;
755  }
756 
757  /*
758  * Still not sure that it's EOF, because some data could have just
759  * arrived.
760  */
761 retry4:
762  nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
763  conn->inBufSize - conn->inEnd);
764  if (nread < 0)
765  {
766  switch (SOCK_ERRNO)
767  {
768  case EINTR:
769  goto retry4;
770 
771  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
772 #ifdef EAGAIN
773  case EAGAIN:
774  return 0;
775 #endif
776 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
777  case EWOULDBLOCK:
778  return 0;
779 #endif
780 
781  /* We might get ECONNRESET etc here if connection failed */
783  goto definitelyFailed;
784 
785  default:
786  /* pqsecure_read set the error message for us */
787  return -1;
788  }
789  }
790  if (nread > 0)
791  {
792  conn->inEnd += nread;
793  return 1;
794  }
795 
796  /*
797  * OK, we are getting a zero read even though select() says ready. This
798  * means the connection has been closed. Cope.
799  */
800 definitelyEOF:
802  libpq_gettext("server closed the connection unexpectedly\n"
803  "\tThis probably means the server terminated abnormally\n"
804  "\tbefore or while processing the request.\n"));
805 
806  /* Come here if lower-level code already set a suitable errorMessage */
807 definitelyFailed:
808  /* Do *not* drop any already-read data; caller still wants it */
809  pqDropConnection(conn, false);
810  conn->status = CONNECTION_BAD; /* No more connection to backend */
811  return -1;
812 }
int inEnd
Definition: libpq-int.h:448
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
int inStart
Definition: libpq-int.h:446
int pqReadReady(PGconn *conn)
Definition: fe-misc.c:1072
#define EAGAIN
Definition: win32_port.h:341
int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:394
ssize_t pqsecure_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:213
void pqDropConnection(PGconn *conn, bool flushInput)
Definition: fe-connect.c:459
#define SOCK_ERRNO
Definition: libpq-int.h:818
int inBufSize
Definition: libpq-int.h:445
bool ssl_in_use
Definition: libpq-int.h:472
pgsocket sock
Definition: libpq-int.h:410
#define PGINVALID_SOCKET
Definition: port.h:33
char * inBuffer
Definition: libpq-int.h:444
PQExpBufferData errorMessage
Definition: libpq-int.h:526
ConnStatusType status
Definition: libpq-int.h:388
#define ALL_CONNECTION_FAILURE_ERRNOS
Definition: port.h:114
#define EWOULDBLOCK
Definition: win32_port.h:349
int inCursor
Definition: libpq-int.h:447
#define EINTR
Definition: win32_port.h:343
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqReadReady()

int pqReadReady ( PGconn conn)

Definition at line 1072 of file fe-misc.c.

References pqSocketCheck().

Referenced by gss_read(), and pqReadData().

1073 {
1074  return pqSocketCheck(conn, 1, 0, (time_t) 0);
1075 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1096

◆ pqSendSome()

static int pqSendSome ( PGconn conn,
int  len 
)
static

Definition at line 835 of file fe-misc.c.

References PQExpBufferData::data, EAGAIN, EINTR, pg_conn::errorMessage, EWOULDBLOCK, libpq_gettext, Min, pg_conn::outBuffer, pg_conn::outCount, PGINVALID_SOCKET, pqIsnonblocking, pqReadData(), pqsecure_write(), pqWait(), printfPQExpBuffer(), remaining, resetPQExpBuffer(), pg_conn::sock, SOCK_ERRNO, pg_conn::write_err_msg, and pg_conn::write_failed.

Referenced by pqFlush(), and pqPutMsgEnd().

836 {
837  char *ptr = conn->outBuffer;
838  int remaining = conn->outCount;
839  int result = 0;
840 
841  /*
842  * If we already had a write failure, we will never again try to send data
843  * on that connection. Even if the kernel would let us, we've probably
844  * lost message boundary sync with the server. conn->write_failed
845  * therefore persists until the connection is reset, and we just discard
846  * all data presented to be written. However, as long as we still have a
847  * valid socket, we should continue to absorb data from the backend, so
848  * that we can collect any final error messages.
849  */
850  if (conn->write_failed)
851  {
852  /* conn->write_err_msg should be set up already */
853  conn->outCount = 0;
854  /* Absorb input data if any, and detect socket closure */
855  if (conn->sock != PGINVALID_SOCKET)
856  {
857  if (pqReadData(conn) < 0)
858  return -1;
859  }
860  return 0;
861  }
862 
863  if (conn->sock == PGINVALID_SOCKET)
864  {
866  libpq_gettext("connection not open\n"));
867  conn->write_failed = true;
868  /* Transfer error message to conn->write_err_msg, if possible */
869  /* (strdup failure is OK, we'll cope later) */
870  conn->write_err_msg = strdup(conn->errorMessage.data);
872  /* Discard queued data; no chance it'll ever be sent */
873  conn->outCount = 0;
874  return 0;
875  }
876 
877  /* while there's still data to send */
878  while (len > 0)
879  {
880  int sent;
881 
882 #ifndef WIN32
883  sent = pqsecure_write(conn, ptr, len);
884 #else
885 
886  /*
887  * Windows can fail on large sends, per KB article Q201213. The
888  * failure-point appears to be different in different versions of
889  * Windows, but 64k should always be safe.
890  */
891  sent = pqsecure_write(conn, ptr, Min(len, 65536));
892 #endif
893 
894  if (sent < 0)
895  {
896  /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
897  switch (SOCK_ERRNO)
898  {
899 #ifdef EAGAIN
900  case EAGAIN:
901  break;
902 #endif
903 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
904  case EWOULDBLOCK:
905  break;
906 #endif
907  case EINTR:
908  continue;
909 
910  default:
911  /* pqsecure_write set the error message for us */
912  conn->write_failed = true;
913 
914  /*
915  * Transfer error message to conn->write_err_msg, if
916  * possible (strdup failure is OK, we'll cope later).
917  *
918  * Note: this assumes that pqsecure_write and its children
919  * will overwrite not append to conn->errorMessage. If
920  * that's ever changed, we could remember the length of
921  * conn->errorMessage at entry to this routine, and then
922  * save and delete just what was appended.
923  */
924  conn->write_err_msg = strdup(conn->errorMessage.data);
926 
927  /* Discard queued data; no chance it'll ever be sent */
928  conn->outCount = 0;
929 
930  /* Absorb input data if any, and detect socket closure */
931  if (conn->sock != PGINVALID_SOCKET)
932  {
933  if (pqReadData(conn) < 0)
934  return -1;
935  }
936  return 0;
937  }
938  }
939  else
940  {
941  ptr += sent;
942  len -= sent;
943  remaining -= sent;
944  }
945 
946  if (len > 0)
947  {
948  /*
949  * We didn't send it all, wait till we can send more.
950  *
951  * There are scenarios in which we can't send data because the
952  * communications channel is full, but we cannot expect the server
953  * to clear the channel eventually because it's blocked trying to
954  * send data to us. (This can happen when we are sending a large
955  * amount of COPY data, and the server has generated lots of
956  * NOTICE responses.) To avoid a deadlock situation, we must be
957  * prepared to accept and buffer incoming data before we try
958  * again. Furthermore, it is possible that such incoming data
959  * might not arrive until after we've gone to sleep. Therefore,
960  * we wait for either read ready or write ready.
961  *
962  * In non-blocking mode, we don't wait here directly, but return 1
963  * to indicate that data is still pending. The caller should wait
964  * for both read and write ready conditions, and call
965  * PQconsumeInput() on read ready, but just in case it doesn't, we
966  * call pqReadData() ourselves before returning. That's not
967  * enough if the data has not arrived yet, but it's the best we
968  * can do, and works pretty well in practice. (The documentation
969  * used to say that you only need to wait for write-ready, so
970  * there are still plenty of applications like that out there.)
971  *
972  * Note that errors here don't result in write_failed becoming
973  * set.
974  */
975  if (pqReadData(conn) < 0)
976  {
977  result = -1; /* error message already set up */
978  break;
979  }
980 
981  if (pqIsnonblocking(conn))
982  {
983  result = 1;
984  break;
985  }
986 
987  if (pqWait(true, true, conn))
988  {
989  result = -1;
990  break;
991  }
992  }
993  }
994 
995  /* shift the remaining contents of the buffer */
996  if (remaining > 0)
997  memmove(conn->outBuffer, ptr, remaining);
998  conn->outCount = remaining;
999 
1000  return result;
1001 }
int remaining
Definition: informix.c:667
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
#define EAGAIN
Definition: win32_port.h:341
#define pqIsnonblocking(conn)
Definition: libpq-int.h:799
int outCount
Definition: libpq-int.h:453
#define Min(x, y)
Definition: c.h:928
char * write_err_msg
Definition: libpq-int.h:421
ssize_t pqsecure_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:295
bool write_failed
Definition: libpq-int.h:420
#define SOCK_ERRNO
Definition: libpq-int.h:818
int pqReadData(PGconn *conn)
Definition: fe-misc.c:615
int pqWait(int forRead, int forWrite, PGconn *conn)
Definition: fe-misc.c:1035
pgsocket sock
Definition: libpq-int.h:410
#define PGINVALID_SOCKET
Definition: port.h:33
PQExpBufferData errorMessage
Definition: libpq-int.h:526
char * outBuffer
Definition: libpq-int.h:451
#define EWOULDBLOCK
Definition: win32_port.h:349
#define EINTR
Definition: win32_port.h:343
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqSkipnchar()

int pqSkipnchar ( size_t  len,
PGconn conn 
)

Definition at line 210 of file fe-misc.c.

References fprintf, pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, and pg_conn::Pfdebug.

Referenced by getAnotherTuple().

211 {
212  if (len > (size_t) (conn->inEnd - conn->inCursor))
213  return EOF;
214 
215  if (conn->Pfdebug)
216  {
217  fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
218  fwrite(conn->inBuffer + conn->inCursor, 1, len, conn->Pfdebug);
219  fprintf(conn->Pfdebug, "\n");
220  }
221 
222  conn->inCursor += len;
223 
224  return 0;
225 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:219
FILE * Pfdebug
Definition: libpq-int.h:377
char * inBuffer
Definition: libpq-int.h:444
int inCursor
Definition: libpq-int.h:447

◆ pqSocketCheck()

static int pqSocketCheck ( PGconn conn,
int  forRead,
int  forWrite,
time_t  end_time 
)
static

Definition at line 1096 of file fe-misc.c.

References EINTR, pg_conn::errorMessage, libpq_gettext, PG_STRERROR_R_BUFLEN, PGINVALID_SOCKET, pgtls_read_pending(), pqSocketPoll(), printfPQExpBuffer(), pg_conn::sock, SOCK_ERRNO, SOCK_STRERROR, and pg_conn::ssl_in_use.

Referenced by pqReadReady(), pqWaitTimed(), and pqWriteReady().

1097 {
1098  int result;
1099 
1100  if (!conn)
1101  return -1;
1102  if (conn->sock == PGINVALID_SOCKET)
1103  {
1105  libpq_gettext("invalid socket\n"));
1106  return -1;
1107  }
1108 
1109 #ifdef USE_SSL
1110  /* Check for SSL library buffering read bytes */
1111  if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
1112  {
1113  /* short-circuit the select */
1114  return 1;
1115  }
1116 #endif
1117 
1118  /* We will retry as long as we get EINTR */
1119  do
1120  result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1121  while (result < 0 && SOCK_ERRNO == EINTR);
1122 
1123  if (result < 0)
1124  {
1125  char sebuf[PG_STRERROR_R_BUFLEN];
1126 
1128  libpq_gettext("select() failed: %s\n"),
1129  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1130  }
1131 
1132  return result;
1133 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
#define PG_STRERROR_R_BUFLEN
Definition: port.h:233
static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1146
#define SOCK_STRERROR
Definition: libpq-int.h:819
int64 end_time
Definition: pgbench.c:148
#define SOCK_ERRNO
Definition: libpq-int.h:818
bool ssl_in_use
Definition: libpq-int.h:472
bool pgtls_read_pending(PGconn *conn)
pgsocket sock
Definition: libpq-int.h:410
#define PGINVALID_SOCKET
Definition: port.h:33
PQExpBufferData errorMessage
Definition: libpq-int.h:526
#define EINTR
Definition: win32_port.h:343
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqSocketPoll()

static int pqSocketPoll ( int  sock,
int  forRead,
int  forWrite,
time_t  end_time 
)
static

Definition at line 1146 of file fe-misc.c.

References now(), and select.

Referenced by pqSocketCheck().

1147 {
1148  /* We use poll(2) if available, otherwise select(2) */
1149 #ifdef HAVE_POLL
1150  struct pollfd input_fd;
1151  int timeout_ms;
1152 
1153  if (!forRead && !forWrite)
1154  return 0;
1155 
1156  input_fd.fd = sock;
1157  input_fd.events = POLLERR;
1158  input_fd.revents = 0;
1159 
1160  if (forRead)
1161  input_fd.events |= POLLIN;
1162  if (forWrite)
1163  input_fd.events |= POLLOUT;
1164 
1165  /* Compute appropriate timeout interval */
1166  if (end_time == ((time_t) -1))
1167  timeout_ms = -1;
1168  else
1169  {
1170  time_t now = time(NULL);
1171 
1172  if (end_time > now)
1173  timeout_ms = (end_time - now) * 1000;
1174  else
1175  timeout_ms = 0;
1176  }
1177 
1178  return poll(&input_fd, 1, timeout_ms);
1179 #else /* !HAVE_POLL */
1180 
1181  fd_set input_mask;
1182  fd_set output_mask;
1183  fd_set except_mask;
1184  struct timeval timeout;
1185  struct timeval *ptr_timeout;
1186 
1187  if (!forRead && !forWrite)
1188  return 0;
1189 
1190  FD_ZERO(&input_mask);
1191  FD_ZERO(&output_mask);
1192  FD_ZERO(&except_mask);
1193  if (forRead)
1194  FD_SET(sock, &input_mask);
1195 
1196  if (forWrite)
1197  FD_SET(sock, &output_mask);
1198  FD_SET(sock, &except_mask);
1199 
1200  /* Compute appropriate timeout interval */
1201  if (end_time == ((time_t) -1))
1202  ptr_timeout = NULL;
1203  else
1204  {
1205  time_t now = time(NULL);
1206 
1207  if (end_time > now)
1208  timeout.tv_sec = end_time - now;
1209  else
1210  timeout.tv_sec = 0;
1211  timeout.tv_usec = 0;
1212  ptr_timeout = &timeout;
1213  }
1214 
1215  return select(sock + 1, &input_mask, &output_mask,
1216  &except_mask, ptr_timeout);
1217 #endif /* HAVE_POLL */
1218 }
int64 end_time
Definition: pgbench.c:148
#define select(n, r, w, e, timeout)
Definition: win32_port.h:464
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1542

◆ pqWait()

int pqWait ( int  forRead,
int  forWrite,
PGconn conn 
)

Definition at line 1035 of file fe-misc.c.

References pqWaitTimed().

Referenced by pqFunctionCall2(), pqFunctionCall3(), pqGetCopyData2(), pqGetCopyData3(), pqGetline2(), pqGetline3(), PQgetResult(), and pqSendSome().

1036 {
1037  return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
1038 }
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:1048

◆ pqWaitTimed()

int pqWaitTimed ( int  forRead,
int  forWrite,
PGconn conn,
time_t  finish_time 
)

Definition at line 1048 of file fe-misc.c.

References pg_conn::errorMessage, libpq_gettext, pqSocketCheck(), and printfPQExpBuffer().

Referenced by connectDBComplete(), and pqWait().

1049 {
1050  int result;
1051 
1052  result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1053 
1054  if (result < 0)
1055  return -1; /* errorMessage is already set */
1056 
1057  if (result == 0)
1058  {
1060  libpq_gettext("timeout expired\n"));
1061  return 1;
1062  }
1063 
1064  return 0;
1065 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1096
PQExpBufferData errorMessage
Definition: libpq-int.h:526
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqWriteReady()

int pqWriteReady ( PGconn conn)

Definition at line 1082 of file fe-misc.c.

References pqSocketCheck().

1083 {
1084  return pqSocketCheck(conn, 0, 1, (time_t) 0);
1085 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1096