From 1f3e937bbe7c8849905108812911f6a9e68b2c21 Mon Sep 17 00:00:00 2001 From: Gianni Ciolli Date: Fri, 15 Jul 2016 19:47:49 +0200 Subject: [PATCH] Add local_command function to run commands locally --- repmgr.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/repmgr.c b/repmgr.c index 701c8c1a..9a6739da 100644 --- a/repmgr.c +++ b/repmgr.c @@ -130,6 +130,7 @@ static void exit_with_errors(void); static void print_error_list(ItemList *error_list, int log_level); static bool remote_command(const char *host, const char *user, const char *command, PQExpBufferData *outputbuf); +static bool local_command(const char *command, PQExpBufferData *outputbuf); static void format_db_cli_params(const char *conninfo, char *output); static bool copy_file(const char *old_filename, const char *new_filename); @@ -5970,6 +5971,37 @@ remote_command(const char *host, const char *user, const char *command, PQExpBuf } +/* + * Execute a command locally. + */ +static bool +local_command(const char *command, PQExpBufferData *outputbuf) +{ + FILE *fp; + char output[MAXLEN]; + + fp = popen(command, "r"); + + if (fp == NULL) + { + log_err(_("unable to execute local command:\n%s\n"), command); + return false; + } + + /* TODO: better error handling */ + while (fgets(output, MAXLEN, fp) != NULL) + { + appendPQExpBuffer(outputbuf, "%s", output); + } + + pclose(fp); + + log_verbose(LOG_DEBUG, "local_command(): output returned was:\n%s", outputbuf->data); + + return true; +} + + /* * Extract values from provided conninfo string and return * formatted as command-line parameters suitable for passing to repmgr