diff --git a/repmgr-client.c b/repmgr-client.c index 3c26a7ea..46bc34d1 100644 --- a/repmgr-client.c +++ b/repmgr-client.c @@ -11,6 +11,9 @@ #include "repmgr.h" #include "repmgr-client.h" +/* global configuration structures */ +t_runtime_options runtime_options = T_RUNTIME_OPTIONS_INITIALIZER; + int main(int argc, char **argv) { @@ -31,10 +34,13 @@ main(int argc, char **argv) */ switch (c) { + /* + * Options which cause repmgr to exit in this block; + * these are the only ones which can be executed as root user + */ case OPT_HELP: do_help(); exit(SUCCESS); - case '?': /* Actual help option given */ if (strcmp(argv[optind - 1], "-?") == 0) @@ -43,10 +49,32 @@ main(int argc, char **argv) exit(SUCCESS); } break; - + case 'V': + printf("%s %s\n", progname(), REPMGR_VERSION); + exit(SUCCESS); + /* general options */ + case 'f': + strncpy(runtime_options.config_file, optarg, MAXLEN); + break; } } + /* + * Disallow further running as root to prevent directory ownership problems. + * We check this here to give the root user a chance to execute --help/--version + * options. + */ + if (geteuid() == 0) + { + fprintf(stderr, + _("%s: cannot be run as root\n" + "Please log in (using, e.g., \"su\") as the " + "(unprivileged) user that owns " + "the data directory.\n" + ), + progname()); + exit(1); + } return SUCCESS; } @@ -56,6 +84,15 @@ do_help(void) { printf(_("%s: replication management tool for PostgreSQL\n"), progname()); printf(_("\n")); + + /* add a big friendly warning if root is executing "repmgr --help" */ + if (geteuid() == 0) + { + printf(_(" **************************************************\n")); + printf(_(" *** repmgr must be executed by a non-superuser ***\n")); + printf(_(" **************************************************\n")); + puts(""); + } printf(_("Usage:\n")); } diff --git a/repmgr-client.h b/repmgr-client.h index b4b23a44..e377796e 100644 --- a/repmgr-client.h +++ b/repmgr-client.h @@ -104,6 +104,17 @@ static struct option long_options[] = {NULL, 0, NULL, 0} }; + +typedef struct +{ + /* general repmgr options */ + char config_file[MAXPGPATH]; +} t_runtime_options; + +#define T_RUNTIME_OPTIONS_INITIALIZER { \ + /* general repmgr options */ \ + ""} + static void do_help(void); #endif diff --git a/repmgr.h b/repmgr.h index c0934cc6..9751b5a2 100644 --- a/repmgr.h +++ b/repmgr.h @@ -12,6 +12,9 @@ #include #include +#include "repmgr_version.h" +#include "strutil.h" + #define MIN_SUPPORTED_VERSION "9.3" #define MIN_SUPPORTED_VERSION_NUM 90300 diff --git a/repmgr_version.h b/repmgr_version.h index d73e0ec1..21611965 100644 --- a/repmgr_version.h +++ b/repmgr_version.h @@ -1,3 +1,8 @@ +/* + * repmgr_version.h + * Copyright (c) 2ndQuadrant, 2010-2017 + */ + #ifndef _VERSION_H_ #define _VERSION_H_ diff --git a/strutil.h b/strutil.h new file mode 100644 index 00000000..a2a07a6d --- /dev/null +++ b/strutil.h @@ -0,0 +1,11 @@ +/* + * strutil.h + * Copyright (c) 2ndQuadrant, 2010-2017 + */ + +#ifndef _STRUTIL_H_ +#define _STRUTIL_H_ + +#define MAXLEN 1024 + +#endif