aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/keybox-init.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2016-01-13 09:16:27 +0000
committerWerner Koch <[email protected]>2016-01-13 09:43:34 +0000
commit160862978628b07ed5150ec2c8abad6af1656bc3 (patch)
treef46606887c7aa112f56354337c533fcbeb9e30ee /kbx/keybox-init.c
parentgpg: Make sure to mark a duplicate registered keybox as primary. (diff)
downloadgnupg-160862978628b07ed5150ec2c8abad6af1656bc3.tar.gz
gnupg-160862978628b07ed5150ec2c8abad6af1656bc3.zip
kbx: Implement keybox_lock for use by gpg.
* kbx/keybox-defs.h: Include dotlock.h and logging.h. (CONST_KB_NAME): Remove. Replace usage by KB_NAME. (struct keybox_name): Add field "lockhd". * kbx/keybox-init.c (keybox_register_file): Init LOCKHD. (keybox_lock): Chnage to return gpg_error_t. Implement locking. -- The keybox locking for gpg was not implemented - This needs to be fixed of course. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'kbx/keybox-init.c')
-rw-r--r--kbx/keybox-init.c58
1 files changed, 48 insertions, 10 deletions
diff --git a/kbx/keybox-init.c b/kbx/keybox-init.c
index 3ff592eaf..cfee7b8f7 100644
--- a/kbx/keybox-init.c
+++ b/kbx/keybox-init.c
@@ -60,7 +60,7 @@ keybox_register_file (const char *fname, int secret, void **r_token)
kr->handle_table = NULL;
kr->handle_table_size = 0;
- /* kr->lockhd = NULL;*/
+ kr->lockhd = NULL;
kr->is_locked = 0;
kr->did_full_scan = 0;
/* keep a list of all issued pointers */
@@ -261,17 +261,55 @@ _keybox_close_file (KEYBOX_HANDLE hd)
/*
- * Lock the keybox at handle HD, or unlock if YES is false. Note that
- * we currently ignore the handle and lock all registered keyboxes.
+ * Lock the keybox at handle HD, or unlock if YES is false.
*/
-int
+gpg_error_t
keybox_lock (KEYBOX_HANDLE hd, int yes)
{
- /* FIXME: We need to implement it before we can use it with gpg.
- gpgsm does the locking in its local keydb.c driver; this should
- be changed as well. */
+ gpg_error_t err;
+ KB_NAME kb = hd->kb;
- (void)hd;
- (void)yes;
- return 0;
+ if (!keybox_is_writable ((void*)kb))
+ return 0;
+
+ /* Make sure the lock handle has been created. */
+ if (!kb->lockhd)
+ {
+ kb->lockhd = dotlock_create (kb->fname, 0);
+ if (!kb->lockhd)
+ {
+ /* Unfortuntaley dotlock_create does not properly set ERRNO. */
+ log_info ("can't allocate lock for '%s'\n", kb->fname );
+ return gpg_error (GPG_ERR_GENERAL);
+ }
+ }
+
+ if (yes) /* Take the lock. */
+ {
+ if (kb->is_locked)
+ ;
+ else if (!dotlock_take (kb->lockhd, -1))
+ kb->is_locked = 1;
+ else
+ {
+ /* Unfortuntaley dotlock_take does not properly set ERRNO. */
+ log_info ("can't lock '%s'\n", kb->fname );
+ err = gpg_error (GPG_ERR_GENERAL);
+ }
+ }
+ else /* Release the lock. */
+ {
+ if (!kb->is_locked)
+ ;
+ else if (!dotlock_release (kb->lockhd))
+ kb->is_locked = 0;
+ else
+ {
+ /* Unfortuntaley dotlock_release does not properly set ERRNO. */
+ log_info ("can't unlock '%s'\n", kb->fname );
+ err = gpg_error (GPG_ERR_GENERAL);
+ }
+ }
+
+ return err;
}