> Ok, where's a "system dependent hack" :)

> It seems that win9x doesn't have the "netmsg.dll" so it defaults to "normal"
> FormatMessage.
> I wonder if one could load wsock32.dll or winsock.dll on those systems
> instead of netmsg.dll.
>
> Mikhail, could you please test this code on your nt4 system?
> Could someone else test this code on a win98/95 system?
>
> It works on win2k over here.

It works on win2k here too but not on win98/95 or winNT.
Anyway, attached is the patch which uses Magnus's my_sock_strerror
function (renamed to winsock_strerror). The only difference is that
I put the code to load and unload netmsg.dll in the libpqdll.c
(is this OK Magnus?).

Mikhail Terekhov
This commit is contained in:
Bruce Momjian 2001-08-21 20:39:54 +00:00
parent f933766ba7
commit 5db5c2db61
7 changed files with 90 additions and 60 deletions

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.175 2001/08/17 15:11:15 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.176 2001/08/21 20:39:52 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -703,7 +703,7 @@ connectMakeNonblocking(PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to non-blocking mode: %s\n"), libpq_gettext("could not set socket to non-blocking mode: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return 0; return 0;
} }
@ -727,7 +727,7 @@ connectNoDelay(PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not set socket to TCP no delay mode: %s\n"), libpq_gettext("could not set socket to TCP no delay mode: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return 0; return 0;
} }
@ -750,7 +750,7 @@ connectFailureMessage(PGconn *conn, int errorno)
"\tIs the server running locally and accepting\n" "\tIs the server running locally and accepting\n"
"\tconnections on Unix domain socket \"%s\"?\n" "\tconnections on Unix domain socket \"%s\"?\n"
), ),
strerror(errorno), SOCK_STRERROR(errorno),
conn->raddr.un.sun_path); conn->raddr.un.sun_path);
else else
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
@ -759,7 +759,7 @@ connectFailureMessage(PGconn *conn, int errorno)
"\tIs the server running on host %s and accepting\n" "\tIs the server running on host %s and accepting\n"
"\tTCP/IP connections on port %s?\n" "\tTCP/IP connections on port %s?\n"
), ),
strerror(errorno), SOCK_STRERROR(errorno),
conn->pghost conn->pghost
? conn->pghost ? conn->pghost
: (conn->pghostaddr : (conn->pghostaddr
@ -882,7 +882,7 @@ connectDBStart(PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not create socket: %s\n"), libpq_gettext("could not create socket: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
goto connect_errReturn; goto connect_errReturn;
} }
@ -914,7 +914,7 @@ connectDBStart(PGconn *conn)
*/ */
if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) < 0) if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) < 0)
{ {
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == 0) if (SOCK_ERRNO == EINPROGRESS || SOCK_ERRNO == EWOULDBLOCK || SOCK_ERRNO == 0)
{ {
/* /*
* This is fine - we're in non-blocking mode, and the * This is fine - we're in non-blocking mode, and the
@ -925,7 +925,7 @@ connectDBStart(PGconn *conn)
else else
{ {
/* Something's gone wrong */ /* Something's gone wrong */
connectFailureMessage(conn, errno); connectFailureMessage(conn, SOCK_ERRNO);
goto connect_errReturn; goto connect_errReturn;
} }
} }
@ -945,7 +945,7 @@ connectDBStart(PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not send SSL negotiation packet: %s\n"), libpq_gettext("could not send SSL negotiation packet: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
goto connect_errReturn; goto connect_errReturn;
} }
/* Now receive the postmasters response */ /* Now receive the postmasters response */
@ -953,7 +953,7 @@ connectDBStart(PGconn *conn)
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not receive server response to SSL negotiation packet: %s\n"), libpq_gettext("could not receive server response to SSL negotiation packet: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
goto connect_errReturn; goto connect_errReturn;
} }
if (SSLok == 'S') if (SSLok == 'S')
@ -1203,7 +1203,7 @@ keep_going: /* We will come back to here until there
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not get socket error status: %s\n"), libpq_gettext("could not get socket error status: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
goto error_return; goto error_return;
} }
else if (optval != 0) else if (optval != 0)
@ -1223,7 +1223,7 @@ keep_going: /* We will come back to here until there
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not get client address from socket: %s\n"), libpq_gettext("could not get client address from socket: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
goto error_return; goto error_return;
} }
@ -1262,7 +1262,7 @@ keep_going: /* We will come back to here until there
{ {
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not send startup packet: %s\n"), libpq_gettext("could not send startup packet: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
goto error_return; goto error_return;
} }
@ -2097,7 +2097,7 @@ PQresetPoll(PGconn *conn)
int int
PQrequestCancel(PGconn *conn) PQrequestCancel(PGconn *conn)
{ {
int save_errno = errno; int save_errno = SOCK_ERRNO;
int tmpsock = -1; int tmpsock = -1;
struct struct
{ {
@ -2169,7 +2169,7 @@ PQrequestCancel(PGconn *conn)
return TRUE; return TRUE;
cancel_errReturn: cancel_errReturn:
strcat(conn->errorMessage.data, strerror(errno)); strcat(conn->errorMessage.data, SOCK_STRERROR(SOCK_ERRNO));
strcat(conn->errorMessage.data, "\n"); strcat(conn->errorMessage.data, "\n");
conn->errorMessage.len = strlen(conn->errorMessage.data); conn->errorMessage.len = strlen(conn->errorMessage.data);
if (tmpsock >= 0) if (tmpsock >= 0)

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.107 2001/08/17 15:11:15 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.108 2001/08/21 20:39:53 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -2038,10 +2038,6 @@ PQoidStatus(const PGresult *res)
return buf; return buf;
} }
#ifdef WIN32 /* need to get at normal errno here */
#undef errno
#endif
/* /*
PQoidValue - PQoidValue -
a perhaps preferable form of the above which just returns a perhaps preferable form of the above which just returns
@ -2056,7 +2052,11 @@ PQoidValue(const PGresult *res)
if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0) if (!res || !res->cmdStatus || strncmp(res->cmdStatus, "INSERT ", 7) != 0)
return InvalidOid; return InvalidOid;
#ifdef WIN32
SetLastError(0);
#else
errno = 0; errno = 0;
#endif
result = strtoul(res->cmdStatus + 7, &endptr, 10); result = strtoul(res->cmdStatus + 7, &endptr, 10);
if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE) if (!endptr || (*endptr != ' ' && *endptr != '\0') || errno == ERANGE)
@ -2065,9 +2065,6 @@ PQoidValue(const PGresult *res)
return (Oid) result; return (Oid) result;
} }
#ifdef WIN32 /* back to socket errno */
#define errno WSAGetLastError()
#endif
/* /*
PQcmdTuples - PQcmdTuples -

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.37 2001/08/17 15:11:15 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.38 2001/08/21 20:39:54 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -30,12 +30,6 @@
#include "libpq/libpq-fs.h" /* must come after sys/stat.h */ #include "libpq/libpq-fs.h" /* must come after sys/stat.h */
#ifdef WIN32 /* need to use normal errno in this file */
#undef errno
#endif
#define LO_BUFSIZE 8192 #define LO_BUFSIZE 8192
static int lo_initialize(PGconn *conn); static int lo_initialize(PGconn *conn);

View File

@ -25,7 +25,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.53 2001/08/17 15:11:15 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.54 2001/08/21 20:39:54 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -347,13 +347,13 @@ retry:
if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL, if (select(conn->sock + 1, &input_mask, (fd_set *) NULL, (fd_set *) NULL,
&timeout) < 0) &timeout) < 0)
{ {
if (errno == EINTR) if (SOCK_ERRNO == EINTR)
/* Interrupted system call - we'll just try again */ /* Interrupted system call - we'll just try again */
goto retry; goto retry;
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("select() failed: %s\n"), libpq_gettext("select() failed: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return -1; return -1;
} }
@ -381,13 +381,13 @@ retry:
if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL, if (select(conn->sock + 1, (fd_set *) NULL, &input_mask, (fd_set *) NULL,
&timeout) < 0) &timeout) < 0)
{ {
if (errno == EINTR) if (SOCK_ERRNO == EINTR)
/* Interrupted system call - we'll just try again */ /* Interrupted system call - we'll just try again */
goto retry; goto retry;
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("select() failed: %s\n"), libpq_gettext("select() failed: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return -1; return -1;
} }
return FD_ISSET(conn->sock, &input_mask) ? 1 : 0; return FD_ISSET(conn->sock, &input_mask) ? 1 : 0;
@ -467,25 +467,25 @@ tryAgain:
conn->inBufSize - conn->inEnd, 0); conn->inBufSize - conn->inEnd, 0);
if (nread < 0) if (nread < 0)
{ {
if (errno == EINTR) if (SOCK_ERRNO == EINTR)
goto tryAgain; goto tryAgain;
/* Some systems return EAGAIN/EWOULDBLOCK for no data */ /* Some systems return EAGAIN/EWOULDBLOCK for no data */
#ifdef EAGAIN #ifdef EAGAIN
if (errno == EAGAIN) if (SOCK_ERRNO == EAGAIN)
return someread; return someread;
#endif #endif
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN)) #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
if (errno == EWOULDBLOCK) if (SOCK_ERRNO == EWOULDBLOCK)
return someread; return someread;
#endif #endif
/* We might get ECONNRESET here if using TCP and backend died */ /* We might get ECONNRESET here if using TCP and backend died */
#ifdef ECONNRESET #ifdef ECONNRESET
if (errno == ECONNRESET) if (SOCK_ERRNO == ECONNRESET)
goto definitelyFailed; goto definitelyFailed;
#endif #endif
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not receive data from server: %s\n"), libpq_gettext("could not receive data from server: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return -1; return -1;
} }
if (nread > 0) if (nread > 0)
@ -553,25 +553,25 @@ tryAgain2:
conn->inBufSize - conn->inEnd, 0); conn->inBufSize - conn->inEnd, 0);
if (nread < 0) if (nread < 0)
{ {
if (errno == EINTR) if (SOCK_ERRNO == EINTR)
goto tryAgain2; goto tryAgain2;
/* Some systems return EAGAIN/EWOULDBLOCK for no data */ /* Some systems return EAGAIN/EWOULDBLOCK for no data */
#ifdef EAGAIN #ifdef EAGAIN
if (errno == EAGAIN) if (SOCK_ERRNO == EAGAIN)
return 0; return 0;
#endif #endif
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN)) #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
if (errno == EWOULDBLOCK) if (SOCK_ERRNO == EWOULDBLOCK)
return 0; return 0;
#endif #endif
/* We might get ECONNRESET here if using TCP and backend died */ /* We might get ECONNRESET here if using TCP and backend died */
#ifdef ECONNRESET #ifdef ECONNRESET
if (errno == ECONNRESET) if (SOCK_ERRNO == ECONNRESET)
goto definitelyFailed; goto definitelyFailed;
#endif #endif
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not receive data from server: %s\n"), libpq_gettext("could not receive data from server: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return -1; return -1;
} }
if (nread > 0) if (nread > 0)
@ -653,7 +653,7 @@ pqFlush(PGconn *conn)
* EPIPE or ECONNRESET, assume we've lost the backend * EPIPE or ECONNRESET, assume we've lost the backend
* connection permanently. * connection permanently.
*/ */
switch (errno) switch (SOCK_ERRNO)
{ {
#ifdef EAGAIN #ifdef EAGAIN
case EAGAIN: case EAGAIN:
@ -689,7 +689,7 @@ pqFlush(PGconn *conn)
default: default:
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not send data to server: %s\n"), libpq_gettext("could not send data to server: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
/* We don't assume it's a fatal error... */ /* We don't assume it's a fatal error... */
return EOF; return EOF;
} }
@ -772,11 +772,11 @@ retry:
if (select(conn->sock + 1, &input_mask, &output_mask, &except_mask, if (select(conn->sock + 1, &input_mask, &output_mask, &except_mask,
(struct timeval *) NULL) < 0) (struct timeval *) NULL) < 0)
{ {
if (errno == EINTR) if (SOCK_ERRNO == EINTR)
goto retry; goto retry;
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("select() failed: %s\n"), libpq_gettext("select() failed: %s\n"),
strerror(errno)); SOCK_STRERROR(SOCK_ERRNO));
return EOF; return EOF;
} }
} }
@ -851,3 +851,27 @@ libpq_gettext(const char *msgid)
return dgettext("libpq", msgid); return dgettext("libpq", msgid);
} }
#endif /* ENABLE_NLS */ #endif /* ENABLE_NLS */
#ifdef WIN32
/*
* strerror replacement for windows:
*/
const char*
winsock_strerror(DWORD eno)
{
if (!FormatMessage(
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM | /* always consider system table */
((netmsgModule != NULL) ? FORMAT_MESSAGE_FROM_HMODULE : 0),
netmsgModule, /* module to get message from (NULL == system) */
eno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
winsock_strerror_buf,sizeof(winsock_strerror_buf)-1,
NULL
)){
sprintf(winsock_strerror_buf,"Unknown socket error(%u)",eno);
}
winsock_strerror_buf[sizeof(winsock_strerror_buf)-1]='\0';
return winsock_strerror_buf;
}
#endif

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: libpq-fe.h,v 1.71 2001/03/22 04:01:27 momjian Exp $ * $Id: libpq-fe.h,v 1.72 2001/08/21 20:39:54 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -21,10 +21,21 @@ extern "C"
#endif #endif
#include <stdio.h> #include <stdio.h>
#ifdef WIN32
#define SOCK_ERRNO (WSAGetLastError ())
#define SOCK_STRERROR winsock_strerror
#else
#define SOCK_ERRNO errno
#define SOCK_STRERROR strerror
#endif
/* postgres_ext.h defines the backend's externally visible types, /* postgres_ext.h defines the backend's externally visible types,
* such as Oid. * such as Oid.
*/ */
#include "postgres_ext.h" #include "postgres_ext.h"
#ifdef USE_SSL #ifdef USE_SSL
#include <openssl/ssl.h> #include <openssl/ssl.h>
#endif #endif

View File

@ -1,6 +1,7 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <winsock.h> #include <winsock.h>
#include "win32.h"
BOOL WINAPI BOOL WINAPI
DllMain(HINSTANCE hinstDLL, DWORD fdwReason, DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
@ -20,8 +21,13 @@ DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
*/ */
return FALSE; return FALSE;
} }
if (netmsgModule == NULL){
netmsgModule=LoadLibraryEx("netmsg.dll",NULL,LOAD_LIBRARY_AS_DATAFILE);
}
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
if (netmsgModule != NULL)
FreeLibrary(netmsgModule);
WSACleanup(); WSACleanup();
break; break;
} }

View File

@ -23,19 +23,17 @@
*/ */
#define crypt(a,b) (a) #define crypt(a,b) (a)
/*
* Most of libpq uses "errno" to access error conditions from socket calls,
* so on Windows we want to redirect those usages to WSAGetLastError().
* Rather than #ifdef'ing every single place that has "errno", hack it up
* with a macro instead. But there are a few places that do need to touch
* the regular errno variable. For them, we #undef and then redefine errno.
*/
#define errno WSAGetLastError()
#undef EAGAIN /* doesn't apply on sockets */ #undef EAGAIN /* doesn't apply on sockets */
#undef EINTR #undef EINTR
#define EINTR WSAEINTR #define EINTR WSAEINTR
#define EWOULDBLOCK WSAEWOULDBLOCK #define EWOULDBLOCK WSAEWOULDBLOCK
#define ECONNRESET WSAECONNRESET #define ECONNRESET WSAECONNRESET
#define EINPROGRESS WSAEINPROGRESS #define EINPROGRESS WSAEINPROGRESS
/*
* Windows network messaging stuff:
*/
static HINSTANCE netmsgModule = NULL;
static char winsock_strerror_buf[512];
const char* winsock_strerror(DWORD eno);