Imported Upstream version 2.1.23
[platform/upstream/gpg2.git] / common / t-dotlock.c
1 /* t-dotlock.c - Module test for dotlock.c
2  * Copyright (C) 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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.  */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
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
32 #endif
33 #if !defined (HAVE_DOSISH_SYSTEM) && !defined (HAVE_POSIX_SYSTEM)
34 # define HAVE_POSIX_SYSTEM 1
35 #endif
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <signal.h>
43 #include <unistd.h>
44
45 #include "dotlock.h"
46
47 #define PGM "t-dotlock"
48
49
50 static volatile int ctrl_c_pending;
51
52 static void
53 control_c_handler (int signo)
54 {
55   (void)signo;
56   ctrl_c_pending = 1;
57 }
58
59
60
61 static void
62 die (const char *format, ...)
63 {
64   va_list arg_ptr;
65
66   va_start (arg_ptr, format);
67   fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
68   vfprintf (stderr, format, arg_ptr);
69   putc ('\n', stderr);
70   va_end (arg_ptr);
71   exit (1);
72 }
73
74
75 static void
76 inf (const char *format, ...)
77 {
78   va_list arg_ptr;
79
80   va_start (arg_ptr, format);
81   fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
82   vfprintf (stderr, format, arg_ptr);
83   putc ('\n', stderr);
84   va_end (arg_ptr);
85 }
86
87
88 static void
89 lock_and_unlock (const char *fname)
90 {
91   dotlock_t h;
92
93   h = dotlock_create (fname, 0);
94   if (!h)
95     die ("error creating lock file for '%s': %s", fname, strerror (errno));
96   inf ("lock created");
97
98   while (!ctrl_c_pending)
99     {
100       if (dotlock_take (h, -1))
101         die ("error taking lock");
102       inf ("lock taken");
103       sleep (1);
104       if (dotlock_release (h))
105         die ("error releasing lock");
106       inf ("lock released");
107       sleep (1);
108     }
109   dotlock_destroy (h);
110   inf ("lock destroyed");
111 }
112
113
114 int
115 main (int argc, char **argv)
116 {
117   const char *fname;
118
119   if (argc > 1)
120     fname = argv[1];
121   else
122     fname = "t-dotlock.tmp";
123
124   {
125     struct sigaction nact;
126
127     nact.sa_handler = control_c_handler;
128     nact.sa_flags = 0;
129     sigaction (SIGINT, &nact, NULL);
130   }
131
132   dotlock_create (NULL, 0);  /* Initialize (optional).  */
133
134   lock_and_unlock (fname);
135
136
137   return 0;
138 }
139
140
141 /*
142 Local Variables:
143 compile-command: "cc -Wall -O2 -D_FILE_OFFSET_BITS=64 -o t-dotlock t-dotlock.c dotlock.c"
144 End:
145 */