1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* FILE
|
|
|
|
* fe-misc.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* DESCRIPTION
|
|
|
|
* miscellaneous useful functions
|
1998-05-07 01:51:16 +02:00
|
|
|
*
|
|
|
|
* 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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-09-20 06:51:12 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.22 1998/09/20 04:51:12 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1998-08-17 05:50:43 +02:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "libpq-int.h"
|
|
|
|
#include "postgres.h"
|
|
|
|
#include "pqsignal.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include <stdlib.h>
|
1998-05-07 01:51:16 +02:00
|
|
|
#include <string.h>
|
1998-05-07 18:17:18 +02:00
|
|
|
#include <errno.h>
|
1998-08-17 05:50:43 +02:00
|
|
|
#include <signal.h>
|
1998-05-07 01:51:16 +02:00
|
|
|
#include <time.h>
|
1998-07-03 06:24:16 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "win32.h"
|
|
|
|
#else
|
1998-05-12 17:42:09 +02:00
|
|
|
#include <sys/time.h>
|
1998-05-07 01:51:16 +02:00
|
|
|
#if !defined(NO_UNISTD_H)
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-09-01 06:40:42 +02:00
|
|
|
#endif /* WIN32 */
|
1998-08-17 05:50:43 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
1996-11-03 08:14:32 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-08-09 04:59:33 +02:00
|
|
|
#define DONOTICE(conn,message) \
|
|
|
|
((*(conn)->noticeHook) ((conn)->noticeArg, (message)))
|
|
|
|
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pqGetc:
|
1998-05-07 01:51:16 +02:00
|
|
|
get a character from the connection
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqGetc(char *result, PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->inCursor >= conn->inEnd)
|
|
|
|
return EOF;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
*result = conn->inBuffer[conn->inCursor++];
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->Pfdebug)
|
|
|
|
fprintf(conn->Pfdebug, "From backend> %c\n", *result);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pqPutBytes: local routine to write N bytes to the connection,
|
|
|
|
with buffering
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
pqPutBytes(const char *s, int nbytes, PGconn *conn)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int avail = conn->outBufSize - conn->outCount;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
memcpy(conn->outBuffer + conn->outCount, s, nbytes);
|
|
|
|
conn->outCount += nbytes;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
/* pqGets:
|
|
|
|
get a null-terminated string from the connection,
|
|
|
|
and store it in a buffer of size maxlen bytes.
|
|
|
|
If the incoming string is >= maxlen bytes, all of it is read,
|
|
|
|
but the excess characters are silently discarded.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqGets(char *s, int maxlen, PGconn *conn)
|
|
|
|
{
|
|
|
|
/* Copy conn data to locals for faster search loop */
|
1998-09-01 06:40:42 +02:00
|
|
|
char *inBuffer = conn->inBuffer;
|
|
|
|
int inCursor = conn->inCursor;
|
|
|
|
int inEnd = conn->inEnd;
|
|
|
|
int slen;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
while (inCursor < inEnd && inBuffer[inCursor])
|
|
|
|
inCursor++;
|
|
|
|
|
|
|
|
if (inCursor >= inEnd)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
slen = inCursor - conn->inCursor;
|
|
|
|
if (slen < maxlen)
|
|
|
|
strcpy(s, inBuffer + conn->inCursor);
|
|
|
|
else
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
strncpy(s, inBuffer + conn->inCursor, maxlen - 1);
|
|
|
|
s[maxlen - 1] = '\0';
|
1998-05-07 01:51:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
conn->inCursor = ++inCursor;
|
|
|
|
|
|
|
|
if (conn->Pfdebug)
|
|
|
|
fprintf(conn->Pfdebug, "From backend> \"%s\"\n", s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
int
|
|
|
|
pqPuts(const char *s, PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
if (pqPutBytes(s, strlen(s) + 1, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
|
|
|
|
|
|
|
if (conn->Pfdebug)
|
|
|
|
fprintf(conn->Pfdebug, "To backend> %s\n", s);
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1996-07-09 08:22:35 +02:00
|
|
|
/* pqGetnchar:
|
1998-08-29 04:09:27 +02:00
|
|
|
get a string of exactly len bytes in buffer s, no null termination
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqGetnchar(char *s, int len, PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (len < 0 || len > conn->inEnd - conn->inCursor)
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
memcpy(s, conn->inBuffer + conn->inCursor, len);
|
1998-08-29 04:09:27 +02:00
|
|
|
/* no terminating null */
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->inCursor += len;
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->Pfdebug)
|
1998-09-03 04:10:56 +02:00
|
|
|
fprintf(conn->Pfdebug, "From backend (%d)> %.*s\n", len, len, s);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pqPutnchar:
|
1998-09-03 04:10:56 +02:00
|
|
|
send a string of exactly len bytes, no null termination needed
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqPutnchar(const char *s, int len, PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
if (pqPutBytes(s, len, conn))
|
|
|
|
return EOF;
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->Pfdebug)
|
1998-09-03 04:10:56 +02:00
|
|
|
fprintf(conn->Pfdebug, "To backend> %.*s\n", len, s);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pgGetInt
|
|
|
|
read a 2 or 4 byte integer and convert from network byte order
|
|
|
|
to local byte order
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqGetInt(int *result, int bytes, PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
uint16 tmp2;
|
|
|
|
uint32 tmp4;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (bytes)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 2:
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->inCursor + 2 > conn->inEnd)
|
|
|
|
return EOF;
|
|
|
|
memcpy(&tmp2, conn->inBuffer + conn->inCursor, 2);
|
|
|
|
conn->inCursor += 2;
|
|
|
|
*result = (int) ntohs(tmp2);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->inCursor + 4 > conn->inEnd)
|
|
|
|
return EOF;
|
|
|
|
memcpy(&tmp4, conn->inBuffer + conn->inCursor, 4);
|
|
|
|
conn->inCursor += 4;
|
|
|
|
*result = (int) ntohl(tmp4);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
1998-08-09 04:59:33 +02:00
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqGetInt: int size %d not supported\n", bytes);
|
|
|
|
DONOTICE(conn, conn->errorMessage);
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->Pfdebug)
|
|
|
|
fprintf(conn->Pfdebug, "From backend (#%d)> %d\n", bytes, *result);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pgPutInt
|
|
|
|
send an integer of 2 or 4 bytes, converting from host byte order
|
|
|
|
to network byte order.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqPutInt(int value, int bytes, PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
uint16 tmp2;
|
|
|
|
uint32 tmp4;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (bytes)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 2:
|
1998-05-07 01:51:16 +02:00
|
|
|
tmp2 = htons((uint16) value);
|
1998-09-01 06:40:42 +02:00
|
|
|
if (pqPutBytes((const char *) &tmp2, 2, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
1998-05-07 01:51:16 +02:00
|
|
|
tmp4 = htonl((uint32) value);
|
1998-09-01 06:40:42 +02:00
|
|
|
if (pqPutBytes((const char *) &tmp4, 4, conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
1998-08-09 04:59:33 +02:00
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqPutInt: int size %d not supported\n", bytes);
|
|
|
|
DONOTICE(conn, conn->errorMessage);
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (conn->Pfdebug)
|
|
|
|
fprintf(conn->Pfdebug, "To backend (%d#)> %d\n", bytes, value);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0;
|
1997-03-16 19:51:29 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pqReadReady: is select() saying the file is ready to read?
|
|
|
|
*/
|
1998-08-17 05:50:43 +02:00
|
|
|
static int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqReadReady(PGconn *conn)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
fd_set input_mask;
|
|
|
|
struct timeval timeout;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
if (conn->sock < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
FD_ZERO(&input_mask);
|
|
|
|
FD_SET(conn->sock, &input_mask);
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 0;
|
1998-09-01 06:40:42 +02:00
|
|
|
if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL,
|
1998-05-07 01:51:16 +02:00
|
|
|
&timeout) < 0)
|
|
|
|
{
|
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqReadReady() -- select() failed: errno=%d\n%s\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return FD_ISSET(conn->sock, &input_mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------- */
|
|
|
|
/* pqReadData: read more data, if any is available
|
|
|
|
* Possible return values:
|
1998-09-01 06:40:42 +02:00
|
|
|
* 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
|
1998-05-07 01:51:16 +02:00
|
|
|
* NOTE: callers must not assume that pointers or indexes into conn->inBuffer
|
|
|
|
* remain valid across this call!
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
int
|
1998-05-07 01:51:16 +02:00
|
|
|
pqReadData(PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int nread;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
if (conn->sock < 0)
|
|
|
|
{
|
|
|
|
strcpy(conn->errorMessage, "pqReadData() -- connection not open\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* Left-justify any data in the buffer to make room */
|
|
|
|
if (conn->inStart < conn->inEnd)
|
|
|
|
{
|
|
|
|
memmove(conn->inBuffer, conn->inBuffer + conn->inStart,
|
|
|
|
conn->inEnd - conn->inStart);
|
|
|
|
conn->inEnd -= conn->inStart;
|
|
|
|
conn->inCursor -= conn->inStart;
|
|
|
|
conn->inStart = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
conn->inStart = conn->inCursor = conn->inEnd = 0;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* here should be at least one TCP packet.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
if (conn->inBufSize - conn->inEnd < 2000)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
int newSize = conn->inBufSize * 2;
|
|
|
|
char *newBuf = (char *) realloc(conn->inBuffer, newSize);
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (newBuf)
|
|
|
|
{
|
|
|
|
conn->inBuffer = newBuf;
|
|
|
|
conn->inBufSize = newSize;
|
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
/* OK, try to read some data */
|
|
|
|
tryAgain:
|
1998-09-01 06:40:42 +02:00
|
|
|
nread = recv(conn->sock, conn->inBuffer + conn->inEnd,
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->inBufSize - conn->inEnd, 0);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto tryAgain;
|
1998-05-07 01:53:48 +02:00
|
|
|
/* Some systems return EAGAIN/EWOULDBLOCK for no data */
|
|
|
|
#ifdef EAGAIN
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
1998-09-20 06:51:12 +02:00
|
|
|
#endif
|
|
|
|
/* We might get ECONNRESET here if using TCP and backend died */
|
|
|
|
#ifdef ECONNRESET
|
|
|
|
if (errno == ECONNRESET)
|
|
|
|
goto definitelyFailed;
|
1998-05-07 01:53:48 +02:00
|
|
|
#endif
|
1998-05-07 01:51:16 +02:00
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqReadData() -- read() failed: errno=%d\n%s\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nread > 0)
|
|
|
|
{
|
|
|
|
conn->inEnd += nread;
|
|
|
|
return 1;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
1998-09-01 06:40:42 +02:00
|
|
|
if (!pqReadReady(conn))
|
1998-05-07 01:51:16 +02:00
|
|
|
return 0; /* definitely no data available */
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* Still not sure that it's EOF, because some data could have just
|
|
|
|
* arrived.
|
1998-05-07 01:51:16 +02:00
|
|
|
*/
|
|
|
|
tryAgain2:
|
1998-09-01 06:40:42 +02:00
|
|
|
nread = recv(conn->sock, conn->inBuffer + conn->inEnd,
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->inBufSize - conn->inEnd, 0);
|
|
|
|
if (nread < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto tryAgain2;
|
1998-05-07 01:53:48 +02:00
|
|
|
/* Some systems return EAGAIN/EWOULDBLOCK for no data */
|
|
|
|
#ifdef EAGAIN
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
1998-09-20 06:51:12 +02:00
|
|
|
#endif
|
|
|
|
/* We might get ECONNRESET here if using TCP and backend died */
|
|
|
|
#ifdef ECONNRESET
|
|
|
|
if (errno == ECONNRESET)
|
|
|
|
goto definitelyFailed;
|
1998-05-07 01:53:48 +02:00
|
|
|
#endif
|
1998-05-07 01:51:16 +02:00
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqReadData() -- read() failed: errno=%d\n%s\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (nread > 0)
|
|
|
|
{
|
|
|
|
conn->inEnd += nread;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
|
|
|
* OK, we are getting a zero read even though select() says ready.
|
1998-05-07 01:51:16 +02:00
|
|
|
* This means the connection has been closed. Cope.
|
|
|
|
*/
|
1998-09-20 06:51:12 +02:00
|
|
|
definitelyFailed:
|
1998-05-07 01:51:16 +02:00
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqReadData() -- backend closed the channel unexpectedly.\n"
|
|
|
|
"\tThis probably means the backend terminated abnormally"
|
|
|
|
" before or while processing the request.\n");
|
1998-09-01 06:40:42 +02:00
|
|
|
conn->status = CONNECTION_BAD; /* No more connection to backend */
|
1998-07-03 06:24:16 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
closesocket(conn->sock);
|
|
|
|
#else
|
1998-05-07 01:51:16 +02:00
|
|
|
close(conn->sock);
|
1998-07-03 06:24:16 +02:00
|
|
|
#endif
|
1998-05-07 01:51:16 +02:00
|
|
|
conn->sock = -1;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
return -1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pqFlush: send any data waiting in the output buffer
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pqFlush(PGconn *conn)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
char *ptr = conn->outBuffer;
|
|
|
|
int len = conn->outCount;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
if (conn->sock < 0)
|
|
|
|
{
|
|
|
|
strcpy(conn->errorMessage, "pqFlush() -- connection not open\n");
|
|
|
|
return EOF;
|
|
|
|
}
|
1997-03-16 19:51:29 +01:00
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
while (len > 0)
|
|
|
|
{
|
1998-08-17 05:50:43 +02:00
|
|
|
/* Prevent being SIGPIPEd if backend has closed the connection. */
|
|
|
|
#ifndef WIN32
|
1998-09-01 06:40:42 +02:00
|
|
|
pqsigfunc oldsighandler = pqsignal(SIGPIPE, SIG_IGN);
|
1998-08-17 05:50:43 +02:00
|
|
|
#endif
|
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
int sent = send(conn->sock, ptr, len, 0);
|
1998-08-17 05:50:43 +02:00
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
pqsignal(SIGPIPE, oldsighandler);
|
|
|
|
#endif
|
|
|
|
|
1998-05-07 01:51:16 +02:00
|
|
|
if (sent < 0)
|
|
|
|
{
|
1998-09-20 06:51:12 +02:00
|
|
|
/*
|
|
|
|
* Anything except EAGAIN or EWOULDBLOCK is trouble.
|
|
|
|
* If it's EPIPE or ECONNRESET, assume we've lost the
|
|
|
|
* backend connection permanently.
|
|
|
|
*/
|
1998-05-07 01:51:16 +02:00
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
#ifdef EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
break;
|
|
|
|
#endif
|
1998-09-20 06:51:12 +02:00
|
|
|
case EPIPE:
|
|
|
|
#ifdef ECONNRESET
|
|
|
|
case ECONNRESET:
|
|
|
|
#endif
|
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqFlush() -- backend closed the channel unexpectedly.\n"
|
|
|
|
"\tThis probably means the backend terminated abnormally"
|
|
|
|
" before or while processing the request.\n");
|
|
|
|
conn->status = CONNECTION_BAD; /* No more connection */
|
|
|
|
#ifdef WIN32
|
|
|
|
closesocket(conn->sock);
|
|
|
|
#else
|
|
|
|
close(conn->sock);
|
|
|
|
#endif
|
|
|
|
conn->sock = -1;
|
|
|
|
return EOF;
|
1998-05-07 01:51:16 +02:00
|
|
|
default:
|
|
|
|
sprintf(conn->errorMessage,
|
1998-09-20 06:51:12 +02:00
|
|
|
"pqFlush() -- couldn't send data: errno=%d\n%s\n",
|
1998-05-07 01:51:16 +02:00
|
|
|
errno, strerror(errno));
|
1998-09-20 06:51:12 +02:00
|
|
|
/* We don't assume it's a fatal error... */
|
1998-05-07 01:51:16 +02:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr += sent;
|
|
|
|
len -= sent;
|
|
|
|
}
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
/* We didn't send it all, wait till we can send more */
|
|
|
|
if (pqWait(FALSE, TRUE, conn))
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->outCount = 0;
|
|
|
|
|
|
|
|
if (conn->Pfdebug)
|
|
|
|
fflush(conn->Pfdebug);
|
|
|
|
|
|
|
|
return 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1996-12-31 08:29:17 +01:00
|
|
|
|
1997-03-16 19:51:29 +01:00
|
|
|
/* --------------------------------------------------------------------- */
|
1998-05-07 01:51:16 +02:00
|
|
|
/* pqWait: wait until we can read or write the connection socket
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
pqWait(int forRead, int forWrite, PGconn *conn)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
fd_set input_mask;
|
|
|
|
fd_set output_mask;
|
1998-05-07 01:51:16 +02:00
|
|
|
|
|
|
|
if (conn->sock < 0)
|
|
|
|
{
|
|
|
|
strcpy(conn->errorMessage, "pqWait() -- connection not open\n");
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* loop in case select returns EINTR */
|
1998-09-01 06:40:42 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
1998-05-07 01:51:16 +02:00
|
|
|
FD_ZERO(&input_mask);
|
|
|
|
FD_ZERO(&output_mask);
|
|
|
|
if (forRead)
|
|
|
|
FD_SET(conn->sock, &input_mask);
|
|
|
|
if (forWrite)
|
|
|
|
FD_SET(conn->sock, &output_mask);
|
1998-09-01 06:40:42 +02:00
|
|
|
if (select(conn->sock + 1, &input_mask, &output_mask, (fd_set *) NULL,
|
1998-05-07 01:51:16 +02:00
|
|
|
(struct timeval *) NULL) < 0)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
sprintf(conn->errorMessage,
|
|
|
|
"pqWait() -- select() failed: errno=%d\n%s\n",
|
|
|
|
errno, strerror(errno));
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
/* On nonerror return, assume we're done */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|