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