Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* strerror.c
|
2018-09-26 18:35:57 +02:00
|
|
|
* Replacements for standard strerror() and strerror_r() functions
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
*
|
2024-01-04 02:49:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
1997-03-19 03:37:42 +01:00
|
|
|
*
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
* IDENTIFICATION
|
|
|
|
* src/port/strerror.c
|
1997-03-19 03:37:42 +01:00
|
|
|
*
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
*-------------------------------------------------------------------------
|
1997-03-19 03:37:42 +01:00
|
|
|
*/
|
2005-07-28 06:03:14 +02:00
|
|
|
#include "c.h"
|
|
|
|
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
/*
|
2018-09-26 18:35:57 +02:00
|
|
|
* Within this file, "strerror" means the platform's function not pg_strerror,
|
|
|
|
* and likewise for "strerror_r"
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
*/
|
|
|
|
#undef strerror
|
2018-09-26 18:35:57 +02:00
|
|
|
#undef strerror_r
|
1997-03-19 03:37:42 +01:00
|
|
|
|
2018-09-26 18:35:57 +02:00
|
|
|
static char *gnuish_strerror_r(int errnum, char *buf, size_t buflen);
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
static char *get_errno_symbol(int errnum);
|
|
|
|
#ifdef WIN32
|
2018-09-26 18:35:57 +02:00
|
|
|
static char *win32_socket_strerror(int errnum, char *buf, size_t buflen);
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
#endif
|
1997-03-19 03:37:42 +01:00
|
|
|
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A slightly cleaned-up version of strerror()
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
pg_strerror(int errnum)
|
1997-03-19 03:37:42 +01:00
|
|
|
{
|
2018-09-26 18:35:57 +02:00
|
|
|
static char errorstr_buf[PG_STRERROR_R_BUFLEN];
|
|
|
|
|
|
|
|
return pg_strerror_r(errnum, errorstr_buf, sizeof(errorstr_buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A slightly cleaned-up version of strerror_r()
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
pg_strerror_r(int errnum, char *buf, size_t buflen)
|
|
|
|
{
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
/* If it's a Windows Winsock error, that needs special handling */
|
|
|
|
#ifdef WIN32
|
|
|
|
/* Winsock error code range, per WinError.h */
|
|
|
|
if (errnum >= 10000 && errnum <= 11999)
|
2018-09-26 18:35:57 +02:00
|
|
|
return win32_socket_strerror(errnum, buf, buflen);
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
#endif
|
|
|
|
|
2018-09-26 18:35:57 +02:00
|
|
|
/* Try the platform's strerror_r(), or maybe just strerror() */
|
|
|
|
str = gnuish_strerror_r(errnum, buf, buflen);
|
1997-03-19 03:37:42 +01:00
|
|
|
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
/*
|
|
|
|
* Some strerror()s return an empty string for out-of-range errno. This
|
|
|
|
* is ANSI C spec compliant, but not exactly useful. Also, we may get
|
|
|
|
* back strings of question marks if libc cannot transcode the message to
|
|
|
|
* the codeset specified by LC_CTYPE. If we get nothing useful, first try
|
|
|
|
* get_errno_symbol(), and if that fails, print the numeric errno.
|
|
|
|
*/
|
|
|
|
if (str == NULL || *str == '\0' || *str == '?')
|
|
|
|
str = get_errno_symbol(errnum);
|
|
|
|
|
|
|
|
if (str == NULL)
|
1997-03-19 03:37:42 +01:00
|
|
|
{
|
2018-09-26 18:35:57 +02:00
|
|
|
snprintf(buf, buflen, _("operating system error %d"), errnum);
|
|
|
|
str = buf;
|
1997-03-19 03:37:42 +01:00
|
|
|
}
|
|
|
|
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
return str;
|
1997-03-19 03:37:42 +01:00
|
|
|
}
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
|
2018-09-26 18:35:57 +02:00
|
|
|
/*
|
|
|
|
* Simple wrapper to emulate GNU strerror_r if what the platform provides is
|
|
|
|
* POSIX. Also, if platform lacks strerror_r altogether, fall back to plain
|
|
|
|
* strerror; it might not be very thread-safe, but tough luck.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
gnuish_strerror_r(int errnum, char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_STRERROR_R
|
|
|
|
#ifdef STRERROR_R_INT
|
|
|
|
/* POSIX API */
|
|
|
|
if (strerror_r(errnum, buf, buflen) == 0)
|
|
|
|
return buf;
|
|
|
|
return NULL; /* let caller deal with failure */
|
|
|
|
#else
|
|
|
|
/* GNU API */
|
|
|
|
return strerror_r(errnum, buf, buflen);
|
|
|
|
#endif
|
|
|
|
#else /* !HAVE_STRERROR_R */
|
|
|
|
char *sbuf = strerror(errnum);
|
|
|
|
|
|
|
|
if (sbuf == NULL) /* can this still happen anywhere? */
|
|
|
|
return NULL;
|
|
|
|
/* To minimize thread-unsafety hazard, copy into caller's buffer */
|
|
|
|
strlcpy(buf, sbuf, buflen);
|
|
|
|
return buf;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
/*
|
|
|
|
* Returns a symbol (e.g. "ENOENT") for an errno code.
|
|
|
|
* Returns NULL if the code is unrecognized.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_errno_symbol(int errnum)
|
|
|
|
{
|
|
|
|
switch (errnum)
|
|
|
|
{
|
|
|
|
case E2BIG:
|
|
|
|
return "E2BIG";
|
|
|
|
case EACCES:
|
|
|
|
return "EACCES";
|
|
|
|
case EADDRINUSE:
|
|
|
|
return "EADDRINUSE";
|
|
|
|
case EADDRNOTAVAIL:
|
|
|
|
return "EADDRNOTAVAIL";
|
|
|
|
case EAFNOSUPPORT:
|
|
|
|
return "EAFNOSUPPORT";
|
|
|
|
#ifdef EAGAIN
|
|
|
|
case EAGAIN:
|
|
|
|
return "EAGAIN";
|
|
|
|
#endif
|
|
|
|
#ifdef EALREADY
|
|
|
|
case EALREADY:
|
|
|
|
return "EALREADY";
|
|
|
|
#endif
|
|
|
|
case EBADF:
|
|
|
|
return "EBADF";
|
|
|
|
#ifdef EBADMSG
|
|
|
|
case EBADMSG:
|
|
|
|
return "EBADMSG";
|
|
|
|
#endif
|
|
|
|
case EBUSY:
|
|
|
|
return "EBUSY";
|
|
|
|
case ECHILD:
|
|
|
|
return "ECHILD";
|
|
|
|
case ECONNABORTED:
|
|
|
|
return "ECONNABORTED";
|
|
|
|
case ECONNREFUSED:
|
|
|
|
return "ECONNREFUSED";
|
|
|
|
case ECONNRESET:
|
|
|
|
return "ECONNRESET";
|
|
|
|
case EDEADLK:
|
|
|
|
return "EDEADLK";
|
|
|
|
case EDOM:
|
|
|
|
return "EDOM";
|
|
|
|
case EEXIST:
|
|
|
|
return "EEXIST";
|
|
|
|
case EFAULT:
|
|
|
|
return "EFAULT";
|
|
|
|
case EFBIG:
|
|
|
|
return "EFBIG";
|
Recognize network-failure errnos as indicating hard connection loss.
Up to now, only ECONNRESET (and EPIPE, in most but not quite all places)
received special treatment in our error handling logic. This patch
changes things so that related error codes such as ECONNABORTED are
also recognized as indicating that the connection's dead and unlikely
to come back.
We continue to think, however, that only ECONNRESET and EPIPE should be
reported as probable server crashes; the other cases indicate network
connectivity problems but prove little about the server's state. Thus,
there's no change in the error message texts that are output for such
cases. The key practical effect is that errcode_for_socket_access()
will report ERRCODE_CONNECTION_FAILURE rather than
ERRCODE_INTERNAL_ERROR for a network failure. It's expected that this
will fix buildfarm member lorikeet's failures since commit 32a9c0bdf,
as that seems to be due to not treating ECONNABORTED equivalently to
ECONNRESET.
The set of errnos treated this way now includes ECONNABORTED, EHOSTDOWN,
EHOSTUNREACH, ENETDOWN, ENETRESET, and ENETUNREACH. Several of these
were second-class citizens in terms of their handling in places like
get_errno_symbol(), so upgrade the infrastructure where necessary.
As committed, this patch assumes that all these symbols are defined
everywhere. POSIX specifies all of them except EHOSTDOWN, but that
seems to exist on all platforms of interest; we'll see what the
buildfarm says about that.
Probably this should be back-patched, but let's see what the buildfarm
thinks of it first.
Fujii Masao and Tom Lane
Discussion: https://postgr.es/m/2621622.1602184554@sss.pgh.pa.us
2020-10-10 19:28:12 +02:00
|
|
|
case EHOSTDOWN:
|
|
|
|
return "EHOSTDOWN";
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
case EHOSTUNREACH:
|
|
|
|
return "EHOSTUNREACH";
|
|
|
|
case EIDRM:
|
|
|
|
return "EIDRM";
|
|
|
|
case EINPROGRESS:
|
|
|
|
return "EINPROGRESS";
|
|
|
|
case EINTR:
|
|
|
|
return "EINTR";
|
|
|
|
case EINVAL:
|
|
|
|
return "EINVAL";
|
|
|
|
case EIO:
|
|
|
|
return "EIO";
|
|
|
|
case EISCONN:
|
|
|
|
return "EISCONN";
|
|
|
|
case EISDIR:
|
|
|
|
return "EISDIR";
|
|
|
|
#ifdef ELOOP
|
|
|
|
case ELOOP:
|
|
|
|
return "ELOOP";
|
|
|
|
#endif
|
|
|
|
case EMFILE:
|
|
|
|
return "EMFILE";
|
|
|
|
case EMLINK:
|
|
|
|
return "EMLINK";
|
|
|
|
case EMSGSIZE:
|
|
|
|
return "EMSGSIZE";
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
return "ENAMETOOLONG";
|
Recognize network-failure errnos as indicating hard connection loss.
Up to now, only ECONNRESET (and EPIPE, in most but not quite all places)
received special treatment in our error handling logic. This patch
changes things so that related error codes such as ECONNABORTED are
also recognized as indicating that the connection's dead and unlikely
to come back.
We continue to think, however, that only ECONNRESET and EPIPE should be
reported as probable server crashes; the other cases indicate network
connectivity problems but prove little about the server's state. Thus,
there's no change in the error message texts that are output for such
cases. The key practical effect is that errcode_for_socket_access()
will report ERRCODE_CONNECTION_FAILURE rather than
ERRCODE_INTERNAL_ERROR for a network failure. It's expected that this
will fix buildfarm member lorikeet's failures since commit 32a9c0bdf,
as that seems to be due to not treating ECONNABORTED equivalently to
ECONNRESET.
The set of errnos treated this way now includes ECONNABORTED, EHOSTDOWN,
EHOSTUNREACH, ENETDOWN, ENETRESET, and ENETUNREACH. Several of these
were second-class citizens in terms of their handling in places like
get_errno_symbol(), so upgrade the infrastructure where necessary.
As committed, this patch assumes that all these symbols are defined
everywhere. POSIX specifies all of them except EHOSTDOWN, but that
seems to exist on all platforms of interest; we'll see what the
buildfarm says about that.
Probably this should be back-patched, but let's see what the buildfarm
thinks of it first.
Fujii Masao and Tom Lane
Discussion: https://postgr.es/m/2621622.1602184554@sss.pgh.pa.us
2020-10-10 19:28:12 +02:00
|
|
|
case ENETDOWN:
|
|
|
|
return "ENETDOWN";
|
|
|
|
case ENETRESET:
|
|
|
|
return "ENETRESET";
|
|
|
|
case ENETUNREACH:
|
|
|
|
return "ENETUNREACH";
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
case ENFILE:
|
|
|
|
return "ENFILE";
|
|
|
|
case ENOBUFS:
|
|
|
|
return "ENOBUFS";
|
|
|
|
case ENODEV:
|
|
|
|
return "ENODEV";
|
|
|
|
case ENOENT:
|
|
|
|
return "ENOENT";
|
|
|
|
case ENOEXEC:
|
|
|
|
return "ENOEXEC";
|
|
|
|
case ENOMEM:
|
|
|
|
return "ENOMEM";
|
|
|
|
case ENOSPC:
|
|
|
|
return "ENOSPC";
|
|
|
|
case ENOSYS:
|
|
|
|
return "ENOSYS";
|
|
|
|
case ENOTCONN:
|
|
|
|
return "ENOTCONN";
|
|
|
|
case ENOTDIR:
|
|
|
|
return "ENOTDIR";
|
|
|
|
case ENOTEMPTY:
|
|
|
|
return "ENOTEMPTY";
|
|
|
|
case ENOTSOCK:
|
|
|
|
return "ENOTSOCK";
|
|
|
|
#ifdef ENOTSUP
|
|
|
|
case ENOTSUP:
|
|
|
|
return "ENOTSUP";
|
|
|
|
#endif
|
|
|
|
case ENOTTY:
|
|
|
|
return "ENOTTY";
|
|
|
|
case ENXIO:
|
|
|
|
return "ENXIO";
|
|
|
|
#if defined(EOPNOTSUPP) && (!defined(ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
|
|
|
|
case EOPNOTSUPP:
|
|
|
|
return "EOPNOTSUPP";
|
|
|
|
#endif
|
|
|
|
#ifdef EOVERFLOW
|
|
|
|
case EOVERFLOW:
|
|
|
|
return "EOVERFLOW";
|
|
|
|
#endif
|
|
|
|
case EPERM:
|
|
|
|
return "EPERM";
|
|
|
|
case EPIPE:
|
|
|
|
return "EPIPE";
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
return "EPROTONOSUPPORT";
|
|
|
|
case ERANGE:
|
|
|
|
return "ERANGE";
|
|
|
|
#ifdef EROFS
|
|
|
|
case EROFS:
|
|
|
|
return "EROFS";
|
|
|
|
#endif
|
|
|
|
case ESRCH:
|
|
|
|
return "ESRCH";
|
|
|
|
case ETIMEDOUT:
|
|
|
|
return "ETIMEDOUT";
|
|
|
|
#ifdef ETXTBSY
|
|
|
|
case ETXTBSY:
|
|
|
|
return "ETXTBSY";
|
|
|
|
#endif
|
|
|
|
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
return "EWOULDBLOCK";
|
|
|
|
#endif
|
|
|
|
case EXDEV:
|
|
|
|
return "EXDEV";
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Windows' strerror() doesn't know the Winsock codes, so handle them this way
|
|
|
|
*/
|
|
|
|
static char *
|
2018-09-26 18:35:57 +02:00
|
|
|
win32_socket_strerror(int errnum, char *buf, size_t buflen)
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
{
|
|
|
|
static HANDLE handleDLL = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
if (handleDLL == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
handleDLL = LoadLibraryEx("netmsg.dll", NULL,
|
|
|
|
DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
|
|
|
|
if (handleDLL == NULL)
|
|
|
|
{
|
2018-09-26 18:35:57 +02:00
|
|
|
snprintf(buf, buflen,
|
|
|
|
"winsock error %d (could not load netmsg.dll to translate: error code %lu)",
|
|
|
|
errnum, GetLastError());
|
|
|
|
return buf;
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 18:35:57 +02:00
|
|
|
ZeroMemory(buf, buflen);
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
|
|
FORMAT_MESSAGE_FROM_HMODULE,
|
|
|
|
handleDLL,
|
|
|
|
errnum,
|
|
|
|
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
|
2018-09-26 18:35:57 +02:00
|
|
|
buf,
|
|
|
|
buflen - 1,
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
NULL) == 0)
|
|
|
|
{
|
|
|
|
/* Failed to get id */
|
2018-09-26 18:35:57 +02:00
|
|
|
snprintf(buf, buflen, "unrecognized winsock error %d", errnum);
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 18:35:57 +02:00
|
|
|
return buf;
|
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assorted
possible failures or deficiencies of the platform's strerror. On Windows,
it also knows how to translate Winsock error codes, which the native
strerror does not. Move all this code into src/port/strerror.c and
define strerror() as a macro that invokes it, so that both our frontend
and backend code will have all of this behavior.
I believe this constitutes an actual bug fix on Windows, since AFAICS
our frontend code did not report Winsock error codes properly before this.
However, the main point is to lay the groundwork for implementing %m
in src/port/snprintf.c: the behavior we want %m to have is this one,
not the native strerror's.
Note that this throws away the prior use of src/port/strerror.c,
which was to implement strerror() on platforms lacking it. That's
been dead code for nigh twenty years now, since strerror() was
already required by C89.
We should likewise cause strerror_r to use this behavior, but
I'll tackle that separately.
Patch by me, reviewed by Michael Paquier
Discussion: https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
2018-09-26 17:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WIN32 */
|