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