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.
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
# 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 */
may_coredump = disable_core_dumps();
init_signals();
create_dotlock(NULL); /* register locking cleanup */
dotlock_create (NULL, 0); /* Register locking cleanup. */
i18n_init();
opt.command_fd = -1; /* no command fd */
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 oLockOnce: opt.lock_once = 1; break;
case oLockNever:
disable_dotlock ();
dotlock_disable ();
random_disable_locking ();
break;
case oLockMultiple:

View File

@ -148,7 +148,7 @@ main( int argc, char **argv )
tty_no_terminal(1);
tty_batchmode(1);
disable_dotlock();
dotlock_disable ();
set_native_charset (NULL); /* Try to auto set the character set */
@ -430,9 +430,10 @@ void rl_free_line_state (void) {}
#endif
/* We do not do any locking, so use these stubs here */
void disable_dotlock(void) {}
DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; }
void destroy_dotlock (DOTLOCK h) {}
int make_dotlock( DOTLOCK h, long timeout ) { return 0;}
int release_dotlock( DOTLOCK h ) {return 0;}
void remove_lockfiles(void) {}
void dotlock_disable(void) {}
dotlock_t dotlock_create (const char *file_to_lock, unsigned int flags)
{ return NULL; }
void dotlock_destroy (dotlock_t h) {}
int dotlock_take (dotlock_t h, long timeout) { return 0;}
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
maybe_create_keyring (char *filename, int force)
{
DOTLOCK lockhd = NULL;
dotlock_t lockhd = NULL;
IOBUF iobuf;
int rc;
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
update the keyring (it is removed during an update for a short
time), we do the next stuff in a locked state. */
lockhd = create_dotlock (filename);
lockhd = dotlock_create (filename, 0);
if (!lockhd)
{
/* 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;
}
if ( make_dotlock (lockhd, -1) )
if ( dotlock_take (lockhd, -1) )
{
/* This is something bad. Probably a stale lockfile. */
log_info ("can't lock `%s'\n", filename );
@ -180,8 +180,8 @@ maybe_create_keyring (char *filename, int force)
leave:
if (lockhd)
{
release_dotlock (lockhd);
destroy_dotlock (lockhd);
dotlock_release (lockhd);
dotlock_destroy (lockhd);
}
return rc;
}

View File

@ -30,7 +30,7 @@
#include "util.h"
#include "keyring.h"
#include "packet.h"
#include "keydb.h"
#include "keydb.h"
#include "options.h"
#include "main.h" /*for check_key_signature()*/
#include "i18n.h"
@ -45,14 +45,14 @@ struct off_item {
/*off_t off;*/
};
typedef struct off_item **OffsetHashTable;
typedef struct off_item **OffsetHashTable;
typedef struct keyring_name *KR_NAME;
struct keyring_name {
struct keyring_name *next;
int secret;
DOTLOCK lockhd;
dotlock_t lockhd;
int is_locked;
int did_full_scan;
char fname[1];
@ -76,7 +76,7 @@ struct keyring_handle {
int error;
} current;
struct {
CONST_KR_NAME kr;
CONST_KR_NAME kr;
off_t offset;
size_t pk_no;
size_t uid_no;
@ -99,7 +99,7 @@ static struct off_item *
new_offset_item (void)
{
struct off_item *k;
k = xmalloc_clear (sizeof *k);
return k;
}
@ -118,7 +118,7 @@ release_offset_items (struct off_item *k)
}
#endif
static OffsetHashTable
static OffsetHashTable
new_offset_hash_table (void)
{
struct off_item **tbl;
@ -159,7 +159,7 @@ update_offset_hash_table (OffsetHashTable tbl, u32 *kid, off_t off)
for (k = tbl[(kid[1] & 0x07ff)]; k; k = k->next)
{
if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
{
/*k->off = off;*/
return;
@ -189,7 +189,7 @@ update_offset_hash_table_from_kb (OffsetHashTable tbl, KBNODE node, off_t off)
}
}
/*
/*
* Register a filename for plain keyring files. ptr is set to a
* pointer to be used to create a handles etc, or the already-issued
* pointer if it has already been registered. The function returns 1
@ -241,12 +241,12 @@ keyring_is_writable (void *token)
return r? !access (r->fname, W_OK) : 0;
}
/* Create a new handle for the resource associated with TOKEN. SECRET
is just just as a cross-check.
The returned handle must be released using keyring_release (). */
KEYRING_HANDLE
keyring_new (void *token, int secret)
@ -255,7 +255,7 @@ keyring_new (void *token, int secret)
KR_NAME resource = token;
assert (resource && !resource->secret == !secret);
hd = xmalloc_clear (sizeof *hd);
hd->resource = resource;
hd->secret = !!secret;
@ -263,7 +263,7 @@ keyring_new (void *token, int secret)
return hd;
}
void
void
keyring_release (KEYRING_HANDLE hd)
{
if (!hd)
@ -290,7 +290,7 @@ keyring_get_resource_name (KEYRING_HANDLE hd)
* Lock the keyring with the given handle, or unlok if yes is false.
* We ignore the handle and lock all registered files.
*/
int
int
keyring_lock (KEYRING_HANDLE hd, int yes)
{
KR_NAME kr;
@ -302,7 +302,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
if (!keyring_is_writable(kr))
continue;
if (!kr->lockhd) {
kr->lockhd = create_dotlock( kr->fname );
kr->lockhd = dotlock_create (kr->fname, 0);
if (!kr->lockhd) {
log_info ("can't allocate lock for `%s'\n", kr->fname );
rc = G10ERR_GENERAL;
@ -311,18 +311,18 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
}
if (rc)
return rc;
/* and now set the locks */
for (kr=kr_names; kr; kr = kr->next) {
if (!keyring_is_writable(kr))
continue;
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 );
rc = G10ERR_GENERAL;
}
else
else
kr->is_locked = 1;
}
}
@ -333,12 +333,12 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
continue;
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 );
else
else
kr->is_locked = 0;
}
}
}
return rc;
}
@ -348,7 +348,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
/*
* Return the last found keyring. Caller must free it.
* The returned keyblock has the kbode flag bit 0 set for the node with
* the public key used to locate the keyblock or flag bit 1 set for
* the public key used to locate the keyblock or flag bit 1 set for
* the user ID node.
*/
int
@ -394,7 +394,7 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
init_packet (pkt);
continue;
}
if (rc) {
if (rc) {
log_error ("keyring_get_keyblock: read error: %s\n",
g10_errstr(rc) );
rc = G10ERR_INV_KEYRING;
@ -416,14 +416,14 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
in_cert = 1;
if (pkt->pkttype == PKT_RING_TRUST) {
/*(this code is duplicated after the loop)*/
if ( lastnode
if ( lastnode
&& lastnode->pkt->pkttype == PKT_SIGNATURE
&& (pkt->pkt.ring_trust->sigcache & 1) ) {
/* This is a ring trust packet with a checked signature
/* This is a ring trust packet with a checked signature
* status cache following directly a signature paket.
* Set the cache status into that signature packet. */
PKT_signature *sig = lastnode->pkt->pkt.signature;
sig->flags.checked = 1;
sig->flags.valid = !!(pkt->pkt.ring_trust->sigcache & 2);
}
@ -441,27 +441,27 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
keyblock = node;
else
add_kbnode (keyblock, node);
if ( pkt->pkttype == PKT_PUBLIC_KEY
|| pkt->pkttype == PKT_PUBLIC_SUBKEY
|| pkt->pkttype == PKT_SECRET_KEY
|| pkt->pkttype == PKT_SECRET_SUBKEY)
|| pkt->pkttype == PKT_SECRET_SUBKEY)
{
if (++pk_no == hd->found.pk_no)
node->flag |= 1;
}
else if ( pkt->pkttype == PKT_USER_ID)
else if ( pkt->pkttype == PKT_USER_ID)
{
if (++uid_no == hd->found.uid_no)
node->flag |= 2;
}
pkt = xmalloc (sizeof *pkt);
init_packet(pkt);
}
set_packet_list_mode(save_mode);
if (rc == -1 && keyblock)
if (rc == -1 && keyblock)
rc = 0; /* got the entire keyblock */
if (rc || !ret_kb)
@ -469,7 +469,7 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
else {
/*(duplicated form the loop body)*/
if ( pkt && pkt->pkttype == PKT_RING_TRUST
&& lastnode
&& lastnode
&& lastnode->pkt->pkttype == PKT_SIGNATURE
&& (pkt->pkt.ring_trust->sigcache & 1) ) {
PKT_signature *sig = lastnode->pkt->pkt.signature;
@ -483,7 +483,7 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
iobuf_close(a);
/* Make sure that future search operations fail immediately when
* we know that we are working on a invalid keyring
* we know that we are working on a invalid keyring
*/
if (rc == G10ERR_INV_KEYRING)
hd->current.error = rc;
@ -543,11 +543,11 @@ keyring_insert_keyblock (KEYRING_HANDLE hd, KBNODE kb)
fname = hd->found.kr->fname;
else if (hd->current.kr)
fname = hd->current.kr->fname;
else
else
fname = hd->resource? hd->resource->fname:NULL;
if (!fname)
return G10ERR_GENERAL;
return G10ERR_GENERAL;
/* close this one otherwise we will lose the position for
* a next search. Fixme: it would be better to adjust the position
@ -562,7 +562,7 @@ keyring_insert_keyblock (KEYRING_HANDLE hd, KBNODE kb)
{
update_offset_hash_table_from_kb (kr_offtbl, kb, 0);
}
return rc;
}
@ -608,10 +608,10 @@ keyring_delete_keyblock (KEYRING_HANDLE hd)
/*
/*
* Start the next search on this handle right at the beginning
*/
int
int
keyring_search_reset (KEYRING_HANDLE hd)
{
assert (hd);
@ -621,17 +621,17 @@ keyring_search_reset (KEYRING_HANDLE hd)
hd->current.iobuf = NULL;
hd->current.eof = 0;
hd->current.error = 0;
hd->found.kr = NULL;
hd->found.offset = 0;
return 0;
return 0;
}
static int
prepare_search (KEYRING_HANDLE hd)
{
if (hd->current.error)
if (hd->current.error)
return hd->current.error; /* still in error state */
if (hd->current.kr && !hd->current.eof) {
@ -640,7 +640,7 @@ prepare_search (KEYRING_HANDLE hd)
return 0; /* okay */
}
if (!hd->current.kr && hd->current.eof)
if (!hd->current.kr && hd->current.eof)
return -1; /* still EOF */
if (!hd->current.kr) { /* start search with first keyring */
@ -652,7 +652,7 @@ prepare_search (KEYRING_HANDLE hd)
assert (!hd->current.iobuf);
}
else { /* EOF */
iobuf_close (hd->current.iobuf);
iobuf_close (hd->current.iobuf);
hd->current.iobuf = NULL;
hd->current.kr = NULL;
hd->current.eof = 1;
@ -809,7 +809,7 @@ compare_name (int mode, const char *name, const char *uid, size_t uidlen)
int i;
const char *s, *se;
if (mode == KEYDB_SEARCH_MODE_EXACT) {
if (mode == KEYDB_SEARCH_MODE_EXACT) {
for (i=0; name[i] && uidlen; i++, uidlen--)
if (uid[i] != name[i])
break;
@ -820,7 +820,7 @@ compare_name (int mode, const char *name, const char *uid, size_t uidlen)
if (ascii_memistr( uid, uidlen, name ))
return 0;
}
else if ( mode == KEYDB_SEARCH_MODE_MAIL
else if ( mode == KEYDB_SEARCH_MODE_MAIL
|| mode == KEYDB_SEARCH_MODE_MAILSUB
|| mode == KEYDB_SEARCH_MODE_MAILEND) {
for (i=0, s= uid; i < uidlen && *s != '<'; s++, i++)
@ -832,7 +832,7 @@ compare_name (int mode, const char *name, const char *uid, size_t uidlen)
;
if (i < uidlen) {
i = se - s;
if (mode == KEYDB_SEARCH_MODE_MAIL) {
if (mode == KEYDB_SEARCH_MODE_MAIL) {
if( strlen(name)-2 == i
&& !ascii_memcasecmp( s, name+1, i) )
return 0;
@ -856,11 +856,11 @@ compare_name (int mode, const char *name, const char *uid, size_t uidlen)
}
/*
/*
* Search through the keyring(s), starting at the current position,
* for a keyblock which contains one of the keys described in the DESC array.
*/
int
int
keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
size_t ndesc, size_t *descindex)
{
@ -880,28 +880,28 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
/* figure out what information we need */
need_uid = need_words = need_keyid = need_fpr = any_skip = 0;
for (n=0; n < ndesc; n++)
for (n=0; n < ndesc; n++)
{
switch (desc[n].mode)
switch (desc[n].mode)
{
case KEYDB_SEARCH_MODE_EXACT:
case KEYDB_SEARCH_MODE_EXACT:
case KEYDB_SEARCH_MODE_SUBSTR:
case KEYDB_SEARCH_MODE_MAIL:
case KEYDB_SEARCH_MODE_MAILSUB:
case KEYDB_SEARCH_MODE_MAILEND:
need_uid = 1;
break;
case KEYDB_SEARCH_MODE_WORDS:
case KEYDB_SEARCH_MODE_WORDS:
need_uid = 1;
need_words = 1;
break;
case KEYDB_SEARCH_MODE_SHORT_KID:
case KEYDB_SEARCH_MODE_SHORT_KID:
case KEYDB_SEARCH_MODE_LONG_KID:
need_keyid = 1;
break;
case KEYDB_SEARCH_MODE_FPR16:
case KEYDB_SEARCH_MODE_FPR16:
case KEYDB_SEARCH_MODE_FPR20:
case KEYDB_SEARCH_MODE_FPR:
case KEYDB_SEARCH_MODE_FPR:
need_fpr = 1;
break;
case KEYDB_SEARCH_MODE_FIRST:
@ -910,7 +910,7 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
break;
default: break;
}
if (desc[n].skipfnc)
if (desc[n].skipfnc)
{
any_skip = 1;
need_keyid = 1;
@ -929,7 +929,7 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
else if (ndesc == 1 && desc[0].mode == KEYDB_SEARCH_MODE_LONG_KID)
{
struct off_item *oi;
oi = lookup_offset_hash_table (kr_offtbl, desc[0].u.kid);
if (!oi)
{ /* We know that we don't have this key */
@ -938,9 +938,9 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
return -1;
}
/* We could now create a positive search status and return.
* However the problem is that another instance of gpg may
* However the problem is that another instance of gpg may
* have changed the keyring so that the offsets are not valid
* anymore - therefore we don't do it
* anymore - therefore we don't do it
*/
}
@ -951,13 +951,13 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
log_debug ("word search mode does not yet work\n");
/* FIXME: here is a long standing bug in our function and in addition we
just use the first search description */
for (n=0; n < ndesc && !name; n++)
for (n=0; n < ndesc && !name; n++)
{
if (desc[n].mode == KEYDB_SEARCH_MODE_WORDS)
if (desc[n].mode == KEYDB_SEARCH_MODE_WORDS)
name = desc[n].u.name;
}
assert (name);
if ( !hd->word_match.name || strcmp (hd->word_match.name, name) )
if ( !hd->word_match.name || strcmp (hd->word_match.name, name) )
{
/* name changed */
xfree (hd->word_match.name);
@ -975,23 +975,23 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
main_offset = 0;
pk_no = uid_no = 0;
initial_skip = 1; /* skip until we see the start of a keyblock */
while (!(rc=search_packet (hd->current.iobuf, &pkt, &offset, need_uid)))
while (!(rc=search_packet (hd->current.iobuf, &pkt, &offset, need_uid)))
{
byte afp[MAX_FINGERPRINT_LEN];
size_t an;
if (pkt.pkttype == PKT_PUBLIC_KEY || pkt.pkttype == PKT_SECRET_KEY)
if (pkt.pkttype == PKT_PUBLIC_KEY || pkt.pkttype == PKT_SECRET_KEY)
{
main_offset = offset;
pk_no = uid_no = 0;
initial_skip = 0;
}
if (initial_skip)
if (initial_skip)
{
free_packet (&pkt);
continue;
}
pk = NULL;
sk = NULL;
uid = NULL;
@ -1012,13 +1012,13 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
if (use_offtbl && !kr_offtbl_ready)
update_offset_hash_table (kr_offtbl, aki, main_offset);
}
else if (pkt.pkttype == PKT_USER_ID)
else if (pkt.pkttype == PKT_USER_ID)
{
uid = pkt.pkt.user_id;
++uid_no;
}
else if ( pkt.pkttype == PKT_SECRET_KEY
|| pkt.pkttype == PKT_SECRET_SUBKEY)
|| pkt.pkttype == PKT_SECRET_SUBKEY)
{
sk = pkt.pkt.secret_key;
++pk_no;
@ -1030,28 +1030,28 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
}
if (need_keyid)
keyid_from_sk (sk, aki);
}
for (n=0; n < ndesc; n++)
for (n=0; n < ndesc; n++)
{
switch (desc[n].mode) {
case KEYDB_SEARCH_MODE_NONE:
case KEYDB_SEARCH_MODE_NONE:
BUG ();
break;
case KEYDB_SEARCH_MODE_EXACT:
case KEYDB_SEARCH_MODE_EXACT:
case KEYDB_SEARCH_MODE_SUBSTR:
case KEYDB_SEARCH_MODE_MAIL:
case KEYDB_SEARCH_MODE_MAILSUB:
case KEYDB_SEARCH_MODE_MAILEND:
case KEYDB_SEARCH_MODE_WORDS:
case KEYDB_SEARCH_MODE_WORDS:
if ( uid && !compare_name (desc[n].mode,
desc[n].u.name,
uid->name, uid->len))
uid->name, uid->len))
goto found;
break;
case KEYDB_SEARCH_MODE_SHORT_KID:
case KEYDB_SEARCH_MODE_SHORT_KID:
if ((pk||sk) && desc[n].u.kid[1] == aki[1])
goto found;
break;
@ -1065,19 +1065,19 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
goto found;
break;
case KEYDB_SEARCH_MODE_FPR20:
case KEYDB_SEARCH_MODE_FPR:
case KEYDB_SEARCH_MODE_FPR:
if ((pk||sk) && !memcmp (desc[n].u.fpr, afp, 20))
goto found;
break;
case KEYDB_SEARCH_MODE_FIRST:
case KEYDB_SEARCH_MODE_FIRST:
if (pk||sk)
goto found;
break;
case KEYDB_SEARCH_MODE_NEXT:
case KEYDB_SEARCH_MODE_NEXT:
if (pk||sk)
goto found;
break;
default:
default:
rc = G10ERR_INV_ARG;
goto found;
}
@ -1089,7 +1089,7 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
meaningful if this function returns with no errors. */
if(descindex)
*descindex=n;
for (n=any_skip?0:ndesc; n < ndesc; n++)
for (n=any_skip?0:ndesc; n < ndesc; n++)
{
if (desc[n].skipfnc
&& desc[n].skipfnc (desc[n].skipfncvalue, aki, uid))
@ -1115,12 +1115,12 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
if (use_offtbl && !kr_offtbl_ready)
{
KR_NAME kr;
/* First set the did_full_scan flag for this keyring (ignore
secret keyrings) */
for (kr=kr_names; kr; kr = kr->next)
{
if (!kr->secret && hd->resource == kr)
if (!kr->secret && hd->resource == kr)
{
kr->did_full_scan = 1;
break;
@ -1130,14 +1130,14 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
offtbl ready */
for (kr=kr_names; kr; kr = kr->next)
{
if (!kr->secret && !kr->did_full_scan)
if (!kr->secret && !kr->did_full_scan)
break;
}
if (!kr)
kr_offtbl_ready = 1;
}
}
else
else
hd->current.error = rc;
free_packet(&pkt);
@ -1149,7 +1149,7 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
static int
create_tmp_file (const char *template,
char **r_bakfname, char **r_tmpfname, IOBUF *r_fp)
{
{
char *bakfname, *tmpfname;
mode_t oldmask;
@ -1173,7 +1173,7 @@ create_tmp_file (const char *template,
strcpy (tmpfname,template);
strcpy (tmpfname+strlen(template)-4, EXTSEP_S "tmp");
}
else
else
{ /* file does not end with gpg; hmmm */
bakfname = xmalloc (strlen( template ) + 5);
strcpy (stpcpy(bakfname, template), EXTSEP_S "bak");
@ -1206,7 +1206,7 @@ create_tmp_file (const char *template,
xfree (bakfname);
return G10ERR_OPEN_FILE;
}
*r_bakfname = bakfname;
*r_tmpfname = tmpfname;
return 0;
@ -1233,7 +1233,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
/* first make a backup file except for secret keyrings */
if (!secret)
{
{
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
remove (bakfname);
#endif
@ -1244,7 +1244,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
return G10ERR_RENAME_FILE;
}
}
/* then rename the file */
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
remove( fname );
@ -1298,10 +1298,10 @@ write_keyblock (IOBUF fp, KBNODE keyblock)
{
KBNODE kbctx = NULL, node;
int rc;
while ( (node = walk_kbnode (keyblock, &kbctx, 0)) )
while ( (node = walk_kbnode (keyblock, &kbctx, 0)) )
{
if (node->pkt->pkttype == PKT_RING_TRUST)
if (node->pkt->pkttype == PKT_RING_TRUST)
continue; /* we write it later on our own */
if ( (rc = build_packet (fp, node->pkt) ))
@ -1310,12 +1310,12 @@ write_keyblock (IOBUF fp, KBNODE keyblock)
node->pkt->pkttype, g10_errstr(rc) );
return rc;
}
if (node->pkt->pkttype == PKT_SIGNATURE)
if (node->pkt->pkttype == PKT_SIGNATURE)
{ /* always write a signature cache packet */
PKT_signature *sig = node->pkt->pkt.signature;
unsigned int cacheval = 0;
if (sig->flags.checked)
if (sig->flags.checked)
{
cacheval |= 1;
if (sig->flags.valid)
@ -1333,7 +1333,7 @@ write_keyblock (IOBUF fp, KBNODE keyblock)
return 0;
}
/*
/*
* Walk over all public keyrings, check the signatures and replace the
* keyring with a new one where the signature cache is then updated.
* This is only done for the public keyrings.
@ -1378,7 +1378,7 @@ keyring_rebuild_cache (void *token,int noisy)
* the original file is closed */
tmpfp = NULL;
}
rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
lastresname, 0) : 0;
xfree (tmpfilename); tmpfilename = NULL;
xfree (bakfilename); bakfilename = NULL;
@ -1391,10 +1391,10 @@ keyring_rebuild_cache (void *token,int noisy)
if (rc)
goto leave;
}
release_kbnode (keyblock);
rc = keyring_get_keyblock (hd, &keyblock);
if (rc)
if (rc)
{
log_error ("keyring_get_keyblock failed: %s\n", g10_errstr(rc));
goto leave;
@ -1438,7 +1438,7 @@ keyring_rebuild_cache (void *token,int noisy)
sigcount++;
}
}
/* write the keyblock to the temporary file */
rc = write_keyblock (tmpfp, keyblock);
if (rc)
@ -1448,10 +1448,10 @@ keyring_rebuild_cache (void *token,int noisy)
log_info(_("%lu keys cached so far (%lu signatures)\n"),
count, sigcount );
} /* end main loop */
} /* end main loop */
if (rc == -1)
rc = 0;
if (rc)
if (rc)
{
log_error ("keyring_search failed: %s\n", g10_errstr(rc));
goto leave;
@ -1479,8 +1479,8 @@ keyring_rebuild_cache (void *token,int noisy)
leave:
if (tmpfp)
iobuf_cancel (tmpfp);
xfree (tmpfilename);
xfree (bakfilename);
xfree (tmpfilename);
xfree (bakfilename);
release_kbnode (keyblock);
keyring_lock (hd, 0);
keyring_release (hd);
@ -1503,13 +1503,13 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
char *bakfname = NULL;
char *tmpfname = NULL;
/* Open the source file. Because we do a rename, we have to check the
/* Open the source file. Because we do a rename, we have to check the
permissions of the file */
if (access (fname, W_OK))
return G10ERR_WRITE_FILE;
fp = iobuf_open (fname);
if (mode == 1 && !fp && errno == ENOENT) {
if (mode == 1 && !fp && errno == ENOENT) {
/* insert mode but file does not exist: create a new file */
KBNODE kbctx, node;
mode_t oldmask;

View File

@ -66,7 +66,7 @@ init_one_signal (int sig, RETSIGTYPE (*handler)(int), int check_ign )
sigemptyset (&nact.sa_mask);
nact.sa_flags = 0;
sigaction ( sig, &nact, NULL);
#else
#else
RETSIGTYPE (*ohandler)(int);
ohandler = signal (sig, handler);
@ -122,7 +122,7 @@ got_fatal_signal( int sig )
/* Reset action to default action and raise signal again. */
init_one_signal (sig, SIG_DFL, 0);
remove_lockfiles ();
dotlock_remove_lockfiles ();
#ifdef __riscos__
riscos_close_fds ();
#endif /* __riscos__ */
@ -165,7 +165,7 @@ pause_on_sigusr( int which )
sigsuspend( &oldmask );
caught_sigusr1 = 0;
sigprocmask( SIG_UNBLOCK, &mask, NULL );
#else
#else
assert (which == 1);
sighold (SIGUSR1);
while (!caught_sigusr1)

View File

@ -86,7 +86,7 @@ struct cmp_xdir_struct {
static char *db_name;
static DOTLOCK lockhandle;
static dotlock_t lockhandle;
static int is_locked;
static int db_fd = -1;
static int in_transaction;
@ -248,7 +248,7 @@ put_record_into_cache( ulong recno, const char *data )
if( !n )
n = 1;
if( !is_locked ) {
if( make_dotlock( lockhandle, -1 ) )
if (dotlock_take (lockhandle, -1))
log_fatal("can't acquire lock - giving up\n");
else
is_locked = 1;
@ -267,7 +267,7 @@ put_record_into_cache( ulong recno, const char *data )
}
}
if( !opt.lock_once ) {
if( !release_dotlock( lockhandle ) )
if (!dotlock_release (lockhandle))
is_locked = 0;
}
assert( unused );
@ -309,7 +309,7 @@ tdbio_sync()
return 0;
if( !is_locked ) {
if( make_dotlock( lockhandle, -1 ) )
if (dotlock_take (lockhandle, -1))
log_fatal("can't acquire lock - giving up\n");
else
is_locked = 1;
@ -324,7 +324,7 @@ tdbio_sync()
}
cache_is_dirty = 0;
if( did_lock && !opt.lock_once ) {
if( !release_dotlock( lockhandle ) )
if (!dotlock_release (lockhandle))
is_locked = 0;
}
@ -364,7 +364,7 @@ tdbio_end_transaction()
if( !in_transaction )
log_bug("tdbio: no active transaction\n");
if( !is_locked ) {
if( make_dotlock( lockhandle, -1 ) )
if (dotlock_take (lockhandle, -1))
log_fatal("can't acquire lock - giving up\n");
else
is_locked = 1;
@ -374,7 +374,7 @@ tdbio_end_transaction()
rc = tdbio_sync();
unblock_all_signals();
if( !opt.lock_once ) {
if( !release_dotlock( lockhandle ) )
if (!dotlock_release (lockhandle))
is_locked = 0;
}
return rc;
@ -414,7 +414,7 @@ static void
cleanup(void)
{
if( is_locked ) {
if( !release_dotlock(lockhandle) )
if (!dotlock_release (lockhandle))
is_locked = 0;
}
}
@ -447,7 +447,7 @@ create_version_record (void)
{
TRUSTREC rec;
int rc;
memset( &rec, 0, sizeof rec );
rec.r.ver.version = 3;
rec.r.ver.created = make_timestamp();
@ -517,10 +517,10 @@ tdbio_set_dbname( const char *new_dbname, int create )
db_name = fname;
#ifdef __riscos__
if( !lockhandle )
lockhandle = create_dotlock( db_name );
lockhandle = dotlock_create (db_name, 0);
if( !lockhandle )
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 );
#endif /* __riscos__ */
oldmask=umask(077);
@ -540,7 +540,7 @@ tdbio_set_dbname( const char *new_dbname, int create )
#ifndef __riscos__
if( !lockhandle )
lockhandle = create_dotlock( db_name );
lockhandle = dotlock_create (db_name, 0);
if( !lockhandle )
log_fatal( _("can't create lock for `%s'\n"), db_name );
#endif /* !__riscos__ */
@ -583,11 +583,11 @@ open_db()
assert( db_fd == -1 );
if (!lockhandle )
lockhandle = create_dotlock( db_name );
lockhandle = dotlock_create (db_name, 0);
if (!lockhandle )
log_fatal( _("can't create lock for `%s'\n"), db_name );
#ifdef __riscos__
if (make_dotlock( lockhandle, -1 ) )
if (dotlock_take (lockhandle, -1))
log_fatal( _("can't lock `%s'\n"), db_name );
#endif /* __riscos__ */
db_fd = open (db_name, O_RDWR | MY_O_BINARY );
@ -613,7 +613,7 @@ open_db()
{
migrate_from_v2 ();
}
/* read the version record */
if (tdbio_read_record (0, &rec, RECTYPE_VER ) )
log_fatal( _("%s: invalid trustdb\n"), db_name );
@ -690,7 +690,7 @@ tdbio_read_model(void)
{
TRUSTREC vr;
int rc;
rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc )
log_fatal( _("%s: error reading version record: %s\n"),
@ -1008,7 +1008,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
*/
static int
lookup_hashtable( ulong table, const byte *key, size_t keylen,
int (*cmpfnc)(const void*, const TRUSTREC *),
int (*cmpfnc)(const void*, const TRUSTREC *),
const void *cmpdata, TRUSTREC *rec )
{
int rc;
@ -1534,12 +1534,12 @@ migrate_from_v2 ()
/* We have some restrictions here. We can't use the version record
* and we can't use any of the old hashtables because we dropped the
* code. So we first collect all ownertrusts and then use a second
* pass fo find the associated keys. We have to do this all without using
* pass fo find the associated keys. We have to do this all without using
* the regular record read functions.
*/
/* get all the ownertrusts */
if (lseek (db_fd, 0, SEEK_SET ) == -1 )
if (lseek (db_fd, 0, SEEK_SET ) == -1 )
log_fatal ("migrate_from_v2: lseek failed: %s\n", strerror (errno));
for (recno=0;;recno++)
{
@ -1553,7 +1553,7 @@ migrate_from_v2 ()
if (*oldbuf != 2)
continue;
/* v2 dir record */
if (ottable_used == ottable_size)
{
@ -1570,7 +1570,7 @@ migrate_from_v2 ()
log_info ("found %d ownertrust records\n", ottable_used);
/* Read again and find the fingerprints */
if (lseek (db_fd, 0, SEEK_SET ) == -1 )
if (lseek (db_fd, 0, SEEK_SET ) == -1 )
log_fatal ("migrate_from_v2: lseek failed: %s\n", strerror (errno));
for (recno=0;;recno++)
{
@ -1582,7 +1582,7 @@ migrate_from_v2 ()
if (n != 40)
log_fatal ("migrate_from_v2: read error or short read\n");
if (*oldbuf != 3)
if (*oldbuf != 3)
continue;
/* v2 key record */
@ -1603,7 +1603,7 @@ migrate_from_v2 ()
if (create_version_record ())
log_fatal ("failed to recreate version record of `%s'\n", db_name);
/* access the hash table, so it is store just after the version record,
/* access the hash table, so it is store just after the version record,
* this is not needed put a dump is more pretty */
get_trusthashrec ();
@ -1613,7 +1613,7 @@ migrate_from_v2 ()
{
if (!ottable[i].okay)
continue;
memset (&rec, 0, sizeof rec);
rec.recnum = tdbio_new_recnum ();
rec.rectype = RECTYPE_TRUST;

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 --*/
struct dotlock_handle;
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);
#include "../include/dotlock.h"
/*-- fileutil.c --*/
char * make_basename(const char *filepath, const char *inputpath);
@ -217,10 +209,10 @@ int strncasecmp (const char *, const char *b, size_t n);
/* The definition of the structure is private, we only need it here,
so it can be allocated on the stack. */
struct private_membuf_s {
size_t len;
size_t size;
char *buf;
int out_of_core;
size_t len;
size_t size;
char *buf;
int out_of_core;
};
typedef struct private_membuf_s membuf_t;

File diff suppressed because it is too large Load Diff