2003-01-09 12:36:05 +00:00
|
|
|
|
/* dotlock.c - dotfile locking
|
2006-05-23 16:19:43 +00:00
|
|
|
|
* Copyright (C) 1998, 2000, 2001, 2003, 2004,
|
2008-06-13 16:18:59 +00:00
|
|
|
|
* 2005, 2006, 2008 Free Software Foundation, Inc.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
|
* This file is part of JNLIB.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
|
* JNLIB is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* published by the Free Software Foundation; either version 3 of
|
2006-10-02 11:54:35 +00:00
|
|
|
|
* the License, or (at your option) any later version.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
|
* JNLIB is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*
|
2006-10-02 11:54:35 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2007-07-04 19:49:40 +00:00
|
|
|
|
* License along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
#else
|
|
|
|
|
# include <sys/utsname.h>
|
2003-01-09 12:36:05 +00:00
|
|
|
|
#endif
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
|
#include "libjnlib-config.h"
|
2007-06-06 18:12:30 +00:00
|
|
|
|
#include "stringhelp.h"
|
2003-01-09 12:36:05 +00:00
|
|
|
|
#include "dotlock.h"
|
|
|
|
|
|
2003-11-17 12:20:11 +00:00
|
|
|
|
#if !defined(DIRSEP_C) && !defined(EXTSEP_C) \
|
|
|
|
|
&& !defined(DIRSEP_S) && !defined(EXTSEP_S)
|
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
#define DIRSEP_C '\\'
|
|
|
|
|
#define EXTSEP_C '.'
|
|
|
|
|
#define DIRSEP_S "\\"
|
|
|
|
|
#define EXTSEP_S "."
|
|
|
|
|
#else
|
|
|
|
|
#define DIRSEP_C '/'
|
|
|
|
|
#define EXTSEP_C '.'
|
|
|
|
|
#define DIRSEP_S "/"
|
|
|
|
|
#define EXTSEP_S "."
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* The object describing a lock. */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
struct dotlock_handle
|
|
|
|
|
{
|
|
|
|
|
struct dotlock_handle *next;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
char *lockname; /* Name of the actual lockfile. */
|
|
|
|
|
int locked; /* Lock status. */
|
|
|
|
|
int disable; /* If true, locking is disabled. */
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
HANDLE lockhd; /* The W32 handle of the lock file. */
|
|
|
|
|
#else
|
|
|
|
|
char *tname; /* Name of the lockfile template. */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
size_t nodename_off; /* Offset in TNAME of the nodename part. */
|
2008-06-13 16:18:59 +00:00
|
|
|
|
size_t nodename_len; /* Length of the nodename part. */
|
|
|
|
|
#endif /* HAVE_DOSISH_SYSTEM */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* A list of of all lock handles. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
static volatile DOTLOCK all_lockfiles;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
/* If this has the value true all locking is disabled. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
static int never_lock;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
/* Local protototypes. */
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2006-06-28 15:14:09 +00:00
|
|
|
|
static int read_lockfile (DOTLOCK h, int *same_node);
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Entirely disable all locking. This function should be called
|
|
|
|
|
before any locking is done. It may be called right at startup of
|
|
|
|
|
the process as it only sets a global value. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
void
|
|
|
|
|
disable_dotlock(void)
|
|
|
|
|
{
|
2006-06-28 15:14:09 +00:00
|
|
|
|
never_lock = 1;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Create a lockfile for a file name FILE_TO_LOCK and returns an
|
|
|
|
|
object of type DOTLOCK which may be used later to actually acquire
|
|
|
|
|
the lock. A cleanup routine gets installed to cleanup left over
|
|
|
|
|
locks or other files used internally by the lock mechanism.
|
|
|
|
|
|
|
|
|
|
Calling this function with NULL does only install the atexit
|
|
|
|
|
handler and may thus be used to assure that the cleanup is called
|
|
|
|
|
after all other atexit handlers.
|
|
|
|
|
|
|
|
|
|
This function creates a lock file in the same directory as
|
|
|
|
|
FILE_TO_LOCK using that name and a suffix of ".lock". Note that on
|
|
|
|
|
POSIX systems a temporary file ".#lk.<hostname>.pid[.threadid] is
|
|
|
|
|
used.
|
|
|
|
|
|
|
|
|
|
The function returns an new handle which needs to be released using
|
|
|
|
|
destroy_dotlock but gets also released at the termination of the
|
|
|
|
|
process. On error NULL is returned.
|
2003-01-09 12:36:05 +00:00
|
|
|
|
*/
|
|
|
|
|
DOTLOCK
|
2008-06-13 16:18:59 +00:00
|
|
|
|
create_dotlock (const char *file_to_lock)
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2006-06-28 15:14:09 +00:00
|
|
|
|
static int initialized;
|
|
|
|
|
DOTLOCK h;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2006-06-28 15:14:09 +00:00
|
|
|
|
int fd = -1;
|
|
|
|
|
char pidstr[16];
|
|
|
|
|
const char *nodename;
|
|
|
|
|
const char *dirpart;
|
|
|
|
|
int dirpartlen;
|
|
|
|
|
struct utsname utsbuf;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
size_t tnamelen;
|
2006-06-28 15:14:09 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if ( !initialized )
|
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
atexit (dotlock_remove_lockfiles);
|
2006-06-28 15:14:09 +00:00
|
|
|
|
initialized = 1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( !file_to_lock )
|
|
|
|
|
return NULL; /* Only initialization was requested. */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
h = jnlib_calloc (1, sizeof *h);
|
|
|
|
|
if (!h)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (never_lock)
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
|
|
|
|
h->disable = 1;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#ifdef _REENTRANT
|
2006-06-28 15:14:09 +00:00
|
|
|
|
/* fixme: aquire mutex on all_lockfiles */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#endif
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->next = all_lockfiles;
|
|
|
|
|
all_lockfiles = h;
|
|
|
|
|
return h;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-01-09 12:36:05 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/*
|
|
|
|
|
This is the POSIX version which uses a temporary file and the
|
|
|
|
|
link system call to make locking an atomic operation.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
snprintf (pidstr, sizeof pidstr, "%10d\n", (int)getpid() );
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|
|
|
|
|
/* Create a temporary file. */
|
|
|
|
|
if ( uname ( &utsbuf ) )
|
|
|
|
|
nodename = "unknown";
|
|
|
|
|
else
|
|
|
|
|
nodename = utsbuf.nodename;
|
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#ifdef __riscos__
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
|
|
|
|
char *iter = (char *) nodename;
|
|
|
|
|
for (; iter[0]; iter++)
|
|
|
|
|
if (iter[0] == '.')
|
|
|
|
|
iter[0] = '/';
|
|
|
|
|
}
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#endif /* __riscos__ */
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
if ( !(dirpart = strrchr (file_to_lock, DIRSEP_C)) )
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
|
|
|
|
dirpart = EXTSEP_S;
|
|
|
|
|
dirpartlen = 1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dirpartlen = dirpart - file_to_lock;
|
|
|
|
|
dirpart = file_to_lock;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
|
#ifdef _REENTRANT
|
2003-01-09 12:36:05 +00:00
|
|
|
|
/* fixme: aquire mutex on all_lockfiles */
|
2006-05-23 16:19:43 +00:00
|
|
|
|
#endif
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->next = all_lockfiles;
|
|
|
|
|
all_lockfiles = h;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
tnamelen = dirpartlen + 6 + 30 + strlen(nodename) + 10;
|
|
|
|
|
h->tname = jnlib_malloc (tnamelen + 1);
|
|
|
|
|
if (!h->tname)
|
|
|
|
|
{
|
|
|
|
|
all_lockfiles = h->next;
|
|
|
|
|
jnlib_free (h);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->nodename_len = strlen (nodename);
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#ifndef __riscos__
|
2008-06-13 16:18:59 +00:00
|
|
|
|
snprintf (h->tname, tnamelen, "%.*s/.#lk%p.", dirpartlen, dirpart, h );
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->nodename_off = strlen (h->tname);
|
2008-06-13 16:18:59 +00:00
|
|
|
|
snprintf (h->tname+h->nodename_off, tnamelen - h->nodename_off,
|
|
|
|
|
"%s.%d", nodename, (int)getpid ());
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#else /* __riscos__ */
|
2008-06-13 16:18:59 +00:00
|
|
|
|
snprintf (h->tname, tnamelen, "%.*s.lk%p/", dirpartlen, dirpart, h );
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->nodename_off = strlen (h->tname);
|
2008-06-13 16:18:59 +00:00
|
|
|
|
snprintf (h->tname+h->nodename_off, tnamelen - h->modename_off,
|
|
|
|
|
"%s/%d", nodename, (int)getpid () );
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#endif /* __riscos__ */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
errno = 0;
|
|
|
|
|
fd = open (h->tname, O_WRONLY|O_CREAT|O_EXCL,
|
|
|
|
|
S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR );
|
|
|
|
|
}
|
|
|
|
|
while (fd == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
|
|
if ( fd == -1 )
|
|
|
|
|
{
|
|
|
|
|
all_lockfiles = h->next;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_error (_("failed to create temporary file `%s': %s\n"),
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->tname, strerror(errno));
|
2008-06-13 16:18:59 +00:00
|
|
|
|
jnlib_free (h->tname);
|
|
|
|
|
jnlib_free (h);
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return NULL;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( write (fd, pidstr, 11 ) != 11 )
|
|
|
|
|
goto write_failed;
|
|
|
|
|
if ( write (fd, nodename, strlen (nodename) ) != strlen (nodename) )
|
|
|
|
|
goto write_failed;
|
|
|
|
|
if ( write (fd, "\n", 1 ) != 1 )
|
|
|
|
|
goto write_failed;
|
|
|
|
|
if ( close (fd) )
|
|
|
|
|
goto write_failed;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
|
# ifdef _REENTRANT
|
2006-06-28 15:14:09 +00:00
|
|
|
|
/* release mutex */
|
2006-05-23 16:19:43 +00:00
|
|
|
|
# endif
|
2008-06-13 16:18:59 +00:00
|
|
|
|
h->lockname = jnlib_malloc ( strlen (file_to_lock) + 6 );
|
|
|
|
|
if (!h->lockname)
|
|
|
|
|
{
|
|
|
|
|
all_lockfiles = h->next;
|
|
|
|
|
unlink (h->tname);
|
|
|
|
|
jnlib_free (h->tname);
|
|
|
|
|
jnlib_free (h);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
strcpy (stpcpy (h->lockname, file_to_lock), EXTSEP_S "lock");
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return h;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
write_failed:
|
|
|
|
|
all_lockfiles = h->next;
|
|
|
|
|
# ifdef _REENTRANT
|
|
|
|
|
/* fixme: release mutex */
|
|
|
|
|
# endif
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_error ( _("error writing to `%s': %s\n"), h->tname, strerror(errno) );
|
|
|
|
|
close (fd);
|
|
|
|
|
unlink (h->tname);
|
|
|
|
|
jnlib_free (h->tname);
|
|
|
|
|
jnlib_free (h);
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return NULL;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
#else /* HAVE_DOSISH_SYSTEM */
|
|
|
|
|
|
|
|
|
|
/* The Windows version does not need a temporary file but uses the
|
|
|
|
|
plain lock file along with record locking. We create this file
|
|
|
|
|
here so that we later do only need to do the file locking. For
|
|
|
|
|
error reporting it is useful to keep the name of the file in the
|
|
|
|
|
handle. */
|
|
|
|
|
h->next = all_lockfiles;
|
|
|
|
|
all_lockfiles = h;
|
|
|
|
|
|
|
|
|
|
h->lockname = jnlib_malloc ( strlen (file_to_lock) + 6 );
|
|
|
|
|
if (!h->lockname)
|
|
|
|
|
{
|
|
|
|
|
all_lockfiles = h->next;
|
|
|
|
|
jnlib_free (h);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
strcpy (stpcpy(h->lockname, file_to_lock), EXTSEP_S "lock");
|
|
|
|
|
|
|
|
|
|
/* If would be nice if we would use the FILE_FLAG_DELETE_ON_CLOSE
|
|
|
|
|
along with FILE_SHARE_DELETE but that does not work due to a race
|
|
|
|
|
condition: Despite the OPEN_ALWAYS flag CreateFile may return an
|
|
|
|
|
error and we can't reliable create/open the lock file unless we
|
|
|
|
|
would wait here until it works - however there are other valid
|
|
|
|
|
reasons why a lock file can't be created and thus the process
|
|
|
|
|
would not stop as expected but spin til until Windows crashes.
|
|
|
|
|
Our solution is to keep the lock file open; that does not
|
|
|
|
|
harm. */
|
|
|
|
|
h->lockhd = CreateFile (h->lockname,
|
|
|
|
|
GENERIC_READ|GENERIC_WRITE,
|
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE,
|
|
|
|
|
NULL, OPEN_ALWAYS, 0, NULL);
|
|
|
|
|
if (h->lockhd == INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("can't create `%s': %s\n"), h->lockname, w32_strerror (-1));
|
|
|
|
|
all_lockfiles = h->next;
|
|
|
|
|
jnlib_free (h->lockname);
|
|
|
|
|
jnlib_free (h);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
return h;
|
|
|
|
|
|
|
|
|
|
#endif /* HAVE_DOSISH_SYSTEM */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* Destroy the local handle H and release the lock. */
|
2006-05-23 16:19:43 +00:00
|
|
|
|
void
|
|
|
|
|
destroy_dotlock ( DOTLOCK h )
|
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
DOTLOCK hprev, htmp;
|
|
|
|
|
|
|
|
|
|
if ( !h )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/* First remove the handle from our global list of all locks. */
|
|
|
|
|
for (hprev=NULL, htmp=all_lockfiles; htmp; hprev=htmp, htmp=htmp->next)
|
|
|
|
|
if (htmp == h)
|
|
|
|
|
{
|
|
|
|
|
if (hprev)
|
|
|
|
|
hprev->next = htmp->next;
|
|
|
|
|
else
|
|
|
|
|
all_lockfiles = htmp->next;
|
|
|
|
|
h->next = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Then destroy the lock. */
|
|
|
|
|
if (!h->disable)
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
if (h->locked)
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
UnlockFile (h->lockhd, 0, 0, 1, 0);
|
2006-06-28 15:14:09 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
CloseHandle (h->lockhd);
|
|
|
|
|
#else /* !HAVE_DOSISH_SYSTEM */
|
|
|
|
|
if (h->locked && h->lockname)
|
|
|
|
|
unlink (h->lockname);
|
|
|
|
|
if (h->tname)
|
|
|
|
|
unlink (h->tname);
|
|
|
|
|
jnlib_free (h->tname);
|
|
|
|
|
#endif /* HAVE_DOSISH_SYSTEM */
|
|
|
|
|
jnlib_free (h->lockname);
|
2006-06-28 15:14:09 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
jnlib_free(h);
|
2006-05-23 16:19:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2003-01-09 12:36:05 +00:00
|
|
|
|
static int
|
|
|
|
|
maybe_deadlock( DOTLOCK h )
|
|
|
|
|
{
|
2006-06-28 15:14:09 +00:00
|
|
|
|
DOTLOCK r;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
for ( r=all_lockfiles; r; r = r->next )
|
|
|
|
|
{
|
|
|
|
|
if ( r != h && r->locked )
|
|
|
|
|
return 1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Do a lock on H. A TIMEOUT of 0 returns immediately, -1 waits
|
|
|
|
|
forever (hopefully not), other values are reserved (should then be
|
|
|
|
|
timeouts in milliseconds). Returns: 0 on success */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
int
|
2008-06-13 16:18:59 +00:00
|
|
|
|
make_dotlock ( DOTLOCK h, long timeout )
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
int backoff = 0;
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2006-06-28 15:14:09 +00:00
|
|
|
|
int pid;
|
|
|
|
|
const char *maybe_dead="";
|
|
|
|
|
int same_node;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( h->disable )
|
|
|
|
|
return 0; /* Locks are completely disabled. Return success. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( h->locked )
|
|
|
|
|
{
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#ifndef __riscos__
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_debug ("Oops, `%s' is already locked\n", h->lockname);
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#endif /* !__riscos__ */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
for (;;)
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
|
# ifndef __riscos__
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( !link(h->tname, h->lockname) )
|
|
|
|
|
{
|
|
|
|
|
/* fixme: better use stat to check the link count */
|
|
|
|
|
h->locked = 1;
|
|
|
|
|
return 0; /* okay */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( errno != EEXIST )
|
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_error ( "lock not made: link() failed: %s\n", strerror(errno) );
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
# else /* __riscos__ */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( !renamefile(h->tname, h->lockname) )
|
|
|
|
|
{
|
|
|
|
|
h->locked = 1;
|
|
|
|
|
return 0; /* okay */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( errno != EEXIST )
|
|
|
|
|
{
|
|
|
|
|
log_error( "lock not made: rename() failed: %s\n", strerror(errno) );
|
|
|
|
|
return -1;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
# endif /* __riscos__ */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|
|
|
|
|
if ( (pid = read_lockfile (h, &same_node)) == -1 )
|
|
|
|
|
{
|
|
|
|
|
if ( errno != ENOENT )
|
|
|
|
|
{
|
|
|
|
|
log_info ("cannot read lockfile\n");
|
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
log_info( "lockfile disappeared\n");
|
|
|
|
|
continue;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
else if ( pid == getpid() && same_node )
|
|
|
|
|
{
|
|
|
|
|
log_info( "Oops: lock already held by us\n");
|
|
|
|
|
h->locked = 1;
|
|
|
|
|
return 0; /* okay */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
else if ( same_node && kill (pid, 0) && errno == ESRCH )
|
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
# ifndef __riscos__
|
|
|
|
|
log_info (_("removing stale lockfile (created by %d)\n"), pid );
|
2006-06-28 15:14:09 +00:00
|
|
|
|
unlink (h->lockname);
|
|
|
|
|
continue;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
# else /* __riscos__ */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
/* Under RISCOS we are *pretty* sure that the other task
|
|
|
|
|
is dead and therefore we remove the stale lock file. */
|
2008-06-13 16:18:59 +00:00
|
|
|
|
maybe_dead = _(" - probably dead - removing lock");
|
2006-06-28 15:14:09 +00:00
|
|
|
|
unlink(h->lockname);
|
2008-06-13 16:18:59 +00:00
|
|
|
|
# endif /* __riscos__ */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|
|
|
|
|
if ( timeout == -1 )
|
|
|
|
|
{
|
|
|
|
|
/* Wait until lock has been released. */
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_info (_("waiting for lock (held by %d%s) %s...\n"),
|
|
|
|
|
pid, maybe_dead, maybe_deadlock(h)? _("(deadlock?) "):"");
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* We can't use sleep, cause signals may be blocked. */
|
|
|
|
|
tv.tv_sec = 1 + backoff;
|
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
select(0, NULL, NULL, NULL, &tv);
|
|
|
|
|
if ( backoff < 10 )
|
|
|
|
|
backoff++ ;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
else
|
|
|
|
|
return -1;
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#else /*HAVE_DOSISH_SYSTEM*/
|
|
|
|
|
int w32err;
|
|
|
|
|
|
|
|
|
|
if (LockFile (h->lockhd, 0, 0, 1, 0))
|
|
|
|
|
{
|
|
|
|
|
h->locked = 1;
|
|
|
|
|
return 0; /* okay */
|
|
|
|
|
}
|
|
|
|
|
w32err = GetLastError ();
|
|
|
|
|
if (w32err != ERROR_LOCK_VIOLATION)
|
|
|
|
|
{
|
|
|
|
|
log_error (_("lock `%s' not made: %s\n"),
|
|
|
|
|
h->lockname, w32_strerror (w32err));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( timeout == -1 )
|
|
|
|
|
{
|
|
|
|
|
/* Wait until lock has been released. */
|
|
|
|
|
log_info (_("waiting for lock %s...\n"), h->lockname);
|
|
|
|
|
Sleep ((1 + backoff)*1000);
|
|
|
|
|
if ( backoff < 10 )
|
|
|
|
|
backoff++ ;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
|
|
|
|
#endif /*HAVE_DOSISH_SYSTEM*/
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/*NOTREACHED*/
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* Release a lock. Returns 0 on success. */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
int
|
|
|
|
|
release_dotlock( DOTLOCK h )
|
|
|
|
|
{
|
2008-10-20 13:53:23 +00:00
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
|
|
|
|
int pid, same_node;
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
/* To avoid atexit race conditions we first check whether there are
|
|
|
|
|
any locks left. It might happen that another atexit handler
|
|
|
|
|
tries to release the lock while the atexit handler of this module
|
|
|
|
|
already ran and thus H is undefined. */
|
|
|
|
|
if (!all_lockfiles)
|
|
|
|
|
return 0;
|
2006-05-23 16:19:43 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( h->disable )
|
|
|
|
|
return 0;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( !h->locked )
|
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_debug("Oops, `%s' is not locked\n", h->lockname);
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#ifdef HAVE_DOSISH_SYSTEM
|
|
|
|
|
if (!UnlockFile (h->lockhd, 0, 0, 1, 0))
|
|
|
|
|
{
|
|
|
|
|
log_error ("release_dotlock: error removing lockfile `%s': %s\n",
|
|
|
|
|
h->lockname, w32_strerror (-1));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
2006-06-28 15:14:09 +00:00
|
|
|
|
pid = read_lockfile (h, &same_node);
|
|
|
|
|
if ( pid == -1 )
|
|
|
|
|
{
|
|
|
|
|
log_error( "release_dotlock: lockfile error\n");
|
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( pid != getpid() || !same_node )
|
|
|
|
|
{
|
|
|
|
|
log_error( "release_dotlock: not our lock (pid=%d)\n", pid);
|
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#ifndef __riscos__
|
2006-06-28 15:14:09 +00:00
|
|
|
|
if ( unlink( h->lockname ) )
|
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_error ("release_dotlock: error removing lockfile `%s'\n",
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->lockname);
|
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* Fixme: As an extra check we could check whether the link count is
|
|
|
|
|
now really at 1. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#else /* __riscos__ */
|
2008-06-13 16:18:59 +00:00
|
|
|
|
if ( renamefile (h->lockname, h->tname) )
|
2006-06-28 15:14:09 +00:00
|
|
|
|
{
|
2008-06-13 16:18:59 +00:00
|
|
|
|
log_error ("release_dotlock: error renaming lockfile `%s' to `%s'\n",
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->lockname, h->tname);
|
|
|
|
|
return -1;
|
2003-06-18 19:56:13 +00:00
|
|
|
|
}
|
|
|
|
|
#endif /* __riscos__ */
|
2008-06-13 16:18:59 +00:00
|
|
|
|
|
|
|
|
|
#endif /* !HAVE_DOSISH_SYSTEM */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
h->locked = 0;
|
|
|
|
|
return 0;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* Read the lock file and return the pid, returns -1 on error. True
|
|
|
|
|
will be stored in the integer at address SAME_NODE if the lock file
|
|
|
|
|
has been created on the same node. */
|
|
|
|
|
#ifndef HAVE_DOSISH_SYSTEM
|
2003-01-09 12:36:05 +00:00
|
|
|
|
static int
|
2006-06-28 15:14:09 +00:00
|
|
|
|
read_lockfile (DOTLOCK h, int *same_node )
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2006-06-28 15:14:09 +00:00
|
|
|
|
char buffer_space[10+1+70+1]; /* 70 is just an estimated value; node
|
|
|
|
|
name are usually shorter. */
|
2008-03-20 15:31:43 +00:00
|
|
|
|
int fd;
|
|
|
|
|
int pid = -1;
|
2006-06-28 15:14:09 +00:00
|
|
|
|
char *buffer, *p;
|
|
|
|
|
size_t expected_len;
|
|
|
|
|
int res, nread;
|
|
|
|
|
|
|
|
|
|
*same_node = 0;
|
|
|
|
|
expected_len = 10 + 1 + h->nodename_len + 1;
|
|
|
|
|
if ( expected_len >= sizeof buffer_space)
|
2009-07-07 16:51:33 +00:00
|
|
|
|
{
|
|
|
|
|
buffer = jnlib_malloc (expected_len);
|
|
|
|
|
if (!buffer)
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
else
|
|
|
|
|
buffer = buffer_space;
|
|
|
|
|
|
|
|
|
|
if ( (fd = open (h->lockname, O_RDONLY)) == -1 )
|
|
|
|
|
{
|
|
|
|
|
int e = errno;
|
|
|
|
|
log_info ("error opening lockfile `%s': %s\n",
|
|
|
|
|
h->lockname, strerror(errno) );
|
|
|
|
|
if (buffer != buffer_space)
|
|
|
|
|
jnlib_free (buffer);
|
|
|
|
|
errno = e; /* Need to return ERRNO here. */
|
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|
|
|
|
|
p = buffer;
|
|
|
|
|
nread = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
res = read (fd, p, expected_len - nread);
|
|
|
|
|
if (res == -1 && errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
if (res < 0)
|
|
|
|
|
{
|
|
|
|
|
log_info ("error reading lockfile `%s'", h->lockname );
|
|
|
|
|
close (fd);
|
|
|
|
|
if (buffer != buffer_space)
|
|
|
|
|
jnlib_free (buffer);
|
|
|
|
|
errno = 0; /* Do not return an inappropriate ERRNO. */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
p += res;
|
|
|
|
|
nread += res;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
while (res && nread != expected_len);
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
|
|
if (nread < 11)
|
|
|
|
|
{
|
|
|
|
|
log_info ("invalid size of lockfile `%s'", h->lockname );
|
|
|
|
|
if (buffer != buffer_space)
|
|
|
|
|
jnlib_free (buffer);
|
2008-06-13 16:18:59 +00:00
|
|
|
|
errno = 0; /* Better don't return an inappropriate ERRNO. */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer[10] != '\n'
|
|
|
|
|
|| (buffer[10] = 0, pid = atoi (buffer)) == -1
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#ifndef __riscos__
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|| !pid
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#else /* __riscos__ */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|| (!pid && riscos_getpid())
|
2003-06-18 19:56:13 +00:00
|
|
|
|
#endif /* __riscos__ */
|
2006-06-28 15:14:09 +00:00
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
log_error ("invalid pid %d in lockfile `%s'", pid, h->lockname );
|
|
|
|
|
if (buffer != buffer_space)
|
|
|
|
|
jnlib_free (buffer);
|
|
|
|
|
errno = 0;
|
|
|
|
|
return -1;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2006-06-28 15:14:09 +00:00
|
|
|
|
|
|
|
|
|
if (nread == expected_len
|
|
|
|
|
&& !memcmp (h->tname+h->nodename_off, buffer+11, h->nodename_len)
|
|
|
|
|
&& buffer[11+h->nodename_len] == '\n')
|
|
|
|
|
*same_node = 1;
|
|
|
|
|
|
|
|
|
|
if (buffer != buffer_space)
|
|
|
|
|
jnlib_free (buffer);
|
|
|
|
|
return pid;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
2008-06-13 16:18:59 +00:00
|
|
|
|
#endif /* !HAVE_DOSISH_SYSTEM */
|
2003-01-09 12:36:05 +00:00
|
|
|
|
|
|
|
|
|
|
2008-06-13 16:18:59 +00:00
|
|
|
|
/* Remove all lockfiles. This is usually called by the atexit handler
|
|
|
|
|
installed by this module but may also be called by other
|
|
|
|
|
termination handlers. */
|
2003-06-18 19:56:13 +00:00
|
|
|
|
void
|
|
|
|
|
dotlock_remove_lockfiles()
|
2003-01-09 12:36:05 +00:00
|
|
|
|
{
|
2006-05-23 16:19:43 +00:00
|
|
|
|
DOTLOCK h, h2;
|
|
|
|
|
|
|
|
|
|
h = all_lockfiles;
|
|
|
|
|
all_lockfiles = NULL;
|
|
|
|
|
|
|
|
|
|
while ( h )
|
|
|
|
|
{
|
|
|
|
|
h2 = h->next;
|
|
|
|
|
destroy_dotlock (h);
|
|
|
|
|
h = h2;
|
2003-01-09 12:36:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|