mirror of
git://git.gnupg.org/gnupg.git
synced 2025-01-03 12:11:33 +01:00
Implement dotlocking for W32.
This commit is contained in:
parent
04e965c7fc
commit
5bc7732a73
@ -589,7 +589,7 @@ do_compressed( IOBUF out, int ctb, PKT_compressed *cd )
|
|||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
/* We must use the old convention and don't use blockmode for tyhe
|
/* We must use the old convention and don't use blockmode for the
|
||||||
sake of PGP 2 compatibility. However if the new_ctb flag was
|
sake of PGP 2 compatibility. However if the new_ctb flag was
|
||||||
set, CTB is already formatted as new style and write_header2
|
set, CTB is already formatted as new style and write_header2
|
||||||
does create a partial length encoding using new the new
|
does create a partial length encoding using new the new
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
2008-06-13 Werner Koch <wk@g10code.com>
|
||||||
|
|
||||||
|
* dotlock.c: Reformat code and implement locking for W32.
|
||||||
|
(create_dotlock): Use snprintf.
|
||||||
|
|
||||||
2008-06-11 Werner Koch <wk@g10code.com>
|
2008-06-11 Werner Koch <wk@g10code.com>
|
||||||
|
|
||||||
* utf8conv.c: Remove useless variable ACTIVE_CHARSET. Suggested
|
* utf8conv.c: Remove useless variable ACTIVE_CHARSET. Suggested
|
||||||
|
312
jnlib/dotlock.c
312
jnlib/dotlock.c
@ -1,6 +1,6 @@
|
|||||||
/* dotlock.c - dotfile locking
|
/* dotlock.c - dotfile locking
|
||||||
* Copyright (C) 1998, 2000, 2001, 2003, 2004,
|
* Copyright (C) 1998, 2000, 2001, 2003, 2004,
|
||||||
* 2005, 2006 Free Software Foundation, Inc.
|
* 2005, 2006, 2008 Free Software Foundation, Inc.
|
||||||
*
|
*
|
||||||
* This file is part of JNLIB.
|
* This file is part of JNLIB.
|
||||||
*
|
*
|
||||||
@ -25,7 +25,10 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
|
# define WIN32_LEAN_AND_MEAN
|
||||||
|
# include <windows.h>
|
||||||
|
#else
|
||||||
# include <sys/utsname.h>
|
# include <sys/utsname.h>
|
||||||
#endif
|
#endif
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@ -54,57 +57,81 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* The object describing a lock. */
|
||||||
struct dotlock_handle
|
struct dotlock_handle
|
||||||
{
|
{
|
||||||
struct dotlock_handle *next;
|
struct dotlock_handle *next;
|
||||||
char *tname; /* Name of lockfile template. */
|
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. */
|
||||||
size_t nodename_off; /* Offset in TNAME of the nodename part. */
|
size_t nodename_off; /* Offset in TNAME of the nodename part. */
|
||||||
size_t nodename_len; /* Length of the nodename part. */
|
size_t nodename_len; /* Length of the nodename part. */
|
||||||
char *lockname; /* Name of the real lockfile. */
|
#endif /* HAVE_DOSISH_SYSTEM */
|
||||||
int locked; /* Lock status. */
|
|
||||||
int disable; /* When true, locking is disabled. */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* A list of of all lock handles. */
|
||||||
static volatile DOTLOCK all_lockfiles;
|
static volatile DOTLOCK all_lockfiles;
|
||||||
|
|
||||||
|
/* If this has the value true all locking is disabled. */
|
||||||
static int never_lock;
|
static int never_lock;
|
||||||
|
|
||||||
static int read_lockfile (DOTLOCK h, int *same_node);
|
|
||||||
|
|
||||||
|
/* Local protototypes. */
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
|
static int read_lockfile (DOTLOCK h, int *same_node);
|
||||||
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* 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. */
|
||||||
void
|
void
|
||||||
disable_dotlock(void)
|
disable_dotlock(void)
|
||||||
{
|
{
|
||||||
never_lock = 1;
|
never_lock = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************
|
|
||||||
* Create a lockfile with the given name and return an object of
|
|
||||||
* type DOTLOCK which may be used later to actually do the lock.
|
/* Create a lockfile for a file name FILE_TO_LOCK and returns an
|
||||||
* A cleanup routine gets installed to cleanup left over locks
|
object of type DOTLOCK which may be used later to actually acquire
|
||||||
* or other files used together with the lock mechanism.
|
the lock. A cleanup routine gets installed to cleanup left over
|
||||||
* Although the function is called dotlock, this does not necessarily
|
locks or other files used internally by the lock mechanism.
|
||||||
* mean that real lockfiles are used - the function may decide to
|
|
||||||
* use fcntl locking. Calling the function with NULL only install
|
Calling this function with NULL does only install the atexit
|
||||||
* the atexit handler and maybe used to assure that the cleanup
|
handler and may thus be used to assure that the cleanup is called
|
||||||
* is called after all other atexit handlers.
|
after all other atexit handlers.
|
||||||
*
|
|
||||||
* Notes: This function creates a lock file in the same directory
|
This function creates a lock file in the same directory as
|
||||||
* as file_to_lock with the name "file_to_lock.lock"
|
FILE_TO_LOCK using that name and a suffix of ".lock". Note that on
|
||||||
* A temporary file ".#lk.<hostname>.pid[.threadid] is used.
|
POSIX systems a temporary file ".#lk.<hostname>.pid[.threadid] is
|
||||||
* This function does nothing for Windoze.
|
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.
|
||||||
*/
|
*/
|
||||||
DOTLOCK
|
DOTLOCK
|
||||||
create_dotlock (const char *file_to_lock)
|
create_dotlock (const char *file_to_lock)
|
||||||
{
|
{
|
||||||
static int initialized;
|
static int initialized;
|
||||||
DOTLOCK h;
|
DOTLOCK h;
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
char pidstr[16];
|
char pidstr[16];
|
||||||
const char *nodename;
|
const char *nodename;
|
||||||
const char *dirpart;
|
const char *dirpart;
|
||||||
int dirpartlen;
|
int dirpartlen;
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
|
||||||
struct utsname utsbuf;
|
struct utsname utsbuf;
|
||||||
|
size_t tnamelen;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ( !initialized )
|
if ( !initialized )
|
||||||
@ -112,10 +139,14 @@ create_dotlock( const char *file_to_lock )
|
|||||||
atexit (dotlock_remove_lockfiles);
|
atexit (dotlock_remove_lockfiles);
|
||||||
initialized = 1;
|
initialized = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !file_to_lock )
|
if ( !file_to_lock )
|
||||||
return NULL; /* Only initialization was requested. */
|
return NULL; /* Only initialization was requested. */
|
||||||
|
|
||||||
h = jnlib_xcalloc ( 1, sizeof *h );
|
h = jnlib_calloc (1, sizeof *h);
|
||||||
|
if (!h)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (never_lock)
|
if (never_lock)
|
||||||
{
|
{
|
||||||
h->disable = 1;
|
h->disable = 1;
|
||||||
@ -128,7 +159,12 @@ create_dotlock( const char *file_to_lock )
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
sprintf (pidstr, "%10d\n", (int)getpid() );
|
/*
|
||||||
|
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() );
|
||||||
/* fixme: add the hostname to the second line (FQDN or IP addr?) */
|
/* fixme: add the hostname to the second line (FQDN or IP addr?) */
|
||||||
|
|
||||||
/* Create a temporary file. */
|
/* Create a temporary file. */
|
||||||
@ -163,16 +199,26 @@ create_dotlock( const char *file_to_lock )
|
|||||||
h->next = all_lockfiles;
|
h->next = all_lockfiles;
|
||||||
all_lockfiles = h;
|
all_lockfiles = h;
|
||||||
|
|
||||||
h->tname = jnlib_xmalloc ( dirpartlen + 6+30+ strlen(nodename) + 11 );
|
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;
|
||||||
|
}
|
||||||
h->nodename_len = strlen (nodename);
|
h->nodename_len = strlen (nodename);
|
||||||
|
|
||||||
#ifndef __riscos__
|
#ifndef __riscos__
|
||||||
sprintf (h->tname, "%.*s/.#lk%p.", dirpartlen, dirpart, h );
|
snprintf (h->tname, tnamelen, "%.*s/.#lk%p.", dirpartlen, dirpart, h );
|
||||||
h->nodename_off = strlen (h->tname);
|
h->nodename_off = strlen (h->tname);
|
||||||
sprintf (h->tname+h->nodename_off, "%s.%d", nodename, (int)getpid ());
|
snprintf (h->tname+h->nodename_off, tnamelen - h->nodename_off,
|
||||||
|
"%s.%d", nodename, (int)getpid ());
|
||||||
#else /* __riscos__ */
|
#else /* __riscos__ */
|
||||||
sprintf (h->tname, "%.*s.lk%p/", dirpartlen, dirpart, h );
|
snprintf (h->tname, tnamelen, "%.*s.lk%p/", dirpartlen, dirpart, h );
|
||||||
h->nodename_off = strlen (h->tname);
|
h->nodename_off = strlen (h->tname);
|
||||||
sprintf (h->tname+h->nodename_off, "%s/%d", nodename, (int)getpid () );
|
snprintf (h->tname+h->nodename_off, tnamelen - h->modename_off,
|
||||||
|
"%s/%d", nodename, (int)getpid () );
|
||||||
#endif /* __riscos__ */
|
#endif /* __riscos__ */
|
||||||
|
|
||||||
do
|
do
|
||||||
@ -186,7 +232,7 @@ create_dotlock( const char *file_to_lock )
|
|||||||
if ( fd == -1 )
|
if ( fd == -1 )
|
||||||
{
|
{
|
||||||
all_lockfiles = h->next;
|
all_lockfiles = h->next;
|
||||||
log_error ( "failed to create temporary file `%s': %s\n",
|
log_error (_("failed to create temporary file `%s': %s\n"),
|
||||||
h->tname, strerror(errno));
|
h->tname, strerror(errno));
|
||||||
jnlib_free (h->tname);
|
jnlib_free (h->tname);
|
||||||
jnlib_free (h);
|
jnlib_free (h);
|
||||||
@ -204,32 +250,85 @@ create_dotlock( const char *file_to_lock )
|
|||||||
# ifdef _REENTRANT
|
# ifdef _REENTRANT
|
||||||
/* release mutex */
|
/* release mutex */
|
||||||
# endif
|
# endif
|
||||||
#endif /* !HAVE_DOSISH_SYSTEM */
|
h->lockname = jnlib_malloc ( strlen (file_to_lock) + 6 );
|
||||||
h->lockname = jnlib_xmalloc ( strlen (file_to_lock) + 6 );
|
if (!h->lockname)
|
||||||
strcpy (stpcpy(h->lockname, file_to_lock), EXTSEP_S "lock");
|
{
|
||||||
return h;
|
|
||||||
write_failed:
|
|
||||||
all_lockfiles = h->next;
|
all_lockfiles = h->next;
|
||||||
# ifdef _REENTRANT
|
|
||||||
/* fixme: release mutex */
|
|
||||||
# endif
|
|
||||||
log_error ( "error writing to `%s': %s\n", h->tname, strerror(errno) );
|
|
||||||
close(fd);
|
|
||||||
unlink (h->tname);
|
unlink (h->tname);
|
||||||
jnlib_free (h->tname);
|
jnlib_free (h->tname);
|
||||||
jnlib_free (h);
|
jnlib_free (h);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
strcpy (stpcpy (h->lockname, file_to_lock), EXTSEP_S "lock");
|
||||||
|
return h;
|
||||||
|
|
||||||
|
write_failed:
|
||||||
|
all_lockfiles = h->next;
|
||||||
|
# ifdef _REENTRANT
|
||||||
|
/* fixme: release mutex */
|
||||||
|
# endif
|
||||||
|
log_error ( _("error writing to `%s': %s\n"), h->tname, strerror(errno) );
|
||||||
|
close (fd);
|
||||||
|
unlink (h->tname);
|
||||||
|
jnlib_free (h->tname);
|
||||||
|
jnlib_free (h);
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
#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 */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Destroy the local handle H and release the lock. */
|
||||||
void
|
void
|
||||||
destroy_dotlock ( DOTLOCK h )
|
destroy_dotlock ( DOTLOCK h )
|
||||||
{
|
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
|
||||||
if ( h )
|
|
||||||
{
|
{
|
||||||
DOTLOCK hprev, htmp;
|
DOTLOCK hprev, htmp;
|
||||||
|
|
||||||
|
if ( !h )
|
||||||
|
return;
|
||||||
|
|
||||||
/* First remove the handle from our global list of all locks. */
|
/* First remove the handle from our global list of all locks. */
|
||||||
for (hprev=NULL, htmp=all_lockfiles; htmp; hprev=htmp, htmp=htmp->next)
|
for (hprev=NULL, htmp=all_lockfiles; htmp; hprev=htmp, htmp=htmp->next)
|
||||||
if (htmp == h)
|
if (htmp == h)
|
||||||
@ -242,23 +341,29 @@ destroy_dotlock ( DOTLOCK h )
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Second destroy the lock. */
|
/* Then destroy the lock. */
|
||||||
if (!h->disable)
|
if (!h->disable)
|
||||||
{
|
{
|
||||||
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
|
if (h->locked)
|
||||||
|
{
|
||||||
|
UnlockFile (h->lockhd, 0, 0, 1, 0);
|
||||||
|
}
|
||||||
|
CloseHandle (h->lockhd);
|
||||||
|
#else /* !HAVE_DOSISH_SYSTEM */
|
||||||
if (h->locked && h->lockname)
|
if (h->locked && h->lockname)
|
||||||
unlink (h->lockname);
|
unlink (h->lockname);
|
||||||
if (h->tname)
|
if (h->tname)
|
||||||
unlink (h->tname);
|
unlink (h->tname);
|
||||||
jnlib_free (h->tname);
|
jnlib_free (h->tname);
|
||||||
|
#endif /* HAVE_DOSISH_SYSTEM */
|
||||||
jnlib_free (h->lockname);
|
jnlib_free (h->lockname);
|
||||||
}
|
}
|
||||||
jnlib_free(h);
|
jnlib_free(h);
|
||||||
}
|
}
|
||||||
#endif /*!HAVE_DOSISH_SYSTEM*/
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
static int
|
static int
|
||||||
maybe_deadlock( DOTLOCK h )
|
maybe_deadlock( DOTLOCK h )
|
||||||
{
|
{
|
||||||
@ -271,22 +376,22 @@ maybe_deadlock( DOTLOCK h )
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
||||||
|
|
||||||
/****************
|
|
||||||
* Do a lock on H. A TIMEOUT of 0 returns immediately, -1 waits
|
|
||||||
* forever (hopefully not), other values are reserved (should then be
|
/* Do a lock on H. A TIMEOUT of 0 returns immediately, -1 waits
|
||||||
* timeouts in milliseconds). Returns: 0 on success
|
forever (hopefully not), other values are reserved (should then be
|
||||||
*/
|
timeouts in milliseconds). Returns: 0 on success */
|
||||||
int
|
int
|
||||||
make_dotlock ( DOTLOCK h, long timeout )
|
make_dotlock ( DOTLOCK h, long timeout )
|
||||||
{
|
{
|
||||||
#ifdef HAVE_DOSISH_SYSTEM
|
int backoff = 0;
|
||||||
return 0;
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
#else
|
|
||||||
int pid;
|
int pid;
|
||||||
const char *maybe_dead="";
|
const char *maybe_dead="";
|
||||||
int backoff=0;
|
|
||||||
int same_node;
|
int same_node;
|
||||||
|
#endif /*!HAVE_DOSISH_SYSTEM*/
|
||||||
|
|
||||||
if ( h->disable )
|
if ( h->disable )
|
||||||
return 0; /* Locks are completely disabled. Return success. */
|
return 0; /* Locks are completely disabled. Return success. */
|
||||||
@ -294,13 +399,14 @@ make_dotlock( DOTLOCK h, long timeout )
|
|||||||
if ( h->locked )
|
if ( h->locked )
|
||||||
{
|
{
|
||||||
#ifndef __riscos__
|
#ifndef __riscos__
|
||||||
log_debug("oops, `%s' is already locked\n", h->lockname );
|
log_debug ("Oops, `%s' is already locked\n", h->lockname);
|
||||||
#endif /* !__riscos__ */
|
#endif /* !__riscos__ */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
# ifndef __riscos__
|
# ifndef __riscos__
|
||||||
if ( !link(h->tname, h->lockname) )
|
if ( !link(h->tname, h->lockname) )
|
||||||
{
|
{
|
||||||
@ -345,13 +451,13 @@ make_dotlock( DOTLOCK h, long timeout )
|
|||||||
else if ( same_node && kill (pid, 0) && errno == ESRCH )
|
else if ( same_node && kill (pid, 0) && errno == ESRCH )
|
||||||
{
|
{
|
||||||
# ifndef __riscos__
|
# ifndef __riscos__
|
||||||
log_info ("removing stale lockfile (created by %d)", pid );
|
log_info (_("removing stale lockfile (created by %d)\n"), pid );
|
||||||
unlink (h->lockname);
|
unlink (h->lockname);
|
||||||
continue;
|
continue;
|
||||||
# else /* __riscos__ */
|
# else /* __riscos__ */
|
||||||
/* Under RISCOS we are *pretty* sure that the other task
|
/* Under RISCOS we are *pretty* sure that the other task
|
||||||
is dead and therefore we remove the stale lock file. */
|
is dead and therefore we remove the stale lock file. */
|
||||||
maybe_dead = " - probably dead - removing lock";
|
maybe_dead = _(" - probably dead - removing lock");
|
||||||
unlink(h->lockname);
|
unlink(h->lockname);
|
||||||
# endif /* __riscos__ */
|
# endif /* __riscos__ */
|
||||||
}
|
}
|
||||||
@ -361,8 +467,8 @@ make_dotlock( DOTLOCK h, long timeout )
|
|||||||
/* Wait until lock has been released. */
|
/* Wait until lock has been released. */
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
log_info ("waiting for lock (held by %d%s) %s...\n",
|
log_info (_("waiting for lock (held by %d%s) %s...\n"),
|
||||||
pid, maybe_dead, maybe_deadlock(h)? "(deadlock?) ":"");
|
pid, maybe_dead, maybe_deadlock(h)? _("(deadlock?) "):"");
|
||||||
|
|
||||||
|
|
||||||
/* We can't use sleep, cause signals may be blocked. */
|
/* We can't use sleep, cause signals may be blocked. */
|
||||||
@ -374,24 +480,42 @@ make_dotlock( DOTLOCK h, long timeout )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
return -1;
|
return -1;
|
||||||
|
#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*/
|
||||||
}
|
}
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
#endif /* !HAVE_DOSISH_SYSTEM */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/****************
|
/* Release a lock. Returns 0 on success. */
|
||||||
* release a lock
|
|
||||||
* Returns: 0 := success
|
|
||||||
*/
|
|
||||||
int
|
int
|
||||||
release_dotlock( DOTLOCK h )
|
release_dotlock( DOTLOCK h )
|
||||||
{
|
{
|
||||||
#ifdef HAVE_DOSISH_SYSTEM
|
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
int pid, same_node;
|
|
||||||
|
|
||||||
/* To avoid atexit race conditions we first check whether there are
|
/* To avoid atexit race conditions we first check whether there are
|
||||||
any locks left. It might happen that another atexit handler
|
any locks left. It might happen that another atexit handler
|
||||||
tries to release the lock while the atexit handler of this module
|
tries to release the lock while the atexit handler of this module
|
||||||
@ -404,10 +528,20 @@ release_dotlock( DOTLOCK h )
|
|||||||
|
|
||||||
if ( !h->locked )
|
if ( !h->locked )
|
||||||
{
|
{
|
||||||
log_debug("oops, `%s' is not locked\n", h->lockname );
|
log_debug("Oops, `%s' is not locked\n", h->lockname);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#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
|
||||||
|
int pid, same_node;
|
||||||
|
|
||||||
pid = read_lockfile (h, &same_node);
|
pid = read_lockfile (h, &same_node);
|
||||||
if ( pid == -1 )
|
if ( pid == -1 )
|
||||||
{
|
{
|
||||||
@ -419,39 +553,38 @@ release_dotlock( DOTLOCK h )
|
|||||||
log_error( "release_dotlock: not our lock (pid=%d)\n", pid);
|
log_error( "release_dotlock: not our lock (pid=%d)\n", pid);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef __riscos__
|
#ifndef __riscos__
|
||||||
if ( unlink( h->lockname ) )
|
if ( unlink( h->lockname ) )
|
||||||
{
|
{
|
||||||
log_error( "release_dotlock: error removing lockfile `%s'",
|
log_error ("release_dotlock: error removing lockfile `%s'\n",
|
||||||
h->lockname);
|
h->lockname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
/* Fixme: As an extra check we could check whether the link count is
|
||||||
|
now really at 1. */
|
||||||
#else /* __riscos__ */
|
#else /* __riscos__ */
|
||||||
if ( renamefile (h->lockname, h->tname) )
|
if ( renamefile (h->lockname, h->tname) )
|
||||||
{
|
{
|
||||||
log_error( "release_dotlock: error renaming lockfile `%s' to `%s'",
|
log_error ("release_dotlock: error renaming lockfile `%s' to `%s'\n",
|
||||||
h->lockname, h->tname);
|
h->lockname, h->tname);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#endif /* __riscos__ */
|
#endif /* __riscos__ */
|
||||||
/* fixme: check that the link count is now 1 */
|
|
||||||
|
#endif /* !HAVE_DOSISH_SYSTEM */
|
||||||
h->locked = 0;
|
h->locked = 0;
|
||||||
return 0;
|
return 0;
|
||||||
#endif /* !HAVE_DOSISH_SYSTEM */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/* Read the lock file and return the pid, returns -1 on error. True
|
||||||
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
|
||||||
will be stored at SAME_NODE if the lock file has been created on
|
has been created on the same node. */
|
||||||
the same node.
|
#ifndef HAVE_DOSISH_SYSTEM
|
||||||
*/
|
|
||||||
static int
|
static int
|
||||||
read_lockfile (DOTLOCK h, int *same_node )
|
read_lockfile (DOTLOCK h, int *same_node )
|
||||||
{
|
{
|
||||||
#ifdef HAVE_DOSISH_SYSTEM
|
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
char buffer_space[10+1+70+1]; /* 70 is just an estimated value; node
|
char buffer_space[10+1+70+1]; /* 70 is just an estimated value; node
|
||||||
name are usually shorter. */
|
name are usually shorter. */
|
||||||
int fd;
|
int fd;
|
||||||
@ -505,7 +638,7 @@ read_lockfile (DOTLOCK h, int *same_node )
|
|||||||
log_info ("invalid size of lockfile `%s'", h->lockname );
|
log_info ("invalid size of lockfile `%s'", h->lockname );
|
||||||
if (buffer != buffer_space)
|
if (buffer != buffer_space)
|
||||||
jnlib_free (buffer);
|
jnlib_free (buffer);
|
||||||
errno = 0; /* Do not return an inappropriate ERRNO. */
|
errno = 0; /* Better don't return an inappropriate ERRNO. */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -533,14 +666,16 @@ read_lockfile (DOTLOCK h, int *same_node )
|
|||||||
if (buffer != buffer_space)
|
if (buffer != buffer_space)
|
||||||
jnlib_free (buffer);
|
jnlib_free (buffer);
|
||||||
return pid;
|
return pid;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif /* !HAVE_DOSISH_SYSTEM */
|
||||||
|
|
||||||
|
|
||||||
|
/* Remove all lockfiles. This is usually called by the atexit handler
|
||||||
|
installed by this module but may also be called by other
|
||||||
|
termination handlers. */
|
||||||
void
|
void
|
||||||
dotlock_remove_lockfiles()
|
dotlock_remove_lockfiles()
|
||||||
{
|
{
|
||||||
#ifndef HAVE_DOSISH_SYSTEM
|
|
||||||
DOTLOCK h, h2;
|
DOTLOCK h, h2;
|
||||||
|
|
||||||
h = all_lockfiles;
|
h = all_lockfiles;
|
||||||
@ -552,6 +687,5 @@ dotlock_remove_lockfiles()
|
|||||||
destroy_dotlock (h);
|
destroy_dotlock (h);
|
||||||
h = h2;
|
h = h2;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,6 +67,7 @@ g10/verify.c
|
|||||||
jnlib/argparse.c
|
jnlib/argparse.c
|
||||||
jnlib/logging.c
|
jnlib/logging.c
|
||||||
jnlib/utf8conv.c
|
jnlib/utf8conv.c
|
||||||
|
jnlib/dotlock.c
|
||||||
|
|
||||||
kbx/kbxutil.c
|
kbx/kbxutil.c
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user