PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 "port/pg_bswap.h"
#include "pg_config_paths.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

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

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

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

80 {
81  while (n-- > 0)
82  fputc(*str++, f);
83 }
int pqCheckInBufferSpace ( size_t  bytes_needed,
PGconn conn 
)

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

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

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

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

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

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

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

345 {
346  int newsize = conn->outBufSize;
347  char *newbuf;
348 
349  /* Quick exit if we have enough space */
350  if (bytes_needed <= (size_t) newsize)
351  return 0;
352 
353  /*
354  * If we need to enlarge the buffer, we first try to double it in size; if
355  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
356  * the malloc pool by repeated small enlargements.
357  *
358  * Note: tests for newsize > 0 are to catch integer overflow.
359  */
360  do
361  {
362  newsize *= 2;
363  } while (newsize > 0 && bytes_needed > (size_t) newsize);
364 
365  if (newsize > 0 && bytes_needed <= (size_t) newsize)
366  {
367  newbuf = realloc(conn->outBuffer, newsize);
368  if (newbuf)
369  {
370  /* realloc succeeded */
371  conn->outBuffer = newbuf;
372  conn->outBufSize = newsize;
373  return 0;
374  }
375  }
376 
377  newsize = conn->outBufSize;
378  do
379  {
380  newsize += 8192;
381  } while (newsize > 0 && bytes_needed > (size_t) newsize);
382 
383  if (newsize > 0 && bytes_needed <= (size_t) newsize)
384  {
385  newbuf = realloc(conn->outBuffer, newsize);
386  if (newbuf)
387  {
388  /* realloc succeeded */
389  conn->outBuffer = newbuf;
390  conn->outBufSize = newsize;
391  return 0;
392  }
393  }
394 
395  /* realloc failed. Probably out of memory */
397  "cannot allocate memory for output buffer\n");
398  return EOF;
399 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PQExpBufferData errorMessage
Definition: libpq-int.h:491
int outBufSize
Definition: libpq-int.h:437
#define realloc(a, b)
Definition: header.h:60
char * outBuffer
Definition: libpq-int.h:436
int PQdsplen ( const char *  s,
int  encoding 
)

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

References pg_encoding_dsplen().

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

1191 {
1192  return pg_encoding_dsplen(encoding, s);
1193 }
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:1796
static char * encoding
Definition: initdb.c:123
int PQenv2encoding ( void  )

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

References encoding, pg_char_to_encoding(), and PG_SQL_ASCII.

Referenced by main().

1200 {
1201  char *str;
1202  int encoding = PG_SQL_ASCII;
1203 
1204  str = getenv("PGCLIENTENCODING");
1205  if (str && *str != '\0')
1206  {
1207  encoding = pg_char_to_encoding(str);
1208  if (encoding < 0)
1209  encoding = PG_SQL_ASCII;
1210  }
1211  return encoding;
1212 }
int pg_char_to_encoding(const char *name)
Definition: encnames.c:551
static char * encoding
Definition: initdb.c:123
int pqFlush ( PGconn conn)

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

962 {
963  if (conn->Pfdebug)
964  fflush(conn->Pfdebug);
965 
966  if (conn->outCount > 0)
967  return pqSendSome(conn, conn->outCount);
968 
969  return 0;
970 }
int outCount
Definition: libpq-int.h:438
FILE * Pfdebug
Definition: libpq-int.h:368
static int pqSendSome(PGconn *conn, int len)
Definition: fe-misc.c:829
int pqGetc ( char *  result,
PGconn conn 
)

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

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

95 {
96  if (conn->inCursor >= conn->inEnd)
97  return EOF;
98 
99  *result = conn->inBuffer[conn->inCursor++];
100 
101  if (conn->Pfdebug)
102  fprintf(conn->Pfdebug, "From backend> %c\n", *result);
103 
104  return 0;
105 }
int inEnd
Definition: libpq-int.h:433
FILE * Pfdebug
Definition: libpq-int.h:368
char * inBuffer
Definition: libpq-int.h:429
int inCursor
Definition: libpq-int.h:432
int pqGetInt ( int *  result,
size_t  bytes,
PGconn conn 
)

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

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

268 {
269  uint16 tmp2;
270  uint32 tmp4;
271 
272  switch (bytes)
273  {
274  case 2:
275  if (conn->inCursor + 2 > conn->inEnd)
276  return EOF;
277  memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
278  conn->inCursor += 2;
279  *result = (int) pg_ntoh16(tmp2);
280  break;
281  case 4:
282  if (conn->inCursor + 4 > conn->inEnd)
283  return EOF;
284  memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
285  conn->inCursor += 4;
286  *result = (int) pg_ntoh32(tmp4);
287  break;
288  default:
290  "integer of size %lu not supported by pqGetInt",
291  (unsigned long) bytes);
292  return EOF;
293  }
294 
295  if (conn->Pfdebug)
296  fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
297 
298  return 0;
299 }
int inEnd
Definition: libpq-int.h:433
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
FILE * Pfdebug
Definition: libpq-int.h:368
unsigned short uint16
Definition: c.h:257
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
PGNoticeHooks noticeHooks
Definition: libpq-int.h:371
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
Definition: fe-exec.c:822
unsigned int uint32
Definition: c.h:258
char * inBuffer
Definition: libpq-int.h:429
int inCursor
Definition: libpq-int.h:432
int pqGetnchar ( char *  s,
size_t  len,
PGconn conn 
)

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

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

196 {
197  if (len > (size_t) (conn->inEnd - conn->inCursor))
198  return EOF;
199 
200  memcpy(s, conn->inBuffer + conn->inCursor, len);
201  /* no terminating null */
202 
203  conn->inCursor += len;
204 
205  if (conn->Pfdebug)
206  {
207  fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
208  fputnbytes(conn->Pfdebug, s, len);
209  fprintf(conn->Pfdebug, "\n");
210  }
211 
212  return 0;
213 }
int inEnd
Definition: libpq-int.h:433
FILE * Pfdebug
Definition: libpq-int.h:368
char * inBuffer
Definition: libpq-int.h:429
static void fputnbytes(FILE *f, const char *str, size_t n)
Definition: fe-misc.c:79
int inCursor
Definition: libpq-int.h:432
int pqGets ( PQExpBuffer  buf,
PGconn conn 
)

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

References pqGets_internal().

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

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

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

References pqGets_internal().

Referenced by PQconnectPoll().

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

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

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

Referenced by pqGets(), and pqGets_append().

133 {
134  /* Copy conn data to locals for faster search loop */
135  char *inBuffer = conn->inBuffer;
136  int inCursor = conn->inCursor;
137  int inEnd = conn->inEnd;
138  int slen;
139 
140  while (inCursor < inEnd && inBuffer[inCursor])
141  inCursor++;
142 
143  if (inCursor >= inEnd)
144  return EOF;
145 
146  slen = inCursor - conn->inCursor;
147 
148  if (resetbuffer)
149  resetPQExpBuffer(buf);
150 
151  appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
152 
153  conn->inCursor = ++inCursor;
154 
155  if (conn->Pfdebug)
156  fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
157  buf->data);
158 
159  return 0;
160 }
int inEnd
Definition: libpq-int.h:433
FILE * Pfdebug
Definition: libpq-int.h:368
char * inBuffer
Definition: libpq-int.h:429
int inCursor
Definition: libpq-int.h:432
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:415
int PQlibVersion ( void  )

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

68 {
69  return PG_VERSION_NUM;
70 }
int PQmblen ( const char *  s,
int  encoding 
)

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

References pg_encoding_mblen().

Referenced by appendStringLiteral(), command_no_begin(), do_field(), is_select_command(), pg_wcsformat(), pg_wcssize(), pg_wcswidth(), processSQLNamePattern(), quote_if_needed(), skip_white_space(), strip_quotes(), strlen_max_width(), and strtokx().

1181 {
1182  return pg_encoding_mblen(encoding, s);
1183 }
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:1785
static char * encoding
Definition: initdb.c:123
int pqPutc ( char  c,
PGconn conn 
)

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

References pg_conn::Pfdebug, and pqPutMsgBytes().

Referenced by PQsendDescribe(), and PQsendQueryGuts().

113 {
114  if (pqPutMsgBytes(&c, 1, conn))
115  return EOF;
116 
117  if (conn->Pfdebug)
118  fprintf(conn->Pfdebug, "To backend> %c\n", c);
119 
120  return 0;
121 }
FILE * Pfdebug
Definition: libpq-int.h:368
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:564
char * c
int pqPutInt ( int  value,
size_t  bytes,
PGconn conn 
)

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

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

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

308 {
309  uint16 tmp2;
310  uint32 tmp4;
311 
312  switch (bytes)
313  {
314  case 2:
315  tmp2 = pg_hton16((uint16) value);
316  if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
317  return EOF;
318  break;
319  case 4:
320  tmp4 = pg_hton32((uint32) value);
321  if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
322  return EOF;
323  break;
324  default:
326  "integer of size %lu not supported by pqPutInt",
327  (unsigned long) bytes);
328  return EOF;
329  }
330 
331  if (conn->Pfdebug)
332  fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
333 
334  return 0;
335 }
#define pg_hton16(x)
Definition: pg_bswap.h:120
FILE * Pfdebug
Definition: libpq-int.h:368
unsigned short uint16
Definition: c.h:257
#define pg_hton32(x)
Definition: pg_bswap.h:121
static struct @121 value
PGNoticeHooks noticeHooks
Definition: libpq-int.h:371
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:564
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
Definition: fe-exec.c:822
unsigned int uint32
Definition: c.h:258
static int pqPutMsgBytes ( const void *  buf,
size_t  len,
PGconn conn 
)
static

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

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

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

565 {
566  /* make sure there is room for it */
567  if (pqCheckOutBufferSpace(conn->outMsgEnd + len, conn))
568  return EOF;
569  /* okay, save the data */
570  memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
571  conn->outMsgEnd += len;
572  /* no Pfdebug call here, caller should do it */
573  return 0;
574 }
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:344
int outMsgEnd
Definition: libpq-int.h:443
static char * buf
Definition: pg_test_fsync.c:67
char * outBuffer
Definition: libpq-int.h:436
int pqPutMsgEnd ( PGconn conn)

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

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

588 {
589  if (conn->Pfdebug)
590  fprintf(conn->Pfdebug, "To backend> Msg complete, length %u\n",
591  conn->outMsgEnd - conn->outCount);
592 
593  /* Fill in length word if needed */
594  if (conn->outMsgStart >= 0)
595  {
596  uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
597 
598  msgLen = pg_hton32(msgLen);
599  memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
600  }
601 
602  /* Make message eligible to send */
603  conn->outCount = conn->outMsgEnd;
604 
605  if (conn->outCount >= 8192)
606  {
607  int toSend = conn->outCount - (conn->outCount % 8192);
608 
609  if (pqSendSome(conn, toSend) < 0)
610  return EOF;
611  /* in nonblock mode, don't complain if unable to send it all */
612  }
613 
614  return 0;
615 }
int outCount
Definition: libpq-int.h:438
int outMsgEnd
Definition: libpq-int.h:443
FILE * Pfdebug
Definition: libpq-int.h:368
#define pg_hton32(x)
Definition: pg_bswap.h:121
unsigned int uint32
Definition: c.h:258
int outMsgStart
Definition: libpq-int.h:441
char * outBuffer
Definition: libpq-int.h:436
static int pqSendSome(PGconn *conn, int len)
Definition: fe-misc.c:829
int pqPutMsgStart ( char  msg_type,
bool  force_len,
PGconn conn 
)

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

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

520 {
521  int lenPos;
522  int endPos;
523 
524  /* allow room for message type byte */
525  if (msg_type)
526  endPos = conn->outCount + 1;
527  else
528  endPos = conn->outCount;
529 
530  /* do we want a length word? */
531  if (force_len || PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
532  {
533  lenPos = endPos;
534  /* allow room for message length */
535  endPos += 4;
536  }
537  else
538  lenPos = -1;
539 
540  /* make sure there is room for message header */
541  if (pqCheckOutBufferSpace(endPos, conn))
542  return EOF;
543  /* okay, save the message type byte if any */
544  if (msg_type)
545  conn->outBuffer[conn->outCount] = msg_type;
546  /* set up the message pointers */
547  conn->outMsgStart = lenPos;
548  conn->outMsgEnd = endPos;
549  /* length word, if needed, will be filled in by pqPutMsgEnd */
550 
551  if (conn->Pfdebug)
552  fprintf(conn->Pfdebug, "To backend> Msg %c\n",
553  msg_type ? msg_type : ' ');
554 
555  return 0;
556 }
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:344
int outCount
Definition: libpq-int.h:438
int outMsgEnd
Definition: libpq-int.h:443
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
FILE * Pfdebug
Definition: libpq-int.h:368
int outMsgStart
Definition: libpq-int.h:441
ProtocolVersion pversion
Definition: libpq-int.h:404
char * outBuffer
Definition: libpq-int.h:436
int pqPutnchar ( const char *  s,
size_t  len,
PGconn conn 
)

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

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

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

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

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

References pg_conn::Pfdebug, and pqPutMsgBytes().

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

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

Definition at line 629 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, memmove, PGINVALID_SOCKET, pqCheckInBufferSpace(), pqDropConnection(), pqReadReady(), pqsecure_read(), printfPQExpBuffer(), pg_conn::sock, SOCK_ERRNO, and pg_conn::status.

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

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

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

References pqSocketCheck().

Referenced by pqReadData().

1022 {
1023  return pqSocketCheck(conn, 1, 0, (time_t) 0);
1024 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1045
static int pqSendSome ( PGconn conn,
int  len 
)
static

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

References EAGAIN, EINTR, pg_conn::errorMessage, EWOULDBLOCK, libpq_gettext, memmove, Min, pg_conn::outBuffer, pg_conn::outCount, PGINVALID_SOCKET, pqIsnonblocking, pqReadData(), pqsecure_write(), pqWait(), printfPQExpBuffer(), remaining, pg_conn::sock, SOCK_ERRNO, and TRUE.

Referenced by pqFlush(), and pqPutMsgEnd().

830 {
831  char *ptr = conn->outBuffer;
832  int remaining = conn->outCount;
833  int result = 0;
834 
835  if (conn->sock == PGINVALID_SOCKET)
836  {
838  libpq_gettext("connection not open\n"));
839  /* Discard queued data; no chance it'll ever be sent */
840  conn->outCount = 0;
841  return -1;
842  }
843 
844  /* while there's still data to send */
845  while (len > 0)
846  {
847  int sent;
848 
849 #ifndef WIN32
850  sent = pqsecure_write(conn, ptr, len);
851 #else
852 
853  /*
854  * Windows can fail on large sends, per KB article Q201213. The
855  * failure-point appears to be different in different versions of
856  * Windows, but 64k should always be safe.
857  */
858  sent = pqsecure_write(conn, ptr, Min(len, 65536));
859 #endif
860 
861  if (sent < 0)
862  {
863  /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
864  switch (SOCK_ERRNO)
865  {
866 #ifdef EAGAIN
867  case EAGAIN:
868  break;
869 #endif
870 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
871  case EWOULDBLOCK:
872  break;
873 #endif
874  case EINTR:
875  continue;
876 
877  default:
878  /* pqsecure_write set the error message for us */
879 
880  /*
881  * We used to close the socket here, but that's a bad idea
882  * since there might be unread data waiting (typically, a
883  * NOTICE message from the backend telling us it's
884  * committing hara-kiri...). Leave the socket open until
885  * pqReadData finds no more data can be read. But abandon
886  * attempt to send data.
887  */
888  conn->outCount = 0;
889  return -1;
890  }
891  }
892  else
893  {
894  ptr += sent;
895  len -= sent;
896  remaining -= sent;
897  }
898 
899  if (len > 0)
900  {
901  /*
902  * We didn't send it all, wait till we can send more.
903  *
904  * There are scenarios in which we can't send data because the
905  * communications channel is full, but we cannot expect the server
906  * to clear the channel eventually because it's blocked trying to
907  * send data to us. (This can happen when we are sending a large
908  * amount of COPY data, and the server has generated lots of
909  * NOTICE responses.) To avoid a deadlock situation, we must be
910  * prepared to accept and buffer incoming data before we try
911  * again. Furthermore, it is possible that such incoming data
912  * might not arrive until after we've gone to sleep. Therefore,
913  * we wait for either read ready or write ready.
914  *
915  * In non-blocking mode, we don't wait here directly, but return 1
916  * to indicate that data is still pending. The caller should wait
917  * for both read and write ready conditions, and call
918  * PQconsumeInput() on read ready, but just in case it doesn't, we
919  * call pqReadData() ourselves before returning. That's not
920  * enough if the data has not arrived yet, but it's the best we
921  * can do, and works pretty well in practice. (The documentation
922  * used to say that you only need to wait for write-ready, so
923  * there are still plenty of applications like that out there.)
924  */
925  if (pqReadData(conn) < 0)
926  {
927  result = -1; /* error message already set up */
928  break;
929  }
930 
931  if (pqIsnonblocking(conn))
932  {
933  result = 1;
934  break;
935  }
936 
937  if (pqWait(TRUE, TRUE, conn))
938  {
939  result = -1;
940  break;
941  }
942  }
943  }
944 
945  /* shift the remaining contents of the buffer */
946  if (remaining > 0)
947  memmove(conn->outBuffer, ptr, remaining);
948  conn->outCount = remaining;
949 
950  return result;
951 }
int remaining
Definition: informix.c:692
#define EWOULDBLOCK
Definition: win32.h:291
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
#define pqIsnonblocking(conn)
Definition: libpq-int.h:676
int outCount
Definition: libpq-int.h:438
#define Min(x, y)
Definition: c.h:812
#define EAGAIN
Definition: win32.h:283
ssize_t pqsecure_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:282
#define SOCK_ERRNO
Definition: libpq-int.h:695
int pqReadData(PGconn *conn)
Definition: fe-misc.c:629
int pqWait(int forRead, int forWrite, PGconn *conn)
Definition: fe-misc.c:984
#define memmove(d, s, c)
Definition: c.h:1064
pgsocket sock
Definition: libpq-int.h:400
#define PGINVALID_SOCKET
Definition: port.h:24
#define EINTR
Definition: win32.h:285
PQExpBufferData errorMessage
Definition: libpq-int.h:491
char * outBuffer
Definition: libpq-int.h:436
#define TRUE
Definition: c.h:215
#define libpq_gettext(x)
Definition: libpq-int.h:682
int pqSkipnchar ( size_t  len,
PGconn conn 
)

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

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

Referenced by getAnotherTuple().

225 {
226  if (len > (size_t) (conn->inEnd - conn->inCursor))
227  return EOF;
228 
229  if (conn->Pfdebug)
230  {
231  fprintf(conn->Pfdebug, "From backend (%lu)> ", (unsigned long) len);
232  fputnbytes(conn->Pfdebug, conn->inBuffer + conn->inCursor, len);
233  fprintf(conn->Pfdebug, "\n");
234  }
235 
236  conn->inCursor += len;
237 
238  return 0;
239 }
int inEnd
Definition: libpq-int.h:433
FILE * Pfdebug
Definition: libpq-int.h:368
char * inBuffer
Definition: libpq-int.h:429
static void fputnbytes(FILE *f, const char *str, size_t n)
Definition: fe-misc.c:79
int inCursor
Definition: libpq-int.h:432
static int pqSocketCheck ( PGconn conn,
int  forRead,
int  forWrite,
time_t  end_time 
)
static

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

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

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

1046 {
1047  int result;
1048 
1049  if (!conn)
1050  return -1;
1051  if (conn->sock == PGINVALID_SOCKET)
1052  {
1054  libpq_gettext("invalid socket\n"));
1055  return -1;
1056  }
1057 
1058 #ifdef USE_SSL
1059  /* Check for SSL library buffering read bytes */
1060  if (forRead && conn->ssl_in_use && pgtls_read_pending(conn) > 0)
1061  {
1062  /* short-circuit the select */
1063  return 1;
1064  }
1065 #endif
1066 
1067  /* We will retry as long as we get EINTR */
1068  do
1069  result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1070  while (result < 0 && SOCK_ERRNO == EINTR);
1071 
1072  if (result < 0)
1073  {
1074  char sebuf[256];
1075 
1077  libpq_gettext("select() failed: %s\n"),
1078  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1079  }
1080 
1081  return result;
1082 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1095
#define SOCK_STRERROR
Definition: libpq-int.h:696
int64 end_time
Definition: pgbench.c:100
#define SOCK_ERRNO
Definition: libpq-int.h:695
bool pgtls_read_pending(PGconn *conn)
pgsocket sock
Definition: libpq-int.h:400
#define PGINVALID_SOCKET
Definition: port.h:24
#define EINTR
Definition: win32.h:285
PQExpBufferData errorMessage
Definition: libpq-int.h:491
#define libpq_gettext(x)
Definition: libpq-int.h:682
static int pqSocketPoll ( int  sock,
int  forRead,
int  forWrite,
time_t  end_time 
)
static

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

References now(), and select.

Referenced by pqSocketCheck().

1096 {
1097  /* We use poll(2) if available, otherwise select(2) */
1098 #ifdef HAVE_POLL
1099  struct pollfd input_fd;
1100  int timeout_ms;
1101 
1102  if (!forRead && !forWrite)
1103  return 0;
1104 
1105  input_fd.fd = sock;
1106  input_fd.events = POLLERR;
1107  input_fd.revents = 0;
1108 
1109  if (forRead)
1110  input_fd.events |= POLLIN;
1111  if (forWrite)
1112  input_fd.events |= POLLOUT;
1113 
1114  /* Compute appropriate timeout interval */
1115  if (end_time == ((time_t) -1))
1116  timeout_ms = -1;
1117  else
1118  {
1119  time_t now = time(NULL);
1120 
1121  if (end_time > now)
1122  timeout_ms = (end_time - now) * 1000;
1123  else
1124  timeout_ms = 0;
1125  }
1126 
1127  return poll(&input_fd, 1, timeout_ms);
1128 #else /* !HAVE_POLL */
1129 
1130  fd_set input_mask;
1131  fd_set output_mask;
1132  fd_set except_mask;
1133  struct timeval timeout;
1134  struct timeval *ptr_timeout;
1135 
1136  if (!forRead && !forWrite)
1137  return 0;
1138 
1139  FD_ZERO(&input_mask);
1140  FD_ZERO(&output_mask);
1141  FD_ZERO(&except_mask);
1142  if (forRead)
1143  FD_SET(sock, &input_mask);
1144 
1145  if (forWrite)
1146  FD_SET(sock, &output_mask);
1147  FD_SET(sock, &except_mask);
1148 
1149  /* Compute appropriate timeout interval */
1150  if (end_time == ((time_t) -1))
1151  ptr_timeout = NULL;
1152  else
1153  {
1154  time_t now = time(NULL);
1155 
1156  if (end_time > now)
1157  timeout.tv_sec = end_time - now;
1158  else
1159  timeout.tv_sec = 0;
1160  timeout.tv_usec = 0;
1161  ptr_timeout = &timeout;
1162  }
1163 
1164  return select(sock + 1, &input_mask, &output_mask,
1165  &except_mask, ptr_timeout);
1166 #endif /* HAVE_POLL */
1167 }
#define select(n, r, w, e, timeout)
Definition: win32.h:374
int64 end_time
Definition: pgbench.c:100
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
int pqWait ( int  forRead,
int  forWrite,
PGconn conn 
)

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

References pqWaitTimed().

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

985 {
986  return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
987 }
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:997
int pqWaitTimed ( int  forRead,
int  forWrite,
PGconn conn,
time_t  finish_time 
)

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

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

Referenced by connectDBComplete(), and pqWait().

998 {
999  int result;
1000 
1001  result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1002 
1003  if (result < 0)
1004  return -1; /* errorMessage is already set */
1005 
1006  if (result == 0)
1007  {
1009  libpq_gettext("timeout expired\n"));
1010  return 1;
1011  }
1012 
1013  return 0;
1014 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1045
PQExpBufferData errorMessage
Definition: libpq-int.h:491
#define libpq_gettext(x)
Definition: libpq-int.h:682
int pqWriteReady ( PGconn conn)

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

References pqSocketCheck().

1032 {
1033  return pqSocketCheck(conn, 0, 1, (time_t) 0);
1034 }
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1045