repmgrd: create pid file by default

Traditionally repmgrd will only write a pidfile if explicitly requested with
-p/--pid-file. However it's normally desirable to have a pidfile, and it's
preferable to have one used by default to prevent accidentally starting a second
repmgrd instance.

Following changes made:

 - add configuration file parameter "repmgrd_pid_file" (initially overridden by
   -p/--pid-file for backwards compatibility, though eventually we'll want to
   drop -p/--pid-file altogether)
 - add command line option --no-pid-file
 - if neither "repmgrd_pid_file" nor -p/--pid-file is set, create the pid file
   in a temporary directory

Implements GitHub #457.
This commit is contained in:
Ian Barwick
2018-06-29 14:33:52 +09:00
parent b2081dca52
commit 8d636690bd
5 changed files with 82 additions and 10 deletions

View File

@@ -35,8 +35,10 @@
static char *config_file = NULL;
static bool verbose = false;
static char *pid_file = NULL;
static char pid_file[MAXPGPATH];
static bool daemonize = false;
static bool show_pid_file = false;
static bool no_pid_file = false;
t_configuration_options config_file_options = T_CONFIGURATION_OPTIONS_INITIALIZER;
@@ -101,6 +103,8 @@ main(int argc, char **argv)
/* daemon options */
{"daemonize", no_argument, NULL, 'd'},
{"pid-file", required_argument, NULL, 'p'},
{"show-pid-file", no_argument, NULL, 's'},
{"no-pid-file", no_argument, NULL, OPT_NO_PID_FILE},
/* logging options */
{"log-level", required_argument, NULL, 'L'},
@@ -113,8 +117,6 @@ main(int argc, char **argv)
set_progname(argv[0]);
srand(time(NULL));
/* Disallow running as root */
if (geteuid() == 0)
{
@@ -128,6 +130,10 @@ main(int argc, char **argv)
exit(1);
}
srand(time(NULL));
memset(pid_file, 0, MAXPGPATH);
while ((c = getopt_long(argc, argv, "?Vf:L:vdp:m", long_options, &optindex)) != -1)
{
switch (c)
@@ -173,7 +179,15 @@ main(int argc, char **argv)
break;
case 'p':
pid_file = optarg;
strncpy(pid_file, optarg, MAXPGPATH);
break;
case 's':
show_pid_file = true;
break;
case OPT_NO_PID_FILE:
no_pid_file = true;
break;
/* logging options */
@@ -239,6 +253,48 @@ main(int argc, char **argv)
*/
load_config(config_file, verbose, false, &config_file_options, argv[0]);
/* Determine pid file location, unless --no-pid-file supplied */
if (no_pid_file == false)
{
if (config_file_options.repmgrd_pid_file[0] != '\0')
{
if (pid_file[0] != '\0')
{
log_warning(_("\"repmgrd_pid_file\" will be overridden by --pid-file"));
}
else
{
strncpy(pid_file, config_file_options.repmgrd_pid_file, MAXPGPATH);
}
}
/* no pid file provided - determine location */
if (pid_file[0] == '\0')
{
const char *tmpdir = getenv("TMPDIR");
if (!tmpdir)
tmpdir = "/tmp";
maxpath_snprintf(pid_file, "%s/repmgrd.pid", tmpdir);
}
}
else
{
/* --no-pid-file supplied - overwrite any value provided with --pid-file ... */
memset(pid_file, 0, MAXPGPATH);
}
/* If --show-pid-file supplied, output the location (if set) and exit */
if (show_pid_file == true)
{
printf("%s\n", pid_file);
exit(SUCCESS);
}
/* Some configuration file items can be overriden by command line options */
@@ -414,7 +470,7 @@ main(int argc, char **argv)
daemonize_process();
}
if (pid_file != NULL)
if (pid_file[0] != '\0')
{
check_and_create_pid_file(pid_file);
}
@@ -669,6 +725,8 @@ show_help(void)
{
printf(_("%s: replication management daemon for PostgreSQL\n"), progname());
puts("");
printf(_("%s monitors a cluster of servers and optionally performs failover.\n"), progname());
puts("");
printf(_("Usage:\n"));
printf(_(" %s [OPTIONS]\n"), progname());
@@ -688,12 +746,13 @@ show_help(void)
puts("");
printf(_("General configuration options:\n"));
printf(_("Daemon configuration options:\n"));
printf(_(" -d, --daemonize detach process from foreground\n"));
printf(_(" -p, --pid-file=PATH write a PID file\n"));
printf(_(" -p, --pid-file=PATH use the specified PID file\n"));
printf(_(" -s, --show-pid-file show PID file which would be used by the current configuration\n"));
printf(_(" --no-pid-file don't write a PID file\n"));
puts("");
printf(_("%s monitors a cluster of servers and optionally performs failover.\n"), progname());
}
@@ -802,7 +861,7 @@ terminate(int retval)
{
logger_shutdown();
if (pid_file)
if (pid_file[0] != '\0')
{
unlink(pid_file);
}