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-2018, 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
57 static int pqPutMsgBytes(const void *buf, size_t len, PGconn *conn);
58 static int pqSendSome(PGconn *conn, int len);
59 static int pqSocketCheck(PGconn *conn, int forRead, int forWrite,
60 time_t end_time);
61 static int pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time);
62
63 /*
64 * PQlibVersion: return the libpq version number
65 */
66 int
PQlibVersion(void)67 PQlibVersion(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 */
78 static void
fputnbytes(FILE * f,const char * str,size_t n)79 fputnbytes(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 */
93 int
pqGetc(char * result,PGconn * conn)94 pqGetc(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 */
111 int
pqPutc(char c,PGconn * conn)112 pqPutc(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 */
131 static int
pqGets_internal(PQExpBuffer buf,PGconn * conn,bool resetbuffer)132 pqGets_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
162 int
pqGets(PQExpBuffer buf,PGconn * conn)163 pqGets(PQExpBuffer buf, PGconn *conn)
164 {
165 return pqGets_internal(buf, conn, true);
166 }
167
168 int
pqGets_append(PQExpBuffer buf,PGconn * conn)169 pqGets_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 */
178 int
pqPuts(const char * s,PGconn * conn)179 pqPuts(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 */
194 int
pqGetnchar(char * s,size_t len,PGconn * conn)195 pqGetnchar(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 */
223 int
pqSkipnchar(size_t len,PGconn * conn)224 pqSkipnchar(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 */
245 int
pqPutnchar(const char * s,size_t len,PGconn * conn)246 pqPutnchar(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 */
266 int
pqGetInt(int * result,size_t bytes,PGconn * conn)267 pqGetInt(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 */
306 int
pqPutInt(int value,size_t bytes,PGconn * conn)307 pqPutInt(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 */
343 int
pqCheckOutBufferSpace(size_t bytes_needed,PGconn * conn)344 pqCheckOutBufferSpace(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 */
407 int
pqCheckInBufferSpace(size_t bytes_needed,PGconn * conn)408 pqCheckInBufferSpace(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 */
518 int
pqPutMsgStart(char msg_type,bool force_len,PGconn * conn)519 pqPutMsgStart(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 */
563 static int
pqPutMsgBytes(const void * buf,size_t len,PGconn * conn)564 pqPutMsgBytes(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 */
586 int
pqPutMsgEnd(PGconn * conn)587 pqPutMsgEnd(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 */
628 int
pqReadData(PGconn * conn)629 pqReadData(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 */
680 retry3:
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 */
770 retry4:
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 */
804 definitelyEOF:
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 */
812 definitelyFailed:
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 static int
pqSendSome(PGconn * conn,int len)829 pqSendSome(PGconn *conn, int len)
830 {
831 char *ptr = conn->outBuffer;
832 int remaining = conn->outCount;
833 int result = 0;
834
835 if (conn->sock == PGINVALID_SOCKET)
836 {
837 printfPQExpBuffer(&conn->errorMessage,
838 libpq_gettext("connection not open\n"));
839 /* Discard queued data; no chance it'll ever be sent */
840 conn->outCount = 0;
841 return -1;
842 }
843
844 /* while there's still data to send */
845 while (len > 0)
846 {
847 int sent;
848
849 #ifndef WIN32
850 sent = pqsecure_write(conn, ptr, len);
851 #else
852
853 /*
854 * Windows can fail on large sends, per KB article Q201213. The
855 * failure-point appears to be different in different versions of
856 * Windows, but 64k should always be safe.
857 */
858 sent = pqsecure_write(conn, ptr, Min(len, 65536));
859 #endif
860
861 if (sent < 0)
862 {
863 /* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
864 switch (SOCK_ERRNO)
865 {
866 #ifdef EAGAIN
867 case EAGAIN:
868 break;
869 #endif
870 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
871 case EWOULDBLOCK:
872 break;
873 #endif
874 case EINTR:
875 continue;
876
877 default:
878 /* pqsecure_write set the error message for us */
879
880 /*
881 * We used to close the socket here, but that's a bad idea
882 * since there might be unread data waiting (typically, a
883 * NOTICE message from the backend telling us it's
884 * committing hara-kiri...). Leave the socket open until
885 * pqReadData finds no more data can be read. But abandon
886 * attempt to send data.
887 */
888 conn->outCount = 0;
889 return -1;
890 }
891 }
892 else
893 {
894 ptr += sent;
895 len -= sent;
896 remaining -= sent;
897 }
898
899 if (len > 0)
900 {
901 /*
902 * We didn't send it all, wait till we can send more.
903 *
904 * There are scenarios in which we can't send data because the
905 * communications channel is full, but we cannot expect the server
906 * to clear the channel eventually because it's blocked trying to
907 * send data to us. (This can happen when we are sending a large
908 * amount of COPY data, and the server has generated lots of
909 * NOTICE responses.) To avoid a deadlock situation, we must be
910 * prepared to accept and buffer incoming data before we try
911 * again. Furthermore, it is possible that such incoming data
912 * might not arrive until after we've gone to sleep. Therefore,
913 * we wait for either read ready or write ready.
914 *
915 * In non-blocking mode, we don't wait here directly, but return 1
916 * to indicate that data is still pending. The caller should wait
917 * for both read and write ready conditions, and call
918 * PQconsumeInput() on read ready, but just in case it doesn't, we
919 * call pqReadData() ourselves before returning. That's not
920 * enough if the data has not arrived yet, but it's the best we
921 * can do, and works pretty well in practice. (The documentation
922 * used to say that you only need to wait for write-ready, so
923 * there are still plenty of applications like that out there.)
924 */
925 if (pqReadData(conn) < 0)
926 {
927 result = -1; /* error message already set up */
928 break;
929 }
930
931 if (pqIsnonblocking(conn))
932 {
933 result = 1;
934 break;
935 }
936
937 if (pqWait(true, true, conn))
938 {
939 result = -1;
940 break;
941 }
942 }
943 }
944
945 /* shift the remaining contents of the buffer */
946 if (remaining > 0)
947 memmove(conn->outBuffer, ptr, remaining);
948 conn->outCount = remaining;
949
950 return result;
951 }
952
953
954 /*
955 * pqFlush: send any data waiting in the output buffer
956 *
957 * Return 0 on success, -1 on failure and 1 when not all data could be sent
958 * because the socket would block and the connection is non-blocking.
959 */
960 int
pqFlush(PGconn * conn)961 pqFlush(PGconn *conn)
962 {
963 if (conn->Pfdebug)
964 fflush(conn->Pfdebug);
965
966 if (conn->outCount > 0)
967 return pqSendSome(conn, conn->outCount);
968
969 return 0;
970 }
971
972
973 /*
974 * pqWait: wait until we can read or write the connection socket
975 *
976 * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
977 * call to select().
978 *
979 * We also stop waiting and return if the kernel flags an exception condition
980 * on the socket. The actual error condition will be detected and reported
981 * when the caller tries to read or write the socket.
982 */
983 int
pqWait(int forRead,int forWrite,PGconn * conn)984 pqWait(int forRead, int forWrite, PGconn *conn)
985 {
986 return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
987 }
988
989 /*
990 * pqWaitTimed: wait, but not past finish_time.
991 *
992 * finish_time = ((time_t) -1) disables the wait limit.
993 *
994 * Returns -1 on failure, 0 if the socket is readable/writable, 1 if it timed out.
995 */
996 int
pqWaitTimed(int forRead,int forWrite,PGconn * conn,time_t finish_time)997 pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
998 {
999 int result;
1000
1001 result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1002
1003 if (result < 0)
1004 return -1; /* errorMessage is already set */
1005
1006 if (result == 0)
1007 {
1008 printfPQExpBuffer(&conn->errorMessage,
1009 libpq_gettext("timeout expired\n"));
1010 return 1;
1011 }
1012
1013 return 0;
1014 }
1015
1016 /*
1017 * pqReadReady: is select() saying the file is ready to read?
1018 * Returns -1 on failure, 0 if not ready, 1 if ready.
1019 */
1020 int
pqReadReady(PGconn * conn)1021 pqReadReady(PGconn *conn)
1022 {
1023 return pqSocketCheck(conn, 1, 0, (time_t) 0);
1024 }
1025
1026 /*
1027 * pqWriteReady: is select() saying the file is ready to write?
1028 * Returns -1 on failure, 0 if not ready, 1 if ready.
1029 */
1030 int
pqWriteReady(PGconn * conn)1031 pqWriteReady(PGconn *conn)
1032 {
1033 return pqSocketCheck(conn, 0, 1, (time_t) 0);
1034 }
1035
1036 /*
1037 * Checks a socket, using poll or select, for data to be read, written,
1038 * or both. Returns >0 if one or more conditions are met, 0 if it timed
1039 * out, -1 if an error occurred.
1040 *
1041 * If SSL is in use, the SSL buffer is checked prior to checking the socket
1042 * for read data directly.
1043 */
1044 static int
pqSocketCheck(PGconn * conn,int forRead,int forWrite,time_t end_time)1045 pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
1046 {
1047 int result;
1048
1049 if (!conn)
1050 return -1;
1051 if (conn->sock == PGINVALID_SOCKET)
1052 {
1053 printfPQExpBuffer(&conn->errorMessage,
1054 libpq_gettext("invalid socket\n"));
1055 return -1;
1056 }
1057
1058 #ifdef USE_SSL
1059 /* Check for SSL library buffering read bytes */
1060 if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
1061 {
1062 /* short-circuit the select */
1063 return 1;
1064 }
1065 #endif
1066
1067 /* We will retry as long as we get EINTR */
1068 do
1069 result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1070 while (result < 0 && SOCK_ERRNO == EINTR);
1071
1072 if (result < 0)
1073 {
1074 char sebuf[256];
1075
1076 printfPQExpBuffer(&conn->errorMessage,
1077 libpq_gettext("select() failed: %s\n"),
1078 SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1079 }
1080
1081 return result;
1082 }
1083
1084
1085 /*
1086 * Check a file descriptor for read and/or write data, possibly waiting.
1087 * If neither forRead nor forWrite are set, immediately return a timeout
1088 * condition (without waiting). Return >0 if condition is met, 0
1089 * if a timeout occurred, -1 if an error or interrupt occurred.
1090 *
1091 * Timeout is infinite if end_time is -1. Timeout is immediate (no blocking)
1092 * if end_time is 0 (or indeed, any time before now).
1093 */
1094 static int
pqSocketPoll(int sock,int forRead,int forWrite,time_t end_time)1095 pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
1096 {
1097 /* We use poll(2) if available, otherwise select(2) */
1098 #ifdef HAVE_POLL
1099 struct pollfd input_fd;
1100 int timeout_ms;
1101
1102 if (!forRead && !forWrite)
1103 return 0;
1104
1105 input_fd.fd = sock;
1106 input_fd.events = POLLERR;
1107 input_fd.revents = 0;
1108
1109 if (forRead)
1110 input_fd.events |= POLLIN;
1111 if (forWrite)
1112 input_fd.events |= POLLOUT;
1113
1114 /* Compute appropriate timeout interval */
1115 if (end_time == ((time_t) -1))
1116 timeout_ms = -1;
1117 else
1118 {
1119 time_t now = time(NULL);
1120
1121 if (end_time > now)
1122 timeout_ms = (end_time - now) * 1000;
1123 else
1124 timeout_ms = 0;
1125 }
1126
1127 return poll(&input_fd, 1, timeout_ms);
1128 #else /* !HAVE_POLL */
1129
1130 fd_set input_mask;
1131 fd_set output_mask;
1132 fd_set except_mask;
1133 struct timeval timeout;
1134 struct timeval *ptr_timeout;
1135
1136 if (!forRead && !forWrite)
1137 return 0;
1138
1139 FD_ZERO(&input_mask);
1140 FD_ZERO(&output_mask);
1141 FD_ZERO(&except_mask);
1142 if (forRead)
1143 FD_SET(sock, &input_mask);
1144
1145 if (forWrite)
1146 FD_SET(sock, &output_mask);
1147 FD_SET(sock, &except_mask);
1148
1149 /* Compute appropriate timeout interval */
1150 if (end_time == ((time_t) -1))
1151 ptr_timeout = NULL;
1152 else
1153 {
1154 time_t now = time(NULL);
1155
1156 if (end_time > now)
1157 timeout.tv_sec = end_time - now;
1158 else
1159 timeout.tv_sec = 0;
1160 timeout.tv_usec = 0;
1161 ptr_timeout = &timeout;
1162 }
1163
1164 return select(sock + 1, &input_mask, &output_mask,
1165 &except_mask, ptr_timeout);
1166 #endif /* HAVE_POLL */
1167 }
1168
1169
1170 /*
1171 * A couple of "miscellaneous" multibyte related functions. They used
1172 * to be in fe-print.c but that file is doomed.
1173 */
1174
1175 /*
1176 * returns the byte length of the character beginning at s, using the
1177 * specified encoding.
1178 */
1179 int
PQmblen(const char * s,int encoding)1180 PQmblen(const char *s, int encoding)
1181 {
1182 return pg_encoding_mblen(encoding, s);
1183 }
1184
1185 /*
1186 * returns the display length of the character beginning at s, using the
1187 * specified encoding.
1188 */
1189 int
PQdsplen(const char * s,int encoding)1190 PQdsplen(const char *s, int encoding)
1191 {
1192 return pg_encoding_dsplen(encoding, s);
1193 }
1194
1195 /*
1196 * Get encoding id from environment variable PGCLIENTENCODING.
1197 */
1198 int
PQenv2encoding(void)1199 PQenv2encoding(void)
1200 {
1201 char *str;
1202 int encoding = PG_SQL_ASCII;
1203
1204 str = getenv("PGCLIENTENCODING");
1205 if (str && *str != '\0')
1206 {
1207 encoding = pg_char_to_encoding(str);
1208 if (encoding < 0)
1209 encoding = PG_SQL_ASCII;
1210 }
1211 return encoding;
1212 }
1213
1214
1215 #ifdef ENABLE_NLS
1216
1217 static void
libpq_binddomain()1218 libpq_binddomain()
1219 {
1220 static bool already_bound = false;
1221
1222 if (!already_bound)
1223 {
1224 /* bindtextdomain() does not preserve errno */
1225 #ifdef WIN32
1226 int save_errno = GetLastError();
1227 #else
1228 int save_errno = errno;
1229 #endif
1230 const char *ldir;
1231
1232 already_bound = true;
1233 /* No relocatable lookup here because the binary could be anywhere */
1234 ldir = getenv("PGLOCALEDIR");
1235 if (!ldir)
1236 ldir = LOCALEDIR;
1237 bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1238 #ifdef WIN32
1239 SetLastError(save_errno);
1240 #else
1241 errno = save_errno;
1242 #endif
1243 }
1244 }
1245
1246 char *
libpq_gettext(const char * msgid)1247 libpq_gettext(const char *msgid)
1248 {
1249 libpq_binddomain();
1250 return dgettext(PG_TEXTDOMAIN("libpq"), msgid);
1251 }
1252
1253 char *
libpq_ngettext(const char * msgid,const char * msgid_plural,unsigned long n)1254 libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)
1255 {
1256 libpq_binddomain();
1257 return dngettext(PG_TEXTDOMAIN("libpq"), msgid, msgid_plural, n);
1258 }
1259
1260 #endif /* ENABLE_NLS */
1261