1 /* t-dotlock.c - Module test for dotlock.c
2 * Copyright (C) 2011 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20 /* Note: This is a standalone test program which does not rely on any
21 GnuPG helper files. However, it may also be build as part of the
22 GnuPG build system. */
28 /* Some quick replacements for stuff we usually expect to be defined
29 in config.h. Define HAVE_POSIX_SYSTEM for better readability. */
30 #if !defined (HAVE_DOSISH_SYSTEM) && defined(_WIN32)
31 # define HAVE_DOSISH_SYSTEM 1
33 #if !defined (HAVE_DOSISH_SYSTEM) && !defined (HAVE_POSIX_SYSTEM)
34 # define HAVE_POSIX_SYSTEM 1
47 #define PGM "t-dotlock"
50 static volatile int ctrl_c_pending;
53 control_c_handler (int signo)
62 die (const char *format, ...)
66 va_start (arg_ptr, format);
67 fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
68 vfprintf (stderr, format, arg_ptr);
76 inf (const char *format, ...)
80 va_start (arg_ptr, format);
81 fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
82 vfprintf (stderr, format, arg_ptr);
89 lock_and_unlock (const char *fname)
93 h = dotlock_create (fname, 0);
95 die ("error creating lock file for '%s': %s", fname, strerror (errno));
98 while (!ctrl_c_pending)
100 if (dotlock_take (h, -1))
101 die ("error taking lock");
104 if (dotlock_release (h))
105 die ("error releasing lock");
106 inf ("lock released");
110 inf ("lock destroyed");
115 main (int argc, char **argv)
122 fname = "t-dotlock.tmp";
125 struct sigaction nact;
127 nact.sa_handler = control_c_handler;
129 sigaction (SIGINT, &nact, NULL);
132 dotlock_create (NULL, 0); /* Initialize (optional). */
134 lock_and_unlock (fname);
143 compile-command: "cc -Wall -O2 -D_FILE_OFFSET_BITS=64 -o t-dotlock t-dotlock.c dotlock.c"