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 1231 of file fe-misc.c.

References pg_encoding_dsplen().

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

1232 {
1233  return pg_encoding_dsplen(encoding, s);
1234 }
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 1240 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().

1241 {
1242  char *str;
1243  int encoding = PG_SQL_ASCII;
1244 
1245  str = getenv("PGCLIENTENCODING");
1246  if (str && *str != '\0')
1247  {
1248  encoding = pg_char_to_encoding(str);
1249  if (encoding < 0)
1250  encoding = PG_SQL_ASCII;
1251  }
1252  return encoding;
1253 }
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 1002 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().

1003 {
1004  if (conn->Pfdebug)
1005  fflush(conn->Pfdebug);
1006 
1007  if (conn->outCount > 0)
1008  return pqSendSome(conn, conn->outCount);
1009 
1010  return 0;
1011 }
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:825

◆ 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:197
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:197
FILE * Pfdebug
Definition: libpq-int.h:377
unsigned short uint16
Definition: c.h:366
#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:367
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:197
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:197
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:197
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:197
FILE * Pfdebug
Definition: libpq-int.h:377
unsigned short uint16
Definition: c.h:366
#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:367
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:67
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:197
FILE * Pfdebug
Definition: libpq-int.h:377
#define pg_hton32(x)
Definition: pg_bswap.h:121
unsigned int uint32
Definition: c.h:367
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:825

◆ 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:197
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:197
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:197
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 CONNECTION_BAD, EAGAIN, ECONNRESET, 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  if (SOCK_ERRNO == EINTR)
672  goto retry3;
673  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
674 #ifdef EAGAIN
675  if (SOCK_ERRNO == EAGAIN)
676  return someread;
677 #endif
678 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
679  if (SOCK_ERRNO == EWOULDBLOCK)
680  return someread;
681 #endif
682  /* We might get ECONNRESET here if using TCP and backend died */
683 #ifdef ECONNRESET
684  if (SOCK_ERRNO == ECONNRESET)
685  goto definitelyFailed;
686 #endif
687  /* pqsecure_read set the error message for us */
688  return -1;
689  }
690  if (nread > 0)
691  {
692  conn->inEnd += nread;
693 
694  /*
695  * Hack to deal with the fact that some kernels will only give us back
696  * 1 packet per recv() call, even if we asked for more and there is
697  * more available. If it looks like we are reading a long message,
698  * loop back to recv() again immediately, until we run out of data or
699  * buffer space. Without this, the block-and-restart behavior of
700  * libpq's higher levels leads to O(N^2) performance on long messages.
701  *
702  * Since we left-justified the data above, conn->inEnd gives the
703  * amount of data already read in the current message. We consider
704  * the message "long" once we have acquired 32k ...
705  */
706  if (conn->inEnd > 32768 &&
707  (conn->inBufSize - conn->inEnd) >= 8192)
708  {
709  someread = 1;
710  goto retry3;
711  }
712  return 1;
713  }
714 
715  if (someread)
716  return 1; /* got a zero read after successful tries */
717 
718  /*
719  * A return value of 0 could mean just that no data is now available, or
720  * it could mean EOF --- that is, the server has closed the connection.
721  * Since we have the socket in nonblock mode, the only way to tell the
722  * difference is to see if select() is saying that the file is ready.
723  * Grumble. Fortunately, we don't expect this path to be taken much,
724  * since in normal practice we should not be trying to read data unless
725  * the file selected for reading already.
726  *
727  * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
728  * data could arrive before we make the pqReadReady() test, but the second
729  * SSL_read() could still say WANT_READ because the data received was not
730  * a complete SSL record. So we must play dumb and assume there is more
731  * data, relying on the SSL layer to detect true EOF.
732  */
733 
734 #ifdef USE_SSL
735  if (conn->ssl_in_use)
736  return 0;
737 #endif
738 
739  switch (pqReadReady(conn))
740  {
741  case 0:
742  /* definitely no data available */
743  return 0;
744  case 1:
745  /* ready for read */
746  break;
747  default:
748  /* we override pqReadReady's message with something more useful */
749  goto definitelyEOF;
750  }
751 
752  /*
753  * Still not sure that it's EOF, because some data could have just
754  * arrived.
755  */
756 retry4:
757  nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
758  conn->inBufSize - conn->inEnd);
759  if (nread < 0)
760  {
761  if (SOCK_ERRNO == EINTR)
762  goto retry4;
763  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
764 #ifdef EAGAIN
765  if (SOCK_ERRNO == EAGAIN)
766  return 0;
767 #endif
768 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
769  if (SOCK_ERRNO == EWOULDBLOCK)
770  return 0;
771 #endif
772  /* We might get ECONNRESET here if using TCP and backend died */
773 #ifdef ECONNRESET
774  if (SOCK_ERRNO == ECONNRESET)
775  goto definitelyFailed;
776 #endif
777  /* pqsecure_read set the error message for us */
778  return -1;
779  }
780  if (nread > 0)
781  {
782  conn->inEnd += nread;
783  return 1;
784  }
785 
786  /*
787  * OK, we are getting a zero read even though select() says ready. This
788  * means the connection has been closed. Cope.
789  */
790 definitelyEOF:
792  libpq_gettext("server closed the connection unexpectedly\n"
793  "\tThis probably means the server terminated abnormally\n"
794  "\tbefore or while processing the request.\n"));
795 
796  /* Come here if lower-level code already set a suitable errorMessage */
797 definitelyFailed:
798  /* Do *not* drop any already-read data; caller still wants it */
799  pqDropConnection(conn, false);
800  conn->status = CONNECTION_BAD; /* No more connection to backend */
801  return -1;
802 }
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:1062
#define EAGAIN
Definition: win32_port.h:321
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:458
#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 ECONNRESET
Definition: win32_port.h:333
#define EWOULDBLOCK
Definition: win32_port.h:329
int inCursor
Definition: libpq-int.h:447
#define EINTR
Definition: win32_port.h:323
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqReadReady()

int pqReadReady ( PGconn conn)

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

References pqSocketCheck().

Referenced by gss_read(), and pqReadData().

1063 {
1064  return pqSocketCheck(conn, 1, 0, (time_t) 0);
1065 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1086

◆ pqSendSome()

static int pqSendSome ( PGconn conn,
int  len 
)
static

Definition at line 825 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().

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

1087 {
1088  int result;
1089 
1090  if (!conn)
1091  return -1;
1092  if (conn->sock == PGINVALID_SOCKET)
1093  {
1095  libpq_gettext("invalid socket\n"));
1096  return -1;
1097  }
1098 
1099 #ifdef USE_SSL
1100  /* Check for SSL library buffering read bytes */
1101  if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
1102  {
1103  /* short-circuit the select */
1104  return 1;
1105  }
1106 #endif
1107 
1108  /* We will retry as long as we get EINTR */
1109  do
1110  result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1111  while (result < 0 && SOCK_ERRNO == EINTR);
1112 
1113  if (result < 0)
1114  {
1115  char sebuf[PG_STRERROR_R_BUFLEN];
1116 
1118  libpq_gettext("select() failed: %s\n"),
1119  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1120  }
1121 
1122  return result;
1123 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
#define PG_STRERROR_R_BUFLEN
Definition: port.h:211
static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1136
#define SOCK_STRERROR
Definition: libpq-int.h:819
int64 end_time
Definition: pgbench.c:147
#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:323
#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 1136 of file fe-misc.c.

References now(), and select.

Referenced by pqSocketCheck().

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

◆ pqWait()

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

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

References pqWaitTimed().

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

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

◆ pqWaitTimed()

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

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

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

Referenced by connectDBComplete(), and pqWait().

1039 {
1040  int result;
1041 
1042  result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1043 
1044  if (result < 0)
1045  return -1; /* errorMessage is already set */
1046 
1047  if (result == 0)
1048  {
1050  libpq_gettext("timeout expired\n"));
1051  return 1;
1052  }
1053 
1054  return 0;
1055 }
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:1086
PQExpBufferData errorMessage
Definition: libpq-int.h:526
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqWriteReady()

int pqWriteReady ( PGconn conn)

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

References pqSocketCheck().

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