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 
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  * Note that this is also responsible for consuming data from the socket
829  * (putting it in conn->inBuffer) in any situation where we can't send
830  * all the specified data immediately.
831  *
832  * Upon write failure, conn->write_failed is set and the error message is
833  * saved in conn->write_err_msg, but we clear the output buffer and return
834  * zero anyway; this is because callers should soldier on until it's possible
835  * to read from the server and check for an error message.  write_err_msg
836  * should be reported only when we are unable to obtain a server error first.
837  * (Thus, a -1 result is returned only for an internal *read* failure.)
838  */
839 static int
pqSendSome(PGconn * conn,int len)840 pqSendSome(PGconn *conn, int len)
841 {
842 	char	   *ptr = conn->outBuffer;
843 	int			remaining = conn->outCount;
844 	int			result = 0;
845 
846 	/*
847 	 * If we already had a write failure, we will never again try to send data
848 	 * on that connection.  Even if the kernel would let us, we've probably
849 	 * lost message boundary sync with the server.  conn->write_failed
850 	 * therefore persists until the connection is reset, and we just discard
851 	 * all data presented to be written.  However, as long as we still have a
852 	 * valid socket, we should continue to absorb data from the backend, so
853 	 * that we can collect any final error messages.
854 	 */
855 	if (conn->write_failed)
856 	{
857 		/* conn->write_err_msg should be set up already */
858 		conn->outCount = 0;
859 		/* Absorb input data if any, and detect socket closure */
860 		if (conn->sock != PGINVALID_SOCKET)
861 		{
862 			if (pqReadData(conn) < 0)
863 				return -1;
864 		}
865 		return 0;
866 	}
867 
868 	if (conn->sock == PGINVALID_SOCKET)
869 	{
870 		printfPQExpBuffer(&conn->errorMessage,
871 						  libpq_gettext("connection not open\n"));
872 		conn->write_failed = true;
873 		/* Transfer error message to conn->write_err_msg, if possible */
874 		/* (strdup failure is OK, we'll cope later) */
875 		conn->write_err_msg = strdup(conn->errorMessage.data);
876 		resetPQExpBuffer(&conn->errorMessage);
877 		/* Discard queued data; no chance it'll ever be sent */
878 		conn->outCount = 0;
879 		return 0;
880 	}
881 
882 	/* while there's still data to send */
883 	while (len > 0)
884 	{
885 		int			sent;
886 
887 #ifndef WIN32
888 		sent = pqsecure_write(conn, ptr, len);
889 #else
890 
891 		/*
892 		 * Windows can fail on large sends, per KB article Q201213. The
893 		 * failure-point appears to be different in different versions of
894 		 * Windows, but 64k should always be safe.
895 		 */
896 		sent = pqsecure_write(conn, ptr, Min(len, 65536));
897 #endif
898 
899 		if (sent < 0)
900 		{
901 			/* Anything except EAGAIN/EWOULDBLOCK/EINTR is trouble */
902 			switch (SOCK_ERRNO)
903 			{
904 #ifdef EAGAIN
905 				case EAGAIN:
906 					break;
907 #endif
908 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
909 				case EWOULDBLOCK:
910 					break;
911 #endif
912 				case EINTR:
913 					continue;
914 
915 				default:
916 					/* pqsecure_write set the error message for us */
917 					conn->write_failed = true;
918 
919 					/*
920 					 * Transfer error message to conn->write_err_msg, if
921 					 * possible (strdup failure is OK, we'll cope later).
922 					 *
923 					 * Note: this assumes that pqsecure_write and its children
924 					 * will overwrite not append to conn->errorMessage.  If
925 					 * that's ever changed, we could remember the length of
926 					 * conn->errorMessage at entry to this routine, and then
927 					 * save and delete just what was appended.
928 					 */
929 					conn->write_err_msg = strdup(conn->errorMessage.data);
930 					resetPQExpBuffer(&conn->errorMessage);
931 
932 					/* Discard queued data; no chance it'll ever be sent */
933 					conn->outCount = 0;
934 
935 					/* Absorb input data if any, and detect socket closure */
936 					if (conn->sock != PGINVALID_SOCKET)
937 					{
938 						if (pqReadData(conn) < 0)
939 							return -1;
940 					}
941 					return 0;
942 			}
943 		}
944 		else
945 		{
946 			ptr += sent;
947 			len -= sent;
948 			remaining -= sent;
949 		}
950 
951 		if (len > 0)
952 		{
953 			/*
954 			 * We didn't send it all, wait till we can send more.
955 			 *
956 			 * There are scenarios in which we can't send data because the
957 			 * communications channel is full, but we cannot expect the server
958 			 * to clear the channel eventually because it's blocked trying to
959 			 * send data to us.  (This can happen when we are sending a large
960 			 * amount of COPY data, and the server has generated lots of
961 			 * NOTICE responses.)  To avoid a deadlock situation, we must be
962 			 * prepared to accept and buffer incoming data before we try
963 			 * again.  Furthermore, it is possible that such incoming data
964 			 * might not arrive until after we've gone to sleep.  Therefore,
965 			 * we wait for either read ready or write ready.
966 			 *
967 			 * In non-blocking mode, we don't wait here directly, but return 1
968 			 * to indicate that data is still pending.  The caller should wait
969 			 * for both read and write ready conditions, and call
970 			 * PQconsumeInput() on read ready, but just in case it doesn't, we
971 			 * call pqReadData() ourselves before returning.  That's not
972 			 * enough if the data has not arrived yet, but it's the best we
973 			 * can do, and works pretty well in practice.  (The documentation
974 			 * used to say that you only need to wait for write-ready, so
975 			 * there are still plenty of applications like that out there.)
976 			 *
977 			 * Note that errors here don't result in write_failed becoming
978 			 * set.
979 			 */
980 			if (pqReadData(conn) < 0)
981 			{
982 				result = -1;	/* error message already set up */
983 				break;
984 			}
985 
986 			if (pqIsnonblocking(conn))
987 			{
988 				result = 1;
989 				break;
990 			}
991 
992 			if (pqWait(true, true, conn))
993 			{
994 				result = -1;
995 				break;
996 			}
997 		}
998 	}
999 
1000 	/* shift the remaining contents of the buffer */
1001 	if (remaining > 0)
1002 		memmove(conn->outBuffer, ptr, remaining);
1003 	conn->outCount = remaining;
1004 
1005 	return result;
1006 }
1007 
1008 
1009 /*
1010  * pqFlush: send any data waiting in the output buffer
1011  *
1012  * Return 0 on success, -1 on failure and 1 when not all data could be sent
1013  * because the socket would block and the connection is non-blocking.
1014  * (See pqSendSome comments about how failure should be handled.)
1015  */
1016 int
pqFlush(PGconn * conn)1017 pqFlush(PGconn *conn)
1018 {
1019 	if (conn->Pfdebug)
1020 		fflush(conn->Pfdebug);
1021 
1022 	if (conn->outCount > 0)
1023 		return pqSendSome(conn, conn->outCount);
1024 
1025 	return 0;
1026 }
1027 
1028 
1029 /*
1030  * pqWait: wait until we can read or write the connection socket
1031  *
1032  * JAB: If SSL enabled and used and forRead, buffered bytes short-circuit the
1033  * call to select().
1034  *
1035  * We also stop waiting and return if the kernel flags an exception condition
1036  * on the socket.  The actual error condition will be detected and reported
1037  * when the caller tries to read or write the socket.
1038  */
1039 int
pqWait(int forRead,int forWrite,PGconn * conn)1040 pqWait(int forRead, int forWrite, PGconn *conn)
1041 {
1042 	return pqWaitTimed(forRead, forWrite, conn, (time_t) -1);
1043 }
1044 
1045 /*
1046  * pqWaitTimed: wait, but not past finish_time.
1047  *
1048  * finish_time = ((time_t) -1) disables the wait limit.
1049  *
1050  * Returns -1 on failure, 0 if the socket is readable/writable, 1 if it timed out.
1051  */
1052 int
pqWaitTimed(int forRead,int forWrite,PGconn * conn,time_t finish_time)1053 pqWaitTimed(int forRead, int forWrite, PGconn *conn, time_t finish_time)
1054 {
1055 	int			result;
1056 
1057 	result = pqSocketCheck(conn, forRead, forWrite, finish_time);
1058 
1059 	if (result < 0)
1060 		return -1;				/* errorMessage is already set */
1061 
1062 	if (result == 0)
1063 	{
1064 		printfPQExpBuffer(&conn->errorMessage,
1065 						  libpq_gettext("timeout expired\n"));
1066 		return 1;
1067 	}
1068 
1069 	return 0;
1070 }
1071 
1072 /*
1073  * pqReadReady: is select() saying the file is ready to read?
1074  * Returns -1 on failure, 0 if not ready, 1 if ready.
1075  */
1076 int
pqReadReady(PGconn * conn)1077 pqReadReady(PGconn *conn)
1078 {
1079 	return pqSocketCheck(conn, 1, 0, (time_t) 0);
1080 }
1081 
1082 /*
1083  * pqWriteReady: is select() saying the file is ready to write?
1084  * Returns -1 on failure, 0 if not ready, 1 if ready.
1085  */
1086 int
pqWriteReady(PGconn * conn)1087 pqWriteReady(PGconn *conn)
1088 {
1089 	return pqSocketCheck(conn, 0, 1, (time_t) 0);
1090 }
1091 
1092 /*
1093  * Checks a socket, using poll or select, for data to be read, written,
1094  * or both.  Returns >0 if one or more conditions are met, 0 if it timed
1095  * out, -1 if an error occurred.
1096  *
1097  * If SSL is in use, the SSL buffer is checked prior to checking the socket
1098  * for read data directly.
1099  */
1100 static int
pqSocketCheck(PGconn * conn,int forRead,int forWrite,time_t end_time)1101 pqSocketCheck(PGconn *conn, int forRead, int forWrite, time_t end_time)
1102 {
1103 	int			result;
1104 
1105 	if (!conn)
1106 		return -1;
1107 	if (conn->sock == PGINVALID_SOCKET)
1108 	{
1109 		printfPQExpBuffer(&conn->errorMessage,
1110 						  libpq_gettext("invalid socket\n"));
1111 		return -1;
1112 	}
1113 
1114 #ifdef USE_SSL
1115 	/* Check for SSL library buffering read bytes */
1116 	if (forRead && conn->ssl_in_use && pgtls_read_pending(conn))
1117 	{
1118 		/* short-circuit the select */
1119 		return 1;
1120 	}
1121 #endif
1122 
1123 	/* We will retry as long as we get EINTR */
1124 	do
1125 		result = pqSocketPoll(conn->sock, forRead, forWrite, end_time);
1126 	while (result < 0 && SOCK_ERRNO == EINTR);
1127 
1128 	if (result < 0)
1129 	{
1130 		char		sebuf[PG_STRERROR_R_BUFLEN];
1131 
1132 		printfPQExpBuffer(&conn->errorMessage,
1133 						  libpq_gettext("select() failed: %s\n"),
1134 						  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1135 	}
1136 
1137 	return result;
1138 }
1139 
1140 
1141 /*
1142  * Check a file descriptor for read and/or write data, possibly waiting.
1143  * If neither forRead nor forWrite are set, immediately return a timeout
1144  * condition (without waiting).  Return >0 if condition is met, 0
1145  * if a timeout occurred, -1 if an error or interrupt occurred.
1146  *
1147  * Timeout is infinite if end_time is -1.  Timeout is immediate (no blocking)
1148  * if end_time is 0 (or indeed, any time before now).
1149  */
1150 static int
pqSocketPoll(int sock,int forRead,int forWrite,time_t end_time)1151 pqSocketPoll(int sock, int forRead, int forWrite, time_t end_time)
1152 {
1153 	/* We use poll(2) if available, otherwise select(2) */
1154 #ifdef HAVE_POLL
1155 	struct pollfd input_fd;
1156 	int			timeout_ms;
1157 
1158 	if (!forRead && !forWrite)
1159 		return 0;
1160 
1161 	input_fd.fd = sock;
1162 	input_fd.events = POLLERR;
1163 	input_fd.revents = 0;
1164 
1165 	if (forRead)
1166 		input_fd.events |= POLLIN;
1167 	if (forWrite)
1168 		input_fd.events |= POLLOUT;
1169 
1170 	/* Compute appropriate timeout interval */
1171 	if (end_time == ((time_t) -1))
1172 		timeout_ms = -1;
1173 	else
1174 	{
1175 		time_t		now = time(NULL);
1176 
1177 		if (end_time > now)
1178 			timeout_ms = (end_time - now) * 1000;
1179 		else
1180 			timeout_ms = 0;
1181 	}
1182 
1183 	return poll(&input_fd, 1, timeout_ms);
1184 #else							/* !HAVE_POLL */
1185 
1186 	fd_set		input_mask;
1187 	fd_set		output_mask;
1188 	fd_set		except_mask;
1189 	struct timeval timeout;
1190 	struct timeval *ptr_timeout;
1191 
1192 	if (!forRead && !forWrite)
1193 		return 0;
1194 
1195 	FD_ZERO(&input_mask);
1196 	FD_ZERO(&output_mask);
1197 	FD_ZERO(&except_mask);
1198 	if (forRead)
1199 		FD_SET(sock, &input_mask);
1200 
1201 	if (forWrite)
1202 		FD_SET(sock, &output_mask);
1203 	FD_SET(sock, &except_mask);
1204 
1205 	/* Compute appropriate timeout interval */
1206 	if (end_time == ((time_t) -1))
1207 		ptr_timeout = NULL;
1208 	else
1209 	{
1210 		time_t		now = time(NULL);
1211 
1212 		if (end_time > now)
1213 			timeout.tv_sec = end_time - now;
1214 		else
1215 			timeout.tv_sec = 0;
1216 		timeout.tv_usec = 0;
1217 		ptr_timeout = &timeout;
1218 	}
1219 
1220 	return select(sock + 1, &input_mask, &output_mask,
1221 				  &except_mask, ptr_timeout);
1222 #endif							/* HAVE_POLL */
1223 }
1224 
1225 
1226 /*
1227  * A couple of "miscellaneous" multibyte related functions. They used
1228  * to be in fe-print.c but that file is doomed.
1229  */
1230 
1231 /*
1232  * returns the byte length of the character beginning at s, using the
1233  * specified encoding.
1234  */
1235 int
PQmblen(const char * s,int encoding)1236 PQmblen(const char *s, int encoding)
1237 {
1238 	return pg_encoding_mblen(encoding, s);
1239 }
1240 
1241 /*
1242  * returns the display length of the character beginning at s, using the
1243  * specified encoding.
1244  */
1245 int
PQdsplen(const char * s,int encoding)1246 PQdsplen(const char *s, int encoding)
1247 {
1248 	return pg_encoding_dsplen(encoding, s);
1249 }
1250 
1251 /*
1252  * Get encoding id from environment variable PGCLIENTENCODING.
1253  */
1254 int
PQenv2encoding(void)1255 PQenv2encoding(void)
1256 {
1257 	char	   *str;
1258 	int			encoding = PG_SQL_ASCII;
1259 
1260 	str = getenv("PGCLIENTENCODING");
1261 	if (str && *str != '\0')
1262 	{
1263 		encoding = pg_char_to_encoding(str);
1264 		if (encoding < 0)
1265 			encoding = PG_SQL_ASCII;
1266 	}
1267 	return encoding;
1268 }
1269 
1270 
1271 #ifdef ENABLE_NLS
1272 
1273 static void
libpq_binddomain()1274 libpq_binddomain()
1275 {
1276 	static bool already_bound = false;
1277 
1278 	if (!already_bound)
1279 	{
1280 		/* bindtextdomain() does not preserve errno */
1281 #ifdef WIN32
1282 		int			save_errno = GetLastError();
1283 #else
1284 		int			save_errno = errno;
1285 #endif
1286 		const char *ldir;
1287 
1288 		already_bound = true;
1289 		/* No relocatable lookup here because the binary could be anywhere */
1290 		ldir = getenv("PGLOCALEDIR");
1291 		if (!ldir)
1292 			ldir = LOCALEDIR;
1293 		bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
1294 #ifdef WIN32
1295 		SetLastError(save_errno);
1296 #else
1297 		errno = save_errno;
1298 #endif
1299 	}
1300 }
1301 
1302 char *
libpq_gettext(const char * msgid)1303 libpq_gettext(const char *msgid)
1304 {
1305 	libpq_binddomain();
1306 	return dgettext(PG_TEXTDOMAIN("libpq"), msgid);
1307 }
1308 
1309 char *
libpq_ngettext(const char * msgid,const char * msgid_plural,unsigned long n)1310 libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)
1311 {
1312 	libpq_binddomain();
1313 	return dngettext(PG_TEXTDOMAIN("libpq"), msgid, msgid_plural, n);
1314 }
1315 
1316 #endif							/* ENABLE_NLS */
1317