diff options
Diffstat (limited to 'tools.c')
-rw-r--r-- | tools.c | 76 |
1 files changed, 73 insertions, 3 deletions
@@ -4,10 +4,9 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.46 2001/09/22 12:13:40 kls Exp $ + * $Id: tools.c 1.50 2001/10/19 13:12:45 kls Exp $ */ -#define _GNU_SOURCE #include "tools.h" #include <ctype.h> #include <dirent.h> @@ -74,6 +73,10 @@ char *strcpyrealloc(char *dest, const char *src) else esyslog(LOG_ERR, "ERROR: out of memory"); } + else { + delete dest; + dest = NULL; + } return dest; } @@ -413,7 +416,8 @@ const char *DayDateTime(time_t t) static char buffer[32]; if (t == 0) time(&t); - tm *tm = localtime(&t); + struct tm tm_r; + tm *tm = localtime_r(&t, &tm_r); int weekday = tm->tm_wday == 0 ? 6 : tm->tm_wday - 1; // we start with monday==0! const char *day = tr("MonTueWedThuFriSatSun"); day += weekday * 3; @@ -590,6 +594,72 @@ bool cSafeFile::Close(void) return result; } +// --- cLockFile ------------------------------------------------------------- + +#define LOCKFILENAME ".lock-vdr" +#define LOCKFILESTALETIME 600 // seconds before considering a lock file "stale" + +cLockFile::cLockFile(const char *Directory) +{ + fileName = NULL; + f = -1; + if (DirectoryOk(Directory)) + asprintf(&fileName, "%s/%s", Directory, LOCKFILENAME); +} + +cLockFile::~cLockFile() +{ + Unlock(); + delete fileName; +} + +bool cLockFile::Lock(int WaitSeconds) +{ + if (f < 0 && fileName) { + time_t Timeout = time(NULL) + WaitSeconds; + do { + f = open(fileName, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (f < 0) { + if (errno == EEXIST) { + struct stat fs; + if (stat(fileName, &fs) == 0) { + if (time(NULL) - fs.st_mtime > LOCKFILESTALETIME) { + esyslog(LOG_ERR, "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) +{ + if (f >= 0) { + close(f); + remove(fileName); + f = -1; + } + else + esyslog(LOG_ERR, "ERROR: attempt to unlock %s without holding a lock!", fileName); +} + // --- cListObject ----------------------------------------------------------- cListObject::cListObject(void) |