aboutsummaryrefslogtreecommitdiffstats
path: root/tools/dotlock.c
blob: 27ae5205b074745de5b6e168f8fc40a1083e1696 (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
/* dotlock.c - Command to handle dotlock.
 *	Copyright (C) 2023 g10 Code GmbH
 *
 * 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/>.
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include <config.h>

#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"

static void
lock (const char *fname)
{
  dotlock_t h;
  unsigned int flags = DOTLOCK_FLAG_LOCK_BY_PARENT;

  h = dotlock_create (fname, flags);
  if (!h)
    die ("error creating lock file for '%s': %s", fname, strerror (errno));

  if (dotlock_take (h, 0))
    die ("error taking lock");
}

static void
unlock (const char *fname, long timeout)
{
  dotlock_t h;
  unsigned int flags = (DOTLOCK_FLAG_LOCK_BY_PARENT
                        | DOTLOCK_FLAG_READONLY);

  h = dotlock_create (fname, flags);
  if (!h)
    die ("error creating lock file for '%s': %s", fname, strerror (errno));

  dotlock_destroy (h);
}


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

  fname = argv[argc-1];

  if ()
    lock (fname);
  else
    unlock (fname);

  return 0;
}


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