From 3442cbc707d4af586b0b8960e206346fa91df466 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Fri, 6 May 2011 22:58:12 +0000 Subject: [PATCH] Task #2933 - *nix side of task (used lock file approach) --- src/test/integtests/Main.cpp | 65 ++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/src/test/integtests/Main.cpp b/src/test/integtests/Main.cpp index 97588ba3..32f32c1b 100644 --- a/src/test/integtests/Main.cpp +++ b/src/test/integtests/Main.cpp @@ -23,13 +23,31 @@ #include "CArchMiscWindows.h" #endif +#if SYSAPI_UNIX +#include +#include +#include +#include +#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