summaryrefslogtreecommitdiffstats
path: root/common/t-dotlock.c
blob: 87e62bb337d87374654172e298479f82ab107106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* t-dotlock.c - Module test for dotlock.c
 * Copyright (C) 2011 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

/* Note: This is a standalone test program which does not rely on any
   GnuPG helper files.  However, it may also be build as part of the
   GnuPG build system.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

/* Some quick replacements for stuff we usually expect to be defined
   in config.h.  Define HAVE_POSIX_SYSTEM for better readability. */
#if !defined (HAVE_DOSISH_SYSTEM) && defined(_WIN32)
# define HAVE_DOSISH_SYSTEM 1
#endif
#if !defined (HAVE_DOSISH_SYSTEM) && !defined (HAVE_POSIX_SYSTEM)
# define HAVE_POSIX_SYSTEM 1
#endif

#include <stdio.h>
#include <stdlib.h>
#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"

#ifdef HAVE_W32_SYSTEM
#define DIM(v)		     (sizeof(v)/sizeof((v)[0]))


const char *
w32_strerror (int ec)
{
  static char strerr[256];

  if (ec == -1)
    ec = (int)GetLastError ();
  FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec,
                 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
                 strerr, DIM (strerr)-1, NULL);
  {
    /* Strip the CR,LF - we want just the string.  */
    size_t n = strlen (strerr);
    if (n > 2 && strerr[n-2] == '\r' && strerr[n-1] == '\n' )
      strerr[n-2] = 0;
  }
  return strerr;
}

static wchar_t *
cp_to_wchar (const char *string, unsigned int codepage)
{
  int n;
  size_t nbytes;
  wchar_t *result;

  n = MultiByteToWideChar (codepage, 0, string, -1, NULL, 0);
  if (n < 0)
    {
      return NULL;
    }

  nbytes = (size_t)(n+1) * sizeof(*result);
  if (nbytes / sizeof(*result) != (n+1))
    {
      return NULL;
    }
  result = malloc (nbytes);
  if (!result)
    return NULL;

  n = MultiByteToWideChar (codepage, 0, string, -1, result, n);
  if (n < 0)
    {
      free (result);
      result = NULL;
    }
  return result;
}

wchar_t *
utf8_to_wchar (const char *string)
{
  return cp_to_wchar (string, CP_UTF8);
}

char *
stpcpy(char *a,const char *b)
{
    while( *b )
	*a++ = *b++;
    *a = 0;

    return (char*)a;
}

static char *
do_strconcat (const char *s1, va_list arg_ptr)
{
  const char *argv[48];
  size_t argc;
  size_t needed;
  char *buffer, *p;

  argc = 0;
  argv[argc++] = s1;
  needed = strlen (s1);
  while (((argv[argc] = va_arg (arg_ptr, const char *))))
    {
      needed += strlen (argv[argc]);
      if (argc >= DIM (argv)-1)
        {
          return NULL;
        }
      argc++;
    }
  needed++;
  buffer = malloc (needed);
  if (buffer)
    {
      for (p = buffer, argc=0; argv[argc]; argc++)
        p = stpcpy (p, argv[argc]);
    }
  return buffer;
}

/* Concatenate the string S1 with all the following strings up to a
   NULL.  Returns a malloced buffer with the new string or NULL on a
   malloc error or if too many arguments are given.  */
char *
strconcat (const char *s1, ...)
{
  va_list arg_ptr;
  char *result;

  if (!s1)
    result = calloc (1, 1);
  else
    {
      va_start (arg_ptr, s1);
      result = do_strconcat (s1, arg_ptr);
      va_end (arg_ptr);
    }
  return result;
}
#endif /*HAVE_W32_SYSTEM*/


#include "dotlock.c"

#define PGM "t-dotlock"

static int opt_silent;




#ifndef HAVE_W32_SYSTEM
static volatile int ctrl_c_pending_flag;
static void
control_c_handler (int signo)
{
  (void)signo;
  ctrl_c_pending_flag = 1;
}
#endif


static int
ctrl_c_pending (void)
{
#if HAVE_W32_SYSTEM
  static int count;

  return (++count > 9);
#else
  return ctrl_c_pending_flag;
#endif
}


static void
die (const char *format, ...)
{
  va_list arg_ptr;

  va_start (arg_ptr, format);
  fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
  vfprintf (stderr, format, arg_ptr);
  putc ('\n', stderr);
  va_end (arg_ptr);
  exit (1);
}


static void
inf (const char *format, ...)
{
  va_list arg_ptr;

  if (opt_silent)
    return;

  va_start (arg_ptr, format);
  fprintf (stderr, PGM "[%lu]: ", (unsigned long)getpid ());
  vfprintf (stderr, format, arg_ptr);
  putc ('\n', stderr);
  va_end (arg_ptr);
}


static int
lock_info_cb (dotlock_t h, void *opaque, enum dotlock_reasons reason,
              const char *format, ...)
{
  va_list arg_ptr;

  va_start (arg_ptr, format);
  fprintf (stderr, PGM "[%lu]: info_cb: reason %d, ",
           (unsigned long)getpid (), (int)reason);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  return 0;
}


static void
lock_and_unlock (const char *fname)
{
  dotlock_t h;
  unsigned long usec;

  h = dotlock_create (fname, DOTLOCK_PREPARE_CREATE);
  if (!h)
    die ("error creating lock file for '%s': %s", fname, strerror (errno));
  dotlock_set_info_cb (h, lock_info_cb, NULL);
  h = dotlock_finish_create (h, fname);
  if (!h)
    die ("error finishing lock file creation for '%s': %s",
         fname, strerror (errno));
  inf ("lock created");

  do
    {
#ifdef HAVE_W32_SYSTEM
      usec = 10000;
#else
      getrandom (&usec, sizeof (usec), 0);
      usec &= 0xffff;
      usec |= 0x0f00;
#endif
      if (dotlock_take (h, -1))
        die ("error taking lock");
      inf ("lock taken");
      usleep (usec);
      if (dotlock_release (h))
        die ("error releasing lock");
      inf ("lock released");
      usleep (usec);
    }
  while (!ctrl_c_pending ());
  dotlock_destroy (h);
  inf ("lock destroyed");
}


int
main (int argc, char **argv)
{
  const char *fname;

  if (argc > 1 && !strcmp (argv[1], "--one-shot"))
    {
      ctrl_c_pending_flag = 1;
      argc--;
    }
  if (argc > 1 && !strcmp (argv[1], "--silent"))
    {
      opt_silent = 1;
      argc--;
    }

  if (argc > 1)
    fname = argv[argc-1];
  else
    {
#ifdef HAVE_W32_SYSTEM
      fname = "t-dotⒶlock.tmp";
#else
      fname = "t-dotlock.tmp";
#endif
    }

#ifndef HAVE_W32_SYSTEM
  {
    struct sigaction nact;

    nact.sa_handler = control_c_handler;
    nact.sa_flags = 0;
    sigaction (SIGINT, &nact, NULL);
  }
#endif

  dotlock_create (NULL, 0);  /* Initialize (optional).  */

  lock_and_unlock (fname);


  return 0;
}


/*
Local Variables:
compile-command: "cc -Wall -O2 -D_FILE_OFFSET_BITS=64 -o t-dotlock t-dotlock.c"
End:
*/