提交 26e9d4d4 编写于 作者: T Tom Lane

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
上级 a49ceda6
...@@ -15644,19 +15644,6 @@ esac ...@@ -15644,19 +15644,6 @@ esac
fi fi
ac_fn_c_check_func "$LINENO" "strerror" "ac_cv_func_strerror"
if test "x$ac_cv_func_strerror" = xyes; then :
$as_echo "#define HAVE_STRERROR 1" >>confdefs.h
else
case " $LIBOBJS " in
*" strerror.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS strerror.$ac_objext"
;;
esac
fi
ac_fn_c_check_func "$LINENO" "strlcat" "ac_cv_func_strlcat" ac_fn_c_check_func "$LINENO" "strlcat" "ac_cv_func_strlcat"
if test "x$ac_cv_func_strlcat" = xyes; then : if test "x$ac_cv_func_strlcat" = xyes; then :
$as_echo "#define HAVE_STRLCAT 1" >>confdefs.h $as_echo "#define HAVE_STRLCAT 1" >>confdefs.h
......
...@@ -1687,7 +1687,7 @@ else ...@@ -1687,7 +1687,7 @@ else
AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break]) AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break])
fi fi
AC_REPLACE_FUNCS([crypt dlopen fls getopt getrusage inet_aton mkdtemp random rint srandom strerror strlcat strlcpy strnlen]) AC_REPLACE_FUNCS([crypt dlopen fls getopt getrusage inet_aton mkdtemp random rint srandom strlcat strlcpy strnlen])
case $host_os in case $host_os in
......
...@@ -690,39 +690,3 @@ pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, c ...@@ -690,39 +690,3 @@ pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, c
memcpy(writefds, &outwritefds, sizeof(fd_set)); memcpy(writefds, &outwritefds, sizeof(fd_set));
return nummatches; return nummatches;
} }
/*
* Return win32 error string, since strerror can't
* handle winsock codes
*/
static char wserrbuf[256];
const char *
pgwin32_socket_strerror(int err)
{
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)
ereport(FATAL,
(errmsg_internal("could not load netmsg.dll: error code %lu", GetLastError())));
}
ZeroMemory(&wserrbuf, sizeof(wserrbuf));
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE,
handleDLL,
err,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
wserrbuf,
sizeof(wserrbuf) - 1,
NULL) == 0)
{
/* Failed to get id */
sprintf(wserrbuf, "unrecognized winsock error %d", err);
}
return wserrbuf;
}
...@@ -178,8 +178,6 @@ static void send_message_to_server_log(ErrorData *edata); ...@@ -178,8 +178,6 @@ static void send_message_to_server_log(ErrorData *edata);
static void write_pipe_chunks(char *data, int len, int dest); static void write_pipe_chunks(char *data, int len, int dest);
static void send_message_to_frontend(ErrorData *edata); static void send_message_to_frontend(ErrorData *edata);
static char *expand_fmt_string(const char *fmt, ErrorData *edata); static char *expand_fmt_string(const char *fmt, ErrorData *edata);
static const char *useful_strerror(int errnum);
static const char *get_errno_symbol(int errnum);
static const char *error_severity(int elevel); static const char *error_severity(int elevel);
static void append_with_tabs(StringInfo buf, const char *str); static void append_with_tabs(StringInfo buf, const char *str);
static bool is_log_level_output(int elevel, int log_min_level); static bool is_log_level_output(int elevel, int log_min_level);
...@@ -3360,7 +3358,7 @@ expand_fmt_string(const char *fmt, ErrorData *edata) ...@@ -3360,7 +3358,7 @@ expand_fmt_string(const char *fmt, ErrorData *edata)
*/ */
const char *cp2; const char *cp2;
cp2 = useful_strerror(edata->saved_errno); cp2 = strerror(edata->saved_errno);
for (; *cp2; cp2++) for (; *cp2; cp2++)
{ {
if (*cp2 == '%') if (*cp2 == '%')
...@@ -3383,219 +3381,6 @@ expand_fmt_string(const char *fmt, ErrorData *edata) ...@@ -3383,219 +3381,6 @@ expand_fmt_string(const char *fmt, ErrorData *edata)
} }
/*
* A slightly cleaned-up version of strerror()
*/
static const char *
useful_strerror(int errnum)
{
/* this buffer is only used if strerror() and get_errno_symbol() fail */
static char errorstr_buf[48];
const char *str;
#ifdef WIN32
/* Winsock error code range, per WinError.h */
if (errnum >= 10000 && errnum <= 11999)
return pgwin32_socket_strerror(errnum);
#endif
str = strerror(errnum);
/*
* 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)
{
snprintf(errorstr_buf, sizeof(errorstr_buf),
/*------
translator: This string will be truncated at 47
characters expanded. */
_("operating system error %d"), errnum);
str = errorstr_buf;
}
return str;
}
/*
* Returns a symbol (e.g. "ENOENT") for an errno code.
* Returns NULL if the code is unrecognized.
*/
static const char *
get_errno_symbol(int errnum)
{
switch (errnum)
{
case E2BIG:
return "E2BIG";
case EACCES:
return "EACCES";
#ifdef EADDRINUSE
case EADDRINUSE:
return "EADDRINUSE";
#endif
#ifdef EADDRNOTAVAIL
case EADDRNOTAVAIL:
return "EADDRNOTAVAIL";
#endif
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";
#ifdef ECONNABORTED
case ECONNABORTED:
return "ECONNABORTED";
#endif
case ECONNREFUSED:
return "ECONNREFUSED";
#ifdef ECONNRESET
case ECONNRESET:
return "ECONNRESET";
#endif
case EDEADLK:
return "EDEADLK";
case EDOM:
return "EDOM";
case EEXIST:
return "EEXIST";
case EFAULT:
return "EFAULT";
case EFBIG:
return "EFBIG";
#ifdef EHOSTUNREACH
case EHOSTUNREACH:
return "EHOSTUNREACH";
#endif
case EIDRM:
return "EIDRM";
case EINPROGRESS:
return "EINPROGRESS";
case EINTR:
return "EINTR";
case EINVAL:
return "EINVAL";
case EIO:
return "EIO";
#ifdef EISCONN
case EISCONN:
return "EISCONN";
#endif
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";
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";
#ifdef ENOTCONN
case ENOTCONN:
return "ENOTCONN";
#endif
case ENOTDIR:
return "ENOTDIR";
#if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
case ENOTEMPTY:
return "ENOTEMPTY";
#endif
#ifdef ENOTSOCK
case ENOTSOCK:
return "ENOTSOCK";
#endif
#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";
#ifdef ETIMEDOUT
case ETIMEDOUT:
return "ETIMEDOUT";
#endif
#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;
}
/* /*
* error_severity --- get string representing elevel * error_severity --- get string representing elevel
* *
......
...@@ -534,9 +534,6 @@ ...@@ -534,9 +534,6 @@
/* Define to 1 if you have the <stdlib.h> header file. */ /* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H #undef HAVE_STDLIB_H
/* Define to 1 if you have the `strerror' function. */
#undef HAVE_STRERROR
/* Define to 1 if you have the `strerror_r' function. */ /* Define to 1 if you have the `strerror_r' function. */
#undef HAVE_STRERROR_R #undef HAVE_STRERROR_R
......
...@@ -405,11 +405,6 @@ ...@@ -405,11 +405,6 @@
/* Define to 1 if you have the <stdlib.h> header file. */ /* Define to 1 if you have the <stdlib.h> header file. */
#define HAVE_STDLIB_H 1 #define HAVE_STDLIB_H 1
/* Define to 1 if you have the `strerror' function. */
#ifndef HAVE_STRERROR
#define HAVE_STRERROR 1
#endif
/* Define to 1 if you have the `strerror_r' function. */ /* Define to 1 if you have the `strerror_r' function. */
/* #undef HAVE_STRERROR_R */ /* #undef HAVE_STRERROR_R */
......
...@@ -189,6 +189,10 @@ extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2); ...@@ -189,6 +189,10 @@ extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
#endif #endif
#endif /* USE_REPL_SNPRINTF */ #endif /* USE_REPL_SNPRINTF */
/* Replace strerror() with our own, somewhat more robust wrapper */
extern char *pg_strerror(int errnum);
#define strerror pg_strerror
/* Portable prompt handling */ /* Portable prompt handling */
extern void simple_prompt(const char *prompt, char *destination, size_t destlen, extern void simple_prompt(const char *prompt, char *destination, size_t destlen,
bool echo); bool echo);
...@@ -355,7 +359,7 @@ extern int isinf(double x); ...@@ -355,7 +359,7 @@ extern int isinf(double x);
#undef isinf #undef isinf
#define isinf __builtin_isinf #define isinf __builtin_isinf
#endif /* __has_builtin(isinf) */ #endif /* __has_builtin(isinf) */
#endif /* __clang__ && !__cplusplus*/ #endif /* __clang__ && !__cplusplus */
#endif /* !HAVE_ISINF */ #endif /* !HAVE_ISINF */
#ifndef HAVE_MKDTEMP #ifndef HAVE_MKDTEMP
...@@ -403,7 +407,7 @@ extern void srandom(unsigned int seed); ...@@ -403,7 +407,7 @@ extern void srandom(unsigned int seed);
#ifndef HAVE_DLOPEN #ifndef HAVE_DLOPEN
extern void *dlopen(const char *file, int mode); extern void *dlopen(const char *file, int mode);
extern void *dlsym(void *handle, const char *symbol); extern void *dlsym(void *handle, const char *symbol);
extern int dlclose(void *handle); extern int dlclose(void *handle);
extern char *dlerror(void); extern char *dlerror(void);
#endif #endif
......
...@@ -322,8 +322,8 @@ extern int pgwin32_safestat(const char *path, struct stat *buf); ...@@ -322,8 +322,8 @@ extern int pgwin32_safestat(const char *path, struct stat *buf);
* Supplement to <errno.h>. * Supplement to <errno.h>.
* *
* We redefine network-related Berkeley error symbols as the corresponding WSA * We redefine network-related Berkeley error symbols as the corresponding WSA
* constants. This allows elog.c to recognize them as being in the Winsock * constants. This allows strerror.c to recognize them as being in the Winsock
* error code range and pass them off to pgwin32_socket_strerror(), since * error code range and pass them off to win32_socket_strerror(), since
* Windows' version of plain strerror() won't cope. Note that this will break * Windows' version of plain strerror() won't cope. Note that this will break
* if these names are used for anything else besides Windows Sockets errors. * if these names are used for anything else besides Windows Sockets errors.
* See TranslateSocketError() when changing this list. * See TranslateSocketError() when changing this list.
...@@ -456,8 +456,6 @@ int pgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen); ...@@ -456,8 +456,6 @@ int pgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen);
int pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout); int pgwin32_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);
int pgwin32_recv(SOCKET s, char *buf, int len, int flags); int pgwin32_recv(SOCKET s, char *buf, int len, int flags);
int pgwin32_send(SOCKET s, const void *buf, int len, int flags); int pgwin32_send(SOCKET s, const void *buf, int len, int flags);
const char *pgwin32_socket_strerror(int err);
int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout); int pgwin32_waitforsinglesocket(SOCKET s, int what, int timeout);
extern int pgwin32_noblock; extern int pgwin32_noblock;
......
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
/blibecpg_compatdll.def /blibecpg_compatdll.def
/exports.list /exports.list
/snprintf.c /snprintf.c
/strerror.c
/strnlen.c /strnlen.c
...@@ -31,7 +31,7 @@ SHLIB_EXPORTS = exports.txt ...@@ -31,7 +31,7 @@ SHLIB_EXPORTS = exports.txt
# Need to recompile any libpgport object files # Need to recompile any libpgport object files
LIBS := $(filter-out -lpgport, $(LIBS)) LIBS := $(filter-out -lpgport, $(LIBS))
OBJS= informix.o $(filter snprintf.o strnlen.o, $(LIBOBJS)) $(WIN32RES) OBJS= informix.o strerror.o $(filter snprintf.o strnlen.o, $(LIBOBJS)) $(WIN32RES)
PKG_CONFIG_REQUIRES_PRIVATE = libecpg libpgtypes PKG_CONFIG_REQUIRES_PRIVATE = libecpg libpgtypes
...@@ -48,7 +48,7 @@ submake-pgtypeslib: ...@@ -48,7 +48,7 @@ submake-pgtypeslib:
# Shared library stuff # Shared library stuff
include $(top_srcdir)/src/Makefile.shlib include $(top_srcdir)/src/Makefile.shlib
snprintf.c strnlen.c: % : $(top_srcdir)/src/port/% snprintf.c strerror.c strnlen.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< . rm -f $@ && $(LN_S) $< .
install: all installdirs install-lib install: all installdirs install-lib
...@@ -58,6 +58,6 @@ installdirs: installdirs-lib ...@@ -58,6 +58,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib uninstall: uninstall-lib
clean distclean: clean-lib clean distclean: clean-lib
rm -f $(OBJS) snprintf.c strnlen.c rm -f $(OBJS) snprintf.c strerror.c strnlen.c
maintainer-clean: distclean maintainer-clean-lib maintainer-clean: distclean maintainer-clean-lib
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
/path.c /path.c
/pgstrcasecmp.c /pgstrcasecmp.c
/snprintf.c /snprintf.c
/strerror.c
/strlcpy.c /strlcpy.c
/strnlen.c /strnlen.c
/thread.c /thread.c
......
...@@ -26,7 +26,7 @@ override CFLAGS += $(PTHREAD_CFLAGS) ...@@ -26,7 +26,7 @@ override CFLAGS += $(PTHREAD_CFLAGS)
LIBS := $(filter-out -lpgport, $(LIBS)) LIBS := $(filter-out -lpgport, $(LIBS))
OBJS= execute.o typename.o descriptor.o sqlda.o data.o error.o prepare.o memory.o \ OBJS= execute.o typename.o descriptor.o sqlda.o data.o error.o prepare.o memory.o \
connect.o misc.o path.o pgstrcasecmp.o \ connect.o misc.o path.o pgstrcasecmp.o strerror.o \
$(filter snprintf.o strlcpy.o strnlen.o win32setlocale.o isinf.o, $(LIBOBJS)) \ $(filter snprintf.o strlcpy.o strnlen.o win32setlocale.o isinf.o, $(LIBOBJS)) \
$(WIN32RES) $(WIN32RES)
...@@ -57,7 +57,7 @@ include $(top_srcdir)/src/Makefile.shlib ...@@ -57,7 +57,7 @@ include $(top_srcdir)/src/Makefile.shlib
# necessarily use the same object files as the backend uses. Instead, # necessarily use the same object files as the backend uses. Instead,
# symlink the source files in here and build our own object file. # symlink the source files in here and build our own object file.
path.c pgstrcasecmp.c snprintf.c strlcpy.c strnlen.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/% path.c pgstrcasecmp.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32setlocale.c isinf.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< . rm -f $@ && $(LN_S) $< .
misc.o: misc.c $(top_builddir)/src/port/pg_config_paths.h misc.o: misc.c $(top_builddir)/src/port/pg_config_paths.h
...@@ -74,6 +74,6 @@ uninstall: uninstall-lib ...@@ -74,6 +74,6 @@ uninstall: uninstall-lib
clean distclean: clean-lib clean distclean: clean-lib
rm -f $(OBJS) rm -f $(OBJS)
rm -f path.c pgstrcasecmp.c snprintf.c strlcpy.c strnlen.c thread.c win32setlocale.c isinf.c rm -f path.c pgstrcasecmp.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32setlocale.c isinf.c
maintainer-clean: distclean maintainer-clean-lib maintainer-clean: distclean maintainer-clean-lib
...@@ -4,5 +4,6 @@ ...@@ -4,5 +4,6 @@
/pgstrcasecmp.c /pgstrcasecmp.c
/rint.c /rint.c
/snprintf.c /snprintf.c
/strerror.c
/string.c /string.c
/strnlen.c /strnlen.c
...@@ -30,7 +30,7 @@ SHLIB_LINK += $(filter -lm, $(LIBS)) ...@@ -30,7 +30,7 @@ SHLIB_LINK += $(filter -lm, $(LIBS))
SHLIB_EXPORTS = exports.txt SHLIB_EXPORTS = exports.txt
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \ OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
pgstrcasecmp.o \ pgstrcasecmp.o strerror.o \
$(filter rint.o snprintf.o strnlen.o, $(LIBOBJS)) \ $(filter rint.o snprintf.o strnlen.o, $(LIBOBJS)) \
string.o \ string.o \
$(WIN32RES) $(WIN32RES)
...@@ -45,7 +45,7 @@ include $(top_srcdir)/src/Makefile.shlib ...@@ -45,7 +45,7 @@ include $(top_srcdir)/src/Makefile.shlib
# necessarily use the same object files as the backend uses. Instead, # necessarily use the same object files as the backend uses. Instead,
# symlink the source files in here and build our own object file. # symlink the source files in here and build our own object file.
pgstrcasecmp.c rint.c snprintf.c strnlen.c: % : $(top_srcdir)/src/port/% pgstrcasecmp.c rint.c snprintf.c strerror.c strnlen.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< . rm -f $@ && $(LN_S) $< .
string.c: % : $(top_srcdir)/src/common/% string.c: % : $(top_srcdir)/src/common/%
...@@ -58,6 +58,6 @@ installdirs: installdirs-lib ...@@ -58,6 +58,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib uninstall: uninstall-lib
clean distclean: clean-lib clean distclean: clean-lib
rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c string.c rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strerror.c strnlen.c string.c
maintainer-clean: distclean maintainer-clean-lib maintainer-clean: distclean maintainer-clean-lib
...@@ -36,9 +36,9 @@ OBJS= fe-auth.o fe-auth-scram.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-l ...@@ -36,9 +36,9 @@ OBJS= fe-auth.o fe-auth-scram.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-l
libpq-events.o libpq-events.o
# libpgport C files we always use # libpgport C files we always use
OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \ OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \
thread.o strerror.o thread.o
# libpgport C files that are needed if identified by configure # libpgport C files that are needed if identified by configure
OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strerror.o strlcpy.o strnlen.o win32error.o win32setlocale.o, $(LIBOBJS)) OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strlcpy.o strnlen.o win32error.o win32setlocale.o, $(LIBOBJS))
ifeq ($(enable_strong_random), yes) ifeq ($(enable_strong_random), yes)
OBJS += pg_strong_random.o OBJS += pg_strong_random.o
......
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
*/ */
#undef _POSIX_C_SOURCE #undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE #undef _XOPEN_SOURCE
#undef HAVE_STRERROR
#undef HAVE_TZNAME #undef HAVE_TZNAME
/* /*
......
...@@ -33,7 +33,7 @@ LIBS += $(PTHREAD_LIBS) ...@@ -33,7 +33,7 @@ LIBS += $(PTHREAD_LIBS)
OBJS = $(LIBOBJS) $(PG_CRC32C_OBJS) chklocale.o erand48.o inet_net_ntop.o \ OBJS = $(LIBOBJS) $(PG_CRC32C_OBJS) chklocale.o erand48.o inet_net_ntop.o \
noblock.o path.o pgcheckdir.o pgmkdirp.o pgsleep.o \ noblock.o path.o pgcheckdir.o pgmkdirp.o pgsleep.o \
pgstrcasecmp.o pqsignal.o \ pgstrcasecmp.o pqsignal.o \
qsort.o qsort_arg.o quotes.o sprompt.o tar.o thread.o qsort.o qsort_arg.o quotes.o sprompt.o strerror.o tar.o thread.o
ifeq ($(enable_strong_random), yes) ifeq ($(enable_strong_random), yes)
OBJS += pg_strong_random.o OBJS += pg_strong_random.o
......
/* src/port/strerror.c */ /*-------------------------------------------------------------------------
*
/* * strerror.c
* strerror - map error number to descriptive string * Replacement for standard strerror() function
*
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* *
* This version is obviously somewhat Unix-specific. * IDENTIFICATION
* src/port/strerror.c
* *
* based on code by Henry Spencer *-------------------------------------------------------------------------
* modified for ANSI by D'Arcy J.M. Cain
*/ */
#include "c.h" #include "c.h"
/*
* Within this file, "strerror" means the platform's function not pg_strerror
*/
#undef strerror
extern const char *const sys_errlist[]; static char *get_errno_symbol(int errnum);
extern int sys_nerr; #ifdef WIN32
static char *win32_socket_strerror(int errnum);
#endif
const char *
strerror(int errnum) /*
* A slightly cleaned-up version of strerror()
*/
char *
pg_strerror(int errnum)
{ {
static char buf[24]; /* this buffer is only used if strerror() and get_errno_symbol() fail */
static char errorstr_buf[48];
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)
return win32_socket_strerror(errnum);
#endif
/* Try the platform's strerror() */
str = strerror(errnum);
if (errnum < 0 || errnum > sys_nerr) /*
* 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)
{ {
sprintf(buf, _("unrecognized error %d"), errnum); snprintf(errorstr_buf, sizeof(errorstr_buf),
return buf; /*------
translator: This string will be truncated at 47
characters expanded. */
_("operating system error %d"), errnum);
str = errorstr_buf;
} }
return sys_errlist[errnum]; return str;
} }
/*
* 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";
#ifdef EADDRINUSE
case EADDRINUSE:
return "EADDRINUSE";
#endif
#ifdef EADDRNOTAVAIL
case EADDRNOTAVAIL:
return "EADDRNOTAVAIL";
#endif
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";
#ifdef ECONNABORTED
case ECONNABORTED:
return "ECONNABORTED";
#endif
case ECONNREFUSED:
return "ECONNREFUSED";
#ifdef ECONNRESET
case ECONNRESET:
return "ECONNRESET";
#endif
case EDEADLK:
return "EDEADLK";
case EDOM:
return "EDOM";
case EEXIST:
return "EEXIST";
case EFAULT:
return "EFAULT";
case EFBIG:
return "EFBIG";
#ifdef EHOSTUNREACH
case EHOSTUNREACH:
return "EHOSTUNREACH";
#endif
case EIDRM:
return "EIDRM";
case EINPROGRESS:
return "EINPROGRESS";
case EINTR:
return "EINTR";
case EINVAL:
return "EINVAL";
case EIO:
return "EIO";
#ifdef EISCONN
case EISCONN:
return "EISCONN";
#endif
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";
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";
#ifdef ENOTCONN
case ENOTCONN:
return "ENOTCONN";
#endif
case ENOTDIR:
return "ENOTDIR";
#if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
case ENOTEMPTY:
return "ENOTEMPTY";
#endif
#ifdef ENOTSOCK
case ENOTSOCK:
return "ENOTSOCK";
#endif
#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";
#ifdef ETIMEDOUT
case ETIMEDOUT:
return "ETIMEDOUT";
#endif
#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 *
win32_socket_strerror(int errnum)
{
static char wserrbuf[256];
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)
{
sprintf(wserrbuf, "winsock error %d (could not load netmsg.dll to translate: error code %lu)",
errnum, GetLastError());
return wserrbuf;
}
}
ZeroMemory(&wserrbuf, sizeof(wserrbuf));
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE,
handleDLL,
errnum,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
wserrbuf,
sizeof(wserrbuf) - 1,
NULL) == 0)
{
/* Failed to get id */
sprintf(wserrbuf, "unrecognized winsock error %d", errnum);
}
return wserrbuf;
}
#endif /* WIN32 */
...@@ -96,9 +96,10 @@ sub mkvcbuild ...@@ -96,9 +96,10 @@ sub mkvcbuild
chklocale.c crypt.c fls.c fseeko.c getrusage.c inet_aton.c random.c chklocale.c crypt.c fls.c fseeko.c getrusage.c inet_aton.c random.c
srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
dirent.c dlopen.c getopt.c getopt_long.c
pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pg_strong_random.c pgcheckdir.c pgmkdirp.c pgsleep.c pgstrcasecmp.c
pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c system.c pqsignal.c mkdtemp.c qsort.c qsort_arg.c quotes.c system.c
sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c dlopen.c sprompt.c strerror.c tar.c thread.c
win32env.c win32error.c win32security.c win32setlocale.c); win32env.c win32error.c win32security.c win32setlocale.c);
push(@pgportfiles, 'rint.c') if ($vsVersion < '12.00'); push(@pgportfiles, 'rint.c') if ($vsVersion < '12.00');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册