Added more verbose error messages and fixed bugs.
This commit is contained in:
@ -89,7 +89,7 @@
|
|||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
|
|
||||||
#define STRING_BUFFER_SIZE 1024
|
#define STRING_BUFFER_SIZE 1024
|
||||||
#define PIDFD_CLOSED -2
|
#define PIDFD_CLOSED -1
|
||||||
|
|
||||||
/** for procname */
|
/** for procname */
|
||||||
#if !defined(_GNU_SOURCE)
|
#if !defined(_GNU_SOURCE)
|
||||||
@ -1834,7 +1834,6 @@ int main(int argc, char **argv)
|
|||||||
/* Write process pid into MaxScale pidfile */
|
/* Write process pid into MaxScale pidfile */
|
||||||
if(write_pid_file() != 0)
|
if(write_pid_file() != 0)
|
||||||
{
|
{
|
||||||
skygw_log_write(LE,"Error: Failed to write PID file. Exiting.");
|
|
||||||
rc = MAXSCALE_ALREADYRUNNING;
|
rc = MAXSCALE_ALREADYRUNNING;
|
||||||
goto return_main;
|
goto return_main;
|
||||||
}
|
}
|
||||||
@ -2007,6 +2006,7 @@ static void log_flush_cb(
|
|||||||
static void unlock_pidfile()
|
static void unlock_pidfile()
|
||||||
{
|
{
|
||||||
if(pidfd != PIDFD_CLOSED)
|
if(pidfd != PIDFD_CLOSED)
|
||||||
|
{
|
||||||
if(flock(pidfd,LOCK_UN|LOCK_NB) != 0)
|
if(flock(pidfd,LOCK_UN|LOCK_NB) != 0)
|
||||||
{
|
{
|
||||||
char logbuf[STRING_BUFFER_SIZE + PATH_MAX];
|
char logbuf[STRING_BUFFER_SIZE + PATH_MAX];
|
||||||
@ -2015,6 +2015,7 @@ static void unlock_pidfile()
|
|||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
}
|
}
|
||||||
close(pidfd);
|
close(pidfd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2047,8 +2048,9 @@ bool pid_file_exists()
|
|||||||
char logbuf[STRING_BUFFER_SIZE + PATH_MAX];
|
char logbuf[STRING_BUFFER_SIZE + PATH_MAX];
|
||||||
char pidbuf[STRING_BUFFER_SIZE];
|
char pidbuf[STRING_BUFFER_SIZE];
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
bool lock_failed = false;
|
||||||
|
|
||||||
snprintf(pathbuf, PATH_MAX, "%s/maxscale.pid",piddir?piddir:default_piddir);
|
snprintf(pathbuf, PATH_MAX, "%s/maxscale.pid",get_piddir());
|
||||||
pathbuf[PATH_MAX] = '\0';
|
pathbuf[PATH_MAX] = '\0';
|
||||||
|
|
||||||
if(access(pathbuf,F_OK) != 0)
|
if(access(pathbuf,F_OK) != 0)
|
||||||
@ -2067,12 +2069,16 @@ bool pid_file_exists()
|
|||||||
}
|
}
|
||||||
if(flock(fd,LOCK_EX|LOCK_NB))
|
if(flock(fd,LOCK_EX|LOCK_NB))
|
||||||
{
|
{
|
||||||
close(fd);
|
if(errno != EWOULDBLOCK)
|
||||||
|
{
|
||||||
char* logerr = "Failed to lock PID file '%s'.";
|
char* logerr = "Failed to lock PID file '%s'.";
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pidfile);
|
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
|
close(fd);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
lock_failed = true;
|
||||||
|
}
|
||||||
|
|
||||||
pidfd = fd;
|
pidfd = fd;
|
||||||
b = read(fd,pidbuf,sizeof(pidbuf));
|
b = read(fd,pidbuf,sizeof(pidbuf));
|
||||||
@ -2088,7 +2094,9 @@ bool pid_file_exists()
|
|||||||
else if(b == 0)
|
else if(b == 0)
|
||||||
{
|
{
|
||||||
/** Empty file */
|
/** Empty file */
|
||||||
char* logerr = "PID file read from '%s'. File was empty.";
|
char* logerr = "PID file read from '%s'. File was empty.\n"
|
||||||
|
"If the file is the correct PID file and no other MaxScale processes "
|
||||||
|
"are running, please remove it manually and start MaxScale again.";
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
unlock_pidfile();
|
unlock_pidfile();
|
||||||
@ -2101,7 +2109,9 @@ bool pid_file_exists()
|
|||||||
if(pid < 1)
|
if(pid < 1)
|
||||||
{
|
{
|
||||||
/** Bad PID */
|
/** Bad PID */
|
||||||
char* logerr = "PID file read from '%s'. File contents not valid.";
|
char* logerr = "PID file read from '%s'. File contents not valid.\n"
|
||||||
|
"If the file is the correct PID file and no other MaxScale processes "
|
||||||
|
"are running, please remove it manually and start MaxScale again.";
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
unlock_pidfile();
|
unlock_pidfile();
|
||||||
@ -2113,31 +2123,39 @@ bool pid_file_exists()
|
|||||||
if(errno == ESRCH)
|
if(errno == ESRCH)
|
||||||
{
|
{
|
||||||
/** no such process, old PID file */
|
/** no such process, old PID file */
|
||||||
return false;
|
if(lock_failed)
|
||||||
|
{
|
||||||
|
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
|
||||||
{
|
{
|
||||||
char* logerr = "Failed to check the existence of process %d";
|
char* logerr = "Failed to check the existence of process %d read from file '%s'";
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pid);
|
snprintf(logbuf,sizeof(logbuf),logerr,pid,pathbuf);
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
unlock_pidfile();
|
unlock_pidfile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char* logerr = "MaxScale is already running. Process id: %d";
|
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);
|
snprintf(logbuf,sizeof(logbuf),logerr,pid);
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, 0);
|
print_log_n_stderr(true, true, logbuf, logbuf, 0);
|
||||||
unlock_pidfile();
|
unlock_pidfile();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
char* logerr = "Cannot open PID file '%s', no read permissions.";
|
char* logerr = "Cannot open PID file '%s', no read permissions. "
|
||||||
|
"Please confirm that the user running MaxScale has read permissions on the file.";
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
snprintf(logbuf,sizeof(logbuf),logerr,pathbuf);
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -2154,11 +2172,13 @@ static int write_pid_file() {
|
|||||||
char logbuf[STRING_BUFFER_SIZE + PATH_MAX];
|
char logbuf[STRING_BUFFER_SIZE + PATH_MAX];
|
||||||
char pidstr[STRING_BUFFER_SIZE];
|
char pidstr[STRING_BUFFER_SIZE];
|
||||||
|
|
||||||
|
snprintf(pidfile, PATH_MAX, "%s/maxscale.pid",get_piddir());
|
||||||
|
|
||||||
if(pidfd == PIDFD_CLOSED)
|
if(pidfd == PIDFD_CLOSED)
|
||||||
{
|
{
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
snprintf(pidfile, PATH_MAX, "%s/maxscale.pid",get_piddir());
|
|
||||||
fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC, 0777);
|
fd = open(pidfile, O_WRONLY | O_CREAT, 0777);
|
||||||
if (fd == -1) {
|
if (fd == -1) {
|
||||||
char* logerr = "Failed to open PID file '%s'.";
|
char* logerr = "Failed to open PID file '%s'.";
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pidfile);
|
snprintf(logbuf,sizeof(logbuf),logerr,pidfile);
|
||||||
@ -2168,8 +2188,15 @@ static int write_pid_file() {
|
|||||||
|
|
||||||
if(flock(fd,LOCK_EX|LOCK_NB))
|
if(flock(fd,LOCK_EX|LOCK_NB))
|
||||||
{
|
{
|
||||||
char* logerr = "Failed to lock PID file '%s'.";
|
if(errno == EWOULDBLOCK)
|
||||||
snprintf(logbuf,sizeof(logbuf),logerr,pidfile);
|
{
|
||||||
|
snprintf(logbuf,sizeof(logbuf),"Failed to lock PID file '%s', another process is holding a lock on it. "
|
||||||
|
"Please confirm that no other MaxScale process is using the same PID file location.",pidfile);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(logbuf,sizeof(logbuf),"Failed to lock PID file '%s'.",pidfile);
|
||||||
|
}
|
||||||
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
close(fd);
|
close(fd);
|
||||||
return -1;
|
return -1;
|
||||||
@ -2179,7 +2206,9 @@ static int write_pid_file() {
|
|||||||
|
|
||||||
/* truncate pidfile content */
|
/* truncate pidfile content */
|
||||||
if (ftruncate(pidfd, 0)) {
|
if (ftruncate(pidfd, 0)) {
|
||||||
fprintf(stderr, "MaxScale failed to truncate pidfile %s: error %d, %s\n", pidfile, errno, strerror(errno));
|
char* logerr = "MaxScale failed to truncate PID file '%s'.";
|
||||||
|
snprintf(logbuf,sizeof(logbuf),logerr,pidfile);
|
||||||
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
unlock_pidfile();
|
unlock_pidfile();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -2187,8 +2216,9 @@ static int write_pid_file() {
|
|||||||
snprintf(pidstr, sizeof(pidstr)-1, "%d", getpid());
|
snprintf(pidstr, sizeof(pidstr)-1, "%d", getpid());
|
||||||
|
|
||||||
if (pwrite(pidfd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
|
if (pwrite(pidfd, pidstr, strlen(pidstr), 0) != (ssize_t)strlen(pidstr)) {
|
||||||
fprintf(stderr, "MaxScale failed to write into pidfile %s: error %d, %s\n", pidfile, errno, strerror(errno));
|
char* logerr = "MaxScale failed to write into PID file '%s'.";
|
||||||
/* close file and return */
|
snprintf(logbuf,sizeof(logbuf),logerr,pidfile);
|
||||||
|
print_log_n_stderr(true, true, logbuf, logbuf, errno);
|
||||||
unlock_pidfile();
|
unlock_pidfile();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user