PostgreSQL Source Code git master
Loading...
Searching...
No Matches
fe-misc.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <sys/select.h>
#include <poll.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, pg_usec_time_t end_time)
 
static int pqReadData_internal (PGconn *conn)
 
static int pqDrainPending (PGconn *conn)
 
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 (void *s, size_t len, PGconn *conn)
 
int pqSkipnchar (size_t len, PGconn *conn)
 
int pqPutnchar (const void *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)
 
void pqParseDone (PGconn *conn, int newInStart)
 
int pqPutMsgStart (char msg_type, 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, pg_usec_time_t end_time)
 
int pqReadReady (PGconn *conn)
 
int pqWriteReady (PGconn *conn)
 
int PQsocketPoll (int sock, int forRead, int forWrite, pg_usec_time_t end_time)
 
pg_usec_time_t PQgetCurrentTimeUSec (void)
 
int PQmblen (const char *s, int encoding)
 
int PQmblenBounded (const char *s, int encoding)
 
int PQdsplen (const char *s, int encoding)
 
int PQenv2encoding (void)
 
void libpq_append_error (PQExpBuffer errorMessage, const char *fmt,...)
 
void libpq_append_conn_error (PGconn *conn, const char *fmt,...)
 
void libpq_append_grease_info (PGconn *conn)
 

Function Documentation

◆ libpq_append_conn_error()

void libpq_append_conn_error ( PGconn conn,
const char fmt,
  ... 
)

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

1549{
1550 int save_errno = errno;
1551 bool done;
1552 va_list args;
1553
1554 Assert(fmt[strlen(fmt) - 1] != '\n');
1555
1557 return; /* already failed */
1558
1559 /* Loop in case we have to retry after enlarging the buffer. */
1560 do
1561 {
1562 errno = save_errno;
1563 va_start(args, fmt);
1565 va_end(args);
1566 } while (!done);
1567
1569}
#define Assert(condition)
Definition c.h:1002
#define libpq_gettext(x)
Definition oauth-utils.h:44
bool appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
#define PQExpBufferBroken(str)
Definition pqexpbuffer.h:59
static int fb(int x)
PGconn * conn
Definition streamutil.c:52
PQExpBufferData errorMessage
Definition libpq-int.h:686

References appendPQExpBufferChar(), appendPQExpBufferVA(), Assert, conn, pg_conn::errorMessage, fb(), libpq_gettext, and PQExpBufferBroken.

Referenced by build_client_final_message(), build_client_first_message(), check_expected_areq(), client_initial_response(), connectFailureMessage(), connectNoDelay(), fillPGconn(), getBackendKeyData(), getNotify(), getParameterStatus(), handle_oauth_sasl_error(), handleSyncLoss(), init_allowed_encryption_methods(), initialize_SSL(), issuer_from_well_known_uri(), libpq_append_grease_info(), lo_create(), lo_export(), lo_import_internal(), lo_initialize(), lo_lseek64(), lo_read(), lo_tell64(), lo_truncate(), lo_truncate64(), lo_write(), oauth_exchange(), open_client_SSL(), openssl_verify_peer_name_matches_certificate_ip(), openssl_verify_peer_name_matches_certificate_name(), pg_fe_sendauth(), pg_GSS_load_servicename(), pg_GSS_read(), pg_GSS_write(), pg_password_sendauth(), pg_SASL_continue(), pg_SASL_init(), pgpassfileWarning(), pgtls_bytes_pending(), pgtls_get_peer_certificate_hash(), pgtls_read(), pgtls_write(), pq_verify_peer_name_matches_certificate(), pq_verify_peer_name_matches_certificate_ip(), pq_verify_peer_name_matches_certificate_name(), pqAllocCmdQueueEntry(), PQcancelCreate(), PQcancelPoll(), PQcancelStart(), pqConnectOptions2(), PQconnectPoll(), pqCopyPGconn(), pqDrainPending(), PQencryptPasswordConn(), pqEndcopy3(), PQenterPipelineMode(), PQescapeByteaInternal(), PQescapeInternal(), PQescapeStringInternal(), PQexecStart(), PQexitPipelineMode(), pqFunctionCall3(), PQgetCopyData(), pqGetCopyData3(), pqGetErrorNotice3(), pqGetline3(), pqGetNegotiateProtocolVersion3(), PQgetResult(), PQnfn(), pqParseInput3(), pqParseIntParam(), pqParseProtocolVersion(), pqPipelineProcessQueue(), pqPipelineSyncInternal(), pqPrepareAsyncResult(), PQputCopyData(), PQputCopyEnd(), pqReadData(), pqReadData_internal(), pqSaveWriteError(), pqsecure_open_gss(), pqsecure_raw_read(), PQsendFlushRequest(), PQsendPrepare(), PQsendQueryGuts(), PQsendQueryInternal(), PQsendQueryParams(), PQsendQueryPrepared(), PQsendQueryStart(), PQsendTypedCommand(), PQsetdbLogin(), pqSocketCheck(), pqWaitTimed(), read_server_final_message(), read_server_first_message(), run_oauth_flow(), scram_exchange(), select_next_encryption_method(), setKeepalivesCount(), setKeepalivesIdle(), setKeepalivesInterval(), setTCPUserTimeout(), setup_oauth_parameters(), setup_token_request(), and store_conn_addrinfo().

◆ libpq_append_error()

void libpq_append_error ( PQExpBuffer  errorMessage,
const char fmt,
  ... 
)

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

1520{
1521 int save_errno = errno;
1522 bool done;
1523 va_list args;
1524
1525 Assert(fmt[strlen(fmt) - 1] != '\n');
1526
1527 if (PQExpBufferBroken(errorMessage))
1528 return; /* already failed */
1529
1530 /* Loop in case we have to retry after enlarging the buffer. */
1531 do
1532 {
1533 errno = save_errno;
1534 va_start(args, fmt);
1535 done = appendPQExpBufferVA(errorMessage, libpq_gettext(fmt), args);
1536 va_end(args);
1537 } while (!done);
1538
1539 appendPQExpBufferChar(errorMessage, '\n');
1540}

References appendPQExpBufferChar(), appendPQExpBufferVA(), Assert, fb(), libpq_gettext, and PQExpBufferBroken.

Referenced by conninfo_add_defaults(), conninfo_array_parse(), conninfo_init(), conninfo_parse(), conninfo_storeval(), conninfo_uri_decode(), conninfo_uri_parse_options(), conninfo_uri_parse_params(), parseServiceFile(), parseServiceInfo(), pg_fe_getusername(), and read_attr_value().

◆ libpq_append_grease_info()

void libpq_append_grease_info ( PGconn conn)

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

1577{
1578 /* translator: %s is a URL */
1580 "\tThis indicates a bug in either the server being contacted\n"
1581 "\tor a proxy handling the connection. Please consider\n"
1582 "\treporting this to the maintainers of that software.\n"
1583 "\tFor more information, including instructions on how to\n"
1584 "\twork around this issue for now, visit\n"
1585 "\t\t%s",
1586 "https://wiki.postgresql.org/wiki/Grease");
1587}
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition fe-misc.c:1548

References conn, and libpq_append_conn_error().

Referenced by PQconnectPoll(), and pqGetNegotiateProtocolVersion3().

◆ pqCheckInBufferSpace()

int pqCheckInBufferSpace ( size_t  bytes_needed,
PGconn conn 
)

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

354{
355 int newsize = conn->inBufSize;
356 char *newbuf;
357
358 /* Quick exit if we have enough space */
359 if (bytes_needed <= (size_t) newsize)
360 return 0;
361
362 /*
363 * Before concluding that we need to enlarge the buffer, left-justify
364 * whatever is in it and recheck. The caller's value of bytes_needed
365 * includes any data to the left of inStart, but we can delete that in
366 * preference to enlarging the buffer. It's slightly ugly to have this
367 * function do this, but it's better than making callers worry about it.
368 */
370
371 if (conn->inStart < conn->inEnd)
372 {
373 if (conn->inStart > 0)
374 {
376 conn->inEnd - conn->inStart);
377 conn->inEnd -= conn->inStart;
379 conn->inStart = 0;
380 }
381 }
382 else
383 {
384 /* buffer is logically empty, reset it */
385 conn->inStart = conn->inCursor = conn->inEnd = 0;
386 }
387
388 /* Recheck whether we have enough space */
389 if (bytes_needed <= (size_t) newsize)
390 return 0;
391
392 /*
393 * If we need to enlarge the buffer, we first try to double it in size; if
394 * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
395 * the malloc pool by repeated small enlargements.
396 *
397 * Note: tests for newsize > 0 are to catch integer overflow.
398 */
399 do
400 {
401 newsize *= 2;
402 } while (newsize > 0 && bytes_needed > (size_t) newsize);
403
404 if (newsize > 0 && bytes_needed <= (size_t) newsize)
405 {
407 if (newbuf)
408 {
409 /* realloc succeeded */
412 return 0;
413 }
414 }
415
417 do
418 {
419 newsize += 8192;
420 } while (newsize > 0 && bytes_needed > (size_t) newsize);
421
422 if (newsize > 0 && bytes_needed <= (size_t) newsize)
423 {
425 if (newbuf)
426 {
427 /* realloc succeeded */
430 return 0;
431 }
432 }
433
434 /* realloc failed. Probably out of memory */
436 "cannot allocate memory for input buffer\n");
437 return EOF;
438}
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
#define realloc(a, b)
char * inBuffer
Definition libpq-int.h:579
int inCursor
Definition libpq-int.h:582
int inEnd
Definition libpq-int.h:583
int inBufSize
Definition libpq-int.h:580
int inStart
Definition libpq-int.h:581

References appendPQExpBufferStr(), conn, pg_conn::errorMessage, fb(), pg_conn::inBuffer, pg_conn::inBufSize, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, and realloc.

Referenced by getCopyDataMessage(), PQconnectPoll(), pqDrainPending(), pqFunctionCall3(), pqParseInput3(), and pqReadData_internal().

◆ pqCheckOutBufferSpace()

int pqCheckOutBufferSpace ( size_t  bytes_needed,
PGconn conn 
)

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

290{
291 int newsize = conn->outBufSize;
292 char *newbuf;
293
294 /* Quick exit if we have enough space */
295 if (bytes_needed <= (size_t) newsize)
296 return 0;
297
298 /*
299 * If we need to enlarge the buffer, we first try to double it in size; if
300 * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
301 * the malloc pool by repeated small enlargements.
302 *
303 * Note: tests for newsize > 0 are to catch integer overflow.
304 */
305 do
306 {
307 newsize *= 2;
308 } while (newsize > 0 && bytes_needed > (size_t) newsize);
309
310 if (newsize > 0 && bytes_needed <= (size_t) newsize)
311 {
313 if (newbuf)
314 {
315 /* realloc succeeded */
318 return 0;
319 }
320 }
321
323 do
324 {
325 newsize += 8192;
326 } while (newsize > 0 && bytes_needed > (size_t) newsize);
327
328 if (newsize > 0 && bytes_needed <= (size_t) newsize)
329 {
331 if (newbuf)
332 {
333 /* realloc succeeded */
336 return 0;
337 }
338 }
339
340 /* realloc failed. Probably out of memory */
342 "cannot allocate memory for output buffer\n");
343 return EOF;
344}
int outBufSize
Definition libpq-int.h:587
char * outBuffer
Definition libpq-int.h:586

References appendPQExpBufferStr(), conn, pg_conn::errorMessage, fb(), pg_conn::outBuffer, pg_conn::outBufSize, and realloc.

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

◆ pqDrainPending()

static int pqDrainPending ( PGconn conn)
static

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

917{
920
922 if (bytes_pending <= 0)
923 return bytes_pending;
924
925 /* Expand the input buffer if necessary. */
927 return -1; /* errorMessage already set */
928
931
932 /*
933 * When there are bytes pending, pqsecure_read() is not supposed to fail
934 * or do a short read, but let's check anyway to be safe.
935 */
936 if (nread < 0)
937 return -1;
938 conn->inEnd += nread;
939 if (nread != bytes_pending)
940 {
942 "drained only %zd of %zd pending bytes in transport buffer",
944 return -1;
945 }
946 return 0;
947}
int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
Definition fe-misc.c:353
ssize_t pqsecure_bytes_pending(PGconn *conn)
Definition fe-secure.c:255
ssize_t pqsecure_read(PGconn *conn, void *ptr, size_t len)
Definition fe-secure.c:167

References conn, fb(), pg_conn::inBuffer, pg_conn::inEnd, libpq_append_conn_error(), pqCheckInBufferSpace(), pqsecure_bytes_pending(), and pqsecure_read().

Referenced by pqReadData().

◆ PQdsplen()

int PQdsplen ( const char s,
int  encoding 
)

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

1421{
1422 return pg_encoding_dsplen(encoding, s);
1423}
static char * encoding
Definition initdb.c:139
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition wchar.c:1976

References encoding, and pg_encoding_dsplen().

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

◆ PQenv2encoding()

int PQenv2encoding ( void  )

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

1430{
1431 char *str;
1432 int encoding = PG_SQL_ASCII;
1433
1434 str = getenv("PGCLIENTENCODING");
1435 if (str && *str != '\0')
1436 {
1438 if (encoding < 0)
1440 }
1441 return encoding;
1442}
const char * str
@ PG_SQL_ASCII
Definition pg_wchar.h:76
#define pg_char_to_encoding
Definition pg_wchar.h:482

References encoding, fb(), pg_char_to_encoding, PG_SQL_ASCII, and str.

Referenced by main().

◆ pqFlush()

int pqFlush ( PGconn conn)

◆ pqGetc()

int pqGetc ( char result,
PGconn conn 
)

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

80{
81 if (conn->inCursor >= conn->inEnd)
82 return EOF;
83
85
86 return 0;
87}
uint32 result

References conn, fb(), pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, and result.

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

◆ PQgetCurrentTimeUSec()

pg_usec_time_t PQgetCurrentTimeUSec ( void  )

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

1380{
1381 struct timeval tval;
1382
1384 return (pg_usec_time_t) tval.tv_sec * 1000000 + tval.tv_usec;
1385}
int64_t pg_usec_time_t
Definition libpq-fe.h:251
int gettimeofday(struct timeval *tp, void *tzp)

References fb(), and gettimeofday().

Referenced by pqConnectDBComplete(), PQsocketPoll(), and wait_until_connected().

◆ pqGetInt()

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

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

219{
220 uint16 tmp2;
221 uint32 tmp4;
222
223 switch (bytes)
224 {
225 case 2:
226 if (conn->inCursor + 2 > conn->inEnd)
227 return EOF;
229 conn->inCursor += 2;
230 *result = (int) pg_ntoh16(tmp2);
231 break;
232 case 4:
233 if (conn->inCursor + 4 > conn->inEnd)
234 return EOF;
236 conn->inCursor += 4;
237 *result = (int) pg_ntoh32(tmp4);
238 break;
239 default:
241 "integer of size %zu not supported by pqGetInt",
242 bytes);
243 return EOF;
244 }
245
246 return 0;
247}
uint16_t uint16
Definition c.h:682
uint32_t uint32
Definition c.h:683
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
Definition fe-exec.c:944
#define pg_ntoh32(x)
Definition pg_bswap.h:125
#define pg_ntoh16(x)
Definition pg_bswap.h:124
PGNoticeHooks noticeHooks
Definition libpq-int.h:457

References conn, fb(), pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, memcpy(), pg_conn::noticeHooks, pg_ntoh16, pg_ntoh32, pqInternalNotice(), and result.

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

◆ pqGetnchar()

int pqGetnchar ( void s,
size_t  len,
PGconn conn 
)

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

168{
169 if (len > (size_t) (conn->inEnd - conn->inCursor))
170 return EOF;
171
173 /* no terminating null */
174
175 conn->inCursor += len;
176
177 return 0;
178}
const void size_t len

References conn, fb(), pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, len, and memcpy().

Referenced by getBackendKeyData(), pg_password_sendauth(), pg_SASL_continue(), and pqFunctionCall3().

◆ pqGets()

int pqGets ( PQExpBuffer  buf,
PGconn conn 
)

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

139{
140 return pqGets_internal(buf, conn, true);
141}
static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
Definition fe-misc.c:111
static char buf[DEFAULT_XLOG_SEG_SIZE]

References buf, conn, and pqGets_internal().

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

◆ pqGets_append()

int pqGets_append ( PQExpBuffer  buf,
PGconn conn 
)

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

145{
146 return pqGets_internal(buf, conn, false);
147}

References buf, conn, and pqGets_internal().

Referenced by PQconnectPoll().

◆ pqGets_internal()

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

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

112{
113 /* Copy conn data to locals for faster search loop */
114 char *inBuffer = conn->inBuffer;
115 int inCursor = conn->inCursor;
116 int inEnd = conn->inEnd;
117 int slen;
118
119 while (inCursor < inEnd && inBuffer[inCursor])
120 inCursor++;
121
122 if (inCursor >= inEnd)
123 return EOF;
124
125 slen = inCursor - conn->inCursor;
126
127 if (resetbuffer)
129
131
132 conn->inCursor = ++inCursor;
133
134 return 0;
135}
void resetPQExpBuffer(PQExpBuffer str)
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)

References appendBinaryPQExpBuffer(), buf, conn, fb(), pg_conn::inBuffer, pg_conn::inCursor, pg_conn::inEnd, and resetPQExpBuffer().

Referenced by pqGets(), and pqGets_append().

◆ PQlibVersion()

int PQlibVersion ( void  )

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

66{
67 return PG_VERSION_NUM;
68}

References fb().

◆ PQmblen()

int PQmblen ( const char s,
int  encoding 
)

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

1400{
1401 return pg_encoding_mblen(encoding, s);
1402}
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition wchar.c:1935

References encoding, and pg_encoding_mblen().

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

◆ PQmblenBounded()

◆ pqParseDone()

void pqParseDone ( PGconn conn,
int  newInStart 
)

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

446{
447 /* trace server-to-client message */
448 if (conn->Pfdebug)
450
451 /* Mark message as done */
453}
void pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
Definition fe-trace.c:625

References conn, fb(), pg_conn::inBuffer, pg_conn::inStart, pg_conn::Pfdebug, and pqTraceOutputMessage().

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

◆ pqPutc()

int pqPutc ( char  c,
PGconn conn 
)

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

95{
96 if (pqPutMsgBytes(&c, 1, conn))
97 return EOF;
98
99 return 0;
100}
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition fe-misc.c:511
char * c

References conn, fb(), and pqPutMsgBytes().

Referenced by PQsendQueryGuts(), and PQsendTypedCommand().

◆ pqPutInt()

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

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

256{
257 uint16 tmp2;
258 uint32 tmp4;
259
260 switch (bytes)
261 {
262 case 2:
264 if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
265 return EOF;
266 break;
267 case 4:
269 if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
270 return EOF;
271 break;
272 default:
274 "integer of size %zu not supported by pqPutInt",
275 bytes);
276 return EOF;
277 }
278
279 return 0;
280}
static struct @175 value
#define pg_hton32(x)
Definition pg_bswap.h:121
#define pg_hton16(x)
Definition pg_bswap.h:120

References conn, fb(), pg_conn::noticeHooks, pg_hton16, pg_hton32, pqInternalNotice(), pqPutMsgBytes(), and value.

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

◆ pqPutMsgBytes()

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

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

512{
513 /* make sure there is room for it */
515 return EOF;
516 /* okay, save the data */
518 conn->outMsgEnd += len;
519 /* no Pfdebug call here, caller should do it */
520 return 0;
521}
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition fe-misc.c:289
int outMsgEnd
Definition libpq-int.h:593

References buf, conn, fb(), len, memcpy(), pg_conn::outBuffer, pg_conn::outMsgEnd, and pqCheckOutBufferSpace().

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

◆ pqPutMsgEnd()

int pqPutMsgEnd ( PGconn conn)

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

535{
536 /* Fill in length word if needed */
537 if (conn->outMsgStart >= 0)
538 {
540
543 }
544
545 /* trace client-to-server message */
546 if (conn->Pfdebug)
547 {
550 else
553 }
554
555 /* Make message eligible to send */
557
558 /* If appropriate, try to push out some data */
559 if (conn->outCount >= 8192)
560 {
561 int toSend = conn->outCount;
562
563 /*
564 * On Unix-pipe connections, it seems profitable to prefer sending
565 * pipe-buffer-sized packets not randomly-sized ones, so retain the
566 * last partial-8K chunk in our buffer for now. On TCP connections,
567 * the advantage of that is far less clear. Moreover, it flat out
568 * isn't safe when using SSL or GSSAPI, because those code paths have
569 * API stipulations that if they fail to send all the data that was
570 * offered in the previous write attempt, we mustn't offer less data
571 * in this write attempt. The previous write attempt might've been
572 * pqFlush attempting to send everything in the buffer, so we mustn't
573 * offer less now. (Presently, we won't try to use SSL or GSSAPI on
574 * Unix connections, so those checks are just Asserts. They'll have
575 * to become part of the regular if-test if we ever change that.)
576 */
577 if (conn->raddr.addr.ss_family == AF_UNIX)
578 {
579#ifdef USE_SSL
581#endif
582#ifdef ENABLE_GSS
583 Assert(!conn->gssenc);
584#endif
585 toSend -= toSend % 8192;
586 }
587
588 if (pqSendSome(conn, toSend) < 0)
589 return EOF;
590 /* in nonblock mode, don't complain if unable to send it all */
591 }
592
593 return 0;
594}
void pqTraceOutputNoTypeByteMessage(PGconn *conn, const char *message)
Definition fe-trace.c:843
struct sockaddr_storage addr
Definition pqcomm.h:32
int outMsgStart
Definition libpq-int.h:591
SockAddr raddr
Definition libpq-int.h:505
bool ssl_in_use
Definition libpq-int.h:623

References SockAddr::addr, Assert, conn, fb(), memcpy(), pg_conn::outBuffer, pg_conn::outCount, pg_conn::outMsgEnd, pg_conn::outMsgStart, pg_conn::Pfdebug, pg_hton32, pqSendSome(), pqTraceOutputMessage(), pqTraceOutputNoTypeByteMessage(), pg_conn::raddr, and pg_conn::ssl_in_use.

Referenced by pg_SASL_init(), pqEndcopy3(), pqFunctionCall3(), pqPacketSend(), pqPipelineSyncInternal(), PQputCopyData(), PQputCopyEnd(), PQsendCancelRequest(), PQsendFlushRequest(), PQsendPrepare(), PQsendQueryGuts(), PQsendQueryInternal(), PQsendTypedCommand(), and sendTerminateConn().

◆ pqPutMsgStart()

int pqPutMsgStart ( char  msg_type,
PGconn conn 
)

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

476{
477 int lenPos;
478 int endPos;
479
480 /* allow room for message type byte */
481 if (msg_type)
482 endPos = conn->outCount + 1;
483 else
485
486 /* do we want a length word? */
487 lenPos = endPos;
488 /* allow room for message length */
489 endPos += 4;
490
491 /* make sure there is room for message header */
493 return EOF;
494 /* okay, save the message type byte if any */
495 if (msg_type)
497 /* set up the message pointers */
500 /* length word, if needed, will be filled in by pqPutMsgEnd */
501
502 return 0;
503}

References conn, fb(), pg_conn::outBuffer, pg_conn::outCount, pg_conn::outMsgEnd, pg_conn::outMsgStart, and pqCheckOutBufferSpace().

Referenced by pg_SASL_init(), pqEndcopy3(), pqFunctionCall3(), pqPacketSend(), pqPipelineSyncInternal(), PQputCopyData(), PQputCopyEnd(), PQsendCancelRequest(), PQsendFlushRequest(), PQsendPrepare(), PQsendQueryGuts(), PQsendQueryInternal(), PQsendTypedCommand(), and sendTerminateConn().

◆ pqPutnchar()

int pqPutnchar ( const void s,
size_t  len,
PGconn conn 
)

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

205{
206 if (pqPutMsgBytes(s, len, conn))
207 return EOF;
208
209 return 0;
210}

References conn, fb(), len, and pqPutMsgBytes().

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

◆ pqPuts()

int pqPuts ( const char s,
PGconn conn 
)

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

155{
156 if (pqPutMsgBytes(s, strlen(s) + 1, conn))
157 return EOF;
158
159 return 0;
160}

References conn, fb(), and pqPutMsgBytes().

Referenced by pg_SASL_init(), PQputCopyEnd(), PQsendPrepare(), PQsendQueryGuts(), PQsendQueryInternal(), and PQsendTypedCommand().

◆ pqReadData()

int pqReadData ( PGconn conn)

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

616{
617 int available;
618
619 if (conn->sock == PGINVALID_SOCKET)
620 {
621 libpq_append_conn_error(conn, "connection not open");
622 return -1;
623 }
624
626 if (available < 0)
627 return -1;
628 else if (available > 0)
629 {
630 /*
631 * Make sure there are no bytes stuck in layers between conn->inBuffer
632 * and the socket, to make it safe for clients to poll on PQsocket().
633 */
634 if (pqDrainPending(conn))
635 return -1;
636 }
637 else
638 {
639 /*
640 * If we're not returning any bytes from the underlying transport,
641 * that must imply there aren't any in the transport buffer...
642 */
644 }
645
646 return available;
647}
static int pqReadData_internal(PGconn *conn)
Definition fe-misc.c:654
static int pqDrainPending(PGconn *conn)
Definition fe-misc.c:916
#define PGINVALID_SOCKET
Definition port.h:31
pgsocket sock
Definition libpq-int.h:502

References Assert, conn, fb(), libpq_append_conn_error(), PGINVALID_SOCKET, pqDrainPending(), pqReadData_internal(), pqsecure_bytes_pending(), and pg_conn::sock.

Referenced by PQcancelPoll(), PQconnectPoll(), PQconsumeInput(), pqFunctionCall3(), pqGetCopyData3(), pqGetline3(), PQgetResult(), and pqSendSome().

◆ pqReadData_internal()

static int pqReadData_internal ( PGconn conn)
static

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

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

References ALL_CONNECTION_FAILURE_ERRNOS, conn, CONNECTION_BAD, EAGAIN, EINTR, EWOULDBLOCK, fb(), pg_conn::inBuffer, pg_conn::inBufSize, pg_conn::inCursor, pg_conn::inEnd, pg_conn::inStart, libpq_append_conn_error(), pqCheckInBufferSpace(), pqDropConnection(), pqReadReady(), pqsecure_read(), SOCK_ERRNO, pg_conn::ssl_in_use, and pg_conn::status.

Referenced by pqReadData().

◆ pqReadReady()

int pqReadReady ( PGconn conn)

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

1205{
1206 return pqSocketCheck(conn, 1, 0, 0);
1207}
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, pg_usec_time_t end_time)
Definition fe-misc.c:1229

References conn, and pqSocketCheck().

Referenced by gss_read(), and pqReadData_internal().

◆ pqSendSome()

static int pqSendSome ( PGconn conn,
int  len 
)
static

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

972{
973 char *ptr = conn->outBuffer;
974 int remaining = conn->outCount;
975 int result = 0;
976
977 /*
978 * If we already had a write failure, we will never again try to send data
979 * on that connection. Even if the kernel would let us, we've probably
980 * lost message boundary sync with the server. conn->write_failed
981 * therefore persists until the connection is reset, and we just discard
982 * all data presented to be written. However, as long as we still have a
983 * valid socket, we should continue to absorb data from the backend, so
984 * that we can collect any final error messages.
985 */
986 if (conn->write_failed)
987 {
988 /* conn->write_err_msg should be set up already */
989 conn->outCount = 0;
990 /* Absorb input data if any, and detect socket closure */
991 if (conn->sock != PGINVALID_SOCKET)
992 {
993 if (pqReadData(conn) < 0)
994 return -1;
995 }
996 return 0;
997 }
998
999 if (conn->sock == PGINVALID_SOCKET)
1000 {
1001 conn->write_failed = true;
1002 /* Store error message in conn->write_err_msg, if possible */
1003 /* (strdup failure is OK, we'll cope later) */
1004 conn->write_err_msg = strdup(libpq_gettext("connection not open\n"));
1005 /* Discard queued data; no chance it'll ever be sent */
1006 conn->outCount = 0;
1007 return 0;
1008 }
1009
1010 /* while there's still data to send */
1011 while (len > 0)
1012 {
1013 ssize_t sent;
1014
1015#ifndef WIN32
1016 sent = pqsecure_write(conn, ptr, len);
1017#else
1018
1019 /*
1020 * Windows can fail on large sends, per KB article Q201213. The
1021 * failure-point appears to be different in different versions of
1022 * Windows, but 64k should always be safe.
1023 */
1024 sent = pqsecure_write(conn, ptr, Min(len, 65536));
1025#endif
1026
1027 if (sent < 0)
1028 {
1029 /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
1030 switch (SOCK_ERRNO)
1031 {
1032#ifdef EAGAIN
1033 case EAGAIN:
1034 break;
1035#endif
1036#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1037 case EWOULDBLOCK:
1038 break;
1039#endif
1040 case EINTR:
1041 continue;
1042
1043 default:
1044 /* Discard queued data; no chance it'll ever be sent */
1045 conn->outCount = 0;
1046
1047 /* Absorb input data if any, and detect socket closure */
1048 if (conn->sock != PGINVALID_SOCKET)
1049 {
1050 if (pqReadData(conn) < 0)
1051 return -1;
1052 }
1053
1054 /*
1055 * Lower-level code should already have filled
1056 * conn->write_err_msg (and set conn->write_failed) or
1057 * conn->errorMessage. In the former case, we pretend
1058 * there's no problem; the write_failed condition will be
1059 * dealt with later. Otherwise, report the error now.
1060 */
1061 if (conn->write_failed)
1062 return 0;
1063 else
1064 return -1;
1065 }
1066 }
1067 else
1068 {
1069 ptr += sent;
1070 len -= sent;
1071 remaining -= sent;
1072 }
1073
1074 if (len > 0)
1075 {
1076 /*
1077 * We didn't send it all, wait till we can send more.
1078 *
1079 * There are scenarios in which we can't send data because the
1080 * communications channel is full, but we cannot expect the server
1081 * to clear the channel eventually because it's blocked trying to
1082 * send data to us. (This can happen when we are sending a large
1083 * amount of COPY data, and the server has generated lots of
1084 * NOTICE responses.) To avoid a deadlock situation, we must be
1085 * prepared to accept and buffer incoming data before we try
1086 * again. Furthermore, it is possible that such incoming data
1087 * might not arrive until after we've gone to sleep. Therefore,
1088 * we wait for either read ready or write ready.
1089 *
1090 * In non-blocking mode, we don't wait here directly, but return 1
1091 * to indicate that data is still pending. The caller should wait
1092 * for both read and write ready conditions, and call
1093 * PQconsumeInput() on read ready, but just in case it doesn't, we
1094 * call pqReadData() ourselves before returning. That's not
1095 * enough if the data has not arrived yet, but it's the best we
1096 * can do, and works pretty well in practice. (The documentation
1097 * used to say that you only need to wait for write-ready, so
1098 * there are still plenty of applications like that out there.)
1099 *
1100 * Note that errors here don't result in write_failed becoming
1101 * set.
1102 */
1103 if (pqReadData(conn) < 0)
1104 {
1105 result = -1; /* error message already set up */
1106 break;
1107 }
1108
1109 if (pqIsnonblocking(conn))
1110 {
1111 result = 1;
1112 break;
1113 }
1114
1115 if (pqWait(true, true, conn))
1116 {
1117 result = -1;
1118 break;
1119 }
1120 }
1121 }
1122
1123 /* shift the remaining contents of the buffer */
1124 if (remaining > 0)
1127
1128 return result;
1129}
#define Min(x, y)
Definition c.h:1131
int pqReadData(PGconn *conn)
Definition fe-misc.c:615
int pqWait(int forRead, int forWrite, PGconn *conn)
Definition fe-misc.c:1165
ssize_t pqsecure_write(PGconn *conn, const void *ptr, size_t len)
Definition fe-secure.c:291
int remaining
Definition informix.c:692
#define pqIsnonblocking(conn)
Definition libpq-int.h:949
char * write_err_msg
Definition libpq-int.h:516
bool write_failed
Definition libpq-int.h:515

References conn, EAGAIN, EINTR, EWOULDBLOCK, fb(), len, libpq_gettext, Min, pg_conn::outBuffer, pg_conn::outCount, PGINVALID_SOCKET, pqIsnonblocking, pqReadData(), pqsecure_write(), pqWait(), remaining, result, pg_conn::sock, SOCK_ERRNO, pg_conn::write_err_msg, and pg_conn::write_failed.

Referenced by pqFlush(), and pqPutMsgEnd().

◆ pqSkipnchar()

int pqSkipnchar ( size_t  len,
PGconn conn 
)

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

190{
191 if (len > (size_t) (conn->inEnd - conn->inCursor))
192 return EOF;
193
194 conn->inCursor += len;
195
196 return 0;
197}

References conn, fb(), pg_conn::inCursor, pg_conn::inEnd, and len.

Referenced by getAnotherTuple().

◆ pqSocketCheck()

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

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

1230{
1231 int result;
1232 pgsocket sock;
1233
1234 if (!conn)
1235 return -1;
1236
1238 sock = conn->altsock;
1239 else
1240 {
1241 sock = conn->sock;
1242 if (sock == PGINVALID_SOCKET)
1243 {
1244 libpq_append_conn_error(conn, "invalid socket");
1245 return -1;
1246 }
1247
1248 /* Check for SSL/GSS library buffering read bytes */
1249 if (forRead && pqsecure_bytes_pending(conn) != 0)
1250 {
1251 /* short-circuit the select */
1252 return 1;
1253 }
1254 }
1255
1256 /* We will retry as long as we get EINTR */
1257 do
1259 while (result < 0 && SOCK_ERRNO == EINTR);
1260
1261 if (result < 0)
1262 {
1264
1265 libpq_append_conn_error(conn, "%s() failed: %s", "select",
1266 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1267 }
1268
1269 return result;
1270}
int PQsocketPoll(int sock, int forRead, int forWrite, pg_usec_time_t end_time)
Definition fe-misc.c:1285
#define SOCK_STRERROR
Definition libpq-int.h:983
static int64 end_time
Definition pgbench.c:176
#define PG_STRERROR_R_BUFLEN
Definition port.h:279
int pgsocket
Definition port.h:29
pgsocket altsock
Definition libpq-int.h:533

References pg_conn::altsock, conn, EINTR, end_time, fb(), libpq_append_conn_error(), PG_STRERROR_R_BUFLEN, PGINVALID_SOCKET, pqsecure_bytes_pending(), PQsocketPoll(), result, pg_conn::sock, SOCK_ERRNO, and SOCK_STRERROR.

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

◆ PQsocketPoll()

int PQsocketPoll ( int  sock,
int  forRead,
int  forWrite,
pg_usec_time_t  end_time 
)

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

1286{
1287 /* We use poll(2) if available, otherwise select(2) */
1288#ifdef HAVE_POLL
1289 struct pollfd input_fd;
1290 int timeout_ms;
1291
1292 if (!forRead && !forWrite)
1293 return 0;
1294
1295 input_fd.fd = sock;
1296 input_fd.events = POLLERR;
1297 input_fd.revents = 0;
1298
1299 if (forRead)
1300 input_fd.events |= POLLIN;
1301 if (forWrite)
1302 input_fd.events |= POLLOUT;
1303
1304 /* Compute appropriate timeout interval */
1305 if (end_time == -1)
1306 timeout_ms = -1;
1307 else if (end_time == 0)
1308 timeout_ms = 0;
1309 else
1310 {
1312
1313 if (end_time > now)
1314 timeout_ms = (end_time - now) / 1000;
1315 else
1316 timeout_ms = 0;
1317 }
1318
1319 return poll(&input_fd, 1, timeout_ms);
1320#else /* !HAVE_POLL */
1321
1325 struct timeval timeout;
1326 struct timeval *ptr_timeout;
1327
1328 if (!forRead && !forWrite)
1329 return 0;
1330
1334 if (forRead)
1335 FD_SET(sock, &input_mask);
1336
1337 if (forWrite)
1338 FD_SET(sock, &output_mask);
1339 FD_SET(sock, &except_mask);
1340
1341 /* Compute appropriate timeout interval */
1342 if (end_time == -1)
1343 ptr_timeout = NULL;
1344 else if (end_time == 0)
1345 {
1346 timeout.tv_sec = 0;
1347 timeout.tv_usec = 0;
1349 }
1350 else
1351 {
1353
1354 if (end_time > now)
1355 {
1356 timeout.tv_sec = (end_time - now) / 1000000;
1357 timeout.tv_usec = (end_time - now) % 1000000;
1358 }
1359 else
1360 {
1361 timeout.tv_sec = 0;
1362 timeout.tv_usec = 0;
1363 }
1365 }
1366
1367 return select(sock + 1, &input_mask, &output_mask,
1369#endif /* HAVE_POLL */
1370}
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1613
pg_usec_time_t PQgetCurrentTimeUSec(void)
Definition fe-misc.c:1379
#define select(n, r, w, e, timeout)
Definition win32_port.h:517

References end_time, fb(), now(), PQgetCurrentTimeUSec(), and select.

Referenced by pqSocketCheck(), timer_expired(), and wait_until_connected().

◆ pqWait()

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

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

1166{
1167 return pqWaitTimed(forRead, forWrite, conn, -1);
1168}
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, pg_usec_time_t end_time)
Definition fe-misc.c:1181

References conn, fb(), and pqWaitTimed().

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

◆ pqWaitTimed()

int pqWaitTimed ( int  forRead,
int  forWrite,
PGconn conn,
pg_usec_time_t  end_time 
)

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

1182{
1183 int result;
1184
1186
1187 if (result < 0)
1188 return -1; /* errorMessage is already set */
1189
1190 if (result == 0)
1191 {
1192 libpq_append_conn_error(conn, "timeout expired");
1193 return 1;
1194 }
1195
1196 return 0;
1197}

References conn, end_time, fb(), libpq_append_conn_error(), pqSocketCheck(), and result.

Referenced by pqConnectDBComplete(), and pqWait().

◆ pqWriteReady()

int pqWriteReady ( PGconn conn)

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

1215{
1216 return pqSocketCheck(conn, 0, 1, 0);
1217}

References conn, and pqSocketCheck().