From: Amos Jeffries Date: Sun, 7 Aug 2011 09:29:11 +0000 (+1200) Subject: Windows: merge WIN32_strerror() and wsastrerror() into xstrerr() X-Git-Tag: take08~55^2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=979f3ae69383aeda45e52219bceb62bb97c5b8d4;p=thirdparty%2Fsquid.git Windows: merge WIN32_strerror() and wsastrerror() into xstrerr() --- diff --git a/compat/mswin.cc b/compat/mswin.cc index 7538299ea7..f5b3c5f29c 100644 --- a/compat/mswin.cc +++ b/compat/mswin.cc @@ -284,212 +284,6 @@ WIN32_truncate(const char *pathname, off_t length) return res; } -static struct _wsaerrtext { - int err; - const char *errconst; - const char *errdesc; -} _wsaerrtext[] = { - - { - WSA_E_CANCELLED, "WSA_E_CANCELLED", "Lookup cancelled." - }, - { - WSA_E_NO_MORE, "WSA_E_NO_MORE", "No more data available." - }, - { - WSAEACCES, "WSAEACCES", "Permission denied." - }, - { - WSAEADDRINUSE, "WSAEADDRINUSE", "Address already in use." - }, - { - WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL", "Cannot assign requested address." - }, - { - WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT", "Address family not supported by protocol family." - }, - { - WSAEALREADY, "WSAEALREADY", "Operation already in progress." - }, - { - WSAEBADF, "WSAEBADF", "Bad file number." - }, - { - WSAECANCELLED, "WSAECANCELLED", "Operation cancelled." - }, - { - WSAECONNABORTED, "WSAECONNABORTED", "Software caused connection abort." - }, - { - WSAECONNREFUSED, "WSAECONNREFUSED", "Connection refused." - }, - { - WSAECONNRESET, "WSAECONNRESET", "Connection reset by peer." - }, - { - WSAEDESTADDRREQ, "WSAEDESTADDRREQ", "Destination address required." - }, - { - WSAEDQUOT, "WSAEDQUOT", "Disk quota exceeded." - }, - { - WSAEFAULT, "WSAEFAULT", "Bad address." - }, - { - WSAEHOSTDOWN, "WSAEHOSTDOWN", "Host is down." - }, - { - WSAEHOSTUNREACH, "WSAEHOSTUNREACH", "No route to host." - }, - { - WSAEINPROGRESS, "WSAEINPROGRESS", "Operation now in progress." - }, - { - WSAEINTR, "WSAEINTR", "Interrupted function call." - }, - { - WSAEINVAL, "WSAEINVAL", "Invalid argument." - }, - { - WSAEINVALIDPROCTABLE, "WSAEINVALIDPROCTABLE", "Invalid procedure table from service provider." - }, - { - WSAEINVALIDPROVIDER, "WSAEINVALIDPROVIDER", "Invalid service provider version number." - }, - { - WSAEISCONN, "WSAEISCONN", "Socket is already connected." - }, - { - WSAELOOP, "WSAELOOP", "Too many levels of symbolic links." - }, - { - WSAEMFILE, "WSAEMFILE", "Too many open files." - }, - { - WSAEMSGSIZE, "WSAEMSGSIZE", "Message too long." - }, - { - WSAENAMETOOLONG, "WSAENAMETOOLONG", "File name is too long." - }, - { - WSAENETDOWN, "WSAENETDOWN", "Network is down." - }, - { - WSAENETRESET, "WSAENETRESET", "Network dropped connection on reset." - }, - { - WSAENETUNREACH, "WSAENETUNREACH", "Network is unreachable." - }, - { - WSAENOBUFS, "WSAENOBUFS", "No buffer space available." - }, - { - WSAENOMORE, "WSAENOMORE", "No more data available." - }, - { - WSAENOPROTOOPT, "WSAENOPROTOOPT", "Bad protocol option." - }, - { - WSAENOTCONN, "WSAENOTCONN", "Socket is not connected." - }, - { - WSAENOTEMPTY, "WSAENOTEMPTY", "Directory is not empty." - }, - { - WSAENOTSOCK, "WSAENOTSOCK", "Socket operation on nonsocket." - }, - { - WSAEOPNOTSUPP, "WSAEOPNOTSUPP", "Operation not supported." - }, - { - WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT", "Protocol family not supported." - }, - { - WSAEPROCLIM, "WSAEPROCLIM", "Too many processes." - }, - { - WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT", "Protocol not supported." - }, - { - WSAEPROTOTYPE, "WSAEPROTOTYPE", "Protocol wrong type for socket." - }, - { - WSAEPROVIDERFAILEDINIT, "WSAEPROVIDERFAILEDINIT", "Unable to initialise a service provider." - }, - { - WSAEREFUSED, "WSAEREFUSED", "Refused." - }, - { - WSAEREMOTE, "WSAEREMOTE", "Too many levels of remote in path." - }, - { - WSAESHUTDOWN, "WSAESHUTDOWN", "Cannot send after socket shutdown." - }, - { - WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT", "Socket type not supported." - }, - { - WSAESTALE, "WSAESTALE", "Stale NFS file handle." - }, - { - WSAETIMEDOUT, "WSAETIMEDOUT", "Connection timed out." - }, - { - WSAETOOMANYREFS, "WSAETOOMANYREFS", "Too many references." - }, - { - WSAEUSERS, "WSAEUSERS", "Too many users." - }, - { - WSAEWOULDBLOCK, "WSAEWOULDBLOCK", "Resource temporarily unavailable." - }, - { - WSANOTINITIALISED, "WSANOTINITIALISED", "Successful WSAStartup not yet performed." - }, - { - WSASERVICE_NOT_FOUND, "WSASERVICE_NOT_FOUND", "Service not found." - }, - { - WSASYSCALLFAILURE, "WSASYSCALLFAILURE", "System call failure." - }, - { - WSASYSNOTREADY, "WSASYSNOTREADY", "Network subsystem is unavailable." - }, - { - WSATYPE_NOT_FOUND, "WSATYPE_NOT_FOUND", "Class type not found." - }, - { - WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED", "Winsock.dll version out of range." - }, - { - WSAEDISCON, "WSAEDISCON", "Graceful shutdown in progress." - } -}; - -/* - * wsastrerror() - description of WSAGetLastError() - */ -const char * -wsastrerror(int err) -{ - static char xwsaerror_buf[BUFSIZ]; - int i, errind = -1; - - if (err == 0) - return "(0) No error."; - for (i = 0; i < sizeof(_wsaerrtext) / sizeof(struct _wsaerrtext); i++) { - if (_wsaerrtext[i].err != err) - continue; - errind = i; - break; - } - if (errind == -1) - snprintf(xwsaerror_buf, BUFSIZ, "Unknown"); - else - snprintf(xwsaerror_buf, BUFSIZ, "%s, %s", _wsaerrtext[errind].errconst, _wsaerrtext[errind].errdesc); - return xwsaerror_buf; -} - struct passwd * getpwnam(char *unused) { static struct passwd pwd = {NULL, NULL, 100, 100, NULL, NULL, NULL}; @@ -502,21 +296,6 @@ getgrnam(char *unused) { return &grp; } -/* - * WIN32_strerror with argument for late notification */ - -const char * -WIN32_strerror(int err) -{ - static char xbstrerror_buf[BUFSIZ]; - - if (err < 0 || err >= sys_nerr) - strncpy(xbstrerror_buf, wsastrerror(err), BUFSIZ); - else - strncpy(xbstrerror_buf, strerror(err), BUFSIZ); - return xbstrerror_buf; -} - #if defined(__MINGW32__) /* MinGW environment */ int _free_osfhnd(int filehandle) diff --git a/compat/xstrerror.cc b/compat/xstrerror.cc index 0cf722d1e3..eae455adcd 100644 --- a/compat/xstrerror.cc +++ b/compat/xstrerror.cc @@ -5,10 +5,95 @@ #include #endif +#if _SQUID_WINDOWS_ +static struct _wsaerrtext { + int err; + const char *errconst; + const char *errdesc; +} _wsaerrtext[] = { + + { WSA_E_CANCELLED, "WSA_E_CANCELLED", "Lookup cancelled." }, + { WSA_E_NO_MORE, "WSA_E_NO_MORE", "No more data available." }, + { WSAEACCES, "WSAEACCES", "Permission denied." }, + { WSAEADDRINUSE, "WSAEADDRINUSE", "Address already in use." }, + { WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL", "Cannot assign requested address." }, + { WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT", "Address family not supported by protocol family." }, + { WSAEALREADY, "WSAEALREADY", "Operation already in progress." }, + { WSAEBADF, "WSAEBADF", "Bad file number." }, + { WSAECANCELLED, "WSAECANCELLED", "Operation cancelled." }, + { WSAECONNABORTED, "WSAECONNABORTED", "Software caused connection abort." }, + { WSAECONNREFUSED, "WSAECONNREFUSED", "Connection refused." }, + { WSAECONNRESET, "WSAECONNRESET", "Connection reset by peer." }, + { WSAEDESTADDRREQ, "WSAEDESTADDRREQ", "Destination address required." }, + { WSAEDQUOT, "WSAEDQUOT", "Disk quota exceeded." }, + { WSAEFAULT, "WSAEFAULT", "Bad address." }, + { WSAEHOSTDOWN, "WSAEHOSTDOWN", "Host is down." }, + { WSAEHOSTUNREACH, "WSAEHOSTUNREACH", "No route to host." }, + { WSAEINPROGRESS, "WSAEINPROGRESS", "Operation now in progress." }, + { WSAEINTR, "WSAEINTR", "Interrupted function call." }, + { WSAEINVAL, "WSAEINVAL", "Invalid argument." }, + { WSAEINVALIDPROCTABLE, "WSAEINVALIDPROCTABLE", "Invalid procedure table from service provider." }, + { WSAEINVALIDPROVIDER, "WSAEINVALIDPROVIDER", "Invalid service provider version number." }, + { WSAEISCONN, "WSAEISCONN", "Socket is already connected." }, + { WSAELOOP, "WSAELOOP", "Too many levels of symbolic links." }, + { WSAEMFILE, "WSAEMFILE", "Too many open files." }, + { WSAEMSGSIZE, "WSAEMSGSIZE", "Message too long." }, + { WSAENAMETOOLONG, "WSAENAMETOOLONG", "File name is too long." }, + { WSAENETDOWN, "WSAENETDOWN", "Network is down." }, + { WSAENETRESET, "WSAENETRESET", "Network dropped connection on reset." }, + { WSAENETUNREACH, "WSAENETUNREACH", "Network is unreachable." }, + { WSAENOBUFS, "WSAENOBUFS", "No buffer space available." }, + { WSAENOMORE, "WSAENOMORE", "No more data available." }, + { WSAENOPROTOOPT, "WSAENOPROTOOPT", "Bad protocol option." }, + { WSAENOTCONN, "WSAENOTCONN", "Socket is not connected." }, + { WSAENOTEMPTY, "WSAENOTEMPTY", "Directory is not empty." }, + { WSAENOTSOCK, "WSAENOTSOCK", "Socket operation on nonsocket." }, + { WSAEOPNOTSUPP, "WSAEOPNOTSUPP", "Operation not supported." }, + { WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT", "Protocol family not supported." }, + { WSAEPROCLIM, "WSAEPROCLIM", "Too many processes." }, + { WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT", "Protocol not supported." }, + { WSAEPROTOTYPE, "WSAEPROTOTYPE", "Protocol wrong type for socket." }, + { WSAEPROVIDERFAILEDINIT, "WSAEPROVIDERFAILEDINIT", "Unable to initialise a service provider." }, + { WSAEREFUSED, "WSAEREFUSED", "Refused." }, + { WSAEREMOTE, "WSAEREMOTE", "Too many levels of remote in path." }, + { WSAESHUTDOWN, "WSAESHUTDOWN", "Cannot send after socket shutdown." }, + { WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT", "Socket type not supported." }, + { WSAESTALE, "WSAESTALE", "Stale NFS file handle." }, + { WSAETIMEDOUT, "WSAETIMEDOUT", "Connection timed out." }, + { WSAETOOMANYREFS, "WSAETOOMANYREFS", "Too many references." }, + { WSAEUSERS, "WSAEUSERS", "Too many users." }, + { WSAEWOULDBLOCK, "WSAEWOULDBLOCK", "Resource temporarily unavailable." }, + { WSANOTINITIALISED, "WSANOTINITIALISED", "Successful WSAStartup not yet performed." }, + { WSASERVICE_NOT_FOUND, "WSASERVICE_NOT_FOUND", "Service not found." }, + { WSASYSCALLFAILURE, "WSASYSCALLFAILURE", "System call failure." }, + { WSASYSNOTREADY, "WSASYSNOTREADY", "Network subsystem is unavailable." }, + { WSATYPE_NOT_FOUND, "WSATYPE_NOT_FOUND", "Class type not found." }, + { WSAVERNOTSUPPORTED, "WSAVERNOTSUPPORTED", "Winsock.dll version out of range." }, + { WSAEDISCON, "WSAEDISCON", "Graceful shutdown in progress." } +}; +#endif + const char * xstrerr(int error) { static char xstrerror_buf[BUFSIZ]; + + if (error == 0) + return "(0) No error."; + +#if _SQUID_WINDOWS_ + // Description of WSAGetLastError() + if (error < 0 || error >= sys_nerr) { + for (size_t i = 0;i < sizeof(_wsaerrtext) / sizeof(struct _wsaerrtext); i++) { + if (_wsaerrtext[i].err == error) { + // small optimization, save using a temporary buffer and two copies... + snprintf(xstrerror_buf, BUFSIZ, "(%d) %s, %s", error, _wsaerrtext[i].errconst, _wsaerrtext[i].errdesc); + return xstrerror_buf; + } + } + } +#endif + const char *errmsg = strerror(error); if (!errmsg || !*errmsg) diff --git a/compat/xstrerror.h b/compat/xstrerror.h index 8a55f166cb..69fa439e88 100644 --- a/compat/xstrerror.h +++ b/compat/xstrerror.h @@ -10,6 +10,7 @@ /** Provide the textual display of a system error number. * A string is always returned. + * On MS Windows the native Win32 errors are also translated. */ extern const char * xstrerr(int error); diff --git a/include/util.h b/include/util.h index d797d31b3b..18e62e7d40 100644 --- a/include/util.h +++ b/include/util.h @@ -124,7 +124,6 @@ SQUIDCEXTERN int gettimeofday(struct timeval * ,void *); SQUIDCEXTERN int kill(pid_t, int); SQUIDCEXTERN int statfs(const char *, struct statfs *); SQUIDCEXTERN int truncate(const char *, off_t); -SQUIDCEXTERN const char * wsastrerror(int); SQUIDCEXTERN struct passwd *getpwnam(char *); SQUIDCEXTERN struct group *getgrnam(char *); SQUIDCEXTERN uid_t geteuid(void); @@ -135,7 +134,6 @@ SQUIDCEXTERN gid_t getgid(void); SQUIDCEXTERN gid_t getegid(void); SQUIDCEXTERN int setgid(gid_t); SQUIDCEXTERN int setegid(gid_t); -SQUIDCEXTERN const char *WIN32_strerror(int); SQUIDCEXTERN void WIN32_maperror(unsigned long); #endif #endif /* SQUID_UTIL_H */ diff --git a/src/ipc_win32.cc b/src/ipc_win32.cc index c16bee9470..4665d7ffa7 100644 --- a/src/ipc_win32.cc +++ b/src/ipc_win32.cc @@ -576,8 +576,7 @@ ipc_thread_1(void *in_params) hProcess = pi.hProcess; } else { pid = -1; - WIN32_maperror(GetLastError()); - x = errno; + x = GetLastError(); } dup2(t1, 0);