aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorColin Walters <[email protected]>2002-04-14 06:40:19 +0000
committerColin Walters <[email protected]>2002-04-14 06:40:19 +0000
commit7c4f687309da8fc8ef0a6a172d83fbfee1f0c662 (patch)
tree55146bd7fa301c09af4b9753d7a2ace3c5ec5494 /lib-src
parent4cb72a547806511eb811f7774fe367a0f0dc01e7 (diff)
(lock_file): If the lock file is older than an hour, delete it. Reset
attempts to zero if we have to break the lock.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/update-game-score.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c
index 2f49197a6d..27e0baffe8 100644
--- a/lib-src/update-game-score.c
+++ b/lib-src/update-game-score.c
@@ -416,6 +416,7 @@ int
lock_file(const char *filename, void **state)
{
int fd;
+ struct stat buf;
int attempts = 0;
char *lockext = ".lockfile";
char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60);
@@ -426,6 +427,10 @@ lock_file(const char *filename, void **state)
*state = lockpath;
trylock:
attempts++;
+ /* If the lock is over an hour old, delete it. */
+ if (stat(lockpath, &buf) == 0
+ && (difftime(buf.st_ctime, time(NULL) > 60*60)))
+ unlink(lockpath);
if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0)
{
if (errno == EEXIST)
@@ -433,7 +438,10 @@ lock_file(const char *filename, void **state)
/* Break the lock; we won't corrupt the file, but we might
lose some scores. */
if (attempts > MAX_ATTEMPTS)
- unlink(lockpath);
+ {
+ unlink(lockpath);
+ attempts = 0;
+ }
sleep((rand() % 2)+1);
goto trylock;
}