diff options
author | NIIBE Yutaka <gniibe@fsij.org> | 2023-12-20 02:55:26 +0100 |
---|---|---|
committer | NIIBE Yutaka <gniibe@fsij.org> | 2023-12-20 03:05:50 +0100 |
commit | 1f04993cd0acaf6fb4982e822f8d8b5722197e03 (patch) | |
tree | ccec047b2713556d4f15e001018a9f60daa1d017 /tools/dotlock.c | |
parent | common: Clean up the temporary file at dotlock_destroy. (diff) | |
download | gnupg2-1f04993cd0acaf6fb4982e822f8d8b5722197e03.tar.xz gnupg2-1f04993cd0acaf6fb4982e822f8d8b5722197e03.zip |
common: Add dotlock util under libexec.
* tools/Makefile.am (libexec_PROGRAMS): Add dotlock.
* tools/dotlock.c: Finish the first implementation.
--
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
Diffstat (limited to '')
-rw-r--r-- | tools/dotlock.c | 88 |
1 files changed, 58 insertions, 30 deletions
diff --git a/tools/dotlock.c b/tools/dotlock.c index 27ae5205b..3dfb86c80 100644 --- a/tools/dotlock.c +++ b/tools/dotlock.c @@ -1,4 +1,4 @@ -/* dotlock.c - Command to handle dotlock. +/* dotlock.c - A utility to handle dotlock by command line. * Copyright (C) 2023 g10 Code GmbH * * This file is part of GnuPG. @@ -25,63 +25,91 @@ #include <stdarg.h> #include <string.h> #include <errno.h> -#include <signal.h> #include <unistd.h> -#ifdef HAVE_W32_SYSTEM -# include "windows.h" -#else -#include <sys/random.h> -#endif -#include "dotlock.h" +#include <gpg-error.h> +#include "../common/util.h" +#include "../common/stringhelp.h" +#include "../common/dotlock.h" static void -lock (const char *fname) +lock (const char *filename) { dotlock_t h; - unsigned int flags = DOTLOCK_FLAG_LOCK_BY_PARENT; + unsigned int flags = DOTLOCK_LOCK_BY_PARENT; - h = dotlock_create (fname, flags); + h = dotlock_create (filename, flags); if (!h) - die ("error creating lock file for '%s': %s", fname, strerror (errno)); + { + perror ("error creating lock file"); + exit (1); + } if (dotlock_take (h, 0)) - die ("error taking lock"); + { + perror ("error taking lock"); + dotlock_destroy (h); + exit (1); + } + + dotlock_destroy (h); } static void -unlock (const char *fname, long timeout) +unlock (const char *filename) { dotlock_t h; - unsigned int flags = (DOTLOCK_FLAG_LOCK_BY_PARENT - | DOTLOCK_FLAG_READONLY); + unsigned int flags = (DOTLOCK_LOCK_BY_PARENT | DOTLOCK_LOCKED); - h = dotlock_create (fname, flags); + h = dotlock_create (filename, flags); if (!h) - die ("error creating lock file for '%s': %s", fname, strerror (errno)); + { + perror ("no lock file"); + exit (1); + } + dotlock_release (h); dotlock_destroy (h); } int -main (int argc, char **argv) +main (int argc, const char *argv[]) { + const char *name; const char *fname; + char *filename; + int op_unlock = 0; + + if (argc >= 2 && !strcmp (argv[1], "-u")) + { + op_unlock = 1; + argc--; + argv++; + } + + if (argc != 2) + { + printf ("Usage: %s [-u] NAME\n", argv[0]); + exit (1); + } + + name = argv[1]; + + if (!strcmp (name, "pubring.db")) + /* Keybox pubring.db lock */ + fname = "public-keys.d/pubring.db"; + else + /* Other locks. */ + fname = name; - fname = argv[argc-1]; + filename = make_absfilename (gnupg_homedir (), fname, NULL); - if () - lock (fname); + if (op_unlock) + unlock (filename); else - unlock (fname); + lock (filename); + xfree (filename); return 0; } - - -/* -Local Variables: -compile-command: "cc -Wall -O2 -D_FILE_OFFSET_BITS=64 -o t-dotlock t-dotlock.c" -End: -*/ |