Use LOG(CRIT) when existing so a diagnostic message is printed to the console before exiting.
Also clean up code slightly.
This commit is contained in:
parent
32d756c24c
commit
9cf9130509
|
@ -30,6 +30,7 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "UnixSignal.h"
|
#include "UnixSignal.h"
|
||||||
#include "SelfDetect.h"
|
#include "SelfDetect.h"
|
||||||
|
@ -53,11 +54,10 @@ static void sigfcn(int sig)
|
||||||
|
|
||||||
void SelfDetect::RegisterProgram(const char *argv0)
|
void SelfDetect::RegisterProgram(const char *argv0)
|
||||||
{
|
{
|
||||||
const char *p = argv0 + strlen(argv0); // point to end of string
|
const char *p = strrchr((char*)argv0,'/');
|
||||||
while (p > argv0 && *p != '/') p--;
|
if (p == NULL) { p = argv0; }
|
||||||
if (*p == '/') p++;
|
|
||||||
|
|
||||||
char buf[BUFSIZ];
|
char buf[100];
|
||||||
snprintf(buf, sizeof(buf)-1, "/var/run/%s.pid", p);
|
snprintf(buf, sizeof(buf)-1, "/var/run/%s.pid", p);
|
||||||
LOG(NOTICE) << "*** Registering program " << argv0 << " to " << buf;
|
LOG(NOTICE) << "*** Registering program " << argv0 << " to " << buf;
|
||||||
|
|
||||||
|
@ -65,17 +65,15 @@ void SelfDetect::RegisterProgram(const char *argv0)
|
||||||
struct stat stbuf;
|
struct stat stbuf;
|
||||||
if (stat(buf, &stbuf) >= 0)
|
if (stat(buf, &stbuf) >= 0)
|
||||||
{
|
{
|
||||||
LOG(ERR) << "*** An instance of " << p << " is already running. ";
|
LOG(CRIT) << "*** An instance of " << p << " is already running. ";
|
||||||
LOG(ERR) << "*** If this is not the case, deleting this file will allow " << p << " to start: " << buf;
|
LOG(CRIT) << "*** If this is not the case, deleting this file will allow " << p << " to start: " << buf << " exiting...";
|
||||||
std::cout << "*** An instance of " << p << " is already running. " << std::endl;
|
|
||||||
std::cout << "*** If this is not the case, deleting this file will allow " << p << " to start: " << buf << std::endl;
|
|
||||||
Exit::exit(Exit::DETECTFILE);
|
Exit::exit(Exit::DETECTFILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *fp = fopen(buf, "w");
|
FILE *fp = fopen(buf, "w");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
{
|
{
|
||||||
LOG(ERR) << "*** Unable to create " << buf << ": " << strerror(errno);
|
LOG(CRIT) << "*** Unable to create " << buf << ": " << strerror(errno) << " exiting...";
|
||||||
Exit::exit(Exit::CREATEFILE);
|
Exit::exit(Exit::CREATEFILE);
|
||||||
}
|
}
|
||||||
fprintf(fp, "%d\n", getpid());
|
fprintf(fp, "%d\n", getpid());
|
||||||
|
|
Loading…
Reference in New Issue