diff --git a/VERSION b/VERSION index a405d8f12..dbe1148f8 100644 --- a/VERSION +++ b/VERSION @@ -1,4 +1,4 @@ COLUMNSTORE_VERSION_MAJOR=1 COLUMNSTORE_VERSION_MINOR=0 COLUMNSTORE_VERSION_PATCH=11 -COLUMNSTORE_VERSION_RELEASE=1 +COLUMNSTORE_VERSION_RELEASE=2 diff --git a/oam/oamcpp/liboamcpp.cpp b/oam/oamcpp/liboamcpp.cpp index aa8dd4804..6e9175e9a 100644 --- a/oam/oamcpp/liboamcpp.cpp +++ b/oam/oamcpp/liboamcpp.cpp @@ -2523,6 +2523,9 @@ namespace oam string Section = "AlarmConfig"; int returnValue; + struct flock fl; + int fd; + // validate Alarm ID if( alarmid > MAX_ALARM_ID ) @@ -2543,41 +2546,55 @@ namespace oam exceptionControl("setAlarmConfig", API_READONLY_PARAMETER); string fileName = AlarmConfigFile; - - int fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0644); - // Aquire an exclusive lock - if (flock(fd,LOCK_EX) == -1) { - throw runtime_error ("Lock file error: " + fileName); - } + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_RDLCK; // read lock + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; //lock whole file - // write parameter to disk - - Config* alaConfig = Config::makeConfig(AlarmConfigFile.c_str()); - alaConfig->setConfig(Section, name, value); - - try - { - alaConfig->write(); - } - catch(...) - { - // Release lock - if (flock(fd,LOCK_UN)==-1) + // open config file + if ((fd = open(fileName.c_str(), O_RDWR)) >= 0) + { // lock file + if (fcntl(fd, F_SETLKW, &fl) != 0) { - throw runtime_error ("Release lock file error: " + fileName); + ostringstream oss; + oss << "Oam::setAlarmConfig: error locking file " << + fileName << + ": " << + strerror(errno) << + ", proceding anyway."; + cerr << oss.str() << endl; } + + // write parameter to disk - exceptionControl("setAlarmConfig", API_FAILURE); - } + Config* alaConfig = Config::makeConfig(AlarmConfigFile.c_str()); + alaConfig->setConfig(Section, name, value); + + try + { + alaConfig->write(); + } + catch(...) + {} - // Release lock - if (flock(fd,LOCK_UN)==-1) + fl.l_type = F_UNLCK; //unlock + fcntl(fd, F_SETLK, &fl); + + close(fd); + } + else { - throw runtime_error ("Release lock file error: " + fileName); + ostringstream oss; + oss << "Oam::setAlarmConfig: error opening file " << + fileName << + ": " << + strerror(errno); + throw runtime_error(oss.str()); } - close(fd); + return; } /******************************************************************** diff --git a/oamapps/alarmmanager/alarmmanager.cpp b/oamapps/alarmmanager/alarmmanager.cpp index 5d3990b2d..49f6c8638 100644 --- a/oamapps/alarmmanager/alarmmanager.cpp +++ b/oamapps/alarmmanager/alarmmanager.cpp @@ -97,6 +97,9 @@ ALARMManager::~ALARMManager() *****************************************************************************************/ void rewriteActiveLog (const AlarmList& alarmList) { + struct flock fl; + int fd; + if (ALARM_DEBUG) { LoggingID lid(11); MessageLog ml(lid); @@ -110,30 +113,52 @@ void rewriteActiveLog (const AlarmList& alarmList) // delete the old file unlink (ACTIVE_ALARM_FILE.c_str()); + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_RDLCK; // read lock + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; //lock whole file + // create new file - int fd = open(ACTIVE_ALARM_FILE.c_str(), O_RDWR|O_CREAT, 0644); - - // Aquire an exclusive lock - if (flock(fd,LOCK_EX) == -1) { - throw runtime_error ("Lock active alarm log file error"); - } + if ((fd = open(ACTIVE_ALARM_FILE.c_str(), O_RDWR|O_CREAT, 0664)) >= 0) + { // lock file + if (fcntl(fd, F_SETLKW, &fl) != 0) + { + ostringstream oss; + oss << "rewriteActiveLog: error locking file " << + ACTIVE_ALARM_FILE << + ": " << + strerror(errno) << + ", proceding anyway."; + cerr << oss.str() << endl; + } + + ofstream activeAlarmFile (ACTIVE_ALARM_FILE.c_str()); + + AlarmList::const_iterator i; + for (i = alarmList.begin(); i != alarmList.end(); ++i) + { + activeAlarmFile << i->second; + } - ofstream activeAlarmFile (ACTIVE_ALARM_FILE.c_str()); - - AlarmList::const_iterator i; - for (i = alarmList.begin(); i != alarmList.end(); ++i) - { - activeAlarmFile << i->second; - } + activeAlarmFile.close(); - activeAlarmFile.close(); + fl.l_type = F_UNLCK; //unlock + fcntl(fd, F_SETLK, &fl); - // Release lock - if (flock(fd,LOCK_UN)==-1) - { - throw runtime_error ("Release lock active alarm log file error"); + close(fd); } - close(fd); + else + { + ostringstream oss; + oss << "rewriteActiveLog: error opening file " << + ACTIVE_ALARM_FILE << + ": " << + strerror(errno); + throw runtime_error(oss.str()); + } + + return; } /***************************************************************************************** @@ -144,6 +169,9 @@ void rewriteActiveLog (const AlarmList& alarmList) *****************************************************************************************/ void logAlarm (const Alarm& calAlarm, const string& fileName) { + struct flock fl; + int fd; + if (ALARM_DEBUG) { LoggingID lid(11); MessageLog ml(lid); @@ -154,24 +182,48 @@ void logAlarm (const Alarm& calAlarm, const string& fileName) ml.logDebugMessage(msg); } - int fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0644); - ofstream AlarmFile (fileName.c_str(), ios::app); + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_RDLCK; // read lock + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; //lock whole file - // Aquire an exclusive lock - if (flock(fd,LOCK_EX) == -1) { - throw runtime_error ("Lock file error: " + fileName); - } + // create new file + if ((fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0664)) >= 0) + { // lock file + if (fcntl(fd, F_SETLKW, &fl) != 0) + { + ostringstream oss; + oss << "logAlarm: error locking file " << + fileName << + ": " << + strerror(errno) << + ", proceding anyway."; + cerr << oss.str() << endl; + } + + ofstream AlarmFile (fileName.c_str(), ios::app); - AlarmFile << calAlarm; - AlarmFile.close(); - - // Release lock - if (flock(fd,LOCK_UN)==-1) - { - throw runtime_error ("Release lock file error: " + fileName); + AlarmFile << calAlarm; + AlarmFile.close(); + + fl.l_type = F_UNLCK; //unlock + fcntl(fd, F_SETLK, &fl); + + close(fd); } + else + { + ostringstream oss; + oss << "logAlarm: error opening file " << + fileName << + ": " << + strerror(errno); + throw runtime_error(oss.str()); + } + + return; - close(fd); } /***************************************************************************************** @@ -323,7 +375,6 @@ void configAlarm (Alarm& calAlarm) oam.setAlarmConfig (alarmID, "LastIssueTime", now); oam.setAlarmConfig (alarmID, "Occurrences", 1); } - else { // increment counter and check the ctnThreshold @@ -377,7 +428,7 @@ void configAlarm (Alarm& calAlarm) * *****************************************************************************************/ void ALARMManager::sendAlarmReport (const char* componentID, int alarmID, int state, - std::string repModuleName, std::string repProcessName) + std::string repModuleName, std::string repProcessName) { #ifdef SKIP_ALARM @@ -475,58 +526,62 @@ void ALARMManager::sendAlarmReport (const char* componentID, int alarmID, int st *****************************************************************************************/ void ALARMManager::getActiveAlarm(AlarmList& alarmList) const { + struct flock fl; + int fd; + //add-on to fileName with mount name if on non Parent Module Oam oam; string fileName = ACTIVE_ALARM_FILE; - int fd = open(fileName.c_str(),O_RDONLY); + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_RDLCK; // read lock + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; //lock whole file - if (fd == -1) { - // file may being deleted temporarily by trapHandler - sleep (1); - fd = open(fileName.c_str(),O_RDONLY); - if (fd == -1) { - // no active alarms, return - return; - } - } - - ifstream activeAlarm (fileName.c_str(), ios::in); - - // acquire read lock - if (flock(fd,LOCK_SH) == -1) - { - throw runtime_error ("Lock active alarm log file error"); - } - - Alarm alarm; - - while (!activeAlarm.eof()) - { - activeAlarm >> alarm; - if (alarm.getAlarmID() != INVALID_ALARM_ID) - //don't sort -// alarmList.insert (AlarmList::value_type(alarm.getAlarmID(), alarm)); - alarmList.insert (AlarmList::value_type(INVALID_ALARM_ID, alarm)); - } - activeAlarm.close(); - - // release lock - if (flock(fd,LOCK_UN) == -1) - { - throw runtime_error ("Release lock active alarm log file error"); - } - - close(fd); - - if (ALARM_DEBUG) - { - AlarmList :: iterator i; - for (i = alarmList.begin(); i != alarmList.end(); ++i) + // create new file + if (fd = open(fileName.c_str(),O_RDONLY) >= 0) + { // lock file + if (fcntl(fd, F_SETLKW, &fl) != 0) { - cout << i->second << endl; + ostringstream oss; + oss << "getActiveAlarm: error locking file " << + fileName << + ": " << + strerror(errno) << + ", proceding anyway."; + cerr << oss.str() << endl; + } + + ifstream activeAlarm (fileName.c_str(), ios::in); + + Alarm alarm; + + while (!activeAlarm.eof()) + { + activeAlarm >> alarm; + if (alarm.getAlarmID() != INVALID_ALARM_ID) + //don't sort + // alarmList.insert (AlarmList::value_type(alarm.getAlarmID(), alarm)); + alarmList.insert (AlarmList::value_type(INVALID_ALARM_ID, alarm)); + } + activeAlarm.close(); + + fl.l_type = F_UNLCK; //unlock + fcntl(fd, F_SETLK, &fl); + + close(fd); + + if (ALARM_DEBUG) + { + AlarmList :: iterator i; + for (i = alarmList.begin(); i != alarmList.end(); ++i) + { + cout << i->second << endl; + } } } + return; } @@ -540,6 +595,9 @@ void ALARMManager::getActiveAlarm(AlarmList& alarmList) const *****************************************************************************************/ void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const { + struct flock fl; + int fd; + string alarmFile = "/tmp/alarms"; //make 1 alarm log file made up of archive and current alarm.log @@ -568,68 +626,79 @@ void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const cmd = "cat " + ALARM_FILE + " >> /tmp/alarms"; (void)system(cmd.c_str()); - int fd = open(alarmFile.c_str(),O_RDONLY); - - if (fd == -1) - // doesn't exist yet, return - return; + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_RDLCK; // read lock + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; //lock whole file - ifstream hisAlarm (alarmFile.c_str(), ios::in); - - // acquire read lock - if (flock(fd,LOCK_SH) == -1) - { - throw runtime_error ("Lock alarm log file error"); - } - - //get mm / dd / yy from incoming date - string mm = date.substr(0,2); - string dd = date.substr(3,2); - string yy = date.substr(6,2); - - Alarm alarm; - - while (!hisAlarm.eof()) - { - hisAlarm >> alarm; - if (alarm.getAlarmID() != INVALID_ALARM_ID) { - time_t cal = alarm.getTimestampSeconds(); - struct tm tm; - localtime_r(&cal, &tm); - char tmp[3]; - strftime (tmp, 3, "%m", &tm); - string alarm_mm = tmp; - alarm_mm = alarm_mm.substr(0,2); - strftime (tmp, 3, "%d", &tm); - string alarm_dd = tmp; - alarm_dd = alarm_dd.substr(0,2); - strftime (tmp, 3, "%y", &tm); - string alarm_yy = tmp; - alarm_yy = alarm_yy.substr(0,2); - - if ( mm == alarm_mm && dd == alarm_dd && yy == alarm_yy ) - //don't sort - // alarmList.insert (AlarmList::value_type(alarm.getAlarmID(), alarm)); - alarmList.insert (AlarmList::value_type(INVALID_ALARM_ID, alarm)); - } - } - hisAlarm.close(); - unlink (alarmFile.c_str()); - - // release lock - if (flock(fd,LOCK_UN) == -1) - { - throw runtime_error ("Release lock active alarm log file error"); - } - - if (ALARM_DEBUG) - { - AlarmList :: iterator i; - for (i = alarmList.begin(); i != alarmList.end(); ++i) + // create new file + if (fd = open(alarmFile.c_str(),O_RDONLY) >= 0) + { // lock file + if (fcntl(fd, F_SETLKW, &fl) != 0) { - cout << i->second << endl; + ostringstream oss; + oss << "getAlarm: error locking file " << + alarmFile << + ": " << + strerror(errno) << + ", proceding anyway."; + cerr << oss.str() << endl; } + + ifstream hisAlarm (alarmFile.c_str(), ios::in); + + //get mm / dd / yy from incoming date + string mm = date.substr(0,2); + string dd = date.substr(3,2); + string yy = date.substr(6,2); + + Alarm alarm; + + while (!hisAlarm.eof()) + { + hisAlarm >> alarm; + if (alarm.getAlarmID() != INVALID_ALARM_ID) { + time_t cal = alarm.getTimestampSeconds(); + struct tm tm; + localtime_r(&cal, &tm); + char tmp[3]; + strftime (tmp, 3, "%m", &tm); + string alarm_mm = tmp; + alarm_mm = alarm_mm.substr(0,2); + strftime (tmp, 3, "%d", &tm); + string alarm_dd = tmp; + alarm_dd = alarm_dd.substr(0,2); + strftime (tmp, 3, "%y", &tm); + string alarm_yy = tmp; + alarm_yy = alarm_yy.substr(0,2); + + if ( mm == alarm_mm && dd == alarm_dd && yy == alarm_yy ) + //don't sort + // alarmList.insert (AlarmList::value_type(alarm.getAlarmID(), alarm)); + alarmList.insert (AlarmList::value_type(INVALID_ALARM_ID, alarm)); + } + } + hisAlarm.close(); + unlink (alarmFile.c_str()); + + fl.l_type = F_UNLCK; //unlock + fcntl(fd, F_SETLK, &fl); + + + if (ALARM_DEBUG) + { + AlarmList :: iterator i; + for (i = alarmList.begin(); i != alarmList.end(); ++i) + { + cout << i->second << endl; + } + } + + close(fd); } + + return; } } //namespace alarmmanager diff --git a/procmon/processmonitor.cpp b/procmon/processmonitor.cpp index 724650f08..200249675 100644 --- a/procmon/processmonitor.cpp +++ b/procmon/processmonitor.cpp @@ -2840,6 +2840,8 @@ int ProcessMonitor::updateLog(std::string action, std::string level) { MonitorLog log; Oam oam; + struct flock fl; + int fd; string fileName; oam.getSystemConfig("SystemLogConfigFile", fileName); @@ -3080,29 +3082,46 @@ int ProcessMonitor::updateLog(std::string action, std::string level) unlink (fileName.c_str()); ofstream newFile (fileName.c_str()); - // create new file - int fd = open(fileName.c_str(),O_RDWR|O_CREAT, 0644); - - // Aquire an exclusive lock - if (flock(fd,LOCK_EX) == -1) { - log.writeLog(__LINE__, "ERROR: file lock failure on " + fileName, LOG_TYPE_ERROR ); - close(fd); - return -1; - } - - copy(lines.begin(), lines.end(), ostream_iterator(newFile, "\n")); - newFile.close(); - - // Release lock - if (flock(fd,LOCK_UN) == -1) - { - log.writeLog(__LINE__, "ERROR: file unlock failure on " + fileName, LOG_TYPE_ERROR ); - close(fd); - return -1; - } - close(fd); + memset(&fl, 0, sizeof(fl)); + fl.l_type = F_RDLCK; // read lock + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; //lock whole file - oam.syslogAction("restart"); + // create new file + if ((fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0644)) >= 0) + { // lock file + + if (fcntl(fd, F_SETLKW, &fl) != 0) + { + ostringstream oss; + oss << "ProcessMonitor::updateLog: error locking file " << + fileName << + ": " << + strerror(errno) << + ", proceding anyway."; + cerr << oss.str() << endl; + } + + copy(lines.begin(), lines.end(), ostream_iterator(newFile, "\n")); + newFile.close(); + + fl.l_type = F_UNLCK; //unlock + fcntl(fd, F_SETLK, &fl); + + close(fd); + + oam.syslogAction("restart"); + } + else + { + ostringstream oss; + oss << "ProcessMonitor::updateLog: error opening file " << + fileName << + ": " << + strerror(errno); + throw runtime_error(oss.str()); + } } //update file priviledges