Task #2933 - *nix side of task (used lock file approach)

This commit is contained in:
Nick Bolton 2011-05-06 22:58:12 +00:00
parent 933522451a
commit 3442cbc707
1 changed files with 63 additions and 2 deletions

View File

@ -23,13 +23,31 @@
#include "CArchMiscWindows.h"
#endif
#if SYSAPI_UNIX
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#define LOCK_FILE "/tmp/integtests.lock"
#endif
#define ERROR_ALREADY_RUNNING 1
using namespace std;
void
int
ensureSingleInstance();
#if SYSAPI_UNIX
void
signalHandler(int signal);
void
removeLock();
#endif
int
main(int argc, char **argv)
{
@ -38,6 +56,12 @@ main(int argc, char **argv)
if (err != 0)
return err;
#if SYSAPI_UNIX
// register SIGINT handling (to delete lock file)
signal(SIGINT, signalHandler);
atexit(removeLock);
#endif
#if SYSAPI_WIN32
// record window instance for tray icon, etc
CArchMiscWindows::setInstanceWin32(GetModuleHandle(NULL));
@ -90,9 +114,46 @@ ensureSingleInstance()
gotEntry = Process32Next(snapshot, &entry);
}
#elif SYSAPI_UNIX
// TODO
// fail if lock file exists
struct stat info;
int statResult = stat(LOCK_FILE, &info);
if (statResult == 0)
{
cerr << "error: lock file exists: " << LOCK_FILE << endl;
return ERROR_ALREADY_RUNNING;
}
// write an empty lock file
cout << "creating lock: " << LOCK_FILE << endl;
ofstream stream;
stream.open(LOCK_FILE);
if (!stream.is_open())
cerr << "error: could not create lock" << endl;
stream << "";
stream.close();
#endif
return 0;
}
#if SYSAPI_UNIX
void
signalHandler(int signal)
{
removeLock();
}
void
removeLock()
{
// remove lock file so other instances can run.
cout << "removing lock: " << LOCK_FILE << endl;
unlink(LOCK_FILE);
}
#endif