diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2019-04-01 14:24:37 +0200 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2019-04-01 20:01:35 +0200 |
commit | cc8d41511721d25d557fc02a46c053c0a602fed0 (patch) | |
tree | d2f92acac085be1b9cc4756260c7a4f83d1b0041 /src/common/restricted_token.c | |
parent | b4cc19ab01ffe6a72a915b21aa41536de80923f5 (diff) | |
download | postgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.tar.gz postgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.zip |
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
Diffstat (limited to 'src/common/restricted_token.c')
-rw-r--r-- | src/common/restricted_token.c | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/src/common/restricted_token.c b/src/common/restricted_token.c index c4ccc0e55c6..3f57ff8b40b 100644 --- a/src/common/restricted_token.c +++ b/src/common/restricted_token.c @@ -21,6 +21,7 @@ #include "postgres_fe.h" #include "common/restricted_token.h" +#include "fe_utils/logging.h" #ifdef WIN32 @@ -43,7 +44,7 @@ typedef BOOL (WINAPI * __CreateRestrictedToken) (HANDLE, DWORD, DWORD, PSID_AND_ * NOT execute anything. */ HANDLE -CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char *progname) +CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo) { BOOL b; STARTUPINFO si; @@ -65,7 +66,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char if (_CreateRestrictedToken == NULL) { - fprintf(stderr, _("%s: WARNING: cannot create restricted tokens on this platform\n"), progname); + pg_log_warning("cannot create restricted tokens on this platform"); if (Advapi32Handle != NULL) FreeLibrary(Advapi32Handle); return 0; @@ -74,7 +75,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char /* Open the current token to use as a base for the restricted one */ if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &origToken)) { - fprintf(stderr, _("%s: could not open process token: error code %lu\n"), progname, GetLastError()); + pg_log_error("could not open process token: error code %lu", GetLastError()); return 0; } @@ -87,8 +88,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_POWER_USERS, 0, 0, 0, 0, 0, 0, &dropSids[1].Sid)) { - fprintf(stderr, _("%s: could not allocate SIDs: error code %lu\n"), - progname, GetLastError()); + pg_log_error("could not allocate SIDs: error code %lu", GetLastError()); return 0; } @@ -107,8 +107,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char if (!b) { - fprintf(stderr, _("%s: could not create restricted token: error code %lu\n"), - progname, GetLastError()); + pg_log_error("could not create restricted token: error code %lu", GetLastError()); return 0; } @@ -129,7 +128,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char processInfo)) { - fprintf(stderr, _("%s: could not start process for command \"%s\": error code %lu\n"), progname, cmd, GetLastError()); + pg_log_error("could not start process for command \"%s\": error code %lu", cmd, GetLastError()); return 0; } @@ -143,7 +142,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char * On other platforms do nothing. */ void -get_restricted_token(const char *progname) +get_restricted_token(void) { #ifdef WIN32 HANDLE restrictedToken; @@ -165,9 +164,9 @@ get_restricted_token(const char *progname) putenv("PG_RESTRICT_EXEC=1"); - if ((restrictedToken = CreateRestrictedProcess(cmdline, &pi, progname)) == 0) + if ((restrictedToken = CreateRestrictedProcess(cmdline, &pi)) == 0) { - fprintf(stderr, _("%s: could not re-execute with restricted token: error code %lu\n"), progname, GetLastError()); + pg_log_error("could not re-execute with restricted token: error code %lu", GetLastError()); } else { @@ -183,7 +182,7 @@ get_restricted_token(const char *progname) if (!GetExitCodeProcess(pi.hProcess, &x)) { - fprintf(stderr, _("%s: could not get exit code from subprocess: error code %lu\n"), progname, GetLastError()); + pg_log_error("could not get exit code from subprocess: error code %lu", GetLastError()); exit(1); } exit(x); |