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 <time.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h> #include <getopt.h>
#include <set> #include <set>
#include <map> #include <map>
#include <fstream>
#include <ini.h> #include <ini.h>
#include <openssl/opensslconf.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 * 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. * written and a MaxScale process is running, this instance of MaxScale should shut down.
@ -2352,32 +2373,7 @@ bool pid_file_exists()
return true; return true;
} }
if (kill(pid, 0) == -1) if (pid_is_maxscale(pid))
{
if (errno == ESRCH)
{
/** no such process, old PID file */
if (lock_failed)
{
const char* logerr =
"Locking the PID file '%s' failed. "
"Read PID from file and no process found with PID %d. "
"Confirm that no other process holds the lock on the PID file.";
snprintf(logbuf, sizeof(logbuf), logerr, pathbuf, pid);
print_log_n_stderr(true, true, logbuf, logbuf, 0);
close(fd);
}
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 = const char* logerr =
"MaxScale is already running. Process id: %d. " "MaxScale is already running. Process id: %d. "
@ -2387,6 +2383,21 @@ bool pid_file_exists()
print_log_n_stderr(true, true, logbuf, logbuf, 0); print_log_n_stderr(true, true, logbuf, logbuf, 0);
unlock_pidfile(); unlock_pidfile();
} }
else
{
/** no such process, old PID file */
if (lock_failed)
{
const char* logerr =
"Locking the PID file '%s' failed. "
"Read PID from file and no process found with PID %d. "
"Confirm that no other process holds the lock on the PID file.";
snprintf(logbuf, sizeof(logbuf), logerr, pathbuf, pid);
print_log_n_stderr(true, true, logbuf, logbuf, 0);
close(fd);
}
return lock_failed;
}
} }
else else
{ {