From 7c10afdb75b15f4216a7d0408239c85d8f73c815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Wed, 12 Dec 2001 19:49:11 +0100 Subject: [PATCH] (do_lsh_file_lock): If locking fails, optionally retry a few times. Rev: src/lock_file.c:1.3 --- src/lock_file.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/lock_file.c b/src/lock_file.c index e8fd0156..5b2f4603 100644 --- a/src/lock_file.c +++ b/src/lock_file.c @@ -28,8 +28,12 @@ #include "format.h" #include "resource.h" +#include "werror.h" #include "xalloc.h" +#include +#include + #if HAVE_UNISTD_H #include #endif @@ -65,17 +69,38 @@ do_kill_file_lock(struct resource *s) /* FIXME: Probably doesn't work right with NFS */ static struct resource * -do_lsh_file_lock(struct lsh_file_lock_info *self) +do_lsh_file_lock(struct lsh_file_lock_info *self, unsigned retries) { - int fd = open(lsh_get_cstring(self->lockname), + int fd = -1; + unsigned i; + + fd = open(lsh_get_cstring(self->lockname), + O_CREAT | O_EXCL | O_WRONLY, + 0666); + + for (i = 0; (fd < 0) && (errno == EEXIST) && (ilockname), O_CREAT | O_EXCL | O_WRONLY, 0666); - + } + if (fd < 0) - return NULL; + { + if (errno != EEXIST) + werror("Could not create lock file `%S' (errno = %i): %z", + self->lockname, errno, STRERROR(errno)); + return NULL; + } else { NEW(lsh_file_lock, lock); + + trace("lock_file.c: Creation of lock file `%S' succeeded.\n", + self->lockname); init_resource(&lock->super, do_kill_file_lock); lock->info = self; @@ -86,7 +111,7 @@ do_lsh_file_lock(struct lsh_file_lock_info *self) } /* Checks if a file is locked, without actually trying to lock it. */ -static struct resource * +static int do_lsh_file_lock_p(struct lsh_file_lock_info *self) { struct stat sbuf; -- GitLab