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