prevention of 2nd copy start: algorithm changed (should work better)

This commit is contained in:
ars_goldendict 2009-05-16 00:42:51 +00:00
parent ded22cc3fd
commit b14a6271d9
3 changed files with 44 additions and 9 deletions

View file

@ -42,10 +42,13 @@ int main( int argc, char ** argv )
// get pid // get pid
quint64 current_pid = QCoreApplication::applicationPid(); quint64 current_pid = QCoreApplication::applicationPid();
QString app_fname = QFileInfo(QCoreApplication::applicationFilePath()).baseName(); // QString app_fname = QFileInfo(QCoreApplication::applicationFilePath()).baseName();
quint64 pid = ProcessWrapper::findProcess( // quint64 pid = ProcessWrapper::findProcess(
app_fname.toAscii().data(), // app_fname.toAscii().data(),
current_pid); // current_pid);
//
// qDebug() << "pid " << pid;
// qDebug() << "current_pid " << current_pid;
// check pid file // check pid file
QFile pid_file( Config::getPidFileName() ); QFile pid_file( Config::getPidFileName() );
@ -57,11 +60,18 @@ int main( int argc, char ** argv )
quint64 tmp; ds >> tmp; quint64 tmp; ds >> tmp;
pid_file.close(); pid_file.close();
if ( tmp == pid ) // it is active - exiting bool isExist = ProcessWrapper::processExists(tmp);
if ( isExist )
{ {
// to do: switch to pid ? puts( "Another GoldenDict copy started already." );
return 1; return 1;
} }
// if ( tmp == pid ) // it is active - exiting
// {
// // to do: switch to pid ?
// return 1;
// }
} }
pid_file.open( QIODevice::WriteOnly ); pid_file.open( QIODevice::WriteOnly );

View file

@ -13,6 +13,25 @@ unsigned int ProcessWrapper::currentProcessId()
return GetCurrentProcessId(); return GetCurrentProcessId();
} }
bool ProcessWrapper::processExists(unsigned int pid)
{
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return false;
cProcesses = cbNeeded / sizeof(DWORD);
for ( i = 0; i < cProcesses; i++ )
{
unsigned int processID = aProcesses[i];
if ( processID == pid )
return true;
}
return false;
}
unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip) unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip)
{ {
DWORD aProcesses[1024], cbNeeded, cProcesses; DWORD aProcesses[1024], cbNeeded, cProcesses;
@ -80,6 +99,11 @@ unsigned int ProcessWrapper::currentProcessId()
return getpid(); return getpid();
} }
bool ProcessWrapper::processExists(unsigned int pid)
{
return QFile::exists(QString("/proc/%1").arg(pid));
}
unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip) unsigned int ProcessWrapper::findProcess(const char *name, unsigned int pid_skip)
{ {
QString pname("(" + QString(name) + ")"); QString pname("(" + QString(name) + ")");

View file

@ -8,6 +8,7 @@ public:
static unsigned int findProcess(const char *name, unsigned int pid_skip = 0); static unsigned int findProcess(const char *name, unsigned int pid_skip = 0);
static unsigned int currentProcessId(); static unsigned int currentProcessId();
static bool processExists(unsigned int pid);
}; };
#endif // PROCESSWRAPPER_H #endif // PROCESSWRAPPER_H