1 /*-------------------------------------------------------------------------
7 * miscellaneous useful functions
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.
15 * XXX: MOVE DEBUG PRINTOUT TO HIGHER LEVEL. As is, block and restart
16 * will cause repeat printouts.
18 * We must speak the same transmitted data representations as the backend
19 * routines. Note that this module supports *only* network byte order
20 * for transmitted ints, whereas the backend modules (as of this writing)
21 * still handle either network or little-endian byte order.
23 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
24 * Portions Copyright (c) 1994, Regents of the University of California
28 * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.42 2000/11/16 05:51:05 momjian Exp $
30 *-------------------------------------------------------------------------
46 #ifdef HAVE_SYS_SELECT_H
47 #include <sys/select.h>
51 #include "libpq-int.h"
55 #include "miscadmin.h"
56 #include "mb/pg_wchar.h"
60 #define DONOTICE(conn,message) \
61 ((*(conn)->noticeHook) ((conn)->noticeArg, (message)))
64 /* --------------------------------------------------------------------- */
66 get a character from the connection
68 All these routines return 0 on success, EOF on error.
69 Note that for the Get routines, EOF only means there is not enough
70 data in the buffer, not that there is necessarily a hard error.
73 pqGetc(char *result, PGconn *conn)
75 if (conn->inCursor >= conn->inEnd)
78 *result = conn->inBuffer[conn->inCursor++];
81 fprintf(conn->Pfdebug, "From backend> %c\n", *result);
87 /* --------------------------------------------------------------------- */
88 /* pqPutBytes: local routine to write N bytes to the connection,
92 pqPutBytes(const char *s, size_t nbytes, PGconn *conn)
94 size_t avail = Max(conn->outBufSize - conn->outCount, 0);
97 * if we are non-blocking and the send queue is too full to buffer
98 * this request then try to flush some and return an error
100 if (pqIsnonblocking(conn) && nbytes > avail && pqFlush(conn))
104 * even if the flush failed we may still have written some data,
105 * recalculate the size of the send-queue relative to the amount
106 * we have to send, we may be able to queue it afterall even
107 * though it's not sent to the database it's ok, any routines that
108 * check the data coming from the database better call pqFlush()
111 if (nbytes > Max(conn->outBufSize - conn->outCount, 0))
113 printfPQExpBuffer(&conn->errorMessage,
114 "pqPutBytes -- pqFlush couldn't flush enough"
115 " data: space available: %d, space needed %d\n",
116 Max(conn->outBufSize - conn->outCount, 0), nbytes);
119 /* fixup avail for while loop */
120 avail = Max(conn->outBufSize - conn->outCount, 0);
124 * is the amount of data to be sent is larger than the size of the
125 * output buffer then we must flush it to make more room.
127 * the code above will make sure the loop conditional is never true for
128 * non-blocking connections
130 while (nbytes > avail)
132 memcpy(conn->outBuffer + conn->outCount, s, avail);
133 conn->outCount += avail;
138 avail = conn->outBufSize;
141 memcpy(conn->outBuffer + conn->outCount, s, nbytes);
142 conn->outCount += nbytes;
147 /* --------------------------------------------------------------------- */
149 get a null-terminated string from the connection,
150 and store it in an expansible PQExpBuffer.
151 If we run out of memory, all of the string is still read,
152 but the excess characters are silently discarded.
155 pqGets(PQExpBuffer buf, PGconn *conn)
157 /* Copy conn data to locals for faster search loop */
158 char *inBuffer = conn->inBuffer;
159 int inCursor = conn->inCursor;
160 int inEnd = conn->inEnd;
163 while (inCursor < inEnd && inBuffer[inCursor])
166 if (inCursor >= inEnd)
169 slen = inCursor - conn->inCursor;
171 resetPQExpBuffer(buf);
172 appendBinaryPQExpBuffer(buf, inBuffer + conn->inCursor, slen);
174 conn->inCursor = ++inCursor;
177 fprintf(conn->Pfdebug, "From backend> \"%s\"\n",
183 /* --------------------------------------------------------------------- */
185 pqPuts(const char *s, PGconn *conn)
187 if (pqPutBytes(s, strlen(s) + 1, conn))
191 fprintf(conn->Pfdebug, "To backend> %s\n", s);
196 /* --------------------------------------------------------------------- */
198 get a string of exactly len bytes in buffer s, no null termination
201 pqGetnchar(char *s, size_t len, PGconn *conn)
203 if (len < 0 || len > conn->inEnd - conn->inCursor)
206 memcpy(s, conn->inBuffer + conn->inCursor, len);
207 /* no terminating null */
209 conn->inCursor += len;
212 fprintf(conn->Pfdebug, "From backend (%lu)> %.*s\n", (unsigned long)len, (int) len, s);
217 /* --------------------------------------------------------------------- */
219 send a string of exactly len bytes, no null termination needed
222 pqPutnchar(const char *s, size_t len, PGconn *conn)
224 if (pqPutBytes(s, len, conn))
228 fprintf(conn->Pfdebug, "To backend> %.*s\n", (int) len, s);
233 /* --------------------------------------------------------------------- */
235 read a 2 or 4 byte integer and convert from network byte order
239 pqGetInt(int *result, size_t bytes, PGconn *conn)
248 if (conn->inCursor + 2 > conn->inEnd)
250 memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
252 *result = (int) ntohs(tmp2);
255 if (conn->inCursor + 4 > conn->inEnd)
257 memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
259 *result = (int) ntohl(tmp4);
263 "pqGetInt: int size %lu not supported\n", (unsigned long)bytes);
264 DONOTICE(conn, noticeBuf);
269 fprintf(conn->Pfdebug, "From backend (#%lu)> %d\n", (unsigned long)bytes, *result);
274 /* --------------------------------------------------------------------- */
276 send an integer of 2 or 4 bytes, converting from host byte order
277 to network byte order.
280 pqPutInt(int value, size_t bytes, PGconn *conn)
289 tmp2 = htons((uint16) value);
290 if (pqPutBytes((const char *) &tmp2, 2, conn))
294 tmp4 = htonl((uint32) value);
295 if (pqPutBytes((const char *) &tmp4, 4, conn))
300 "pqPutInt: int size %lu not supported\n", (unsigned long)bytes);
301 DONOTICE(conn, noticeBuf);
306 fprintf(conn->Pfdebug, "To backend (%lu#)> %d\n", (unsigned long)bytes, value);
311 /* --------------------------------------------------------------------- */
312 /* pqReadReady: is select() saying the file is ready to read?
313 * Returns -1 on failure, 0 if not ready, 1 if ready.
316 pqReadReady(PGconn *conn)
319 struct timeval timeout;
321 if (!conn || conn->sock < 0)
324 FD_ZERO(&input_mask);
325 FD_SET(conn->sock, &input_mask);
329 if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL,
333 /* Interrupted system call - we'll just try again */
336 printfPQExpBuffer(&conn->errorMessage,
337 "pqReadReady() -- select() failed: errno=%d\n%s\n",
338 errno, strerror(errno));
342 return FD_ISSET(conn->sock, &input_mask) ? 1 : 0;
345 /* --------------------------------------------------------------------- */
346 /* pqWriteReady: is select() saying the file is ready to write?
347 * Returns -1 on failure, 0 if not ready, 1 if ready.
350 pqWriteReady(PGconn *conn)
353 struct timeval timeout;
355 if (!conn || conn->sock < 0)
358 FD_ZERO(&input_mask);
359 FD_SET(conn->sock, &input_mask);
363 if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL,
367 /* Interrupted system call - we'll just try again */
370 printfPQExpBuffer(&conn->errorMessage,
371 "pqWriteReady() -- select() failed: errno=%d\n%s\n",
372 errno, strerror(errno));
375 return FD_ISSET(conn->sock, &input_mask) ? 1 : 0;
378 /* --------------------------------------------------------------------- */
379 /* pqReadData: read more data, if any is available
380 * Possible return values:
381 * 1: successfully loaded at least one more byte
382 * 0: no data is presently available, but no error detected
383 * -1: error detected (including EOF = connection closure);
384 * conn->errorMessage set
385 * NOTE: callers must not assume that pointers or indexes into conn->inBuffer
386 * remain valid across this call!
389 pqReadData(PGconn *conn)
396 printfPQExpBuffer(&conn->errorMessage,
397 "pqReadData() -- connection not open\n");
401 /* Left-justify any data in the buffer to make room */
402 if (conn->inStart < conn->inEnd)
404 memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
405 conn->inEnd - conn->inStart);
406 conn->inEnd -= conn->inStart;
407 conn->inCursor -= conn->inStart;
411 conn->inStart = conn->inCursor = conn->inEnd = 0;
414 * If the buffer is fairly full, enlarge it. We need to be able to
415 * enlarge the buffer in case a single message exceeds the initial
416 * buffer size. We enlarge before filling the buffer entirely so as
417 * to avoid asking the kernel for a partial packet. The magic constant
418 * here should be large enough for a TCP packet or Unix pipe
419 * bufferload. 8K is the usual pipe buffer size, so...
421 if (conn->inBufSize - conn->inEnd < 8192)
423 int newSize = conn->inBufSize * 2;
424 char *newBuf = (char *) realloc(conn->inBuffer, newSize);
428 conn->inBuffer = newBuf;
429 conn->inBufSize = newSize;
433 /* OK, try to read some data */
437 nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd,
438 conn->inBufSize - conn->inEnd);
441 nread = recv(conn->sock, conn->inBuffer + conn->inEnd,
442 conn->inBufSize - conn->inEnd, 0);
447 /* Some systems return EAGAIN/EWOULDBLOCK for no data */
452 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
453 if (errno == EWOULDBLOCK)
456 /* We might get ECONNRESET here if using TCP and backend died */
458 if (errno == ECONNRESET)
459 goto definitelyFailed;
461 printfPQExpBuffer(&conn->errorMessage,
462 "pqReadData() -- read() failed: errno=%d\n%s\n",
463 errno, strerror(errno));
468 conn->inEnd += nread;
471 * Hack to deal with the fact that some kernels will only give us
472 * back 1 packet per recv() call, even if we asked for more and
473 * there is more available. If it looks like we are reading a
474 * long message, loop back to recv() again immediately, until we
475 * run out of data or buffer space. Without this, the
476 * block-and-restart behavior of libpq's higher levels leads to
477 * O(N^2) performance on long messages.
479 * Since we left-justified the data above, conn->inEnd gives the
480 * amount of data already read in the current message. We
481 * consider the message "long" once we have acquired 32k ...
483 if (conn->inEnd > 32768 &&
484 (conn->inBufSize - conn->inEnd) >= 8192)
493 return 1; /* got a zero read after successful tries */
496 * A return value of 0 could mean just that no data is now available,
497 * or it could mean EOF --- that is, the server has closed the
498 * connection. Since we have the socket in nonblock mode, the only way
499 * to tell the difference is to see if select() is saying that the
500 * file is ready. Grumble. Fortunately, we don't expect this path to
501 * be taken much, since in normal practice we should not be trying to
502 * read data unless the file selected for reading already.
504 switch (pqReadReady(conn))
507 /* definitely no data available */
513 goto definitelyFailed;
517 * Still not sure that it's EOF, because some data could have just
523 nread = SSL_read(conn->ssl, conn->inBuffer + conn->inEnd,
524 conn->inBufSize - conn->inEnd);
527 nread = recv(conn->sock, conn->inBuffer + conn->inEnd,
528 conn->inBufSize - conn->inEnd, 0);
533 /* Some systems return EAGAIN/EWOULDBLOCK for no data */
538 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
539 if (errno == EWOULDBLOCK)
542 /* We might get ECONNRESET here if using TCP and backend died */
544 if (errno == ECONNRESET)
545 goto definitelyFailed;
547 printfPQExpBuffer(&conn->errorMessage,
548 "pqReadData() -- read() failed: errno=%d\n%s\n",
549 errno, strerror(errno));
554 conn->inEnd += nread;
559 * OK, we are getting a zero read even though select() says ready.
560 * This means the connection has been closed. Cope.
563 printfPQExpBuffer(&conn->errorMessage,
564 "pqReadData() -- backend closed the channel unexpectedly.\n"
565 "\tThis probably means the backend terminated abnormally\n"
566 "\tbefore or while processing the request.\n");
567 conn->status = CONNECTION_BAD; /* No more connection to backend */
569 closesocket(conn->sock);
578 /* --------------------------------------------------------------------- */
579 /* pqFlush: send any data waiting in the output buffer
582 pqFlush(PGconn *conn)
584 char *ptr = conn->outBuffer;
585 int len = conn->outCount;
589 printfPQExpBuffer(&conn->errorMessage,
590 "pqFlush() -- connection not open\n");
595 * don't try to send zero data, allows us to use this function without
596 * too much worry about overhead
601 /* while there's still data to send */
604 /* Prevent being SIGPIPEd if backend has closed the connection. */
606 pqsigfunc oldsighandler = pqsignal(SIGPIPE, SIG_IGN);
614 sent = SSL_write(conn->ssl, ptr, len);
617 sent = send(conn->sock, ptr, len, 0);
620 pqsignal(SIGPIPE, oldsighandler);
627 * Anything except EAGAIN or EWOULDBLOCK is trouble. If it's
628 * EPIPE or ECONNRESET, assume we've lost the backend
629 * connection permanently.
637 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
648 printfPQExpBuffer(&conn->errorMessage,
649 "pqFlush() -- backend closed the channel unexpectedly.\n"
650 "\tThis probably means the backend terminated abnormally"
651 " before or while processing the request.\n");
654 * We used to close the socket here, but that's a bad
655 * idea since there might be unread data waiting
656 * (typically, a NOTICE message from the backend
657 * telling us it's committing hara-kiri...). Leave
658 * the socket open until pqReadData finds no more data
664 printfPQExpBuffer(&conn->errorMessage,
665 "pqFlush() -- couldn't send data: errno=%d\n%s\n",
666 errno, strerror(errno));
667 /* We don't assume it's a fatal error... */
679 /* We didn't send it all, wait till we can send more */
682 * if the socket is in non-blocking mode we may need to abort
686 /* can't do anything for our SSL users yet */
687 if (conn->ssl == NULL)
690 if (pqIsnonblocking(conn))
692 /* shift the contents of the buffer */
693 memmove(conn->outBuffer, ptr, len);
694 conn->outCount = len;
701 if (pqWait(FALSE, TRUE, conn))
709 fflush(conn->Pfdebug);
714 /* --------------------------------------------------------------------- */
715 /* pqWait: wait until we can read or write the connection socket
718 pqWait(int forRead, int forWrite, PGconn *conn)
725 printfPQExpBuffer(&conn->errorMessage,
726 "pqWait() -- connection not open\n");
730 if (forRead || forWrite)
733 FD_ZERO(&input_mask);
734 FD_ZERO(&output_mask);
736 FD_SET(conn->sock, &input_mask);
738 FD_SET(conn->sock, &output_mask);
739 if (select(conn->sock + 1, &input_mask, &output_mask, (fd_set *) NULL,
740 (struct timeval *) NULL) < 0)
744 printfPQExpBuffer(&conn->errorMessage,
745 "pqWait() -- select() failed: errno=%d\n%s\n",
746 errno, strerror(errno));
757 * A couple of "miscellaneous" multibyte related functions. They used
758 * to be in fe-print.c but that file is doomed.
763 * returns the byte length of the word beginning s, using the
764 * specified encoding.
767 PQmblen(const unsigned char *s, int encoding)
769 return (pg_encoding_mblen(encoding, s));
773 * Get encoding id from environment variable PGCLIENTENCODING.
779 int encoding = SQL_ASCII;
781 str = getenv("PGCLIENTENCODING");
782 if (str && *str != '\0')
783 encoding = pg_char_to_encoding(str);
789 /* Provide a default definition in case someone calls it anyway */
791 PQmblen(const unsigned char *s, int encoding)
803 #endif /* MULTIBYTE */