1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-09-24 15:31:41 +02:00

Replace file locking by the new portable dotlock code.

* include/dotlock.h: New.  From current gnupg master.
* util/dotlock.c: Ditto.  Include util.h.  The major changes done in
master are: Factor Unix and W32 specific code out into specific
functions.  Define HAVE_POSIX_SYSTEM.  Rearrange some functions.
(disable_dotlock): Rename to dotlock_disable.
(create_dotlock): Rename to dotlock_create and add a dummy arg.
(destroy_dotlock): Rename to dotlock_destroy.
(make_dotlock): Rename to dotlock_take.
(release_dotlock): Rename to dotlock_release.
(remove_lockfiles): Rename to dotlock_remove_lockfiles.
This commit is contained in:
Werner Koch 2012-01-10 15:16:44 +01:00
parent dccdcef319
commit b9333cd890
10 changed files with 1432 additions and 643 deletions

View File

@ -933,7 +933,8 @@ AM_CONDITIONAL(ENABLE_AGENT_SUPPORT, test "$agent_support" = yes)
dnl Checks for header files. dnl Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS([unistd.h langinfo.h termio.h locale.h getopt.h pwd.h]) AC_CHECK_HEADERS([unistd.h langinfo.h termio.h locale.h getopt.h pwd.h \
signal.h])
# Note that we do not check for iconv here because this is done anyway # Note that we do not check for iconv here because this is done anyway
# by the gettext checks and thus it allows us to disable the use of # by the gettext checks and thus it allows us to disable the use of

View File

@ -1895,7 +1895,7 @@ main (int argc, char **argv )
secure_randoxmalloc(); /* put random number into secure memory */ secure_randoxmalloc(); /* put random number into secure memory */
may_coredump = disable_core_dumps(); may_coredump = disable_core_dumps();
init_signals(); init_signals();
create_dotlock(NULL); /* register locking cleanup */ dotlock_create (NULL, 0); /* Register locking cleanup. */
i18n_init(); i18n_init();
opt.command_fd = -1; /* no command fd */ opt.command_fd = -1; /* no command fd */
opt.compress_level = -1; /* defaults to standard compress level */ opt.compress_level = -1; /* defaults to standard compress level */
@ -2607,7 +2607,7 @@ main (int argc, char **argv )
case oNoEscapeFrom: opt.escape_from = 0; break; case oNoEscapeFrom: opt.escape_from = 0; break;
case oLockOnce: opt.lock_once = 1; break; case oLockOnce: opt.lock_once = 1; break;
case oLockNever: case oLockNever:
disable_dotlock (); dotlock_disable ();
random_disable_locking (); random_disable_locking ();
break; break;
case oLockMultiple: case oLockMultiple:

View File

@ -148,7 +148,7 @@ main( int argc, char **argv )
tty_no_terminal(1); tty_no_terminal(1);
tty_batchmode(1); tty_batchmode(1);
disable_dotlock(); dotlock_disable ();
set_native_charset (NULL); /* Try to auto set the character set */ set_native_charset (NULL); /* Try to auto set the character set */
@ -430,9 +430,10 @@ void rl_free_line_state (void) {}
#endif #endif
/* We do not do any locking, so use these stubs here */ /* We do not do any locking, so use these stubs here */
void disable_dotlock(void) {} void dotlock_disable(void) {}
DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; } dotlock_t dotlock_create (const char *file_to_lock, unsigned int flags)
void destroy_dotlock (DOTLOCK h) {} { return NULL; }
int make_dotlock( DOTLOCK h, long timeout ) { return 0;} void dotlock_destroy (dotlock_t h) {}
int release_dotlock( DOTLOCK h ) {return 0;} int dotlock_take (dotlock_t h, long timeout) { return 0;}
void remove_lockfiles(void) {} int dotlock_release (dotlock_t h) {return 0;}
void dotlock_remove_lockfiles (void) {}

View File

@ -76,7 +76,7 @@ static void unlock_all (KEYDB_HANDLE hd);
static int static int
maybe_create_keyring (char *filename, int force) maybe_create_keyring (char *filename, int force)
{ {
DOTLOCK lockhd = NULL; dotlock_t lockhd = NULL;
IOBUF iobuf; IOBUF iobuf;
int rc; int rc;
mode_t oldmask; mode_t oldmask;
@ -120,7 +120,7 @@ maybe_create_keyring (char *filename, int force)
/* To avoid races with other instances of gpg trying to create or /* To avoid races with other instances of gpg trying to create or
update the keyring (it is removed during an update for a short update the keyring (it is removed during an update for a short
time), we do the next stuff in a locked state. */ time), we do the next stuff in a locked state. */
lockhd = create_dotlock (filename); lockhd = dotlock_create (filename, 0);
if (!lockhd) if (!lockhd)
{ {
/* A reason for this to fail is that the directory is not /* A reason for this to fail is that the directory is not
@ -136,7 +136,7 @@ maybe_create_keyring (char *filename, int force)
return G10ERR_GENERAL; return G10ERR_GENERAL;
} }
if ( make_dotlock (lockhd, -1) ) if ( dotlock_take (lockhd, -1) )
{ {
/* This is something bad. Probably a stale lockfile. */ /* This is something bad. Probably a stale lockfile. */
log_info ("can't lock `%s'\n", filename ); log_info ("can't lock `%s'\n", filename );
@ -180,8 +180,8 @@ maybe_create_keyring (char *filename, int force)
leave: leave:
if (lockhd) if (lockhd)
{ {
release_dotlock (lockhd); dotlock_release (lockhd);
destroy_dotlock (lockhd); dotlock_destroy (lockhd);
} }
return rc; return rc;
} }

View File

@ -52,7 +52,7 @@ typedef struct keyring_name *KR_NAME;
struct keyring_name { struct keyring_name {
struct keyring_name *next; struct keyring_name *next;
int secret; int secret;
DOTLOCK lockhd; dotlock_t lockhd;
int is_locked; int is_locked;
int did_full_scan; int did_full_scan;
char fname[1]; char fname[1];
@ -302,7 +302,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
if (!keyring_is_writable(kr)) if (!keyring_is_writable(kr))
continue; continue;
if (!kr->lockhd) { if (!kr->lockhd) {
kr->lockhd = create_dotlock( kr->fname ); kr->lockhd = dotlock_create (kr->fname, 0);
if (!kr->lockhd) { if (!kr->lockhd) {
log_info ("can't allocate lock for `%s'\n", kr->fname ); log_info ("can't allocate lock for `%s'\n", kr->fname );
rc = G10ERR_GENERAL; rc = G10ERR_GENERAL;
@ -318,7 +318,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
continue; continue;
if (kr->is_locked) if (kr->is_locked)
; ;
else if (make_dotlock (kr->lockhd, -1) ) { else if (dotlock_take (kr->lockhd, -1) ) {
log_info ("can't lock `%s'\n", kr->fname ); log_info ("can't lock `%s'\n", kr->fname );
rc = G10ERR_GENERAL; rc = G10ERR_GENERAL;
} }
@ -333,7 +333,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
continue; continue;
if (!kr->is_locked) if (!kr->is_locked)
; ;
else if (release_dotlock (kr->lockhd)) else if (dotlock_release (kr->lockhd))
log_info ("can't unlock `%s'\n", kr->fname ); log_info ("can't unlock `%s'\n", kr->fname );
else else
kr->is_locked = 0; kr->is_locked = 0;

View File

@ -122,7 +122,7 @@ got_fatal_signal( int sig )
/* Reset action to default action and raise signal again. */ /* Reset action to default action and raise signal again. */
init_one_signal (sig, SIG_DFL, 0); init_one_signal (sig, SIG_DFL, 0);
remove_lockfiles (); dotlock_remove_lockfiles ();
#ifdef __riscos__ #ifdef __riscos__
riscos_close_fds (); riscos_close_fds ();
#endif /* __riscos__ */ #endif /* __riscos__ */

View File

@ -86,7 +86,7 @@ struct cmp_xdir_struct {
static char *db_name; static char *db_name;
static DOTLOCK lockhandle; static dotlock_t lockhandle;
static int is_locked; static int is_locked;
static int db_fd = -1; static int db_fd = -1;
static int in_transaction; static int in_transaction;
@ -248,7 +248,7 @@ put_record_into_cache( ulong recno, const char *data )
if( !n ) if( !n )
n = 1; n = 1;
if( !is_locked ) { if( !is_locked ) {
if( make_dotlock( lockhandle, -1 ) ) if (dotlock_take (lockhandle, -1))
log_fatal("can't acquire lock - giving up\n"); log_fatal("can't acquire lock - giving up\n");
else else
is_locked = 1; is_locked = 1;
@ -267,7 +267,7 @@ put_record_into_cache( ulong recno, const char *data )
} }
} }
if( !opt.lock_once ) { if( !opt.lock_once ) {
if( !release_dotlock( lockhandle ) ) if (!dotlock_release (lockhandle))
is_locked = 0; is_locked = 0;
} }
assert( unused ); assert( unused );
@ -309,7 +309,7 @@ tdbio_sync()
return 0; return 0;
if( !is_locked ) { if( !is_locked ) {
if( make_dotlock( lockhandle, -1 ) ) if (dotlock_take (lockhandle, -1))
log_fatal("can't acquire lock - giving up\n"); log_fatal("can't acquire lock - giving up\n");
else else
is_locked = 1; is_locked = 1;
@ -324,7 +324,7 @@ tdbio_sync()
} }
cache_is_dirty = 0; cache_is_dirty = 0;
if( did_lock && !opt.lock_once ) { if( did_lock && !opt.lock_once ) {
if( !release_dotlock( lockhandle ) ) if (!dotlock_release (lockhandle))
is_locked = 0; is_locked = 0;
} }
@ -364,7 +364,7 @@ tdbio_end_transaction()
if( !in_transaction ) if( !in_transaction )
log_bug("tdbio: no active transaction\n"); log_bug("tdbio: no active transaction\n");
if( !is_locked ) { if( !is_locked ) {
if( make_dotlock( lockhandle, -1 ) ) if (dotlock_take (lockhandle, -1))
log_fatal("can't acquire lock - giving up\n"); log_fatal("can't acquire lock - giving up\n");
else else
is_locked = 1; is_locked = 1;
@ -374,7 +374,7 @@ tdbio_end_transaction()
rc = tdbio_sync(); rc = tdbio_sync();
unblock_all_signals(); unblock_all_signals();
if( !opt.lock_once ) { if( !opt.lock_once ) {
if( !release_dotlock( lockhandle ) ) if (!dotlock_release (lockhandle))
is_locked = 0; is_locked = 0;
} }
return rc; return rc;
@ -414,7 +414,7 @@ static void
cleanup(void) cleanup(void)
{ {
if( is_locked ) { if( is_locked ) {
if( !release_dotlock(lockhandle) ) if (!dotlock_release (lockhandle))
is_locked = 0; is_locked = 0;
} }
} }
@ -517,10 +517,10 @@ tdbio_set_dbname( const char *new_dbname, int create )
db_name = fname; db_name = fname;
#ifdef __riscos__ #ifdef __riscos__
if( !lockhandle ) if( !lockhandle )
lockhandle = create_dotlock( db_name ); lockhandle = dotlock_create (db_name, 0);
if( !lockhandle ) if( !lockhandle )
log_fatal( _("can't create lock for `%s'\n"), db_name ); log_fatal( _("can't create lock for `%s'\n"), db_name );
if( make_dotlock( lockhandle, -1 ) ) if (dotlock_take (lockhandle, -1))
log_fatal( _("can't lock `%s'\n"), db_name ); log_fatal( _("can't lock `%s'\n"), db_name );
#endif /* __riscos__ */ #endif /* __riscos__ */
oldmask=umask(077); oldmask=umask(077);
@ -540,7 +540,7 @@ tdbio_set_dbname( const char *new_dbname, int create )
#ifndef __riscos__ #ifndef __riscos__
if( !lockhandle ) if( !lockhandle )
lockhandle = create_dotlock( db_name ); lockhandle = dotlock_create (db_name, 0);
if( !lockhandle ) if( !lockhandle )
log_fatal( _("can't create lock for `%s'\n"), db_name ); log_fatal( _("can't create lock for `%s'\n"), db_name );
#endif /* !__riscos__ */ #endif /* !__riscos__ */
@ -583,11 +583,11 @@ open_db()
assert( db_fd == -1 ); assert( db_fd == -1 );
if (!lockhandle ) if (!lockhandle )
lockhandle = create_dotlock( db_name ); lockhandle = dotlock_create (db_name, 0);
if (!lockhandle ) if (!lockhandle )
log_fatal( _("can't create lock for `%s'\n"), db_name ); log_fatal( _("can't create lock for `%s'\n"), db_name );
#ifdef __riscos__ #ifdef __riscos__
if (make_dotlock( lockhandle, -1 ) ) if (dotlock_take (lockhandle, -1))
log_fatal( _("can't lock `%s'\n"), db_name ); log_fatal( _("can't lock `%s'\n"), db_name );
#endif /* __riscos__ */ #endif /* __riscos__ */
db_fd = open (db_name, O_RDWR | MY_O_BINARY ); db_fd = open (db_name, O_RDWR | MY_O_BINARY );

112
include/dotlock.h Normal file
View File

@ -0,0 +1,112 @@
/* dotlock.h - dotfile locking declarations
* Copyright (C) 2000, 2001, 2006, 2011 Free Software Foundation, Inc.
*
* This file is part of JNLIB, which is a subsystem of GnuPG.
*
* JNLIB is free software; you can redistribute it and/or modify it
* under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* or
*
* - the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* or both in parallel, as here.
*
* 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
* General Public License for more details.
*
* You should have received a copies of the GNU General Public License
* and the GNU Lesser General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*
* ALTERNATIVELY, this file may be distributed under the terms of the
* following license, in which case the provisions of this license are
* required INSTEAD OF the GNU Lesser General License or the GNU
* General Public License. If you wish to allow use of your version of
* this file only under the terms of the GNU Lesser General License or
* the GNU General Public License, and not to allow others to use your
* version of this file under the terms of the following license,
* indicate your decision by deleting this paragraph and the license
* below.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef LIBJNLIB_DOTLOCK_H
#define LIBJNLIB_DOTLOCK_H
/* See dotlock.c for a description. */
#ifdef DOTLOCK_EXT_SYM_PREFIX
# ifndef _DOTLOCK_PREFIX
# define _DOTLOCK_PREFIX1(x,y) x ## y
# define _DOTLOCK_PREFIX2(x,y) _DOTLOCK_PREFIX1(x,y)
# define _DOTLOCK_PREFIX(x) _DOTLOCK_PREFIX2(DOTLOCK_EXT_SYM_PREFIX,x)
# endif /*_DOTLOCK_PREFIX*/
# define dotlock_disable _DOTLOCK_PREFIX(dotlock_disable)
# define dotlock_create _DOTLOCK_PREFIX(dotlock_create)
# define dotlock_set_fd _DOTLOCK_PREFIX(dotlock_set_fd)
# define dotlock_get_fd _DOTLOCK_PREFIX(dotlock_get_fd)
# define dotlock_destroy _DOTLOCK_PREFIX(dotlock_destroy)
# define dotlock_take _DOTLOCK_PREFIX(dotlock_take)
# define dotlock_release _DOTLOCK_PREFIX(dotlock_release)
# define dotlock_remove_lockfiles _DOTLOCK_PREFIX(dotlock_remove_lockfiles)
#endif /*DOTLOCK_EXT_SYM_PREFIX*/
#ifdef __cplusplus
extern "C"
{
#if 0
}
#endif
#endif
struct dotlock_handle;
typedef struct dotlock_handle *dotlock_t;
void dotlock_disable (void);
dotlock_t dotlock_create (const char *file_to_lock, unsigned int flags);
void dotlock_set_fd (dotlock_t h, int fd);
int dotlock_get_fd (dotlock_t h);
void dotlock_destroy (dotlock_t h);
int dotlock_take (dotlock_t h, long timeout);
int dotlock_release (dotlock_t h);
void dotlock_remove_lockfiles (void);
#ifdef __cplusplus
}
#endif
#endif /*LIBJNLIB_DOTLOCK_H*/

View File

@ -124,15 +124,7 @@ const char *strusage( int level );
/*-- dotlock.c --*/ /*-- dotlock.c --*/
struct dotlock_handle; #include "../include/dotlock.h"
typedef struct dotlock_handle *DOTLOCK;
void disable_dotlock(void);
DOTLOCK create_dotlock( const char *file_to_lock );
void destroy_dotlock ( DOTLOCK h );
int make_dotlock( DOTLOCK h, long timeout );
int release_dotlock( DOTLOCK h );
void remove_lockfiles (void);
/*-- fileutil.c --*/ /*-- fileutil.c --*/
char * make_basename(const char *filepath, const char *inputpath); char * make_basename(const char *filepath, const char *inputpath);

File diff suppressed because it is too large Load Diff