Fix and clean up the new lock file code.

This commit is contained in:
Jonathan Naylor
2018-10-11 07:50:30 +01:00
parent 3e435c5e82
commit d2e0d82d56
2 changed files with 24 additions and 33 deletions

View File

@@ -345,9 +345,13 @@ int CMMDVMHost::run()
} }
if (m_conf.getLockFileEnabled()) { if (m_conf.getLockFileEnabled()) {
m_lockFileName = m_conf.getLockFileName(); m_lockFileEnabled = true;
m_lockFileName = m_conf.getLockFileName();
LogInfo("Lock File Parameters"); LogInfo("Lock File Parameters");
LogInfo(" Name: %s", m_lockFileName.c_str()); LogInfo(" Name: %s", m_lockFileName.c_str());
removeLockFile();
} }
if (m_conf.getCWIdEnabled()) { if (m_conf.getCWIdEnabled()) {
@@ -605,9 +609,6 @@ int CMMDVMHost::run()
m_ump->setCD(cd); m_ump->setCD(cd);
} }
if (m_mode == MODE_IDLE)
removeLockFile();
unsigned char data[200U]; unsigned char data[200U];
unsigned int len; unsigned int len;
bool ret; bool ret;
@@ -1393,7 +1394,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_DSTAR; m_mode = MODE_DSTAR;
m_modeTimer.start(); m_modeTimer.start();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
createLockFile(); createLockFile("D-Star");
break; break;
case MODE_DMR: case MODE_DMR:
@@ -1417,7 +1418,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_DMR; m_mode = MODE_DMR;
m_modeTimer.start(); m_modeTimer.start();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
createLockFile(); createLockFile("DMR");
break; break;
case MODE_YSF: case MODE_YSF:
@@ -1437,7 +1438,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_YSF; m_mode = MODE_YSF;
m_modeTimer.start(); m_modeTimer.start();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
createLockFile(); createLockFile("System Fusion");
break; break;
case MODE_P25: case MODE_P25:
@@ -1457,7 +1458,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_P25; m_mode = MODE_P25;
m_modeTimer.start(); m_modeTimer.start();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
createLockFile(); createLockFile("P25");
break; break;
case MODE_NXDN: case MODE_NXDN:
@@ -1477,7 +1478,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_NXDN; m_mode = MODE_NXDN;
m_modeTimer.start(); m_modeTimer.start();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
createLockFile(); createLockFile("NXDN");
break; break;
case MODE_POCSAG: case MODE_POCSAG:
@@ -1497,7 +1498,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_POCSAG; m_mode = MODE_POCSAG;
m_modeTimer.start(); m_modeTimer.start();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
createLockFile(); createLockFile("POCSAG");
break; break;
case MODE_LOCKOUT: case MODE_LOCKOUT:
@@ -1525,6 +1526,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_LOCKOUT; m_mode = MODE_LOCKOUT;
m_modeTimer.stop(); m_modeTimer.stop();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
removeLockFile();
break; break;
case MODE_ERROR: case MODE_ERROR:
@@ -1551,6 +1553,7 @@ void CMMDVMHost::setMode(unsigned char mode)
m_mode = MODE_ERROR; m_mode = MODE_ERROR;
m_modeTimer.stop(); m_modeTimer.stop();
m_cwIdTimer.stop(); m_cwIdTimer.stop();
removeLockFile();
break; break;
default: default:
@@ -1582,10 +1585,8 @@ void CMMDVMHost::setMode(unsigned char mode)
m_cwIdTimer.start(); m_cwIdTimer.start();
} }
m_display->setIdle(); m_display->setIdle();
if (mode == MODE_QUIT) { if (mode == MODE_QUIT)
m_display->setQuit(); m_display->setQuit();
removeLockFile();
}
m_mode = MODE_IDLE; m_mode = MODE_IDLE;
m_modeTimer.stop(); m_modeTimer.stop();
removeLockFile(); removeLockFile();
@@ -1593,30 +1594,20 @@ void CMMDVMHost::setMode(unsigned char mode)
} }
} }
void CMMDVMHost::createLockFile() void CMMDVMHost::createLockFile(const char* mode) const
{ {
if (m_lockFileEnabled) { if (m_lockFileEnabled) {
FILE* fp = ::fopen(m_lockFileName.c_str(), "r"); FILE* fp = ::fopen(m_lockFileName.c_str(), "wt");
if (fp == NULL) { //if file does not exist, create it if (fp != NULL) {
fp = ::fopen(m_lockFileName.c_str(), "wt"); ::fprintf(fp, "%s\n", mode);
if (fp != NULL) {
::fputs("ACTIVE\n", fp);
::fclose(fp);
}
}
else {
::fclose(fp); ::fclose(fp);
} }
} }
} }
void CMMDVMHost::removeLockFile() void CMMDVMHost::removeLockFile() const
{ {
if (m_lockFileEnabled) { if (m_lockFileEnabled)
FILE* fp = ::fopen(m_lockFileName.c_str(), "r"); ::remove(m_lockFileName.c_str());
if (fp != NULL) {
::fclose(fp);
::remove(m_lockFileName.c_str());
}
}
} }

View File

@@ -99,8 +99,8 @@ private:
void setMode(unsigned char mode); void setMode(unsigned char mode);
void createLockFile(); void createLockFile(const char* mode) const;
void removeLockFile(); void removeLockFile() const;
}; };
#endif #endif