PostgreSQL Source Code  git master
fe-misc.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * FILE
4  * fe-misc.c
5  *
6  * DESCRIPTION
7  * miscellaneous useful functions
8  *
9  * The communication routines here are analogous to the ones in
10  * backend/libpq/pqcomm.c and backend/libpq/pqformat.c, but operate
11  * in the considerably different environment of the frontend libpq.
12  * In particular, we work with a bare nonblock-mode socket, rather than
13  * a stdio stream, so that we can avoid unwanted blocking of the application.
14  *
15  * XXX: MOVE DEBUG PRINTOUT TO HIGHER LEVEL. As is, block and restart
16  * will cause repeat printouts.
17  *
18  * We must speak the same transmitted data representations as the backend
19  * routines.
20  *
21  *
22  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
23  * Portions Copyright (c) 1994, Regents of the University of California
24  *
25  * IDENTIFICATION
26  * src/interfaces/libpq/fe-misc.c
27  *
28  *-------------------------------------------------------------------------
29  */
30 
31 #include "postgres_fe.h"
32 
33 #include <signal.h>
34 #include <time.h>
35 
36 #ifdef WIN32
37 #include "win32.h"
38 #else
39 #include <unistd.h>
40 #include <sys/select.h>
41 #include <sys/time.h>
42 #endif
43 
44 #ifdef HAVE_POLL_H
45 #include <poll.h>
46 #endif
47 
48 #include "libpq-fe.h"
49 #include "libpq-int.h"
50 #include "mb/pg_wchar.h"
51 #include "pg_config_paths.h"
52 #include "port/pg_bswap.h"
53 
54 static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn);
55 static int pqSendSome(PGconn *conn, int len);
56 static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
57  time_t end_time);
58 static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
59 
60 /*
61  * PQlibVersion: return the libpq version number
62  */
63 int
65 {
66  return PG_VERSION_NUM;
67 }
68 
69 
70 /*
71  * pqGetc: get 1 character from the connection
72  *
73  * All these routines return 0 on success, EOF on error.
74  * Note that for the Get routines, EOF only means there is not enough
75  * data in the buffer, not that there is necessarily a hard error.
76  */
77 int
78 pqGetc(char *result, PGconn *conn)
79 {
80  if (conn->inCursor >= conn->inEnd)
81  return EOF;
82 
83  *result = conn->inBuffer[conn->inCursor++];
84 
85  return 0;
86 }
87 
88 
89 /*
90  * pqPutc: write 1 char to the current message
91  */
92 int
94 {
95  if (pqPutMsgBytes(&c, 1, conn))
96  return EOF;
97 
98  return 0;
99 }
100 
101 
102 /*
103  * pqGets[_append]:
104  * get a null-terminated string from the connection,
105  * and store it in an expansible PQExpBuffer.
106  * If we run out of memory, all of the string is still read,
107  * but the excess characters are silently discarded.
108  */
109 static int
111 {
112  /* Copy conn data to locals for faster search loop */
113  char *inBuffer = conn->inBuffer;
114  int inCursor = conn->inCursor;
115  int inEnd = conn->inEnd;
116  int slen;
117 
118  while (inCursor < inEnd && inBuffer[inCursor])
119  inCursor++;
120 
121  if (inCursor >= inEnd)
122  return EOF;
123 
124  slen = inCursor - conn->inCursor;
125 
126  if (resetbuffer)
128 
129  appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
130 
131  conn->inCursor = ++inCursor;
132 
133  return 0;
134 }
135 
136 int
138 {
139  return pqGets_internal(buf, conn, true);
140 }
141 
142 int
144 {
145  return pqGets_internal(buf, conn, false);
146 }
147 
148 
149 /*
150  * pqPuts: write a null-terminated string to the current message
151  */
152 int
153 pqPuts(const char *s, PGconn *conn)
154 {
155  if (pqPutMsgBytes(s, strlen(s) + 1, conn))
156  return EOF;
157 
158  return 0;
159 }
160 
161 /*
162  * pqGetnchar:
163  * get a string of exactly len bytes in buffer s, no null termination
164  */
165 int
166 pqGetnchar(char *s, size_t len, PGconn *conn)
167 {
168  if (len > (size_t) (conn->inEnd - conn->inCursor))
169  return EOF;
170 
171  memcpy(s, conn->inBuffer + conn->inCursor, len);
172  /* no terminating null */
173 
174  conn->inCursor += len;
175 
176  return 0;
177 }
178 
179 /*
180  * pqSkipnchar:
181  * skip over len bytes in input buffer.
182  *
183  * Note: this is primarily useful for its debug output, which should
184  * be exactly the same as for pqGetnchar. We assume the data in question
185  * will actually be used, but just isn't getting copied anywhere as yet.
186  */
187 int
189 {
190  if (len > (size_t) (conn->inEnd - conn->inCursor))
191  return EOF;
192 
193  conn->inCursor += len;
194 
195  return 0;
196 }
197 
198 /*
199  * pqPutnchar:
200  * write exactly len bytes to the current message
201  */
202 int
203 pqPutnchar(const char *s, size_t len, PGconn *conn)
204 {
205  if (pqPutMsgBytes(s, len, conn))
206  return EOF;
207 
208  return 0;
209 }
210 
211 /*
212  * pqGetInt
213  * read a 2 or 4 byte integer and convert from network byte order
214  * to local byte order
215  */
216 int
217 pqGetInt(int *result, size_t bytes, PGconn *conn)
218 {
219  uint16 tmp2;
220  uint32 tmp4;
221 
222  switch (bytes)
223  {
224  case 2:
225  if (conn->inCursor + 2 > conn->inEnd)
226  return EOF;
227  memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
228  conn->inCursor += 2;
229  *result = (int) pg_ntoh16(tmp2);
230  break;
231  case 4:
232  if (conn->inCursor + 4 > conn->inEnd)
233  return EOF;
234  memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
235  conn->inCursor += 4;
236  *result = (int) pg_ntoh32(tmp4);
237  break;
238  default:
240  "integer of size %lu not supported by pqGetInt",
241  (unsigned long) bytes);
242  return EOF;
243  }
244 
245  return 0;
246 }
247 
248 /*
249  * pqPutInt
250  * write an integer of 2 or 4 bytes, converting from host byte order
251  * to network byte order.
252  */
253 int
254 pqPutInt(int value, size_t bytes, PGconn *conn)
255 {
256  uint16 tmp2;
257  uint32 tmp4;
258 
259  switch (bytes)
260  {
261  case 2:
262  tmp2 = pg_hton16((uint16) value);
263  if (pqPutMsgBytes((const char *) &tmp2, 2, conn))
264  return EOF;
265  break;
266  case 4:
267  tmp4 = pg_hton32((uint32) value);
268  if (pqPutMsgBytes((const char *) &tmp4, 4, conn))
269  return EOF;
270  break;
271  default:
273  "integer of size %lu not supported by pqPutInt",
274  (unsigned long) bytes);
275  return EOF;
276  }
277 
278  return 0;
279 }
280 
281 /*
282  * Make sure conn's output buffer can hold bytes_needed bytes (caller must
283  * include already-stored data into the value!)
284  *
285  * Returns 0 on success, EOF if failed to enlarge buffer
286  */
287 int
288 pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
289 {
290  int newsize = conn->outBufSize;
291  char *newbuf;
292 
293  /* Quick exit if we have enough space */
294  if (bytes_needed <= (size_t) newsize)
295  return 0;
296 
297  /*
298  * If we need to enlarge the buffer, we first try to double it in size; if
299  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
300  * the malloc pool by repeated small enlargements.
301  *
302  * Note: tests for newsize > 0 are to catch integer overflow.
303  */
304  do
305  {
306  newsize *= 2;
307  } while (newsize > 0 && bytes_needed > (size_t) newsize);
308 
309  if (newsize > 0 && bytes_needed <= (size_t) newsize)
310  {
311  newbuf = realloc(conn->outBuffer, newsize);
312  if (newbuf)
313  {
314  /* realloc succeeded */
315  conn->outBuffer = newbuf;
316  conn->outBufSize = newsize;
317  return 0;
318  }
319  }
320 
321  newsize = conn->outBufSize;
322  do
323  {
324  newsize += 8192;
325  } while (newsize > 0 && bytes_needed > (size_t) newsize);
326 
327  if (newsize > 0 && bytes_needed <= (size_t) newsize)
328  {
329  newbuf = realloc(conn->outBuffer, newsize);
330  if (newbuf)
331  {
332  /* realloc succeeded */
333  conn->outBuffer = newbuf;
334  conn->outBufSize = newsize;
335  return 0;
336  }
337  }
338 
339  /* realloc failed. Probably out of memory */
341  "cannot allocate memory for output buffer\n");
342  return EOF;
343 }
344 
345 /*
346  * Make sure conn's input buffer can hold bytes_needed bytes (caller must
347  * include already-stored data into the value!)
348  *
349  * Returns 0 on success, EOF if failed to enlarge buffer
350  */
351 int
352 pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
353 {
354  int newsize = conn->inBufSize;
355  char *newbuf;
356 
357  /* Quick exit if we have enough space */
358  if (bytes_needed <= (size_t) newsize)
359  return 0;
360 
361  /*
362  * Before concluding that we need to enlarge the buffer, left-justify
363  * whatever is in it and recheck. The caller's value of bytes_needed
364  * includes any data to the left of inStart, but we can delete that in
365  * preference to enlarging the buffer. It's slightly ugly to have this
366  * function do this, but it's better than making callers worry about it.
367  */
368  bytes_needed -= conn->inStart;
369 
370  if (conn->inStart < conn->inEnd)
371  {
372  if (conn->inStart > 0)
373  {
374  memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
375  conn->inEnd - conn->inStart);
376  conn->inEnd -= conn->inStart;
377  conn->inCursor -= conn->inStart;
378  conn->inStart = 0;
379  }
380  }
381  else
382  {
383  /* buffer is logically empty, reset it */
384  conn->inStart = conn->inCursor = conn->inEnd = 0;
385  }
386 
387  /* Recheck whether we have enough space */
388  if (bytes_needed <= (size_t) newsize)
389  return 0;
390 
391  /*
392  * If we need to enlarge the buffer, we first try to double it in size; if
393  * that doesn't work, enlarge in multiples of 8K. This avoids thrashing
394  * the malloc pool by repeated small enlargements.
395  *
396  * Note: tests for newsize > 0 are to catch integer overflow.
397  */
398  do
399  {
400  newsize *= 2;
401  } while (newsize > 0 && bytes_needed > (size_t) newsize);
402 
403  if (newsize > 0 && bytes_needed <= (size_t) newsize)
404  {
405  newbuf = realloc(conn->inBuffer, newsize);
406  if (newbuf)
407  {
408  /* realloc succeeded */
409  conn->inBuffer = newbuf;
410  conn->inBufSize = newsize;
411  return 0;
412  }
413  }
414 
415  newsize = conn->inBufSize;
416  do
417  {
418  newsize += 8192;
419  } while (newsize > 0 && bytes_needed > (size_t) newsize);
420 
421  if (newsize > 0 && bytes_needed <= (size_t) newsize)
422  {
423  newbuf = realloc(conn->inBuffer, newsize);
424  if (newbuf)
425  {
426  /* realloc succeeded */
427  conn->inBuffer = newbuf;
428  conn->inBufSize = newsize;
429  return 0;
430  }
431  }
432 
433  /* realloc failed. Probably out of memory */
435  "cannot allocate memory for input buffer\n");
436  return EOF;
437 }
438 
439 /*
440  * pqPutMsgStart: begin construction of a message to the server
441  *
442  * msg_type is the message type byte, or 0 for a message without type byte
443  * (only startup messages have no type byte)
444  *
445  * Returns 0 on success, EOF on error
446  *
447  * The idea here is that we construct the message in conn->outBuffer,
448  * beginning just past any data already in outBuffer (ie, at
449  * outBuffer+outCount). We enlarge the buffer as needed to hold the message.
450  * When the message is complete, we fill in the length word (if needed) and
451  * then advance outCount past the message, making it eligible to send.
452  *
453  * The state variable conn->outMsgStart points to the incomplete message's
454  * length word: it is either outCount or outCount+1 depending on whether
455  * there is a type byte. The state variable conn->outMsgEnd is the end of
456  * the data collected so far.
457  */
458 int
459 pqPutMsgStart(char msg_type, PGconn *conn)
460 {
461  int lenPos;
462  int endPos;
463 
464  /* allow room for message type byte */
465  if (msg_type)
466  endPos = conn->outCount + 1;
467  else
468  endPos = conn->outCount;
469 
470  /* do we want a length word? */
471  lenPos = endPos;
472  /* allow room for message length */
473  endPos += 4;
474 
475  /* make sure there is room for message header */
476  if (pqCheckOutBufferSpace(endPos, conn))
477  return EOF;
478  /* okay, save the message type byte if any */
479  if (msg_type)
480  conn->outBuffer[conn->outCount] = msg_type;
481  /* set up the message pointers */
482  conn->outMsgStart = lenPos;
483  conn->outMsgEnd = endPos;
484  /* length word, if needed, will be filled in by pqPutMsgEnd */
485 
486  return 0;
487 }
488 
489 /*
490  * pqPutMsgBytes: add bytes to a partially-constructed message
491  *
492  * Returns 0 on success, EOF on error
493  */
494 static int
495 pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
496 {
497  /* make sure there is room for it */
499  return EOF;
500  /* okay, save the data */
501  memcpy(conn->outBuffer + conn->outMsgEnd, buf, len);
502  conn->outMsgEnd += len;
503  /* no Pfdebug call here, caller should do it */
504  return 0;
505 }
506 
507 /*
508  * pqPutMsgEnd: finish constructing a message and possibly send it
509  *
510  * Returns 0 on success, EOF on error
511  *
512  * We don't actually send anything here unless we've accumulated at least
513  * 8K worth of data (the typical size of a pipe buffer on Unix systems).
514  * This avoids sending small partial packets. The caller must use pqFlush
515  * when it's important to flush all the data out to the server.
516  */
517 int
519 {
520  /* Fill in length word if needed */
521  if (conn->outMsgStart >= 0)
522  {
523  uint32 msgLen = conn->outMsgEnd - conn->outMsgStart;
524 
525  msgLen = pg_hton32(msgLen);
526  memcpy(conn->outBuffer + conn->outMsgStart, &msgLen, 4);
527  }
528 
529  /* trace client-to-server message */
530  if (conn->Pfdebug)
531  {
532  if (conn->outCount < conn->outMsgStart)
534  else
537  }
538 
539  /* Make message eligible to send */
541 
542  if (conn->outCount >= 8192)
543  {
544  int toSend = conn->outCount - (conn->outCount % 8192);
545 
546  if (pqSendSome(conn, toSend) < 0)
547  return EOF;
548  /* in nonblock mode, don't complain if unable to send it all */
549  }
550 
551  return 0;
552 }
553 
554 /* ----------
555  * pqReadData: read more data, if any is available
556  * Possible return values:
557  * 1: successfully loaded at least one more byte
558  * 0: no data is presently available, but no error detected
559  * -1: error detected (including EOF = connection closure);
560  * conn->errorMessage set
561  * NOTE: callers must not assume that pointers or indexes into conn->inBuffer
562  * remain valid across this call!
563  * ----------
564  */
565 int
567 {
568  int someread = 0;
569  int nread;
570 
571  if (conn->sock == PGINVALID_SOCKET)
572  {
573  libpq_append_conn_error(conn, "connection not open");
574  return -1;
575  }
576 
577  /* Left-justify any data in the buffer to make room */
578  if (conn->inStart < conn->inEnd)
579  {
580  if (conn->inStart > 0)
581  {
582  memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
583  conn->inEnd - conn->inStart);
584  conn->inEnd -= conn->inStart;
585  conn->inCursor -= conn->inStart;
586  conn->inStart = 0;
587  }
588  }
589  else
590  {
591  /* buffer is logically empty, reset it */
592  conn->inStart = conn->inCursor = conn->inEnd = 0;
593  }
594 
595  /*
596  * If the buffer is fairly full, enlarge it. We need to be able to enlarge
597  * the buffer in case a single message exceeds the initial buffer size. We
598  * enlarge before filling the buffer entirely so as to avoid asking the
599  * kernel for a partial packet. The magic constant here should be large
600  * enough for a TCP packet or Unix pipe bufferload. 8K is the usual pipe
601  * buffer size, so...
602  */
603  if (conn->inBufSize - conn->inEnd < 8192)
604  {
605  if (pqCheckInBufferSpace(conn->inEnd + (size_t) 8192, conn))
606  {
607  /*
608  * We don't insist that the enlarge worked, but we need some room
609  */
610  if (conn->inBufSize - conn->inEnd < 100)
611  return -1; /* errorMessage already set */
612  }
613  }
614 
615  /* OK, try to read some data */
616 retry3:
617  nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
618  conn->inBufSize - conn->inEnd);
619  if (nread < 0)
620  {
621  switch (SOCK_ERRNO)
622  {
623  case EINTR:
624  goto retry3;
625 
626  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
627 #ifdef EAGAIN
628  case EAGAIN:
629  return someread;
630 #endif
631 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
632  case EWOULDBLOCK:
633  return someread;
634 #endif
635 
636  /* We might get ECONNRESET etc here if connection failed */
638  goto definitelyFailed;
639 
640  default:
641  /* pqsecure_read set the error message for us */
642  return -1;
643  }
644  }
645  if (nread > 0)
646  {
647  conn->inEnd += nread;
648 
649  /*
650  * Hack to deal with the fact that some kernels will only give us back
651  * 1 packet per recv() call, even if we asked for more and there is
652  * more available. If it looks like we are reading a long message,
653  * loop back to recv() again immediately, until we run out of data or
654  * buffer space. Without this, the block-and-restart behavior of
655  * libpq's higher levels leads to O(N^2) performance on long messages.
656  *
657  * Since we left-justified the data above, conn->inEnd gives the
658  * amount of data already read in the current message. We consider
659  * the message "long" once we have acquired 32k ...
660  */
661  if (conn->inEnd > 32768 &&
662  (conn->inBufSize - conn->inEnd) >= 8192)
663  {
664  someread = 1;
665  goto retry3;
666  }
667  return 1;
668  }
669 
670  if (someread)
671  return 1; /* got a zero read after successful tries */
672 
673  /*
674  * A return value of 0 could mean just that no data is now available, or
675  * it could mean EOF --- that is, the server has closed the connection.
676  * Since we have the socket in nonblock mode, the only way to tell the
677  * difference is to see if select() is saying that the file is ready.
678  * Grumble. Fortunately, we don't expect this path to be taken much,
679  * since in normal practice we should not be trying to read data unless
680  * the file selected for reading already.
681  *
682  * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
683  * data could arrive before we make the pqReadReady() test, but the second
684  * SSL_read() could still say WANT_READ because the data received was not
685  * a complete SSL record. So we must play dumb and assume there is more
686  * data, relying on the SSL layer to detect true EOF.
687  */
688 
689 #ifdef USE_SSL
690  if (conn->ssl_in_use)
691  return 0;
692 #endif
693 
694  switch (pqReadReady(conn))
695  {
696  case 0:
697  /* definitely no data available */
698  return 0;
699  case 1:
700  /* ready for read */
701  break;
702  default:
703  /* we override pqReadReady's message with something more useful */
704  goto definitelyEOF;
705  }
706 
707  /*
708  * Still not sure that it's EOF, because some data could have just
709  * arrived.
710  */
711 retry4:
712  nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
713  conn->inBufSize - conn->inEnd);
714  if (nread < 0)
715  {
716  switch (SOCK_ERRNO)
717  {
718  case EINTR:
719  goto retry4;
720 
721  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
722 #ifdef EAGAIN
723  case EAGAIN:
724  return 0;
725 #endif
726 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
727  case EWOULDBLOCK:
728  return 0;
729 #endif
730 
731  /* We might get ECONNRESET etc here if connection failed */
733  goto definitelyFailed;
734 
735  default:
736  /* pqsecure_read set the error message for us */
737  return -1;
738  }
739  }
740  if (nread > 0)
741  {
742  conn->inEnd += nread;
743  return 1;
744  }
745 
746  /*
747  * OK, we are getting a zero read even though select() says ready. This
748  * means the connection has been closed. Cope.
749  */
750 definitelyEOF:
751  libpq_append_conn_error(conn, "server closed the connection unexpectedly\n"
752  "\tThis probably means the server terminated abnormally\n"
753  "\tbefore or while processing the request.");
754 
755  /* Come here if lower-level code already set a suitable errorMessage */
756 definitelyFailed:
757  /* Do *not* drop any already-read data; caller still wants it */
758  pqDropConnection(conn, false);
759  conn->status = CONNECTION_BAD; /* No more connection to backend */
760  return -1;
761 }
762 
763 /*
764  * pqSendSome: send data waiting in the output buffer.
765  *
766  * len is how much to try to send (typically equal to outCount, but may
767  * be less).
768  *
769  * Return 0 on success, -1 on failure and 1 when not all data could be sent
770  * because the socket would block and the connection is non-blocking.
771  *
772  * Note that this is also responsible for consuming data from the socket
773  * (putting it in conn->inBuffer) in any situation where we can't send
774  * all the specified data immediately.
775  *
776  * If a socket-level write failure occurs, conn->write_failed is set and the
777  * error message is saved in conn->write_err_msg, but we clear the output
778  * buffer and return zero anyway; this is because callers should soldier on
779  * until we have read what we can from the server and checked for an error
780  * message. write_err_msg should be reported only when we are unable to
781  * obtain a server error first. Much of that behavior is implemented at
782  * lower levels, but this function deals with some edge cases.
783  */
784 static int
786 {
787  char *ptr = conn->outBuffer;
788  int remaining = conn->outCount;
789  int result = 0;
790 
791  /*
792  * If we already had a write failure, we will never again try to send data
793  * on that connection. Even if the kernel would let us, we've probably
794  * lost message boundary sync with the server. conn->write_failed
795  * therefore persists until the connection is reset, and we just discard
796  * all data presented to be written. However, as long as we still have a
797  * valid socket, we should continue to absorb data from the backend, so
798  * that we can collect any final error messages.
799  */
800  if (conn->write_failed)
801  {
802  /* conn->write_err_msg should be set up already */
803  conn->outCount = 0;
804  /* Absorb input data if any, and detect socket closure */
805  if (conn->sock != PGINVALID_SOCKET)
806  {
807  if (pqReadData(conn) < 0)
808  return -1;
809  }
810  return 0;
811  }
812 
813  if (conn->sock == PGINVALID_SOCKET)
814  {
815  conn->write_failed = true;
816  /* Store error message in conn->write_err_msg, if possible */
817  /* (strdup failure is OK, we'll cope later) */
818  conn->write_err_msg = strdup(libpq_gettext("connection not open\n"));
819  /* Discard queued data; no chance it'll ever be sent */
820  conn->outCount = 0;
821  return 0;
822  }
823 
824  /* while there's still data to send */
825  while (len > 0)
826  {
827  int sent;
828 
829 #ifndef WIN32
830  sent = pqsecure_write(conn, ptr, len);
831 #else
832 
833  /*
834  * Windows can fail on large sends, per KB article Q201213. The
835  * failure-point appears to be different in different versions of
836  * Windows, but 64k should always be safe.
837  */
838  sent = pqsecure_write(conn, ptr, Min(len, 65536));
839 #endif
840 
841  if (sent < 0)
842  {
843  /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
844  switch (SOCK_ERRNO)
845  {
846 #ifdef EAGAIN
847  case EAGAIN:
848  break;
849 #endif
850 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
851  case EWOULDBLOCK:
852  break;
853 #endif
854  case EINTR:
855  continue;
856 
857  default:
858  /* Discard queued data; no chance it'll ever be sent */
859  conn->outCount = 0;
860 
861  /* Absorb input data if any, and detect socket closure */
862  if (conn->sock != PGINVALID_SOCKET)
863  {
864  if (pqReadData(conn) < 0)
865  return -1;
866  }
867 
868  /*
869  * Lower-level code should already have filled
870  * conn->write_err_msg (and set conn->write_failed) or
871  * conn->errorMessage. In the former case, we pretend
872  * there's no problem; the write_failed condition will be
873  * dealt with later. Otherwise, report the error now.
874  */
875  if (conn->write_failed)
876  return 0;
877  else
878  return -1;
879  }
880  }
881  else
882  {
883  ptr += sent;
884  len -= sent;
885  remaining -= sent;
886  }
887 
888  if (len > 0)
889  {
890  /*
891  * We didn't send it all, wait till we can send more.
892  *
893  * There are scenarios in which we can't send data because the
894  * communications channel is full, but we cannot expect the server
895  * to clear the channel eventually because it's blocked trying to
896  * send data to us. (This can happen when we are sending a large
897  * amount of COPY data, and the server has generated lots of
898  * NOTICE responses.) To avoid a deadlock situation, we must be
899  * prepared to accept and buffer incoming data before we try
900  * again. Furthermore, it is possible that such incoming data
901  * might not arrive until after we've gone to sleep. Therefore,
902  * we wait for either read ready or write ready.
903  *
904  * In non-blocking mode, we don't wait here directly, but return 1
905  * to indicate that data is still pending. The caller should wait
906  * for both read and write ready conditions, and call
907  * PQconsumeInput() on read ready, but just in case it doesn't, we
908  * call pqReadData() ourselves before returning. That's not
909  * enough if the data has not arrived yet, but it's the best we
910  * can do, and works pretty well in practice. (The documentation
911  * used to say that you only need to wait for write-ready, so
912  * there are still plenty of applications like that out there.)
913  *
914  * Note that errors here don't result in write_failed becoming
915  * set.
916  */
917  if (pqReadData(conn) < 0)
918  {
919  result = -1; /* error message already set up */
920  break;
921  }
922 
923  if (pqIsnonblocking(conn))
924  {
925  result = 1;
926  break;
927  }
928 
929  if (pqWait(true, true, conn))
930  {
931  result = -1;
932  break;
933  }
934  }
935  }
936 
937  /* shift the remaining contents of the buffer */
938  if (remaining > 0)
939  memmove(conn->outBuffer, ptr, remaining);
941 
942  return result;
943 }
944 
945 
946 /*
947  * pqFlush: send any data waiting in the output buffer
948  *
949  * Return 0 on success, -1 on failure and 1 when not all data could be sent
950  * because the socket would block and the connection is non-blocking.
951  * (See pqSendSome comments about how failure should be handled.)
952  */
953 int
955 {
956  if (conn->outCount > 0)
957  {
958  if (conn->Pfdebug)
959  fflush(conn->Pfdebug);
960 
961  return pqSendSome(conn, conn->outCount);
962  }
963 
964  return 0;
965 }
966 
967 
968 /*
969  * pqWait: wait until we can read or write the connection socket
970  *
971  * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
972  * call to select().
973  *
974  * We also stop waiting and return if the kernel flags an exception condition
975  * on the socket. The actual error condition will be detected and reported
976  * when the caller tries to read or write the socket.
977  */
978 int
979 pqWait(int forRead, int forWrite, PGconn *conn)
980 {
981  return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
982 }
983 
984 /*
985  * pqWaitTimed: wait, but not past finish_time.
986  *
987  * finish_time = ((time_t) -1) disables the wait limit.
988  *
989  * Returns -1 on failure, 0 if the socket is readable/writable, 1 if it timed out.
990  */
991 int
992 pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
993 {
994  int result;
995 
996  result = pqSocketCheck(conn, forRead, forWrite, finish_time);
997 
998  if (result < 0)
999  return -1; /* errorMessage is already set */
1000 
1001  if (result == 0)
1002  {
1003  libpq_append_conn_error(conn, "timeout expired");
1004  return 1;
1005  }
1006 
1007  return 0;
1008 }
1009 
1010 /*
1011  * pqReadReady: is select() saying the file is ready to read?
1012  * Returns -1 on failure, 0 if not ready, 1 if ready.
1013  */
1014 int
1016 {
1017  return pqSocketCheck(conn, 1, 0, (time_t) 0);
1018 }
1019 
1020 /*
1021  * pqWriteReady: is select() saying the file is ready to write?
1022  * Returns -1 on failure, 0 if not ready, 1 if ready.
1023  */
1024 int
1026 {
1027  return pqSocketCheck(conn, 0, 1, (time_t) 0);
1028 }
1029 
1030 /*
1031  * Checks a socket, using poll or select, for data to be read, written,
1032  * or both. Returns >0 if one or more conditions are met, 0 if it timed
1033  * out, -1 if an error occurred.
1034  *
1035  * If SSL is in use, the SSL buffer is checked prior to checking the socket
1036  * for read data directly.
1037  */
1038 static int
1039 pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
1040 {
1041  int result;
1042 
1043  if (!conn)
1044  return -1;
1045  if (conn->sock == PGINVALID_SOCKET)
1046  {
1047  libpq_append_conn_error(conn, "invalid socket");
1048  return -1;
1049  }
1050 
1051 #ifdef USE_SSL
1052  /* Check for SSL library buffering read bytes */
1053  if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
1054  {
1055  /* short-circuit the select */
1056  return 1;
1057  }
1058 #endif
1059 
1060  /* We will retry as long as we get EINTR */
1061  do
1062  result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1063  while (result < 0 && SOCK_ERRNO == EINTR);
1064 
1065  if (result < 0)
1066  {
1067  char sebuf[PG_STRERROR_R_BUFLEN];
1068 
1069  libpq_append_conn_error(conn, "%s() failed: %s", "select",
1070  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1071  }
1072 
1073  return result;
1074 }
1075 
1076 
1077 /*
1078  * Check a file descriptor for read and/or write data, possibly waiting.
1079  * If neither forRead nor forWrite are set, immediately return a timeout
1080  * condition (without waiting). Return >0 if condition is met, 0
1081  * if a timeout occurred, -1 if an error or interrupt occurred.
1082  *
1083  * Timeout is infinite if end_time is -1. Timeout is immediate (no blocking)
1084  * if end_time is 0 (or indeed, any time before now).
1085  */
1086 static int
1087 pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
1088 {
1089  /* We use poll(2) if available, otherwise select(2) */
1090 #ifdef HAVE_POLL
1091  struct pollfd input_fd;
1092  int timeout_ms;
1093 
1094  if (!forRead && !forWrite)
1095  return 0;
1096 
1097  input_fd.fd = sock;
1098  input_fd.events = POLLERR;
1099  input_fd.revents = 0;
1100 
1101  if (forRead)
1102  input_fd.events |= POLLIN;
1103  if (forWrite)
1104  input_fd.events |= POLLOUT;
1105 
1106  /* Compute appropriate timeout interval */
1107  if (end_time == ((time_t) -1))
1108  timeout_ms = -1;
1109  else
1110  {
1111  time_t now = time(NULL);
1112 
1113  if (end_time > now)
1114  timeout_ms = (end_time - now) * 1000;
1115  else
1116  timeout_ms = 0;
1117  }
1118 
1119  return poll(&input_fd, 1, timeout_ms);
1120 #else /* !HAVE_POLL */
1121 
1122  fd_set input_mask;
1123  fd_set output_mask;
1124  fd_set except_mask;
1125  struct timeval timeout;
1126  struct timeval *ptr_timeout;
1127 
1128  if (!forRead && !forWrite)
1129  return 0;
1130 
1131  FD_ZERO(&input_mask);
1132  FD_ZERO(&output_mask);
1133  FD_ZERO(&except_mask);
1134  if (forRead)
1135  FD_SET(sock, &input_mask);
1136 
1137  if (forWrite)
1138  FD_SET(sock, &output_mask);
1139  FD_SET(sock, &except_mask);
1140 
1141  /* Compute appropriate timeout interval */
1142  if (end_time == ((time_t) -1))
1143  ptr_timeout = NULL;
1144  else
1145  {
1146  time_t now = time(NULL);
1147 
1148  if (end_time > now)
1149  timeout.tv_sec = end_time - now;
1150  else
1151  timeout.tv_sec = 0;
1152  timeout.tv_usec = 0;
1153  ptr_timeout = &timeout;
1154  }
1155 
1156  return select(sock + 1, &input_mask, &output_mask,
1157  &except_mask, ptr_timeout);
1158 #endif /* HAVE_POLL */
1159 }
1160 
1161 
1162 /*
1163  * A couple of "miscellaneous" multibyte related functions. They used
1164  * to be in fe-print.c but that file is doomed.
1165  */
1166 
1167 /*
1168  * Returns the byte length of the character beginning at s, using the
1169  * specified encoding.
1170  *
1171  * Caution: when dealing with text that is not certainly valid in the
1172  * specified encoding, the result may exceed the actual remaining
1173  * string length. Callers that are not prepared to deal with that
1174  * should use PQmblenBounded() instead.
1175  */
1176 int
1177 PQmblen(const char *s, int encoding)
1178 {
1179  return pg_encoding_mblen(encoding, s);
1180 }
1181 
1182 /*
1183  * Returns the byte length of the character beginning at s, using the
1184  * specified encoding; but not more than the distance to end of string.
1185  */
1186 int
1187 PQmblenBounded(const char *s, int encoding)
1188 {
1189  return strnlen(s, pg_encoding_mblen(encoding, s));
1190 }
1191 
1192 /*
1193  * Returns the display length of the character beginning at s, using the
1194  * specified encoding.
1195  */
1196 int
1197 PQdsplen(const char *s, int encoding)
1198 {
1199  return pg_encoding_dsplen(encoding, s);
1200 }
1201 
1202 /*
1203  * Get encoding id from environment variable PGCLIENTENCODING.
1204  */
1205 int
1207 {
1208  char *str;
1209  int encoding = PG_SQL_ASCII;
1210 
1211  str = getenv("PGCLIENTENCODING");
1212  if (str && *str != '\0')
1213  {
1215  if (encoding < 0)
1217  }
1218  return encoding;
1219 }
1220 
1221 
1222 #ifdef ENABLE_NLS
1223 
1224 static void
1225 libpq_binddomain(void)
1226 {
1227  /*
1228  * If multiple threads come through here at about the same time, it's okay
1229  * for more than one of them to call bindtextdomain(). But it's not okay
1230  * for any of them to return to caller before bindtextdomain() is
1231  * complete, so don't set the flag till that's done. Use "volatile" just
1232  * to be sure the compiler doesn't try to get cute.
1233  */
1234  static volatile bool already_bound = false;
1235 
1236  if (!already_bound)
1237  {
1238  /* bindtextdomain() does not preserve errno */
1239 #ifdef WIN32
1240  int save_errno = GetLastError();
1241 #else
1242  int save_errno = errno;
1243 #endif
1244  const char *ldir;
1245 
1246  /* No relocatable lookup here because the binary could be anywhere */
1247  ldir = getenv("PGLOCALEDIR");
1248  if (!ldir)
1249  ldir = LOCALEDIR;
1250  bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1251  already_bound = true;
1252 #ifdef WIN32
1253  SetLastError(save_errno);
1254 #else
1255  errno = save_errno;
1256 #endif
1257  }
1258 }
1259 
1260 char *
1261 libpq_gettext(const char *msgid)
1262 {
1263  libpq_binddomain();
1264  return dgettext(PG_TEXTDOMAIN("libpq"), msgid);
1265 }
1266 
1267 char *
1268 libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)
1269 {
1270  libpq_binddomain();
1271  return dngettext(PG_TEXTDOMAIN("libpq"), msgid, msgid_plural, n);
1272 }
1273 
1274 #endif /* ENABLE_NLS */
1275 
1276 
1277 /*
1278  * Append a formatted string to the given buffer, after translating it. A
1279  * newline is automatically appended; the format should not end with a
1280  * newline.
1281  */
1282 void
1283 libpq_append_error(PQExpBuffer errorMessage, const char *fmt,...)
1284 {
1285  int save_errno = errno;
1286  bool done;
1287  va_list args;
1288 
1289  Assert(fmt[strlen(fmt) - 1] != '\n');
1290 
1291  if (PQExpBufferBroken(errorMessage))
1292  return; /* already failed */
1293 
1294  /* Loop in case we have to retry after enlarging the buffer. */
1295  do
1296  {
1297  errno = save_errno;
1298  va_start(args, fmt);
1299  done = appendPQExpBufferVA(errorMessage, libpq_gettext(fmt), args);
1300  va_end(args);
1301  } while (!done);
1302 
1303  appendPQExpBufferChar(errorMessage, '\n');
1304 }
1305 
1306 /*
1307  * Append a formatted string to the error message buffer of the given
1308  * connection, after translating it. A newline is automatically appended; the
1309  * format should not end with a newline.
1310  */
1311 void
1313 {
1314  int save_errno = errno;
1315  bool done;
1316  va_list args;
1317 
1318  Assert(fmt[strlen(fmt) - 1] != '\n');
1319 
1321  return; /* already failed */
1322 
1323  /* Loop in case we have to retry after enlarging the buffer. */
1324  do
1325  {
1326  errno = save_errno;
1327  va_start(args, fmt);
1329  va_end(args);
1330  } while (!done);
1331 
1333 }
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1613
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
#define Min(x, y)
Definition: c.h:993
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1227
#define dngettext(d, s, p, n)
Definition: c.h:1195
#define dgettext(d, x)
Definition: c.h:1193
void pqDropConnection(PGconn *conn, bool flushInput)
Definition: fe-connect.c:467
void pqInternalNotice(const PGNoticeHooks *hooks, const char *fmt,...)
Definition: fe-exec.c:933
int pqPutc(char c, PGconn *conn)
Definition: fe-misc.c:93
int pqReadData(PGconn *conn)
Definition: fe-misc.c:566
int pqPutInt(int value, size_t bytes, PGconn *conn)
Definition: fe-misc.c:254
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:288
int pqFlush(PGconn *conn)
Definition: fe-misc.c:954
int pqReadReady(PGconn *conn)
Definition: fe-misc.c:1015
int PQenv2encoding(void)
Definition: fe-misc.c:1206
int pqPutMsgStart(char msg_type, PGconn *conn)
Definition: fe-misc.c:459
int pqSkipnchar(size_t len, PGconn *conn)
Definition: fe-misc.c:188
int pqGetc(char *result, PGconn *conn)
Definition: fe-misc.c:78
int PQlibVersion(void)
Definition: fe-misc.c:64
int pqGetInt(int *result, size_t bytes, PGconn *conn)
Definition: fe-misc.c:217
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:166
int PQmblen(const char *s, int encoding)
Definition: fe-misc.c:1177
int pqWait(int forRead, int forWrite, PGconn *conn)
Definition: fe-misc.c:979
int pqGets(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:137
int PQdsplen(const char *s, int encoding)
Definition: fe-misc.c:1197
static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1087
int pqCheckInBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:352
static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn)
Definition: fe-misc.c:495
int pqPutnchar(const char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:203
static int pqSendSome(PGconn *conn, int len)
Definition: fe-misc.c:785
static int pqGets_internal(PQExpBuffer buf, PGconn *conn, bool resetbuffer)
Definition: fe-misc.c:110
int pqPuts(const char *s, PGconn *conn)
Definition: fe-misc.c:153
void libpq_append_error(PQExpBuffer errorMessage, const char *fmt,...)
Definition: fe-misc.c:1283
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1039
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:992
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1187
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition: fe-misc.c:1312
int pqGets_append(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:143
int pqWriteReady(PGconn *conn)
Definition: fe-misc.c:1025
int pqPutMsgEnd(PGconn *conn)
Definition: fe-misc.c:518
bool pgtls_read_pending(PGconn *conn)
ssize_t pqsecure_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:275
ssize_t pqsecure_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:182
void pqTraceOutputMessage(PGconn *conn, const char *message, bool toServer)
Definition: fe-trace.c:529
void pqTraceOutputNoTypeByteMessage(PGconn *conn, const char *message)
Definition: fe-trace.c:704
#define realloc(a, b)
Definition: header.h:60
int remaining
Definition: informix.c:667
static struct @148 value
@ CONNECTION_BAD
Definition: libpq-fe.h:61
#define libpq_gettext(x)
Definition: libpq-int.h:900
#define SOCK_STRERROR
Definition: libpq-int.h:922
#define SOCK_ERRNO
Definition: libpq-int.h:921
#define libpq_ngettext(s, p, n)
Definition: libpq-int.h:901
#define pqIsnonblocking(conn)
Definition: libpq-int.h:889
static void const char * fmt
static void const char fflush(stdout)
va_end(args)
Assert(fmt[strlen(fmt) - 1] !='\n')
va_start(args, fmt)
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define pg_hton32(x)
Definition: pg_bswap.h:121
#define pg_hton16(x)
Definition: pg_bswap.h:120
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
const void size_t len
int32 encoding
Definition: pg_database.h:41
static char * buf
Definition: pg_test_fsync.c:73
@ PG_SQL_ASCII
Definition: pg_wchar.h:229
#define pg_char_to_encoding
Definition: pg_wchar.h:562
int64 end_time
Definition: pgbench.c:175
#define PG_STRERROR_R_BUFLEN
Definition: port.h:256
#define ALL_CONNECTION_FAILURE_ERRNOS
Definition: port.h:121
#define PGINVALID_SOCKET
Definition: port.h:31
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:397
bool appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args)
Definition: pqexpbuffer.c:294
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
#define PQExpBufferBroken(str)
Definition: pqexpbuffer.h:59
char * c
PGconn * conn
Definition: streamutil.c:54
char * write_err_msg
Definition: libpq-int.h:471
pgsocket sock
Definition: libpq-int.h:459
char * inBuffer
Definition: libpq-int.h:508
bool write_failed
Definition: libpq-int.h:470
int inCursor
Definition: libpq-int.h:511
int inEnd
Definition: libpq-int.h:512
int inBufSize
Definition: libpq-int.h:509
int inStart
Definition: libpq-int.h:510
PQExpBufferData errorMessage
Definition: libpq-int.h:617
int outBufSize
Definition: libpq-int.h:516
PGNoticeHooks noticeHooks
Definition: libpq-int.h:417
FILE * Pfdebug
Definition: libpq-int.h:413
int outMsgStart
Definition: libpq-int.h:520
int outCount
Definition: libpq-int.h:517
int outMsgEnd
Definition: libpq-int.h:522
bool ssl_in_use
Definition: libpq-int.h:547
char * outBuffer
Definition: libpq-int.h:515
ConnStatusType status
Definition: libpq-int.h:425
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:2151
int pg_encoding_mblen(int encoding, const char *mbstr)
Definition: wchar.c:2130
#define EINTR
Definition: win32_port.h:374
#define EWOULDBLOCK
Definition: win32_port.h:380
#define EAGAIN
Definition: win32_port.h:372
#define select(n, r, w, e, timeout)
Definition: win32_port.h:495