SASC-NG
DVBSoftCAM

cLockFile Class Reference

#include <tools.h>

List of all members.

Public Member Functions

 cLockFile (const char *Directory)
 ~cLockFile ()
bool Lock (int WaitSeconds=0)
void Unlock (void)

Detailed Description

Definition at line 347 of file tools.h.


Constructor & Destructor Documentation

cLockFile::cLockFile ( const char *  Directory)

Definition at line 850 of file tools.cpp.

References DirectoryOk(), LOCKFILENAME, and NULL.

{
  fileName = NULL;
  f = -1;
  if (DirectoryOk(Directory))
     asprintf(&fileName, "%s/%s", Directory, LOCKFILENAME);
}
cLockFile::~cLockFile ( )

Definition at line 858 of file tools.cpp.

References free, and Unlock().

{
  Unlock();
  free(fileName);
}

Member Function Documentation

bool cLockFile::Lock ( int  WaitSeconds = 0)

Definition at line 864 of file tools.cpp.

References esyslog, LOCKFILESTALETIME, LOG_ERROR_STR, NULL, and open().

{
  if (f < 0 && fileName) {
     time_t Timeout = time(NULL) + WaitSeconds;
     do {
        f = open(fileName, O_WRONLY | O_CREAT | O_EXCL, DEFFILEMODE);
        if (f < 0) {
           if (errno == EEXIST) {
              struct stat fs;
              if (stat(fileName, &fs) == 0) {
                 if (abs(time(NULL) - fs.st_mtime) > LOCKFILESTALETIME) {
                    esyslog("ERROR: removing stale lock file '%s'", fileName);
                    if (remove(fileName) < 0) {
                       LOG_ERROR_STR(fileName);
                       break;
                       }
                    continue;
                    }
                 }
              else if (errno != ENOENT) {
                 LOG_ERROR_STR(fileName);
                 break;
                 }
              }
           else {
              LOG_ERROR_STR(fileName);
              break;
              }
           if (WaitSeconds)
              sleep(1);
           }
        } while (f < 0 && time(NULL) < Timeout);
     }
  return f >= 0;
}
void cLockFile::Unlock ( void  )

Definition at line 900 of file tools.cpp.

Referenced by ~cLockFile().

{
  if (f >= 0) {
     close(f);
     remove(fileName);
     f = -1;
     }
}

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines