repmgr: initial runtime option parsing and root execution detection

This commit is contained in:
Ian Barwick
2017-04-19 23:16:36 +09:00
parent 1631917715
commit 76e8c4624e
5 changed files with 69 additions and 2 deletions

View File

@@ -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"));
}

View File

@@ -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

View File

@@ -12,6 +12,9 @@
#include <postgres_fe.h>
#include <pqexpbuffer.h>
#include "repmgr_version.h"
#include "strutil.h"
#define MIN_SUPPORTED_VERSION "9.3"
#define MIN_SUPPORTED_VERSION_NUM 90300

View File

@@ -1,3 +1,8 @@
/*
* repmgr_version.h
* Copyright (c) 2ndQuadrant, 2010-2017
*/
#ifndef _VERSION_H_
#define _VERSION_H_

11
strutil.h Normal file
View File

@@ -0,0 +1,11 @@
/*
* strutil.h
* Copyright (c) 2ndQuadrant, 2010-2017
*/
#ifndef _STRUTIL_H_
#define _STRUTIL_H_
#define MAXLEN 1024
#endif