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