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