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  switch (SOCK_ERRNO)
672  {
673  case EINTR:
674  goto retry3;
675 
676  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
677 #ifdef EAGAIN
678  case EAGAIN:
679  return someread;
680 #endif
681 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
682  case EWOULDBLOCK:
683  return someread;
684 #endif
685 
686  /* We might get ECONNRESET etc here if connection failed */
688  goto definitelyFailed;
689 
690  default:
691  /* pqsecure_read set the error message for us */
692  return -1;
693  }
694  }
695  if (nread > 0)
696  {
697  conn->inEnd += nread;
698 
699  /*
700  * Hack to deal with the fact that some kernels will only give us back
701  * 1 packet per recv() call, even if we asked for more and there is
702  * more available. If it looks like we are reading a long message,
703  * loop back to recv() again immediately, until we run out of data or
704  * buffer space. Without this, the block-and-restart behavior of
705  * libpq's higher levels leads to O(N^2) performance on long messages.
706  *
707  * Since we left-justified the data above, conn->inEnd gives the
708  * amount of data already read in the current message. We consider
709  * the message "long" once we have acquired 32k ...
710  */
711  if (conn->inEnd > 32768 &&
712  (conn->inBufSize - conn->inEnd) >= 8192)
713  {
714  someread = 1;
715  goto retry3;
716  }
717  return 1;
718  }
719 
720  if (someread)
721  return 1; /* got a zero read after successful tries */
722 
723  /*
724  * A return value of 0 could mean just that no data is now available, or
725  * it could mean EOF --- that is, the server has closed the connection.
726  * Since we have the socket in nonblock mode, the only way to tell the
727  * difference is to see if select() is saying that the file is ready.
728  * Grumble. Fortunately, we don't expect this path to be taken much,
729  * since in normal practice we should not be trying to read data unless
730  * the file selected for reading already.
731  *
732  * In SSL mode it's even worse: SSL_read() could say WANT_READ and then
733  * data could arrive before we make the pqReadReady() test, but the second
734  * SSL_read() could still say WANT_READ because the data received was not
735  * a complete SSL record. So we must play dumb and assume there is more
736  * data, relying on the SSL layer to detect true EOF.
737  */
738 
739 #ifdef USE_SSL
740  if (conn->ssl_in_use)
741  return 0;
742 #endif
743 
744  switch (pqReadReady(conn))
745  {
746  case 0:
747  /* definitely no data available */
748  return 0;
749  case 1:
750  /* ready for read */
751  break;
752  default:
753  /* we override pqReadReady's message with something more useful */
754  goto definitelyEOF;
755  }
756 
757  /*
758  * Still not sure that it's EOF, because some data could have just
759  * arrived.
760  */
761 retry4:
762  nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd,
763  conn->inBufSize - conn->inEnd);
764  if (nread < 0)
765  {
766  switch (SOCK_ERRNO)
767  {
768  case EINTR:
769  goto retry4;
770 
771  /* Some systems return EAGAIN/EWOULDBLOCK for no data */
772 #ifdef EAGAIN
773  case EAGAIN:
774  return 0;
775 #endif
776 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
777  case EWOULDBLOCK:
778  return 0;
779 #endif
780 
781  /* We might get ECONNRESET etc here if connection failed */
783  goto definitelyFailed;
784 
785  default:
786  /* pqsecure_read set the error message for us */
787  return -1;
788  }
789  }
790  if (nread > 0)
791  {
792  conn->inEnd += nread;
793  return 1;
794  }
795 
796  /*
797  * OK, we are getting a zero read even though select() says ready. This
798  * means the connection has been closed. Cope.
799  */
800 definitelyEOF:
802  libpq_gettext("server closed the connection unexpectedly\n"
803  "\tThis probably means the server terminated abnormally\n"
804  "\tbefore or while processing the request.\n"));
805 
806  /* Come here if lower-level code already set a suitable errorMessage */
807 definitelyFailed:
808  /* Do *not* drop any already-read data; caller still wants it */
809  pqDropConnection(conn, false);
810  conn->status = CONNECTION_BAD; /* No more connection to backend */
811  return -1;
812 }
813 
814 /*
815  * pqSendSome: send data waiting in the output buffer.
816  *
817  * len is how much to try to send (typically equal to outCount, but may
818  * be less).
819  *
820  * Return 0 on success, -1 on failure and 1 when not all data could be sent
821  * because the socket would block and the connection is non-blocking.
822  *
823  * Note that this is also responsible for consuming data from the socket
824  * (putting it in conn->inBuffer) in any situation where we can't send
825  * all the specified data immediately.
826  *
827  * Upon write failure, conn->write_failed is set and the error message is
828  * saved in conn->write_err_msg, but we clear the output buffer and return
829  * zero anyway; this is because callers should soldier on until it's possible
830  * to read from the server and check for an error message. write_err_msg
831  * should be reported only when we are unable to obtain a server error first.
832  * (Thus, a -1 result is returned only for an internal *read* failure.)
833  */
834 static int
836 {
837  char *ptr = conn->outBuffer;
838  int remaining = conn->outCount;
839  int result = 0;
840 
841  /*
842  * If we already had a write failure, we will never again try to send data
843  * on that connection. Even if the kernel would let us, we've probably
844  * lost message boundary sync with the server. conn->write_failed
845  * therefore persists until the connection is reset, and we just discard
846  * all data presented to be written. However, as long as we still have a
847  * valid socket, we should continue to absorb data from the backend, so
848  * that we can collect any final error messages.
849  */
850  if (conn->write_failed)
851  {
852  /* conn->write_err_msg should be set up already */
853  conn->outCount = 0;
854  /* Absorb input data if any, and detect socket closure */
855  if (conn->sock != PGINVALID_SOCKET)
856  {
857  if (pqReadData(conn) < 0)
858  return -1;
859  }
860  return 0;
861  }
862 
863  if (conn->sock == PGINVALID_SOCKET)
864  {
866  libpq_gettext("connection not open\n"));
867  conn->write_failed = true;
868  /* Transfer error message to conn->write_err_msg, if possible */
869  /* (strdup failure is OK, we'll cope later) */
870  conn->write_err_msg = strdup(conn->errorMessage.data);
872  /* Discard queued data; no chance it'll ever be sent */
873  conn->outCount = 0;
874  return 0;
875  }
876 
877  /* while there's still data to send */
878  while (len > 0)
879  {
880  int sent;
881 
882 #ifndef WIN32
883  sent = pqsecure_write(conn, ptr, len);
884 #else
885 
886  /*
887  * Windows can fail on large sends, per KB article Q201213. The
888  * failure-point appears to be different in different versions of
889  * Windows, but 64k should always be safe.
890  */
891  sent = pqsecure_write(conn, ptr, Min(len, 65536));
892 #endif
893 
894  if (sent < 0)
895  {
896  /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
897  switch (SOCK_ERRNO)
898  {
899 #ifdef EAGAIN
900  case EAGAIN:
901  break;
902 #endif
903 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
904  case EWOULDBLOCK:
905  break;
906 #endif
907  case EINTR:
908  continue;
909 
910  default:
911  /* pqsecure_write set the error message for us */
912  conn->write_failed = true;
913 
914  /*
915  * Transfer error message to conn->write_err_msg, if
916  * possible (strdup failure is OK, we'll cope later).
917  *
918  * Note: this assumes that pqsecure_write and its children
919  * will overwrite not append to conn->errorMessage. If
920  * that's ever changed, we could remember the length of
921  * conn->errorMessage at entry to this routine, and then
922  * save and delete just what was appended.
923  */
924  conn->write_err_msg = strdup(conn->errorMessage.data);
926 
927  /* Discard queued data; no chance it'll ever be sent */
928  conn->outCount = 0;
929 
930  /* Absorb input data if any, and detect socket closure */
931  if (conn->sock != PGINVALID_SOCKET)
932  {
933  if (pqReadData(conn) < 0)
934  return -1;
935  }
936  return 0;
937  }
938  }
939  else
940  {
941  ptr += sent;
942  len -= sent;
943  remaining -= sent;
944  }
945 
946  if (len > 0)
947  {
948  /*
949  * We didn't send it all, wait till we can send more.
950  *
951  * There are scenarios in which we can't send data because the
952  * communications channel is full, but we cannot expect the server
953  * to clear the channel eventually because it's blocked trying to
954  * send data to us. (This can happen when we are sending a large
955  * amount of COPY data, and the server has generated lots of
956  * NOTICE responses.) To avoid a deadlock situation, we must be
957  * prepared to accept and buffer incoming data before we try
958  * again. Furthermore, it is possible that such incoming data
959  * might not arrive until after we've gone to sleep. Therefore,
960  * we wait for either read ready or write ready.
961  *
962  * In non-blocking mode, we don't wait here directly, but return 1
963  * to indicate that data is still pending. The caller should wait
964  * for both read and write ready conditions, and call
965  * PQconsumeInput() on read ready, but just in case it doesn't, we
966  * call pqReadData() ourselves before returning. That's not
967  * enough if the data has not arrived yet, but it's the best we
968  * can do, and works pretty well in practice. (The documentation
969  * used to say that you only need to wait for write-ready, so
970  * there are still plenty of applications like that out there.)
971  *
972  * Note that errors here don't result in write_failed becoming
973  * set.
974  */
975  if (pqReadData(conn) < 0)
976  {
977  result = -1; /* error message already set up */
978  break;
979  }
980 
981  if (pqIsnonblocking(conn))
982  {
983  result = 1;
984  break;
985  }
986 
987  if (pqWait(true, true, conn))
988  {
989  result = -1;
990  break;
991  }
992  }
993  }
994 
995  /* shift the remaining contents of the buffer */
996  if (remaining > 0)
997  memmove(conn->outBuffer, ptr, remaining);
998  conn->outCount = remaining;
999 
1000  return result;
1001 }
1002 
1003 
1004 /*
1005  * pqFlush: send any data waiting in the output buffer
1006  *
1007  * Return 0 on success, -1 on failure and 1 when not all data could be sent
1008  * because the socket would block and the connection is non-blocking.
1009  * (See pqSendSome comments about how failure should be handled.)
1010  */
1011 int
1013 {
1014  if (conn->Pfdebug)
1015  fflush(conn->Pfdebug);
1016 
1017  if (conn->outCount > 0)
1018  return pqSendSome(conn, conn->outCount);
1019 
1020  return 0;
1021 }
1022 
1023 
1024 /*
1025  * pqWait: wait until we can read or write the connection socket
1026  *
1027  * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
1028  * call to select().
1029  *
1030  * We also stop waiting and return if the kernel flags an exception condition
1031  * on the socket. The actual error condition will be detected and reported
1032  * when the caller tries to read or write the socket.
1033  */
1034 int
1035 pqWait(int forRead, int forWrite, PGconn *conn)
1036 {
1037  return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
1038 }
1039 
1040 /*
1041  * pqWaitTimed: wait, but not past finish_time.
1042  *
1043  * finish_time = ((time_t) -1) disables the wait limit.
1044  *
1045  * Returns -1 on failure, 0 if the socket is readable/writable, 1 if it timed out.
1046  */
1047 int
1048 pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
1049 {
1050  int result;
1051 
1052  result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1053 
1054  if (result < 0)
1055  return -1; /* errorMessage is already set */
1056 
1057  if (result == 0)
1058  {
1060  libpq_gettext("timeout expired\n"));
1061  return 1;
1062  }
1063 
1064  return 0;
1065 }
1066 
1067 /*
1068  * pqReadReady: is select() saying the file is ready to read?
1069  * Returns -1 on failure, 0 if not ready, 1 if ready.
1070  */
1071 int
1073 {
1074  return pqSocketCheck(conn, 1, 0, (time_t) 0);
1075 }
1076 
1077 /*
1078  * pqWriteReady: is select() saying the file is ready to write?
1079  * Returns -1 on failure, 0 if not ready, 1 if ready.
1080  */
1081 int
1083 {
1084  return pqSocketCheck(conn, 0, 1, (time_t) 0);
1085 }
1086 
1087 /*
1088  * Checks a socket, using poll or select, for data to be read, written,
1089  * or both. Returns >0 if one or more conditions are met, 0 if it timed
1090  * out, -1 if an error occurred.
1091  *
1092  * If SSL is in use, the SSL buffer is checked prior to checking the socket
1093  * for read data directly.
1094  */
1095 static int
1096 pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
1097 {
1098  int result;
1099 
1100  if (!conn)
1101  return -1;
1102  if (conn->sock == PGINVALID_SOCKET)
1103  {
1105  libpq_gettext("invalid socket\n"));
1106  return -1;
1107  }
1108 
1109 #ifdef USE_SSL
1110  /* Check for SSL library buffering read bytes */
1111  if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
1112  {
1113  /* short-circuit the select */
1114  return 1;
1115  }
1116 #endif
1117 
1118  /* We will retry as long as we get EINTR */
1119  do
1120  result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1121  while (result < 0 && SOCK_ERRNO == EINTR);
1122 
1123  if (result < 0)
1124  {
1125  char sebuf[PG_STRERROR_R_BUFLEN];
1126 
1128  libpq_gettext("select() failed: %s\n"),
1129  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1130  }
1131 
1132  return result;
1133 }
1134 
1135 
1136 /*
1137  * Check a file descriptor for read and/or write data, possibly waiting.
1138  * If neither forRead nor forWrite are set, immediately return a timeout
1139  * condition (without waiting). Return >0 if condition is met, 0
1140  * if a timeout occurred, -1 if an error or interrupt occurred.
1141  *
1142  * Timeout is infinite if end_time is -1. Timeout is immediate (no blocking)
1143  * if end_time is 0 (or indeed, any time before now).
1144  */
1145 static int
1146 pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
1147 {
1148  /* We use poll(2) if available, otherwise select(2) */
1149 #ifdef HAVE_POLL
1150  struct pollfd input_fd;
1151  int timeout_ms;
1152 
1153  if (!forRead && !forWrite)
1154  return 0;
1155 
1156  input_fd.fd = sock;
1157  input_fd.events = POLLERR;
1158  input_fd.revents = 0;
1159 
1160  if (forRead)
1161  input_fd.events |= POLLIN;
1162  if (forWrite)
1163  input_fd.events |= POLLOUT;
1164 
1165  /* Compute appropriate timeout interval */
1166  if (end_time == ((time_t) -1))
1167  timeout_ms = -1;
1168  else
1169  {
1170  time_t now = time(NULL);
1171 
1172  if (end_time > now)
1173  timeout_ms = (end_time - now) * 1000;
1174  else
1175  timeout_ms = 0;
1176  }
1177 
1178  return poll(&input_fd, 1, timeout_ms);
1179 #else /* !HAVE_POLL */
1180 
1181  fd_set input_mask;
1182  fd_set output_mask;
1183  fd_set except_mask;
1184  struct timeval timeout;
1185  struct timeval *ptr_timeout;
1186 
1187  if (!forRead && !forWrite)
1188  return 0;
1189 
1190  FD_ZERO(&input_mask);
1191  FD_ZERO(&output_mask);
1192  FD_ZERO(&except_mask);
1193  if (forRead)
1194  FD_SET(sock, &input_mask);
1195 
1196  if (forWrite)
1197  FD_SET(sock, &output_mask);
1198  FD_SET(sock, &except_mask);
1199 
1200  /* Compute appropriate timeout interval */
1201  if (end_time == ((time_t) -1))
1202  ptr_timeout = NULL;
1203  else
1204  {
1205  time_t now = time(NULL);
1206 
1207  if (end_time > now)
1208  timeout.tv_sec = end_time - now;
1209  else
1210  timeout.tv_sec = 0;
1211  timeout.tv_usec = 0;
1212  ptr_timeout = &timeout;
1213  }
1214 
1215  return select(sock + 1, &input_mask, &output_mask,
1216  &except_mask, ptr_timeout);
1217 #endif /* HAVE_POLL */
1218 }
1219 
1220 
1221 /*
1222  * A couple of "miscellaneous" multibyte related functions. They used
1223  * to be in fe-print.c but that file is doomed.
1224  */
1225 
1226 /*
1227  * returns the byte length of the character beginning at s, using the
1228  * specified encoding.
1229  */
1230 int
1231 PQmblen(const char *s, int encoding)
1232 {
1233  return pg_encoding_mblen(encoding, s);
1234 }
1235 
1236 /*
1237  * returns the display length of the character beginning at s, using the
1238  * specified encoding.
1239  */
1240 int
1241 PQdsplen(const char *s, int encoding)
1242 {
1243  return pg_encoding_dsplen(encoding, s);
1244 }
1245 
1246 /*
1247  * Get encoding id from environment variable PGCLIENTENCODING.
1248  */
1249 int
1251 {
1252  char *str;
1253  int encoding = PG_SQL_ASCII;
1254 
1255  str = getenv("PGCLIENTENCODING");
1256  if (str && *str != '\0')
1257  {
1258  encoding = pg_char_to_encoding(str);
1259  if (encoding < 0)
1260  encoding = PG_SQL_ASCII;
1261  }
1262  return encoding;
1263 }
1264 
1265 
1266 #ifdef ENABLE_NLS
1267 
1268 static void
1269 libpq_binddomain(void)
1270 {
1271  static bool already_bound = false;
1272 
1273  if (!already_bound)
1274  {
1275  /* bindtextdomain() does not preserve errno */
1276 #ifdef WIN32
1277  int save_errno = GetLastError();
1278 #else
1279  int save_errno = errno;
1280 #endif
1281  const char *ldir;
1282 
1283  already_bound = true;
1284  /* No relocatable lookup here because the binary could be anywhere */
1285  ldir = getenv("PGLOCALEDIR");
1286  if (!ldir)
1287  ldir = LOCALEDIR;
1288  bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1289 #ifdef WIN32
1290  SetLastError(save_errno);
1291 #else
1292  errno = save_errno;
1293 #endif
1294  }
1295 }
1296 
1297 char *
1298 libpq_gettext(const char *msgid)
1299 {
1300  libpq_binddomain();
1301  return dgettext(PG_TEXTDOMAIN("libpq"), msgid);
1302 }
1303 
1304 char *
1305 libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)
1306 {
1307  libpq_binddomain();
1308  return dngettext(PG_TEXTDOMAIN("libpq"), msgid, msgid_plural, n);
1309 }
1310 
1311 #endif /* ENABLE_NLS */
int remaining
Definition: informix.c:667
int pqFlush(PGconn *conn)
Definition: fe-misc.c:1012
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:1072
int pqCheckOutBufferSpace(size_t bytes_needed, PGconn *conn)
Definition: fe-misc.c:330
#define EAGAIN
Definition: win32_port.h:341
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:1241
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
#define PG_STRERROR_R_BUFLEN
Definition: port.h:233
#define Min(x, y)
Definition: c.h:928
static int pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
Definition: fe-misc.c:1096
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:459
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:219
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:1146
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:295
int64 end_time
Definition: pgbench.c:148
unsigned short uint16
Definition: c.h:374
int pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
Definition: fe-misc.c:1048
#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:1035
static char * buf
Definition: pg_test_fsync.c:68
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:464
int pg_encoding_dsplen(int encoding, const char *mbstr)
Definition: wchar.c:1565
unsigned int uint32
Definition: c.h:375
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:1082
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:1250
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1157
#define PGINVALID_SOCKET
Definition: port.h:33
char * inBuffer
Definition: libpq-int.h:444
#define dgettext(d, x)
Definition: c.h:1123
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:1125
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
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
#define ALL_CONNECTION_FAILURE_ERRNOS
Definition: port.h:114
static int pqSendSome(PGconn *conn, int len)
Definition: fe-misc.c:835
int pqSkipnchar(size_t len, PGconn *conn)
Definition: fe-misc.c:210
#define EWOULDBLOCK
Definition: win32_port.h:349
int inCursor
Definition: libpq-int.h:447
#define EINTR
Definition: win32_port.h:343
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
int PQmblen(const char *s, int encoding)
Definition: fe-misc.c:1231
int pqGets_append(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:155
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1542
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
Definition: pqexpbuffer.c:399
#define libpq_gettext(x)
Definition: libpq-int.h:805