fe-misc.c 20.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3 4
 *	 FILE
 *		fe-misc.c
5
 *
6 7
 *	 DESCRIPTION
 *		 miscellaneous useful functions
B
Bruce Momjian 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * The communication routines here are analogous to the ones in
 * backend/libpq/pqcomm.c and backend/libpq/pqcomprim.c, but operate
 * in the considerably different environment of the frontend libpq.
 * In particular, we work with a bare nonblock-mode socket, rather than
 * a stdio stream, so that we can avoid unwanted blocking of the application.
 *
 * XXX: MOVE DEBUG PRINTOUT TO HIGHER LEVEL.  As is, block and restart
 * will cause repeat printouts.
 *
 * We must speak the same transmitted data representations as the backend
 * routines.  Note that this module supports *only* network byte order
 * for transmitted ints, whereas the backend modules (as of this writing)
 * still handle either network or little-endian byte order.
22
 *
23
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
24
 * Portions Copyright (c) 1994, Regents of the University of California
25 26 27
 *
 *
 * IDENTIFICATION
T
Tom Lane 已提交
28
 *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.63 2001/11/27 18:21:51 tgl Exp $
29 30 31 32
 *
 *-------------------------------------------------------------------------
 */

33
#include "postgres_fe.h"
M
 
Marc G. Fournier 已提交
34

35 36 37 38
#include <errno.h>
#include <signal.h>
#include <time.h>

B
Hello!  
Bruce Momjian 已提交
39 40
#ifdef WIN32
#include "win32.h"
41 42
#else
#include <unistd.h>
B
Hi!  
Bruce Momjian 已提交
43
#include <sys/time.h>
B
Bruce Momjian 已提交
44
#endif
M
 
Marc G. Fournier 已提交
45

B
Bruce Momjian 已提交
46 47 48
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
49

50 51 52 53
#include "libpq-fe.h"
#include "libpq-int.h"
#include "pqsignal.h"

P
Peter Eisentraut 已提交
54 55 56 57
#ifdef MULTIBYTE
#include "mb/pg_wchar.h"
#endif

58

B
 
Bruce Momjian 已提交
59 60 61
#define DONOTICE(conn,message) \
	((*(conn)->noticeHook) ((conn)->noticeArg, (message)))

62
static int	pqPutBytes(const char *s, size_t nbytes, PGconn *conn);
63

B
 
Bruce Momjian 已提交
64

65 66
/*
 * pqGetc:
67
 *	get a character from the connection
68
 *
69 70 71
 *	All these routines return 0 on success, EOF on error.
 *	Note that for the Get routines, EOF only means there is not enough
 *	data in the buffer, not that there is necessarily a hard error.
72
 */
73
int
B
Bruce Momjian 已提交
74
pqGetc(char *result, PGconn *conn)
75
{
B
Bruce Momjian 已提交
76 77
	if (conn->inCursor >= conn->inEnd)
		return EOF;
78

B
Bruce Momjian 已提交
79
	*result = conn->inBuffer[conn->inCursor++];
80

B
Bruce Momjian 已提交
81 82
	if (conn->Pfdebug)
		fprintf(conn->Pfdebug, "From backend> %c\n", *result);
83

B
Bruce Momjian 已提交
84
	return 0;
85 86
}

B
Bruce Momjian 已提交
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/*
 * write 1 char to the connection
 */
int
pqPutc(char c, PGconn *conn)
{
	if (pqPutBytes(&c, 1, conn) == EOF)
		return EOF;

	if (conn->Pfdebug)
		fprintf(conn->Pfdebug, "To backend> %c\n", c);

	return 0;
}


104 105 106
/*
 * pqPutBytes: local routine to write N bytes to the connection,
 * with buffering
B
Bruce Momjian 已提交
107 108
 */
static int
B
Bruce Momjian 已提交
109
pqPutBytes(const char *s, size_t nbytes, PGconn *conn)
B
Bruce Momjian 已提交
110
{
111
	size_t		avail = Max(conn->outBufSize - conn->outCount, 0);
B
Bruce Momjian 已提交
112

113
	/*
114 115
	 * if we are non-blocking and the send queue is too full to buffer
	 * this request then try to flush some and return an error
116 117 118
	 */
	if (pqIsnonblocking(conn) && nbytes > avail && pqFlush(conn))
	{
119 120 121 122 123 124 125
		/*
		 * even if the flush failed we may still have written some data,
		 * recalculate the size of the send-queue relative to the amount
		 * we have to send, we may be able to queue it afterall even
		 * though it's not sent to the database it's ok, any routines that
		 * check the data coming from the database better call pqFlush()
		 * anyway.
126 127 128 129
		 */
		if (nbytes > Max(conn->outBufSize - conn->outCount, 0))
		{
			printfPQExpBuffer(&conn->errorMessage,
130
							  libpq_gettext("could not flush enough data (space available: %d, space needed %d)\n"),
131
						 (int) Max(conn->outBufSize - conn->outCount, 0),
132
							  (int) nbytes);
133 134
			return EOF;
		}
135 136
		/* fixup avail for while loop */
		avail = Max(conn->outBufSize - conn->outCount, 0);
137 138
	}

139
	/*
140 141 142
	 * is the amount of data to be sent is larger than the size of the
	 * output buffer then we must flush it to make more room.
	 *
143 144
	 * the code above will make sure the loop conditional is never true for
	 * non-blocking connections
145
	 */
B
Bruce Momjian 已提交
146 147 148 149 150 151 152 153 154 155
	while (nbytes > avail)
	{
		memcpy(conn->outBuffer + conn->outCount, s, avail);
		conn->outCount += avail;
		s += avail;
		nbytes -= avail;
		if (pqFlush(conn))
			return EOF;
		avail = conn->outBufSize;
	}
156

B
Bruce Momjian 已提交
157 158 159 160 161 162
	memcpy(conn->outBuffer + conn->outCount, s, nbytes);
	conn->outCount += nbytes;

	return 0;
}

163 164 165 166 167 168 169
/*
 * pqGets:
 * get a null-terminated string from the connection,
 * and store it in an expansible PQExpBuffer.
 * If we run out of memory, all of the string is still read,
 * but the excess characters are silently discarded.
 */
170
int
171
pqGets(PQExpBuffer buf, PGconn *conn)
B
Bruce Momjian 已提交
172 173
{
	/* Copy conn data to locals for faster search loop */
174 175 176 177
	char	   *inBuffer = conn->inBuffer;
	int			inCursor = conn->inCursor;
	int			inEnd = conn->inEnd;
	int			slen;
B
Bruce Momjian 已提交
178 179 180 181 182 183 184 185

	while (inCursor < inEnd && inBuffer[inCursor])
		inCursor++;

	if (inCursor >= inEnd)
		return EOF;

	slen = inCursor - conn->inCursor;
186 187 188

	resetPQExpBuffer(buf);
	appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
B
Bruce Momjian 已提交
189 190 191 192

	conn->inCursor = ++inCursor;

	if (conn->Pfdebug)
193 194
		fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
				buf->data);
B
Bruce Momjian 已提交
195 196 197 198

	return 0;
}

199

B
Bruce Momjian 已提交
200 201
int
pqPuts(const char *s, PGconn *conn)
202
{
203
	if (pqPutBytes(s, strlen(s) + 1, conn))
B
Bruce Momjian 已提交
204 205 206 207
		return EOF;

	if (conn->Pfdebug)
		fprintf(conn->Pfdebug, "To backend> %s\n", s);
208

B
Bruce Momjian 已提交
209
	return 0;
210 211
}

212 213
/*
 * pqGetnchar:
214
 *	get a string of exactly len bytes in buffer s, no null termination
215
 */
216
int
B
Bruce Momjian 已提交
217
pqGetnchar(char *s, size_t len, PGconn *conn)
218
{
B
Bruce Momjian 已提交
219 220 221 222
	if (len < 0 || len > conn->inEnd - conn->inCursor)
		return EOF;

	memcpy(s, conn->inBuffer + conn->inCursor, len);
223
	/* no terminating null */
224

B
Bruce Momjian 已提交
225
	conn->inCursor += len;
226

B
Bruce Momjian 已提交
227
	if (conn->Pfdebug)
B
Bruce Momjian 已提交
228
		fprintf(conn->Pfdebug, "From backend (%lu)> %.*s\n", (unsigned long) len, (int) len, s);
229

B
Bruce Momjian 已提交
230
	return 0;
231 232
}

233 234
/*
 * pqPutnchar:
235
 *	send a string of exactly len bytes, no null termination needed
236
 */
237
int
B
Bruce Momjian 已提交
238
pqPutnchar(const char *s, size_t len, PGconn *conn)
239
{
B
Bruce Momjian 已提交
240 241
	if (pqPutBytes(s, len, conn))
		return EOF;
242

B
Bruce Momjian 已提交
243
	if (conn->Pfdebug)
244
		fprintf(conn->Pfdebug, "To backend> %.*s\n", (int) len, s);
245

B
Bruce Momjian 已提交
246
	return 0;
247 248
}

249 250
/*
 * pgGetInt
251 252
 *	read a 2 or 4 byte integer and convert from network byte order
 *	to local byte order
253
 */
254
int
B
Bruce Momjian 已提交
255
pqGetInt(int *result, size_t bytes, PGconn *conn)
256
{
257 258
	uint16		tmp2;
	uint32		tmp4;
259
	char		noticeBuf[64];
260 261 262

	switch (bytes)
	{
263
		case 2:
B
Bruce Momjian 已提交
264 265 266 267 268
			if (conn->inCursor + 2 > conn->inEnd)
				return EOF;
			memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
			conn->inCursor += 2;
			*result = (int) ntohs(tmp2);
269 270
			break;
		case 4:
B
Bruce Momjian 已提交
271 272 273 274 275
			if (conn->inCursor + 4 > conn->inEnd)
				return EOF;
			memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
			conn->inCursor += 4;
			*result = (int) ntohl(tmp4);
276 277
			break;
		default:
278 279 280
			snprintf(noticeBuf, sizeof(noticeBuf),
					 libpq_gettext("integer of size %lu not supported by pqGetInt\n"),
					 (unsigned long) bytes);
281
			DONOTICE(conn, noticeBuf);
B
Bruce Momjian 已提交
282
			return EOF;
283 284
	}

B
Bruce Momjian 已提交
285
	if (conn->Pfdebug)
B
Bruce Momjian 已提交
286
		fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long) bytes, *result);
287

B
Bruce Momjian 已提交
288
	return 0;
289 290
}

291 292 293 294 295
/*
 * pgPutInt
 * send an integer of 2 or 4 bytes, converting from host byte order
 * to network byte order.
 */
296
int
B
Bruce Momjian 已提交
297
pqPutInt(int value, size_t bytes, PGconn *conn)
298
{
299 300
	uint16		tmp2;
	uint32		tmp4;
301
	char		noticeBuf[64];
302 303 304

	switch (bytes)
	{
305
		case 2:
B
Bruce Momjian 已提交
306
			tmp2 = htons((uint16) value);
307
			if (pqPutBytes((const char *) &tmp2, 2, conn))
B
Bruce Momjian 已提交
308
				return EOF;
309 310
			break;
		case 4:
B
Bruce Momjian 已提交
311
			tmp4 = htonl((uint32) value);
312
			if (pqPutBytes((const char *) &tmp4, 4, conn))
B
Bruce Momjian 已提交
313
				return EOF;
314 315
			break;
		default:
316 317 318
			snprintf(noticeBuf, sizeof(noticeBuf),
					 libpq_gettext("integer of size %lu not supported by pqPutInt\n"),
					 (unsigned long) bytes);
319
			DONOTICE(conn, noticeBuf);
B
Bruce Momjian 已提交
320
			return EOF;
321 322
	}

B
Bruce Momjian 已提交
323
	if (conn->Pfdebug)
B
Bruce Momjian 已提交
324
		fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long) bytes, value);
325

B
Bruce Momjian 已提交
326
	return 0;
327
}
328

329 330
/*
 * pqReadReady: is select() saying the file is ready to read?
331
 * Returns -1 on failure, 0 if not ready, 1 if ready.
B
Bruce Momjian 已提交
332
 */
333
int
B
Bruce Momjian 已提交
334 335
pqReadReady(PGconn *conn)
{
336 337
	fd_set		input_mask;
	struct timeval timeout;
B
Bruce Momjian 已提交
338

339 340
	if (!conn || conn->sock < 0)
		return -1;
B
Bruce Momjian 已提交
341

342
retry:
B
Bruce Momjian 已提交
343 344 345 346
	FD_ZERO(&input_mask);
	FD_SET(conn->sock, &input_mask);
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
347
	if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL,
B
Bruce Momjian 已提交
348 349
			   &timeout) < 0)
	{
350
		if (SOCK_ERRNO == EINTR)
351 352 353
			/* Interrupted system call - we'll just try again */
			goto retry;

354
		printfPQExpBuffer(&conn->errorMessage,
355
						  libpq_gettext("select() failed: %s\n"),
356
						  SOCK_STRERROR(SOCK_ERRNO));
357 358 359 360 361 362
		return -1;
	}

	return FD_ISSET(conn->sock, &input_mask) ? 1 : 0;
}

363 364
/*
 * pqWriteReady: is select() saying the file is ready to write?
365 366 367 368 369 370 371 372 373 374 375
 * Returns -1 on failure, 0 if not ready, 1 if ready.
 */
int
pqWriteReady(PGconn *conn)
{
	fd_set		input_mask;
	struct timeval timeout;

	if (!conn || conn->sock < 0)
		return -1;

376
retry:
377 378 379 380 381 382 383
	FD_ZERO(&input_mask);
	FD_SET(conn->sock, &input_mask);
	timeout.tv_sec = 0;
	timeout.tv_usec = 0;
	if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL,
			   &timeout) < 0)
	{
384
		if (SOCK_ERRNO == EINTR)
385 386 387 388
			/* Interrupted system call - we'll just try again */
			goto retry;

		printfPQExpBuffer(&conn->errorMessage,
389
						  libpq_gettext("select() failed: %s\n"),
390
						  SOCK_STRERROR(SOCK_ERRNO));
391
		return -1;
B
Bruce Momjian 已提交
392
	}
393
	return FD_ISSET(conn->sock, &input_mask) ? 1 : 0;
B
Bruce Momjian 已提交
394 395
}

396 397
/* ----------
 * pqReadData: read more data, if any is available
B
Bruce Momjian 已提交
398
 * Possible return values:
399 400 401 402
 *	 1: successfully loaded at least one more byte
 *	 0: no data is presently available, but no error detected
 *	-1: error detected (including EOF = connection closure);
 *		conn->errorMessage set
B
Bruce Momjian 已提交
403 404
 * NOTE: callers must not assume that pointers or indexes into conn->inBuffer
 * remain valid across this call!
405
 * ----------
B
Bruce Momjian 已提交
406
 */
407
int
B
Bruce Momjian 已提交
408
pqReadData(PGconn *conn)
409
{
410
	int			someread = 0;
411
	int			nread;
B
Bruce Momjian 已提交
412 413 414

	if (conn->sock < 0)
	{
415
		printfPQExpBuffer(&conn->errorMessage,
416
						  libpq_gettext("connection not open\n"));
B
Bruce Momjian 已提交
417 418
		return -1;
	}
419

B
Bruce Momjian 已提交
420 421 422
	/* Left-justify any data in the buffer to make room */
	if (conn->inStart < conn->inEnd)
	{
423 424 425 426 427 428 429 430
		if (conn->inStart > 0)
		{
			memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
					conn->inEnd - conn->inStart);
			conn->inEnd -= conn->inStart;
			conn->inCursor -= conn->inStart;
			conn->inStart = 0;
		}
B
Bruce Momjian 已提交
431 432
	}
	else
433 434
	{
		/* buffer is logically empty, reset it */
B
Bruce Momjian 已提交
435
		conn->inStart = conn->inCursor = conn->inEnd = 0;
436
	}
437 438 439 440 441 442

	/*
	 * If the buffer is fairly full, enlarge it. We need to be able to
	 * enlarge the buffer in case a single message exceeds the initial
	 * buffer size.  We enlarge before filling the buffer entirely so as
	 * to avoid asking the kernel for a partial packet. The magic constant
443
	 * here should be large enough for a TCP packet or Unix pipe
444
	 * bufferload.	8K is the usual pipe buffer size, so...
B
Bruce Momjian 已提交
445
	 */
446
	if (conn->inBufSize - conn->inEnd < 8192)
B
Bruce Momjian 已提交
447
	{
448 449 450
		int			newSize = conn->inBufSize * 2;
		char	   *newBuf = (char *) realloc(conn->inBuffer, newSize);

B
Bruce Momjian 已提交
451 452 453 454 455 456
		if (newBuf)
		{
			conn->inBuffer = newBuf;
			conn->inBufSize = newSize;
		}
	}
457

B
Bruce Momjian 已提交
458 459
	/* OK, try to read some data */
tryAgain:
460
#ifdef USE_SSL
461 462 463
	if (conn->ssl)
		nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd,
						 conn->inBufSize - conn->inEnd);
464 465
	else
#endif
466 467
		nread = recv(conn->sock, conn->inBuffer + conn->inEnd,
					 conn->inBufSize - conn->inEnd, 0);
B
Bruce Momjian 已提交
468 469
	if (nread < 0)
	{
470
		if (SOCK_ERRNO == EINTR)
B
Bruce Momjian 已提交
471
			goto tryAgain;
472 473
		/* Some systems return EAGAIN/EWOULDBLOCK for no data */
#ifdef EAGAIN
474
		if (SOCK_ERRNO == EAGAIN)
475
			return someread;
476 477
#endif
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
478
		if (SOCK_ERRNO == EWOULDBLOCK)
479
			return someread;
B
Bruce Momjian 已提交
480 481 482
#endif
		/* We might get ECONNRESET here if using TCP and backend died */
#ifdef ECONNRESET
483
		if (SOCK_ERRNO == ECONNRESET)
B
Bruce Momjian 已提交
484
			goto definitelyFailed;
485
#endif
486
		printfPQExpBuffer(&conn->errorMessage,
487
			   libpq_gettext("could not receive data from server: %s\n"),
488
						  SOCK_STRERROR(SOCK_ERRNO));
B
Bruce Momjian 已提交
489 490 491 492 493
		return -1;
	}
	if (nread > 0)
	{
		conn->inEnd += nread;
494

495 496
		/*
		 * Hack to deal with the fact that some kernels will only give us
497 498 499 500 501 502
		 * back 1 packet per recv() call, even if we asked for more and
		 * there is more available.  If it looks like we are reading a
		 * long message, loop back to recv() again immediately, until we
		 * run out of data or buffer space.  Without this, the
		 * block-and-restart behavior of libpq's higher levels leads to
		 * O(N^2) performance on long messages.
503 504
		 *
		 * Since we left-justified the data above, conn->inEnd gives the
505 506
		 * amount of data already read in the current message.	We
		 * consider the message "long" once we have acquired 32k ...
507 508 509 510 511 512 513
		 */
		if (conn->inEnd > 32768 &&
			(conn->inBufSize - conn->inEnd) >= 8192)
		{
			someread = 1;
			goto tryAgain;
		}
B
Bruce Momjian 已提交
514 515
		return 1;
	}
516

517 518 519
	if (someread)
		return 1;				/* got a zero read after successful tries */

520 521 522 523 524 525 526 527
	/*
	 * A return value of 0 could mean just that no data is now available,
	 * or it could mean EOF --- that is, the server has closed the
	 * connection. Since we have the socket in nonblock mode, the only way
	 * to tell the difference is to see if select() is saying that the
	 * file is ready. Grumble.	Fortunately, we don't expect this path to
	 * be taken much, since in normal practice we should not be trying to
	 * read data unless the file selected for reading already.
B
Bruce Momjian 已提交
528
	 */
529 530 531 532 533 534 535 536 537 538 539
	switch (pqReadReady(conn))
	{
		case 0:
			/* definitely no data available */
			return 0;
		case 1:
			/* ready for read */
			break;
		default:
			goto definitelyFailed;
	}
B
Bruce Momjian 已提交
540

541 542 543
	/*
	 * Still not sure that it's EOF, because some data could have just
	 * arrived.
B
Bruce Momjian 已提交
544 545
	 */
tryAgain2:
546
#ifdef USE_SSL
547 548 549
	if (conn->ssl)
		nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd,
						 conn->inBufSize - conn->inEnd);
550 551
	else
#endif
552 553
		nread = recv(conn->sock, conn->inBuffer + conn->inEnd,
					 conn->inBufSize - conn->inEnd, 0);
B
Bruce Momjian 已提交
554 555
	if (nread < 0)
	{
556
		if (SOCK_ERRNO == EINTR)
B
Bruce Momjian 已提交
557
			goto tryAgain2;
558 559
		/* Some systems return EAGAIN/EWOULDBLOCK for no data */
#ifdef EAGAIN
560
		if (SOCK_ERRNO == EAGAIN)
561 562 563
			return 0;
#endif
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
564
		if (SOCK_ERRNO == EWOULDBLOCK)
565
			return 0;
B
Bruce Momjian 已提交
566 567 568
#endif
		/* We might get ECONNRESET here if using TCP and backend died */
#ifdef ECONNRESET
569
		if (SOCK_ERRNO == ECONNRESET)
B
Bruce Momjian 已提交
570
			goto definitelyFailed;
571
#endif
572
		printfPQExpBuffer(&conn->errorMessage,
573
			   libpq_gettext("could not receive data from server: %s\n"),
574
						  SOCK_STRERROR(SOCK_ERRNO));
B
Bruce Momjian 已提交
575 576 577 578 579 580 581 582
		return -1;
	}
	if (nread > 0)
	{
		conn->inEnd += nread;
		return 1;
	}

583 584
	/*
	 * OK, we are getting a zero read even though select() says ready.
B
Bruce Momjian 已提交
585 586
	 * This means the connection has been closed.  Cope.
	 */
B
Bruce Momjian 已提交
587
definitelyFailed:
588
	printfPQExpBuffer(&conn->errorMessage,
589
					  libpq_gettext(
590 591 592
							"server closed the connection unexpectedly\n"
			   "\tThis probably means the server terminated abnormally\n"
						 "\tbefore or while processing the request.\n"));
593
	conn->status = CONNECTION_BAD;		/* No more connection to backend */
B
Hello!  
Bruce Momjian 已提交
594 595 596
#ifdef WIN32
	closesocket(conn->sock);
#else
B
Bruce Momjian 已提交
597
	close(conn->sock);
B
Hello!  
Bruce Momjian 已提交
598
#endif
B
Bruce Momjian 已提交
599
	conn->sock = -1;
600

B
Bruce Momjian 已提交
601
	return -1;
602 603
}

604 605
/*
 * pqFlush: send any data waiting in the output buffer
B
Bruce Momjian 已提交
606 607 608
 */
int
pqFlush(PGconn *conn)
609
{
610 611
	char	   *ptr = conn->outBuffer;
	int			len = conn->outCount;
B
Bruce Momjian 已提交
612 613 614

	if (conn->sock < 0)
	{
615
		printfPQExpBuffer(&conn->errorMessage,
616
						  libpq_gettext("connection not open\n"));
B
Bruce Momjian 已提交
617 618
		return EOF;
	}
619

620 621 622
	/*
	 * don't try to send zero data, allows us to use this function without
	 * too much worry about overhead
623 624 625 626 627
	 */
	if (len == 0)
		return (0);

	/* while there's still data to send */
B
Bruce Momjian 已提交
628 629
	while (len > 0)
	{
M
 
Marc G. Fournier 已提交
630 631
		/* Prevent being SIGPIPEd if backend has closed the connection. */
#ifndef WIN32
632
		pqsigfunc	oldsighandler = pqsignal(SIGPIPE, SIG_IGN);
M
 
Marc G. Fournier 已提交
633 634
#endif

635
		int			sent;
636

637
#ifdef USE_SSL
638 639
		if (conn->ssl)
			sent = SSL_write(conn->ssl, ptr, len);
640 641
		else
#endif
642
			sent = send(conn->sock, ptr, len, 0);
M
 
Marc G. Fournier 已提交
643 644 645 646 647

#ifndef WIN32
		pqsignal(SIGPIPE, oldsighandler);
#endif

B
Bruce Momjian 已提交
648 649
		if (sent < 0)
		{
B
Bruce Momjian 已提交
650
			/*
B
Bruce Momjian 已提交
651 652 653
			 * Anything except EAGAIN or EWOULDBLOCK is trouble. If it's
			 * EPIPE or ECONNRESET, assume we've lost the backend
			 * connection permanently.
B
Bruce Momjian 已提交
654
			 */
655
			switch (SOCK_ERRNO)
B
Bruce Momjian 已提交
656 657 658 659 660 661 662 663 664
			{
#ifdef EAGAIN
				case EAGAIN:
					break;
#endif
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
				case EWOULDBLOCK:
					break;
#endif
665 666
				case EINTR:
					continue;
667

B
Bruce Momjian 已提交
668 669 670 671
				case EPIPE:
#ifdef ECONNRESET
				case ECONNRESET:
#endif
672
					printfPQExpBuffer(&conn->errorMessage,
673
									  libpq_gettext(
674 675 676
							"server closed the connection unexpectedly\n"
													"\tThis probably means the server terminated abnormally\n"
						 "\tbefore or while processing the request.\n"));
677

678 679 680
					/*
					 * We used to close the socket here, but that's a bad
					 * idea since there might be unread data waiting
681 682 683 684
					 * (typically, a NOTICE message from the backend
					 * telling us it's committing hara-kiri...).  Leave
					 * the socket open until pqReadData finds no more data
					 * can be read.
685
					 */
B
Bruce Momjian 已提交
686
					return EOF;
687

B
Bruce Momjian 已提交
688
				default:
689
					printfPQExpBuffer(&conn->errorMessage,
690
					libpq_gettext("could not send data to server: %s\n"),
691
									  SOCK_STRERROR(SOCK_ERRNO));
B
Bruce Momjian 已提交
692
					/* We don't assume it's a fatal error... */
B
Bruce Momjian 已提交
693 694 695 696 697 698 699 700
					return EOF;
			}
		}
		else
		{
			ptr += sent;
			len -= sent;
		}
701

B
Bruce Momjian 已提交
702 703 704
		if (len > 0)
		{
			/* We didn't send it all, wait till we can send more */
705

706 707 708
			/*
			 * if the socket is in non-blocking mode we may need to abort
			 * here
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
			 */
#ifdef USE_SSL
			/* can't do anything for our SSL users yet */
			if (conn->ssl == NULL)
			{
#endif
				if (pqIsnonblocking(conn))
				{
					/* shift the contents of the buffer */
					memmove(conn->outBuffer, ptr, len);
					conn->outCount = len;
					return EOF;
				}
#ifdef USE_SSL
			}
#endif

B
Bruce Momjian 已提交
726 727 728 729 730 731 732 733 734 735 736
			if (pqWait(FALSE, TRUE, conn))
				return EOF;
		}
	}

	conn->outCount = 0;

	if (conn->Pfdebug)
		fflush(conn->Pfdebug);

	return 0;
737
}
738

739 740
/*
 * pqWait: wait until we can read or write the connection socket
741 742 743 744
 *
 * We also stop waiting and return if the kernel flags an exception condition
 * on the socket.  The actual error condition will be detected and reported
 * when the caller tries to read or write the socket.
B
Bruce Momjian 已提交
745 746 747 748
 */
int
pqWait(int forRead, int forWrite, PGconn *conn)
{
749 750
	fd_set		input_mask;
	fd_set		output_mask;
751
	fd_set		except_mask;
B
Bruce Momjian 已提交
752 753 754

	if (conn->sock < 0)
	{
755
		printfPQExpBuffer(&conn->errorMessage,
756
						  libpq_gettext("connection not open\n"));
B
Bruce Momjian 已提交
757 758 759
		return EOF;
	}

760
	if (forRead || forWrite)
761
	{
762
retry:
B
Bruce Momjian 已提交
763 764
		FD_ZERO(&input_mask);
		FD_ZERO(&output_mask);
765
		FD_ZERO(&except_mask);
B
Bruce Momjian 已提交
766 767 768 769
		if (forRead)
			FD_SET(conn->sock, &input_mask);
		if (forWrite)
			FD_SET(conn->sock, &output_mask);
770 771
		FD_SET(conn->sock, &except_mask);
		if (select(conn->sock + 1, &input_mask, &output_mask, &except_mask,
B
Bruce Momjian 已提交
772 773
				   (struct timeval *) NULL) < 0)
		{
774
			if (SOCK_ERRNO == EINTR)
775
				goto retry;
776
			printfPQExpBuffer(&conn->errorMessage,
777
							  libpq_gettext("select() failed: %s\n"),
778
							  SOCK_STRERROR(SOCK_ERRNO));
B
Bruce Momjian 已提交
779 780 781 782 783 784
			return EOF;
		}
	}

	return 0;
}
P
Peter Eisentraut 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810



/*
 * A couple of "miscellaneous" multibyte related functions. They used
 * to be in fe-print.c but that file is doomed.
 */

#ifdef MULTIBYTE
/*
 * returns the byte length of the word beginning s, using the
 * specified encoding.
 */
int
PQmblen(const unsigned char *s, int encoding)
{
	return (pg_encoding_mblen(encoding, s));
}

/*
 * Get encoding id from environment variable PGCLIENTENCODING.
 */
int
PQenv2encoding(void)
{
	char	   *str;
T
Tatsuo Ishii 已提交
811
	int			encoding = PG_SQL_ASCII;
P
Peter Eisentraut 已提交
812 813 814 815

	str = getenv("PGCLIENTENCODING");
	if (str && *str != '\0')
		encoding = pg_char_to_encoding(str);
816
	return (encoding);
P
Peter Eisentraut 已提交
817 818 819 820 821 822 823 824
}

#else

/* Provide a default definition in case someone calls it anyway */
int
PQmblen(const unsigned char *s, int encoding)
{
825 826
	(void) s;
	(void) encoding;
P
Peter Eisentraut 已提交
827 828 829 830 831 832 833
	return 1;
}
int
PQenv2encoding(void)
{
	return 0;
}
834
#endif   /* MULTIBYTE */
835 836 837 838 839 840


#ifdef ENABLE_NLS
char *
libpq_gettext(const char *msgid)
{
841
	static int	already_bound = 0;
842 843 844 845 846 847 848 849 850

	if (!already_bound)
	{
		already_bound = 1;
		bindtextdomain("libpq", LOCALEDIR);
	}

	return dgettext("libpq", msgid);
}
851
#endif   /* ENABLE_NLS */
852 853 854 855

#ifdef WIN32
/*
 * strerror replacement for windows:
856
 *
T
Tom Lane 已提交
857
 * We don't know a fix for win9x yet, but this should work for nt4 and win2k.
858
 * If you can verify this working on win9x or have a solution, let us know, ok?
859
 */
860
const char *
861 862
winsock_strerror(DWORD eno)
{
863 864 865 866
#define WSSE_MAXLEN (sizeof(winsock_strerror_buf)-1-12) /* 12 == "(0x00000000)" */
	int			length;

	/* First try the "system table", this works on Win2k pro */
867

868 869 870 871 872 873
	if (FormatMessage(
			  FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
					  0, eno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					  winsock_strerror_buf, WSSE_MAXLEN, NULL
					  ))
		goto WSSE_GOODEXIT;
874

875
	/* That didn't work, let's try the netmsg.dll */
876

877 878 879 880 881 882 883
	if (netmsgModule &&
		FormatMessage(
			 FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE,
					  0, eno, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					  winsock_strerror_buf, WSSE_MAXLEN, NULL
					  ))
		goto WSSE_GOODEXIT;
884

885
	/* Everything failed, just tell the user that we don't know the desc */
886

T
Tom Lane 已提交
887
	strcpy(winsock_strerror_buf, "Socket error, no description available.");
888 889 890

WSSE_GOODEXIT:

891 892 893
	length = strlen(winsock_strerror_buf);
	sprintf(winsock_strerror_buf + (length < WSSE_MAXLEN ? length : WSSE_MAXLEN),
			"(0x%08X)", eno);
894

895
	return winsock_strerror_buf;
896
}
897

898
#endif