diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2023-01-11 07:22:51 +0100 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2023-01-11 10:42:35 +0100 |
commit | c96de2ce1782116bd0489b1cd69ba88189a495e8 (patch) | |
tree | f0d108a772f4c96966ab877b1b4a24f4e8b26713 /src/backend/libpq/be-secure-common.c | |
parent | 5f6401f81cb24bd3930e0dc589fc4aa8b5424cdc (diff) | |
download | postgresql-c96de2ce1782116bd0489b1cd69ba88189a495e8.tar.gz postgresql-c96de2ce1782116bd0489b1cd69ba88189a495e8.zip |
Common function for percent placeholder replacement
There are a number of places where a shell command is constructed with
percent-placeholders (like %x). It's cumbersome to have to open-code
this several times. This factors out this logic into a separate
function. This also allows us to ensure consistency for and document
some subtle behaviors, such as what to do with unrecognized
placeholders.
The unified handling is now that incorrect and unknown placeholders
are an error, where previously in most cases they were skipped or
ignored. This affects the following settings:
- archive_cleanup_command
- archive_command
- recovery_end_command
- restore_command
- ssl_passphrase_command
The following settings are part of this refactoring but already had
stricter error handling and should be unchanged in their behavior:
- basebackup_to_shell.command
Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/5238bbed-0b01-83a6-d4b2-7eb0562a054e%40enterprisedb.com
Diffstat (limited to 'src/backend/libpq/be-secure-common.c')
-rw-r--r-- | src/backend/libpq/be-secure-common.c | 38 |
1 files changed, 8 insertions, 30 deletions
diff --git a/src/backend/libpq/be-secure-common.c b/src/backend/libpq/be-secure-common.c index f6078c91c34..ab5e2dfa2bf 100644 --- a/src/backend/libpq/be-secure-common.c +++ b/src/backend/libpq/be-secure-common.c @@ -22,6 +22,7 @@ #include <sys/stat.h> #include <unistd.h> +#include "common/percentrepl.h" #include "common/string.h" #include "libpq/libpq.h" #include "storage/fd.h" @@ -39,8 +40,7 @@ int run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, int size) { int loglevel = is_server_start ? ERROR : LOG; - StringInfoData command; - char *p; + char *command; FILE *fh; int pclose_rc; size_t len = 0; @@ -49,37 +49,15 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, Assert(size > 0); buf[0] = '\0'; - initStringInfo(&command); + command = replace_percent_placeholders(ssl_passphrase_command, "ssl_passphrase_command", "p", prompt); - for (p = ssl_passphrase_command; *p; p++) - { - if (p[0] == '%') - { - switch (p[1]) - { - case 'p': - appendStringInfoString(&command, prompt); - p++; - break; - case '%': - appendStringInfoChar(&command, '%'); - p++; - break; - default: - appendStringInfoChar(&command, p[0]); - } - } - else - appendStringInfoChar(&command, p[0]); - } - - fh = OpenPipeStream(command.data, "r"); + fh = OpenPipeStream(command, "r"); if (fh == NULL) { ereport(loglevel, (errcode_for_file_access(), errmsg("could not execute command \"%s\": %m", - command.data))); + command))); goto error; } @@ -91,7 +69,7 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, ereport(loglevel, (errcode_for_file_access(), errmsg("could not read from command \"%s\": %m", - command.data))); + command))); goto error; } } @@ -111,7 +89,7 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, ereport(loglevel, (errcode_for_file_access(), errmsg("command \"%s\" failed", - command.data), + command), errdetail_internal("%s", wait_result_to_str(pclose_rc)))); goto error; } @@ -120,7 +98,7 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, len = pg_strip_crlf(buf); error: - pfree(command.data); + pfree(command); return len; } |