2002-04-24 04:26:06 +02:00
|
|
|
/*
|
|
|
|
* FILE
|
|
|
|
* win32.c
|
|
|
|
*
|
|
|
|
* DESCRIPTION
|
|
|
|
* Win32 support functions.
|
|
|
|
*
|
|
|
|
* Contains table and functions for looking up win32 socket error
|
|
|
|
* descriptions. But will/may contain other win32 helper functions
|
|
|
|
* for libpq.
|
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* The error constants are taken from the Frambak Bakfram LGSOCKET
|
2002-04-24 04:26:06 +02:00
|
|
|
* library guys who in turn took them from the Winsock FAQ.
|
|
|
|
*
|
2004-12-31 23:04:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2002-04-24 04:26:06 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-07-20 07:43:31 +02:00
|
|
|
/* Make stuff compile faster by excluding not used stuff */
|
|
|
|
|
|
|
|
#define VC_EXTRALEAN
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 01:11:31 +01:00
|
|
|
#ifndef __MINGW32__
|
2002-07-20 07:43:31 +02:00
|
|
|
#define NOGDI
|
Briefly,
* configure + Makefile changes
* shared memory attaching in EXEC_BACKEND case (+ minor fix for apparent
cygwin bug under cygwin/EXEC_BACKEND case only)
* PATH env var separator differences
* missing win32 rand functions added
* placeholder replacements for sync etc under port.h
To those who are really interested, and there are a few of you: the attached
patch + file will allow the source base to be compiled (and, for some
definition, "run") under MingW, with the following caveats (I wanted to
first properly fix all but the last of these, but y'all won't quit asking
for a patch :-):
* child death: SIGCHLD not yet sent, so as a minimum, you'll need to
put in some sort of delay after StartupDatabase, and handle setting
StartupPID to 0 etc (ie. the stuff the reaper() signal function is supposed
to do)
* dirmod.c: comment out the elog calls
* dfmgr.c: some hackage required to substitute_libpath_macro
* slru/xact.c: comment out the errno checking after the readdir
(fixed by next version of MingW)
Again, this is only if you *really* want to see postgres compile and start,
and is a nice leg-up for working on the other Win32 TODO list items. Just
don't expect too much else from it at this point...
Claudio Natoli
2004-02-02 01:11:31 +01:00
|
|
|
#endif
|
2002-07-20 07:43:31 +02:00
|
|
|
#define NOCRYPT
|
|
|
|
|
2002-04-24 04:26:06 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "win32.h"
|
2005-02-27 23:57:17 +01:00
|
|
|
#include "postgres_fe.h"
|
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "libpq-int.h"
|
2002-04-24 04:26:06 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static struct WSErrorEntry
|
|
|
|
{
|
|
|
|
DWORD error;
|
|
|
|
const char *description;
|
|
|
|
} WSErrors[] =
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
0, "No error"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEINTR, "Interrupted system call"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEBADF, "Bad file number"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEACCES, "Permission denied"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEFAULT, "Bad address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEINVAL, "Invalid argument"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEMFILE, "Too many open sockets"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEWOULDBLOCK, "Operation would block"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEINPROGRESS, "Operation now in progress"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEALREADY, "Operation already in progress"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENOTSOCK, "Socket operation on non-socket"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEDESTADDRREQ, "Destination address required"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEMSGSIZE, "Message too long"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEPROTOTYPE, "Protocol wrong type for socket"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENOPROTOOPT, "Bad protocol option"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEPROTONOSUPPORT, "Protocol not supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAESOCKTNOSUPPORT, "Socket type not supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEOPNOTSUPP, "Operation not supported on socket"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEPFNOSUPPORT, "Protocol family not supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEAFNOSUPPORT, "Address family not supported"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEADDRINUSE, "Address already in use"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEADDRNOTAVAIL, "Can't assign requested address"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENETDOWN, "Network is down"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENETUNREACH, "Network is unreachable"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENETRESET, "Net connection reset"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAECONNABORTED, "Software caused connection abort"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAECONNRESET, "Connection reset by peer"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENOBUFS, "No buffer space available"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEISCONN, "Socket is already connected"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENOTCONN, "Socket is not connected"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAESHUTDOWN, "Can't send after socket shutdown"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAETOOMANYREFS, "Too many references, can't splice"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAETIMEDOUT, "Connection timed out"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAECONNREFUSED, "Connection refused"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAELOOP, "Too many levels of symbolic links"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENAMETOOLONG, "File name too long"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEHOSTDOWN, "Host is down"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEHOSTUNREACH, "No route to host"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAENOTEMPTY, "Directory not empty"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEPROCLIM, "Too many processes"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEUSERS, "Too many users"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEDQUOT, "Disc quota exceeded"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAESTALE, "Stale NFS file handle"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEREMOTE, "Too many levels of remote in path"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSASYSNOTREADY, "Network system is unavailable"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAVERNOTSUPPORTED, "Winsock version out of range"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSANOTINITIALISED, "WSAStartup not yet called"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAEDISCON, "Graceful shutdown in progress"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSAHOST_NOT_FOUND, "Host not found"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSATRY_AGAIN, "NA Host not found / SERVFAIL"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSANO_RECOVERY, "Non recoverable FORMERR||REFUSED||NOTIMP"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
WSANO_DATA, "No host data of that type was found"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0, 0
|
|
|
|
} /* End of table */
|
2002-04-24 04:26:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
/*
|
2002-04-24 04:26:06 +02:00
|
|
|
* Returns 0 if not found, linear but who cares, at this moment
|
|
|
|
* we're already in pain :)
|
|
|
|
*/
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
static int
|
|
|
|
LookupWSErrorMessage(DWORD err, char *dest)
|
|
|
|
{
|
|
|
|
struct WSErrorEntry *e;
|
|
|
|
|
|
|
|
for (e = WSErrors; e->description; e++)
|
|
|
|
{
|
|
|
|
if (e->error == err)
|
|
|
|
{
|
|
|
|
strcpy(dest, e->description);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2002-04-24 04:26:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
struct MessageDLL
|
|
|
|
{
|
|
|
|
const char *dll_name;
|
|
|
|
void *handle;
|
|
|
|
int loaded; /* BOOL */
|
|
|
|
} dlls[] =
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"netmsg.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"winsock.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wsock32.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ws2_32.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"wsock32n.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"mswsock.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ws2help.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ws2thk.dll", 0, 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
0, 0, 1
|
|
|
|
} /* Last one, no dll, always loaded */
|
2002-04-24 04:26:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DLLS_SIZE (sizeof(dlls)/sizeof(struct MessageDLL))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a description of the socket error by first trying
|
|
|
|
* to find it in the lookup table, and if that fails, tries
|
|
|
|
* to load any of the winsock dlls to find that message.
|
2002-09-04 22:31:48 +02:00
|
|
|
* The DLL thing works from Nt4 (spX ?) up, but some special
|
2002-04-24 04:26:06 +02:00
|
|
|
* versions of winsock might have this aswell (seen on Win98 SE
|
2002-09-04 22:31:48 +02:00
|
|
|
* special install) / Magnus Naeslund (mag@fbab.net)
|
2002-04-24 04:26:06 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
const char *
|
2003-06-14 19:49:54 +02:00
|
|
|
winsock_strerror(int err, char *strerrbuf, size_t buflen)
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int offs,
|
|
|
|
i;
|
2003-06-14 19:49:54 +02:00
|
|
|
int success = LookupWSErrorMessage(err, strerrbuf);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
for (i = 0; !success && i < DLLS_SIZE; i++)
|
|
|
|
{
|
2002-04-24 04:26:06 +02:00
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!dlls[i].loaded)
|
|
|
|
{
|
|
|
|
dlls[i].loaded = 1; /* Only load once */
|
|
|
|
dlls[i].handle = (void *) LoadLibraryEx(
|
|
|
|
dlls[i].dll_name,
|
|
|
|
0,
|
|
|
|
LOAD_LIBRARY_AS_DATAFILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dlls[i].dll_name && !dlls[i].handle)
|
|
|
|
continue; /* Didn't load */
|
|
|
|
|
|
|
|
flags = FORMAT_MESSAGE_FROM_SYSTEM
|
|
|
|
| FORMAT_MESSAGE_IGNORE_INSERTS
|
|
|
|
| (dlls[i].handle ? FORMAT_MESSAGE_FROM_HMODULE : 0);
|
|
|
|
|
|
|
|
success = 0 != FormatMessage(
|
|
|
|
flags,
|
|
|
|
dlls[i].handle, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2003-06-14 19:49:54 +02:00
|
|
|
strerrbuf, buflen - 64,
|
2002-09-04 22:31:48 +02:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success)
|
2005-02-22 05:43:23 +01:00
|
|
|
sprintf(strerrbuf, libpq_gettext("Unknown socket error (0x%08X/%i)"), err, err);
|
2002-09-04 22:31:48 +02:00
|
|
|
else
|
|
|
|
{
|
2003-06-14 19:49:54 +02:00
|
|
|
strerrbuf[buflen - 1] = '\0';
|
|
|
|
offs = strlen(strerrbuf);
|
2004-08-29 07:07:03 +02:00
|
|
|
if (offs > (int) buflen - 64)
|
2003-06-14 19:49:54 +02:00
|
|
|
offs = buflen - 64;
|
2004-04-19 19:42:59 +02:00
|
|
|
sprintf(strerrbuf + offs, " (0x%08X/%i)", err, err);
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|
2003-06-14 19:49:54 +02:00
|
|
|
return strerrbuf;
|
2002-09-04 22:31:48 +02:00
|
|
|
}
|