Commit b6b297d2 authored by Tom Lane's avatar Tom Lane

Make src/common/exec.c's error logging less ugly.

This code used elog where it really ought to use ereport, mainly so that
it can report a SQLSTATE different from ERRCODE_INTERNAL_ERROR.  There
were some other random deviations from typical error report practice too.

In addition, we can make some cleanups that were impractical six months
ago:

* Use one variadic macro, instead of several with different numbers
of arguments, reducing the temptation to force-fit messages into
particular numbers of arguments;

* Use %m, even in the frontend case, simplifying the code.

Discussion: https://postgr.es/m/6025.1527351693@sss.pgh.pa.us
parent 36e9d413
...@@ -25,14 +25,24 @@ ...@@ -25,14 +25,24 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
/*
* Hacky solution to allow expressing both frontend and backend error reports
* in one macro call. First argument of log_error is an errcode() call of
* some sort (ignored if FRONTEND); the rest are errmsg_internal() arguments,
* i.e. message string and any parameters for it.
*
* Caller must provide the gettext wrapper around the message string, if
* appropriate, so that it gets translated in the FRONTEND case; this
* motivates using errmsg_internal() not errmsg(). We handle appending a
* newline, if needed, inside the macro, so that there's only one translatable
* string per call not two.
*/
#ifndef FRONTEND #ifndef FRONTEND
/* We use only 3- and 4-parameter elog calls in this file, for simplicity */ #define log_error(errcodefn, ...) \
/* NOTE: caller must provide gettext call around str! */ ereport(LOG, (errcodefn, errmsg_internal(__VA_ARGS__)))
#define log_error(str, param) elog(LOG, str, param)
#define log_error4(str, param, arg1) elog(LOG, str, param, arg1)
#else #else
#define log_error(str, param) (fprintf(stderr, str, param), fputc('\n', stderr)) #define log_error(errcodefn, ...) \
#define log_error4(str, param, arg1) (fprintf(stderr, str, param, arg1), fputc('\n', stderr)) (fprintf(stderr, __VA_ARGS__), fputc('\n', stderr))
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
...@@ -124,10 +134,8 @@ find_my_exec(const char *argv0, char *retpath) ...@@ -124,10 +134,8 @@ find_my_exec(const char *argv0, char *retpath)
if (!getcwd(cwd, MAXPGPATH)) if (!getcwd(cwd, MAXPGPATH))
{ {
int save_errno = errno; log_error(errcode_for_file_access(),
_("could not identify current directory: %m"));
log_error(_("could not identify current directory: %s"),
strerror(save_errno));
return -1; return -1;
} }
...@@ -145,7 +153,8 @@ find_my_exec(const char *argv0, char *retpath) ...@@ -145,7 +153,8 @@ find_my_exec(const char *argv0, char *retpath)
if (validate_exec(retpath) == 0) if (validate_exec(retpath) == 0)
return resolve_symlinks(retpath); return resolve_symlinks(retpath);
log_error(_("invalid binary \"%s\""), retpath); log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
_("invalid binary \"%s\""), retpath);
return -1; return -1;
} }
...@@ -194,14 +203,16 @@ find_my_exec(const char *argv0, char *retpath) ...@@ -194,14 +203,16 @@ find_my_exec(const char *argv0, char *retpath)
case -1: /* wasn't even a candidate, keep looking */ case -1: /* wasn't even a candidate, keep looking */
break; break;
case -2: /* found but disqualified */ case -2: /* found but disqualified */
log_error(_("could not read binary \"%s\""), log_error(errcode(ERRCODE_WRONG_OBJECT_TYPE),
_("could not read binary \"%s\""),
retpath); retpath);
break; break;
} }
} while (*endp); } while (*endp);
} }
log_error(_("could not find a \"%s\" to execute"), argv0); log_error(errcode(ERRCODE_UNDEFINED_FILE),
_("could not find a \"%s\" to execute"), argv0);
return -1; return -1;
} }
...@@ -240,10 +251,8 @@ resolve_symlinks(char *path) ...@@ -240,10 +251,8 @@ resolve_symlinks(char *path)
*/ */
if (!getcwd(orig_wd, MAXPGPATH)) if (!getcwd(orig_wd, MAXPGPATH))
{ {
int save_errno = errno; log_error(errcode_for_file_access(),
_("could not identify current directory: %m"));
log_error(_("could not identify current directory: %s"),
strerror(save_errno));
return -1; return -1;
} }
...@@ -258,10 +267,8 @@ resolve_symlinks(char *path) ...@@ -258,10 +267,8 @@ resolve_symlinks(char *path)
*lsep = '\0'; *lsep = '\0';
if (chdir(path) == -1) if (chdir(path) == -1)
{ {
int save_errno = errno; log_error(errcode_for_file_access(),
_("could not change directory to \"%s\": %m"), path);
log_error4(_("could not change directory to \"%s\": %s"),
path, strerror(save_errno));
return -1; return -1;
} }
fname = lsep + 1; fname = lsep + 1;
...@@ -273,10 +280,12 @@ resolve_symlinks(char *path) ...@@ -273,10 +280,12 @@ resolve_symlinks(char *path)
!S_ISLNK(buf.st_mode)) !S_ISLNK(buf.st_mode))
break; break;
errno = 0;
rllen = readlink(fname, link_buf, sizeof(link_buf)); rllen = readlink(fname, link_buf, sizeof(link_buf));
if (rllen < 0 || rllen >= sizeof(link_buf)) if (rllen < 0 || rllen >= sizeof(link_buf))
{ {
log_error(_("could not read symbolic link \"%s\""), fname); log_error(errcode_for_file_access(),
_("could not read symbolic link \"%s\": %m"), fname);
return -1; return -1;
} }
link_buf[rllen] = '\0'; link_buf[rllen] = '\0';
...@@ -288,10 +297,8 @@ resolve_symlinks(char *path) ...@@ -288,10 +297,8 @@ resolve_symlinks(char *path)
if (!getcwd(path, MAXPGPATH)) if (!getcwd(path, MAXPGPATH))
{ {
int save_errno = errno; log_error(errcode_for_file_access(),
_("could not identify current directory: %m"));
log_error(_("could not identify current directory: %s"),
strerror(save_errno));
return -1; return -1;
} }
join_path_components(path, path, link_buf); join_path_components(path, path, link_buf);
...@@ -299,10 +306,8 @@ resolve_symlinks(char *path) ...@@ -299,10 +306,8 @@ resolve_symlinks(char *path)
if (chdir(orig_wd) == -1) if (chdir(orig_wd) == -1)
{ {
int save_errno = errno; log_error(errcode_for_file_access(),
_("could not change directory to \"%s\": %m"), orig_wd);
log_error4(_("could not change directory to \"%s\": %s"),
orig_wd, strerror(save_errno));
return -1; return -1;
} }
#endif /* HAVE_READLINK */ #endif /* HAVE_READLINK */
...@@ -532,20 +537,15 @@ pclose_check(FILE *stream) ...@@ -532,20 +537,15 @@ pclose_check(FILE *stream)
if (exitstatus == -1) if (exitstatus == -1)
{ {
/* pclose() itself failed, and hopefully set errno */ /* pclose() itself failed, and hopefully set errno */
int save_errno = errno; log_error(errcode(ERRCODE_SYSTEM_ERROR),
_("pclose failed: %m"));
log_error(_("pclose failed: %s"),
strerror(save_errno));
} }
else else
{ {
reason = wait_result_to_str(exitstatus); reason = wait_result_to_str(exitstatus);
log_error("%s", reason); log_error(errcode(ERRCODE_SYSTEM_ERROR),
#ifdef FRONTEND "%s", reason);
free(reason);
#else
pfree(reason); pfree(reason);
#endif
} }
return exitstatus; return exitstatus;
} }
...@@ -666,19 +666,24 @@ AddUserToTokenDacl(HANDLE hToken) ...@@ -666,19 +666,24 @@ AddUserToTokenDacl(HANDLE hToken)
ptdd = (TOKEN_DEFAULT_DACL *) LocalAlloc(LPTR, dwSize); ptdd = (TOKEN_DEFAULT_DACL *) LocalAlloc(LPTR, dwSize);
if (ptdd == NULL) if (ptdd == NULL)
{ {
log_error("could not allocate %lu bytes of memory", dwSize); log_error(errcode(ERRCODE_OUT_OF_MEMORY),
_("out of memory"));
goto cleanup; goto cleanup;
} }
if (!GetTokenInformation(hToken, tic, (LPVOID) ptdd, dwSize, &dwSize)) if (!GetTokenInformation(hToken, tic, (LPVOID) ptdd, dwSize, &dwSize))
{ {
log_error("could not get token information: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not get token information: error code %lu",
GetLastError());
goto cleanup; goto cleanup;
} }
} }
else else
{ {
log_error("could not get token information buffer size: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not get token information buffer size: error code %lu",
GetLastError());
goto cleanup; goto cleanup;
} }
} }
...@@ -688,7 +693,9 @@ AddUserToTokenDacl(HANDLE hToken) ...@@ -688,7 +693,9 @@ AddUserToTokenDacl(HANDLE hToken)
(DWORD) sizeof(ACL_SIZE_INFORMATION), (DWORD) sizeof(ACL_SIZE_INFORMATION),
AclSizeInformation)) AclSizeInformation))
{ {
log_error("could not get ACL information: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not get ACL information: error code %lu",
GetLastError());
goto cleanup; goto cleanup;
} }
...@@ -704,13 +711,15 @@ AddUserToTokenDacl(HANDLE hToken) ...@@ -704,13 +711,15 @@ AddUserToTokenDacl(HANDLE hToken)
pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize); pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
if (pacl == NULL) if (pacl == NULL)
{ {
log_error("could not allocate %lu bytes of memory", dwNewAclSize); log_error(errcode(ERRCODE_OUT_OF_MEMORY),
_("out of memory"));
goto cleanup; goto cleanup;
} }
if (!InitializeAcl(pacl, dwNewAclSize, ACL_REVISION)) if (!InitializeAcl(pacl, dwNewAclSize, ACL_REVISION))
{ {
log_error("could not initialize ACL: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not initialize ACL: error code %lu", GetLastError());
goto cleanup; goto cleanup;
} }
...@@ -719,13 +728,15 @@ AddUserToTokenDacl(HANDLE hToken) ...@@ -719,13 +728,15 @@ AddUserToTokenDacl(HANDLE hToken)
{ {
if (!GetAce(ptdd->DefaultDacl, i, (LPVOID *) &pace)) if (!GetAce(ptdd->DefaultDacl, i, (LPVOID *) &pace))
{ {
log_error("could not get ACE: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not get ACE: error code %lu", GetLastError());
goto cleanup; goto cleanup;
} }
if (!AddAce(pacl, ACL_REVISION, MAXDWORD, pace, ((PACE_HEADER) pace)->AceSize)) if (!AddAce(pacl, ACL_REVISION, MAXDWORD, pace, ((PACE_HEADER) pace)->AceSize))
{ {
log_error("could not add ACE: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not add ACE: error code %lu", GetLastError());
goto cleanup; goto cleanup;
} }
} }
...@@ -733,7 +744,9 @@ AddUserToTokenDacl(HANDLE hToken) ...@@ -733,7 +744,9 @@ AddUserToTokenDacl(HANDLE hToken)
/* Add the new ACE for the current user */ /* Add the new ACE for the current user */
if (!AddAccessAllowedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE, GENERIC_ALL, pTokenUser->User.Sid)) if (!AddAccessAllowedAceEx(pacl, ACL_REVISION, OBJECT_INHERIT_ACE, GENERIC_ALL, pTokenUser->User.Sid))
{ {
log_error("could not add access allowed ACE: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not add access allowed ACE: error code %lu",
GetLastError());
goto cleanup; goto cleanup;
} }
...@@ -742,7 +755,9 @@ AddUserToTokenDacl(HANDLE hToken) ...@@ -742,7 +755,9 @@ AddUserToTokenDacl(HANDLE hToken)
if (!SetTokenInformation(hToken, tic, (LPVOID) &tddNew, dwNewAclSize)) if (!SetTokenInformation(hToken, tic, (LPVOID) &tddNew, dwNewAclSize))
{ {
log_error("could not set token information: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not set token information: error code %lu",
GetLastError());
goto cleanup; goto cleanup;
} }
...@@ -788,13 +803,16 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser) ...@@ -788,13 +803,16 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
if (*ppTokenUser == NULL) if (*ppTokenUser == NULL)
{ {
log_error("could not allocate %lu bytes of memory", dwLength); log_error(errcode(ERRCODE_OUT_OF_MEMORY),
_("out of memory"));
return FALSE; return FALSE;
} }
} }
else else
{ {
log_error("could not get token information buffer size: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not get token information buffer size: error code %lu",
GetLastError());
return FALSE; return FALSE;
} }
} }
...@@ -808,7 +826,9 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser) ...@@ -808,7 +826,9 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
LocalFree(*ppTokenUser); LocalFree(*ppTokenUser);
*ppTokenUser = NULL; *ppTokenUser = NULL;
log_error("could not get token information: error code %lu", GetLastError()); log_error(errcode(ERRCODE_SYSTEM_ERROR),
"could not get token information: error code %lu",
GetLastError());
return FALSE; return FALSE;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment