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)
 
static void fputnbytes (FILE *f, const char *str, size_t n)
 
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

◆ fputnbytes()

static void fputnbytes ( FILE *  f,
const char *  str,
size_t  n 
)
static

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

Referenced by pqGetnchar(), pqPutnchar(), and pqSkipnchar().

79 {
80  while (n-- > 0)
81  fputc(*str++, f);
82 }

◆ pqCheckInBufferSpace()

int pqCheckInBufferSpace ( size_t  bytes_needed,
PGconn conn 
)

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

408 {
409  int newsize = conn->inBufSize;
410  char *newbuf;
411 
412  /* Quick exit if we have enough space */
413  if (bytes_needed <= (size_t) newsize)
414  return 0;
415 
416  /*
417  * Before concluding that we need to enlarge the buffer, left-justify
418  * whatever is in it and recheck. The caller's value of bytes_needed
419  * includes any data to the left of inStart, but we can delete that in
420  * preference to enlarging the buffer. It's slightly ugly to have this
421  * function do this, but it's better than making callers worry about it.
422  */
423  bytes_needed -= conn->inStart;
424 
425  if (conn->inStart < conn->inEnd)
426  {
427  if (conn->inStart > 0)
428  {
429  memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
430  conn->inEnd - conn->inStart);
431  conn->inEnd -= conn->inStart;
432  conn->inCursor -= conn->inStart;
433  conn->inStart = 0;
434  }
435  }
436  else
437  {
438  /* buffer is logically empty, reset it */
439  conn->inStart = conn->inCursor = conn->inEnd = 0;
440  }
441 
442  /* Recheck whether we have enough space */
443  if (bytes_needed <= (size_t) newsize)
444  return 0;
445 
446  /*
447  * If we need to enlarge the buffer, we first try to double it in size; if
448  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
449  * the malloc pool by repeated small enlargements.
450  *
451  * Note: tests for newsize > 0 are to catch integer overflow.
452  */
453  do
454  {
455  newsize *= 2;
456  } while (newsize > 0 && bytes_needed > (size_t) newsize);
457 
458  if (newsize > 0 && bytes_needed <= (size_t) newsize)
459  {
460  newbuf = realloc(conn->inBuffer, newsize);
461  if (newbuf)
462  {
463  /* realloc succeeded */
464  conn->inBuffer = newbuf;
465  conn->inBufSize = newsize;
466  return 0;
467  }
468  }
469 
470  newsize = conn->inBufSize;
471  do
472  {
473  newsize += 8192;
474  } while (newsize > 0 && bytes_needed > (size_t) newsize);
475 
476  if (newsize > 0 && bytes_needed <= (size_t) newsize)
477  {
478  newbuf = realloc(conn->inBuffer, newsize);
479  if (newbuf)
480  {
481  /* realloc succeeded */
482  conn->inBuffer = newbuf;
483  conn->inBufSize = newsize;
484  return 0;
485  }
486  }
487 
488  /* realloc failed. Probably out of memory */
490  "cannot allocate memory for input buffer\n");
491  return EOF;
492 }
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 343 of file fe-misc.c.

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

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

344 {
345  int newsize = conn->outBufSize;
346  char *newbuf;
347 
348  /* Quick exit if we have enough space */
349  if (bytes_needed <= (size_t) newsize)
350  return 0;
351 
352  /*
353  * If we need to enlarge the buffer, we first try to double it in size; if
354  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
355  * the malloc pool by repeated small enlargements.
356  *
357  * Note: tests for newsize > 0 are to catch integer overflow.
358  */
359  do
360  {
361  newsize *= 2;
362  } while (newsize > 0 && bytes_needed > (size_t) newsize);
363 
364  if (newsize > 0 && bytes_needed <= (size_t) newsize)
365  {
366  newbuf = realloc(conn->outBuffer, newsize);
367  if (newbuf)
368  {
369  /* realloc succeeded */
370  conn->outBuffer = newbuf;
371  conn->outBufSize = newsize;
372  return 0;
373  }
374  }
375 
376  newsize = conn->outBufSize;
377  do
378  {
379  newsize += 8192;
380  } while (newsize > 0 && bytes_needed > (size_t) newsize);
381 
382  if (newsize > 0 && bytes_needed <= (size_t) newsize)
383  {
384  newbuf = realloc(conn->outBuffer, newsize);
385  if (newbuf)
386  {
387  /* realloc succeeded */
388  conn->outBuffer = newbuf;
389  conn->outBufSize = newsize;
390  return 0;
391  }
392  }
393 
394  /* realloc failed. Probably out of memory */
396  "cannot allocate memory for output buffer\n");
397  return EOF;
398 }
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 1225 of file fe-misc.c.

References pg_encoding_dsplen().

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

1226 {
1227  return pg_encoding_dsplen(encoding, s);
1228 }
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 1234 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().

1235 {
1236  char *str;
1237  int encoding = PG_SQL_ASCII;
1238 
1239  str = getenv("PGCLIENTENCODING");
1240  if (str && *str != '\0')
1241  {
1242  encoding = pg_char_to_encoding(str);
1243  if (encoding < 0)
1244  encoding = PG_SQL_ASCII;
1245  }
1246  return encoding;
1247 }
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 996 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().

997 {
998  if (conn->Pfdebug)
999  fflush(conn->Pfdebug);
1000 
1001  if (conn->outCount > 0)
1002  return pqSendSome(conn, conn->outCount);
1003 
1004  return 0;
1005 }
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:834

◆ pqGetc()

int pqGetc ( char *  result,
PGconn conn 
)

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

94 {
95  if (conn->inCursor >= conn->inEnd)
96  return EOF;
97 
98  *result = conn->inBuffer[conn->inCursor++];
99 
100  if (conn->Pfdebug)
101  fprintf(conn->Pfdebug, "From backend> %c\n", *result);
102 
103  return 0;
104 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:196
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 266 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().

267 {
268  uint16 tmp2;
269  uint32 tmp4;
270 
271  switch (bytes)
272  {
273  case 2:
274  if (conn->inCursor + 2 > conn->inEnd)
275  return EOF;
276  memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
277  conn->inCursor += 2;
278  *result = (int) pg_ntoh16(tmp2);
279  break;
280  case 4:
281  if (conn->inCursor + 4 > conn->inEnd)
282  return EOF;
283  memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
284  conn->inCursor += 4;
285  *result = (int) pg_ntoh32(tmp4);
286  break;
287  default:
289  "integer of size %lu not supported by pqGetInt",
290  (unsigned long) bytes);
291  return EOF;
292  }
293 
294  if (conn->Pfdebug)
295  fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
296 
297  return 0;
298 }
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:196
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 194 of file fe-misc.c.

References fprintf, fputnbytes(), 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().

195 {
196  if (len > (size_t) (conn->inEnd - conn->inCursor))
197  return EOF;
198 
199  memcpy(s, conn->inBuffer + conn->inCursor, len);
200  /* no terminating null */
201 
202  conn->inCursor += len;
203 
204  if (conn->Pfdebug)
205  {
206  fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
207  fputnbytes(conn->Pfdebug, s, len);
208  fprintf(conn->Pfdebug, "\n");
209  }
210 
211  return 0;
212 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:196
FILE * Pfdebug
Definition: libpq-int.h:377
char * inBuffer
Definition: libpq-int.h:444
static void fputnbytes(FILE *f, const char *str, size_t n)
Definition: fe-misc.c:78
int inCursor
Definition: libpq-int.h:447

◆ pqGets()

int pqGets ( PQExpBuffer  buf,
PGconn conn 
)

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

References pqGets_internal().

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

163 {
164  return pqGets_internal(buf, conn, true);
165 }
static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
Definition: fe-misc.c:131

◆ pqGets_append()

int pqGets_append ( PQExpBuffer  buf,
PGconn conn 
)

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

References pqGets_internal().

Referenced by PQconnectPoll().

169 {
170  return pqGets_internal(buf, conn, false);
171 }
static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
Definition: fe-misc.c:131

◆ pqGets_internal()

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

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

132 {
133  /* Copy conn data to locals for faster search loop */
134  char *inBuffer = conn->inBuffer;
135  int inCursor = conn->inCursor;
136  int inEnd = conn->inEnd;
137  int slen;
138 
139  while (inCursor < inEnd && inBuffer[inCursor])
140  inCursor++;
141 
142  if (inCursor >= inEnd)
143  return EOF;
144 
145  slen = inCursor - conn->inCursor;
146 
147  if (resetbuffer)
148  resetPQExpBuffer(buf);
149 
150  appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
151 
152  conn->inCursor = ++inCursor;
153 
154  if (conn->Pfdebug)
155  fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
156  buf->data);
157 
158  return 0;
159 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:196
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 111 of file fe-misc.c.

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

Referenced by PQsendDescribe(), and PQsendQueryGuts().

112 {
113  if (pqPutMsgBytes(&c, 1, conn))
114  return EOF;
115 
116  if (conn->Pfdebug)
117  fprintf(conn->Pfdebug, "To backend> %c\n", c);
118 
119  return 0;
120 }
#define fprintf
Definition: port.h:196
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:563
char * c

◆ pqPutInt()

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

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

307 {
308  uint16 tmp2;
309  uint32 tmp4;
310 
311  switch (bytes)
312  {
313  case 2:
314  tmp2 = pg_hton16((uint16) value);
315  if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
316  return EOF;
317  break;
318  case 4:
319  tmp4 = pg_hton32((uint32) value);
320  if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
321  return EOF;
322  break;
323  default:
325  "integer of size %lu not supported by pqPutInt",
326  (unsigned long) bytes);
327  return EOF;
328  }
329 
330  if (conn->Pfdebug)
331  fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
332 
333  return 0;
334 }
def bytes(source, encoding='ascii', errors='strict')
#define pg_hton16(x)
Definition: pg_bswap.h:120
#define fprintf
Definition: port.h:196
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:563
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 563 of file fe-misc.c.

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

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

564 {
565  /* make sure there is room for it */
566  if (pqCheckOutBufferSpace(conn->outMsgEnd + len, conn))
567  return EOF;
568  /* okay, save the data */
569  memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
570  conn->outMsgEnd += len;
571  /* no Pfdebug call here, caller should do it */
572  return 0;
573 }
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:343
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 586 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().

587 {
588  if (conn->Pfdebug)
589  fprintf(conn->Pfdebug, "To backend> Msg complete, length %u\n",
590  conn->outMsgEnd - conn->outCount);
591 
592  /* Fill in length word if needed */
593  if (conn->outMsgStart >= 0)
594  {
595  uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
596 
597  msgLen = pg_hton32(msgLen);
598  memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
599  }
600 
601  /* Make message eligible to send */
602  conn->outCount = conn->outMsgEnd;
603 
604  if (conn->outCount >= 8192)
605  {
606  int toSend = conn->outCount - (conn->outCount % 8192);
607 
608  if (pqSendSome(conn, toSend) < 0)
609  return EOF;
610  /* in nonblock mode, don't complain if unable to send it all */
611  }
612 
613  return 0;
614 }
int outCount
Definition: libpq-int.h:453
int outMsgEnd
Definition: libpq-int.h:458
#define fprintf
Definition: port.h:196
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:834

◆ pqPutMsgStart()

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

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

519 {
520  int lenPos;
521  int endPos;
522 
523  /* allow room for message type byte */
524  if (msg_type)
525  endPos = conn->outCount + 1;
526  else
527  endPos = conn->outCount;
528 
529  /* do we want a length word? */
530  if (force_len || PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
531  {
532  lenPos = endPos;
533  /* allow room for message length */
534  endPos += 4;
535  }
536  else
537  lenPos = -1;
538 
539  /* make sure there is room for message header */
540  if (pqCheckOutBufferSpace(endPos, conn))
541  return EOF;
542  /* okay, save the message type byte if any */
543  if (msg_type)
544  conn->outBuffer[conn->outCount] = msg_type;
545  /* set up the message pointers */
546  conn->outMsgStart = lenPos;
547  conn->outMsgEnd = endPos;
548  /* length word, if needed, will be filled in by pqPutMsgEnd */
549 
550  if (conn->Pfdebug)
551  fprintf(conn->Pfdebug, "To backend> Msg %c\n",
552  msg_type ? msg_type : ' ');
553 
554  return 0;
555 }
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:343
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:196
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 245 of file fe-misc.c.

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

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

246 {
247  if (pqPutMsgBytes(s, len, conn))
248  return EOF;
249 
250  if (conn->Pfdebug)
251  {
252  fprintf(conn->Pfdebug, "To backend> ");
253  fputnbytes(conn->Pfdebug, s, len);
254  fprintf(conn->Pfdebug, "\n");
255  }
256 
257  return 0;
258 }
#define fprintf
Definition: port.h:196
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:563
static void fputnbytes(FILE *f, const char *str, size_t n)
Definition: fe-misc.c:78

◆ pqPuts()

int pqPuts ( const char *  s,
PGconn conn 
)

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

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

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

179 {
180  if (pqPutMsgBytes(s, strlen(s) + 1, conn))
181  return EOF;
182 
183  if (conn->Pfdebug)
184  fprintf(conn->Pfdebug, "To backend> \"%s\"\n", s);
185 
186  return 0;
187 }
#define fprintf
Definition: port.h:196
FILE * Pfdebug
Definition: libpq-int.h:377
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:563

◆ pqReadData()

int pqReadData ( PGconn conn)

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

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

References pqSocketCheck().

Referenced by gss_read(), and pqReadData().

1057 {
1058  return pqSocketCheck(conn, 1, 0, (time_t) 0);
1059 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1080

◆ pqSendSome()

static int pqSendSome ( PGconn conn,
int  len 
)
static

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

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

References fprintf, fputnbytes(), pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, and pg_conn::Pfdebug.

Referenced by getAnotherTuple().

224 {
225  if (len > (size_t) (conn->inEnd - conn->inCursor))
226  return EOF;
227 
228  if (conn->Pfdebug)
229  {
230  fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
231  fputnbytes(conn->Pfdebug, conn->inBuffer + conn->inCursor, len);
232  fprintf(conn->Pfdebug, "\n");
233  }
234 
235  conn->inCursor += len;
236 
237  return 0;
238 }
int inEnd
Definition: libpq-int.h:448
#define fprintf
Definition: port.h:196
FILE * Pfdebug
Definition: libpq-int.h:377
char * inBuffer
Definition: libpq-int.h:444
static void fputnbytes(FILE *f, const char *str, size_t n)
Definition: fe-misc.c:78
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 1080 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().

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

References now(), and select.

Referenced by pqSocketCheck().

1131 {
1132  /* We use poll(2) if available, otherwise select(2) */
1133 #ifdef HAVE_POLL
1134  struct pollfd input_fd;
1135  int timeout_ms;
1136 
1137  if (!forRead && !forWrite)
1138  return 0;
1139 
1140  input_fd.fd = sock;
1141  input_fd.events = POLLERR;
1142  input_fd.revents = 0;
1143 
1144  if (forRead)
1145  input_fd.events |= POLLIN;
1146  if (forWrite)
1147  input_fd.events |= POLLOUT;
1148 
1149  /* Compute appropriate timeout interval */
1150  if (end_time == ((time_t) -1))
1151  timeout_ms = -1;
1152  else
1153  {
1154  time_t now = time(NULL);
1155 
1156  if (end_time > now)
1157  timeout_ms = (end_time - now) * 1000;
1158  else
1159  timeout_ms = 0;
1160  }
1161 
1162  return poll(&input_fd, 1, timeout_ms);
1163 #else /* !HAVE_POLL */
1164 
1165  fd_set input_mask;
1166  fd_set output_mask;
1167  fd_set except_mask;
1168  struct timeval timeout;
1169  struct timeval *ptr_timeout;
1170 
1171  if (!forRead && !forWrite)
1172  return 0;
1173 
1174  FD_ZERO(&input_mask);
1175  FD_ZERO(&output_mask);
1176  FD_ZERO(&except_mask);
1177  if (forRead)
1178  FD_SET(sock, &input_mask);
1179 
1180  if (forWrite)
1181  FD_SET(sock, &output_mask);
1182  FD_SET(sock, &except_mask);
1183 
1184  /* Compute appropriate timeout interval */
1185  if (end_time == ((time_t) -1))
1186  ptr_timeout = NULL;
1187  else
1188  {
1189  time_t now = time(NULL);
1190 
1191  if (end_time > now)
1192  timeout.tv_sec = end_time - now;
1193  else
1194  timeout.tv_sec = 0;
1195  timeout.tv_usec = 0;
1196  ptr_timeout = &timeout;
1197  }
1198 
1199  return select(sock + 1, &input_mask, &output_mask,
1200  &except_mask, ptr_timeout);
1201 #endif /* HAVE_POLL */
1202 }
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:1547

◆ pqWait()

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

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

References pqWaitTimed().

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

1020 {
1021  return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
1022 }
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:1032

◆ pqWaitTimed()

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

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

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

Referenced by connectDBComplete(), and pqWait().

1033 {
1034  int result;
1035 
1036  result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1037 
1038  if (result < 0)
1039  return -1; /* errorMessage is already set */
1040 
1041  if (result == 0)
1042  {
1044  libpq_gettext("timeout expired\n"));
1045  return 1;
1046  }
1047 
1048  return 0;
1049 }
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:1080
PQExpBufferData errorMessage
Definition: libpq-int.h:526
#define libpq_gettext(x)
Definition: libpq-int.h:805

◆ pqWriteReady()

int pqWriteReady ( PGconn conn)

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

References pqSocketCheck().

1067 {
1068  return pqSocketCheck(conn, 0, 1, (time_t) 0);
1069 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1080