Commit 7c10afdb authored by Niels Möller's avatar Niels Möller

(do_lsh_file_lock): If locking fails,

optionally retry a few times.

Rev: src/lock_file.c:1.3
parent 6cc2a3b3
...@@ -28,8 +28,12 @@ ...@@ -28,8 +28,12 @@
#include "format.h" #include "format.h"
#include "resource.h" #include "resource.h"
#include "werror.h"
#include "xalloc.h" #include "xalloc.h"
#include <errno.h>
#include <string.h>
#if HAVE_UNISTD_H #if HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif #endif
...@@ -65,17 +69,38 @@ do_kill_file_lock(struct resource *s) ...@@ -65,17 +69,38 @@ do_kill_file_lock(struct resource *s)
/* FIXME: Probably doesn't work right with NFS */ /* FIXME: Probably doesn't work right with NFS */
static struct resource * 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) && (i<retries); i++)
{
/* Wait a few seconds and try again. */
sleep(17);
fd = open(lsh_get_cstring(self->lockname),
O_CREAT | O_EXCL | O_WRONLY, O_CREAT | O_EXCL | O_WRONLY,
0666); 0666);
}
if (fd < 0) 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 else
{ {
NEW(lsh_file_lock, lock); 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); init_resource(&lock->super, do_kill_file_lock);
lock->info = self; lock->info = self;
...@@ -86,7 +111,7 @@ do_lsh_file_lock(struct lsh_file_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. */ /* 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) do_lsh_file_lock_p(struct lsh_file_lock_info *self)
{ {
struct stat sbuf; struct stat sbuf;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment