1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-04-17 15:44:34 +02:00

common: More heavy test condition for t-dotlock.c.

* common/t-dotlock.c (lock_and_unlock): Use usleep and faster.
Loop at least once.  Use getrandom for random time.
(main): Add new option --one-shot to run lock/unlock once.

--

GnuPG-bug-id: 5884
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2022-03-18 10:53:10 +09:00
parent c6dd9ff929
commit a30359cecb

View File

@ -43,6 +43,8 @@
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_W32_SYSTEM #ifdef HAVE_W32_SYSTEM
# include "windows.h" # include "windows.h"
#else
#include <sys/random.h>
#endif #endif
#include "dotlock.h" #include "dotlock.h"
@ -233,23 +235,32 @@ static void
lock_and_unlock (const char *fname) lock_and_unlock (const char *fname)
{ {
dotlock_t h; dotlock_t h;
unsigned long usec;
h = dotlock_create (fname, 0); h = dotlock_create (fname, 0);
if (!h) if (!h)
die ("error creating lock file for '%s': %s", fname, strerror (errno)); die ("error creating lock file for '%s': %s", fname, strerror (errno));
inf ("lock created"); inf ("lock created");
while (!ctrl_c_pending ()) do
{ {
#ifdef HAVE_W32_SYSTEM
usec = 10000;
#else
getrandom (&usec, sizeof (usec), 0);
usec &= 0xffff;
usec |= 0x0f00;
#endif
if (dotlock_take (h, -1)) if (dotlock_take (h, -1))
die ("error taking lock"); die ("error taking lock");
inf ("lock taken"); inf ("lock taken");
sleep (1); usleep (usec);
if (dotlock_release (h)) if (dotlock_release (h))
die ("error releasing lock"); die ("error releasing lock");
inf ("lock released"); inf ("lock released");
sleep (1); usleep (usec);
} }
while (!ctrl_c_pending ());
dotlock_destroy (h); dotlock_destroy (h);
inf ("lock destroyed"); inf ("lock destroyed");
} }
@ -260,8 +271,14 @@ main (int argc, char **argv)
{ {
const char *fname; const char *fname;
if (argc > 1 && !strcmp (argv[1], "--one-shot"))
{
ctrl_c_pending_flag = 1;
argc--;
}
if (argc > 1) if (argc > 1)
fname = argv[1]; fname = argv[argc-1];
else else
{ {
#ifdef HAVE_W32_SYSTEM #ifdef HAVE_W32_SYSTEM