|
SASC
DVBSoftCAM
|
#include <tools.h>
Public Member Functions | |
| cLockFile (const char *Directory) | |
| ~cLockFile () | |
| bool | Lock (int WaitSeconds=0) |
| void | Unlock (void) |
| 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 | ( | ) |
| 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;
}
}