Improve PID file check

The executable name of the PID is now checked to be maxscale. This fixes
the problem where MaxScale would refuse to start if a stale PID file had a
PID of a process that's not a MaxScale process.
This commit is contained in:
Markus Mäkelä
2018-08-22 11:28:46 +03:00
parent 518a4ef3bd
commit fe7d7475a4

View File

@ -27,8 +27,10 @@
#include <time.h>
#include <unistd.h>
#include <getopt.h>
#include <set>
#include <map>
#include <fstream>
#include <ini.h>
#include <openssl/opensslconf.h>
@ -2265,6 +2267,25 @@ static void unlink_pidfile(void)
}
}
bool pid_is_maxscale(int pid)
{
bool rval = false;
std::stringstream ss;
ss << "/proc/" << pid << "/comm";
std::ifstream file(ss.str());
std::string line;
if (file && std::getline(file, line))
{
if (line == "maxscale")
{
rval = true;
}
}
return rval;
}
/**
* Check if the maxscale.pid file exists and has a valid PID in it. If one has already been
* written and a MaxScale process is running, this instance of MaxScale should shut down.
@ -2352,9 +2373,17 @@ bool pid_file_exists()
return true;
}
if (kill(pid, 0) == -1)
if (pid_is_maxscale(pid))
{
if (errno == ESRCH)
const char* logerr =
"MaxScale is already running. Process id: %d. "
"Use another location for the PID file to run multiple "
"instances of MaxScale on the same machine.";
snprintf(logbuf, sizeof(logbuf), logerr, pid);
print_log_n_stderr(true, true, logbuf, logbuf, 0);
unlock_pidfile();
}
else
{
/** no such process, old PID file */
if (lock_failed)
@ -2369,24 +2398,6 @@ bool pid_file_exists()
}
return lock_failed;
}
else
{
const char* logerr = "Failed to check the existence of process %d read from file '%s'";
snprintf(logbuf, sizeof(logbuf), logerr, pid, pathbuf);
print_log_n_stderr(true, true, logbuf, logbuf, errno);
unlock_pidfile();
}
}
else
{
const char* logerr =
"MaxScale is already running. Process id: %d. "
"Use another location for the PID file to run multiple "
"instances of MaxScale on the same machine.";
snprintf(logbuf, sizeof(logbuf), logerr, pid);
print_log_n_stderr(true, true, logbuf, logbuf, 0);
unlock_pidfile();
}
}
else
{