diff options
| author | Colin Walters | 2002-04-14 06:40:19 +0000 |
|---|---|---|
| committer | Colin Walters | 2002-04-14 06:40:19 +0000 |
| commit | 7c4f687309da8fc8ef0a6a172d83fbfee1f0c662 (patch) | |
| tree | 55146bd7fa301c09af4b9753d7a2ace3c5ec5494 /lib-src | |
| parent | 4cb72a547806511eb811f7774fe367a0f0dc01e7 (diff) | |
| download | emacs-7c4f687309da8fc8ef0a6a172d83fbfee1f0c662.tar.gz emacs-7c4f687309da8fc8ef0a6a172d83fbfee1f0c662.zip | |
(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.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/lib-src/update-game-score.c b/lib-src/update-game-score.c index 2f49197a6dc..27e0baffe82 100644 --- a/lib-src/update-game-score.c +++ b/lib-src/update-game-score.c | |||
| @@ -416,6 +416,7 @@ int | |||
| 416 | lock_file(const char *filename, void **state) | 416 | lock_file(const char *filename, void **state) |
| 417 | { | 417 | { |
| 418 | int fd; | 418 | int fd; |
| 419 | struct stat buf; | ||
| 419 | int attempts = 0; | 420 | int attempts = 0; |
| 420 | char *lockext = ".lockfile"; | 421 | char *lockext = ".lockfile"; |
| 421 | char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60); | 422 | char *lockpath = malloc(strlen(filename) + strlen(lockext) + 60); |
| @@ -426,6 +427,10 @@ lock_file(const char *filename, void **state) | |||
| 426 | *state = lockpath; | 427 | *state = lockpath; |
| 427 | trylock: | 428 | trylock: |
| 428 | attempts++; | 429 | attempts++; |
| 430 | /* If the lock is over an hour old, delete it. */ | ||
| 431 | if (stat(lockpath, &buf) == 0 | ||
| 432 | && (difftime(buf.st_ctime, time(NULL) > 60*60))) | ||
| 433 | unlink(lockpath); | ||
| 429 | if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0) | 434 | if ((fd = open(lockpath, O_CREAT | O_EXCL, 0600)) < 0) |
| 430 | { | 435 | { |
| 431 | if (errno == EEXIST) | 436 | if (errno == EEXIST) |
| @@ -433,7 +438,10 @@ lock_file(const char *filename, void **state) | |||
| 433 | /* Break the lock; we won't corrupt the file, but we might | 438 | /* Break the lock; we won't corrupt the file, but we might |
| 434 | lose some scores. */ | 439 | lose some scores. */ |
| 435 | if (attempts > MAX_ATTEMPTS) | 440 | if (attempts > MAX_ATTEMPTS) |
| 436 | unlink(lockpath); | 441 | { |
| 442 | unlink(lockpath); | ||
| 443 | attempts = 0; | ||
| 444 | } | ||
| 437 | sleep((rand() % 2)+1); | 445 | sleep((rand() % 2)+1); |
| 438 | goto trylock; | 446 | goto trylock; |
| 439 | } | 447 | } |