Coverage Report

Created: 2026-01-17 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/common/dotlock.c
Line
Count
Source
1
/* dotlock.c - dotfile locking
2
 * Copyright (C) 1998, 2000, 2001, 2003, 2004,
3
 *               2005, 2006, 2008, 2010, 2011 Free Software Foundation, Inc.
4
 *
5
 * This file is part of GnuPG.
6
 *
7
 * GnuPG is free software; you can redistribute and/or modify this
8
 * part of GnuPG under the terms of either
9
 *
10
 *   - the GNU Lesser General Public License as published by the Free
11
 *     Software Foundation; either version 3 of the License, or (at
12
 *     your option) any later version.
13
 *
14
 * or
15
 *
16
 *   - the GNU General Public License as published by the Free
17
 *     Software Foundation; either version 2 of the License, or (at
18
 *     your option) any later version.
19
 *
20
 * or both in parallel, as here.
21
 *
22
 * GnuPG is distributed in the hope that it will be useful, but
23
 * WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
 * General Public License for more details.
26
 *
27
 * You should have received a copies of the GNU General Public License
28
 * and the GNU Lesser General Public License along with this program;
29
 * if not, see <https://www.gnu.org/licenses/>.
30
 *
31
 * ALTERNATIVELY, this file may be distributed under the terms of the
32
 * following license, in which case the provisions of this license are
33
 * required INSTEAD OF the GNU Lesser General License or the GNU
34
 * General Public License. If you wish to allow use of your version of
35
 * this file only under the terms of the GNU Lesser General License or
36
 * the GNU General Public License, and not to allow others to use your
37
 * version of this file under the terms of the following license,
38
 * indicate your decision by deleting this paragraph and the license
39
 * below.
40
 *
41
 * Redistribution and use in source and binary forms, with or without
42
 * modification, are permitted provided that the following conditions
43
 * are met:
44
 *
45
 * 1. Redistributions of source code must retain the above copyright
46
 *    notice, and the entire permission notice in its entirety,
47
 *    including the disclaimer of warranties.
48
 * 2. Redistributions in binary form must reproduce the above copyright
49
 *    notice, this list of conditions and the following disclaimer in the
50
 *    documentation and/or other materials provided with the distribution.
51
 * 3. The name of the author may not be used to endorse or promote
52
 *    products derived from this software without specific prior
53
 *    written permission.
54
 *
55
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
56
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
57
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
58
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
59
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
60
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
61
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
63
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
65
 * OF THE POSSIBILITY OF SUCH DAMAGE.
66
 */
67
68
/*
69
   Overview:
70
   =========
71
72
   This module implements advisory file locking in a portable way.
73
   Due to the problems with POSIX fcntl locking a separate lock file
74
   is used.  It would be possible to use fcntl locking on this lock
75
   file and thus avoid the weird auto unlock bug of POSIX while still
76
   having an unproved better performance of fcntl locking.  However
77
   there are still problems left, thus we resort to use a hardlink
78
   which has the well defined property that a link call will fail if
79
   the target file already exists.
80
81
   Given that hardlinks are also available on NTFS file systems since
82
   Windows XP; it will be possible to enhance this module to use
83
   hardlinks even on Windows and thus allow Windows and Posix clients
84
   to use locking on the same directory.  This is not yet implemented;
85
   instead we use a lockfile on Windows along with W32 style file
86
   locking.
87
88
   On FAT file systems hardlinks are not supported.  Thus this method
89
   does not work.  Our solution is to use a O_EXCL locking instead.
90
   Querying the type of the file system is not easy to do in a
91
   portable way (e.g. Linux has a statfs, BSDs have a the same call
92
   but using different structures and constants).  What we do instead
93
   is to check at runtime whether link(2) works for a specific lock
94
   file.
95
96
97
   How to use:
98
   ===========
99
100
   At program initialization time, the module should be explicitly
101
   initialized:
102
103
      dotlock_create (NULL, 0);
104
105
   This installs an atexit handler and may also initialize mutex etc.
106
   It is optional for non-threaded applications.  Only the first call
107
   has an effect.  This needs to be done before any extra threads are
108
   started.
109
110
   To create a lock file (which  prepares it but does not take the
111
   lock) you do:
112
113
     dotlock_t h
114
115
     h = dotlock_create (fname, 0);
116
     if (!h)
117
       error ("error creating lock file: %s\n", strerror (errno));
118
119
   It is important to handle the error.  For example on a read-only
120
   file system a lock can't be created (but is usually not needed).
121
   FNAME is the file you want to lock; the actual lockfile is that
122
   name with the suffix ".lock" appended.  On success a handle to be
123
   used with the other functions is returned or NULL on error.  Note
124
   that the handle shall only be used by one thread at a time.  This
125
   function creates a unique file temporary file (".#lk*") in the same
126
   directory as FNAME and returns a handle for further operations.
127
   The module keeps track of these unique files so that they will be
128
   unlinked using the atexit handler.  If you don't need the lock file
129
   anymore, you may also explicitly remove it with a call to:
130
131
     dotlock_destroy (h);
132
133
   To actually lock the file, you use:
134
135
     if (dotlock_take (h, -1))
136
       error ("error taking lock: %s\n", strerror (errno));
137
138
   This function will wait until the lock is acquired.  If an
139
   unexpected error occurs if will return non-zero and set ERRNO.  If
140
   you pass (0) instead of (-1) the function does not wait in case the
141
   file is already locked but returns -1 and sets ERRNO to EACCES.
142
   Any other positive value for the second parameter is considered a
143
   timeout value in milliseconds.
144
145
   To release the lock you call:
146
147
     if (dotlock_release (h))
148
       error ("error releasing lock: %s\n", strerror (errno));
149
150
   or, if the lock file is not anymore needed, you may just call
151
   dotlock_destroy.  However dotlock_release does some extra checks
152
   before releasing the lock and prints diagnostics to help detecting
153
   bugs.
154
155
   If you want to explicitly destroy all lock files you may call
156
157
     dotlock_remove_lockfiles ();
158
159
   which is the core of the installed atexit handler.  In case your
160
   application wants to disable locking completely it may call
161
162
     dotlock_disable ()
163
164
   before any locks are created.
165
166
   There are two convenience functions to store an integer (e.g. a
167
   file descriptor) value with the handle:
168
169
     void dotlock_set_fd (dotlock_t h, int fd);
170
     int  dotlock_get_fd (dotlock_t h);
171
172
   If nothing has been stored dotlock_get_fd returns -1.
173
174
175
176
   How to build:
177
   =============
178
179
   This module was originally developed for GnuPG but later changed to
180
   allow its use without any GnuPG dependency.  If you want to use it
181
   with you application you may simply use it and it should figure out
182
   most things automagically.
183
184
   You may use the common config.h file to pass macros, but take care
185
   to pass -DHAVE_CONFIG_H to the compiler.  Macros used by this
186
   module are:
187
188
     DOTLOCK_USE_PTHREAD  - Define if POSIX threads are in use.
189
190
     DOTLOCK_EXT_SYM_PREFIX - Prefix all external symbols with the
191
                              string to which this macro evaluates.
192
193
     GNUPG_MAJOR_VERSION - Defined when used by GnuPG.
194
195
     HAVE_DOSISH_SYSTEM  - Defined for Windows etc.  Will be
196
                           automatically defined if a the target is
197
                           Windows.
198
199
     HAVE_POSIX_SYSTEM   - Internally defined to !HAVE_DOSISH_SYSTEM.
200
201
     HAVE_SIGNAL_H       - Should be defined on Posix systems.  If config.h
202
                           is not used defaults to defined.
203
204
     DIRSEP_C            - Separation character for file name parts.
205
                           Usually not redefined.
206
207
     EXTSEP_S            - Separation string for file name suffixes.
208
                           Usually not redefined.
209
210
   Note that there is a test program t-dotlock which has compile
211
   instructions at its end.  At least for SMBFS and CIFS it is
212
   important that 64 bit versions of stat are used; most programming
213
   environments do this these days, just in case you want to compile
214
   it on the command line, remember to pass -D_FILE_OFFSET_BITS=64
215
216
217
   Bugs:
218
   =====
219
220
   On Windows this module is not yet thread-safe.
221
222
223
   Miscellaneous notes:
224
   ====================
225
226
   On hardlinks:
227
   - Hardlinks are supported under Windows with NTFS since XP/Server2003.
228
   - In Linux 2.6.33 both SMBFS and CIFS seem to support hardlinks.
229
   - NFS supports hard links.  But there are solvable problems.
230
   - FAT does not support links
231
232
   On the file locking API:
233
   - CIFS on Linux 2.6.33 supports several locking methods.
234
     SMBFS seems not to support locking.  No closer checks done.
235
   - NFS supports Posix locks.  flock is emulated in the server.
236
     However there are a couple of problems; see below.
237
   - FAT does not support locks.
238
   - An advantage of fcntl locking is that R/W locks can be
239
     implemented which is not easy with a straight lock file.
240
241
   On O_EXCL:
242
   - Does not work reliable on NFS
243
   - Should work on CIFS and SMBFS but how can we delete lockfiles?
244
245
   On NFS problems:
246
   - Locks vanish if the server crashes and reboots.
247
   - Client crashes keep the lock in the server until the client
248
     re-connects.
249
   - Communication problems may return unreliable error codes.  The
250
     MUA Postfix's workaround is to compare the link count after
251
     seeing an error for link.  However that gives a race.  If using a
252
     unique file to link to a lockfile and using stat to check the
253
     link count instead of looking at the error return of link(2) is
254
     the best solution.
255
   - O_EXCL seems to have a race and may re-create a file anyway.
256
257
*/
258
259
#ifdef HAVE_CONFIG_H
260
# include <config.h>
261
#endif
262
263
/* Some quick replacements for stuff we usually expect to be defined
264
   in config.h.  Define HAVE_POSIX_SYSTEM for better readability. */
265
#if !defined (HAVE_DOSISH_SYSTEM) && defined(_WIN32)
266
# define HAVE_DOSISH_SYSTEM 1
267
#endif
268
#if !defined (HAVE_DOSISH_SYSTEM) && !defined (HAVE_POSIX_SYSTEM)
269
# define HAVE_POSIX_SYSTEM 1
270
#endif
271
272
/* With no config.h assume that we have sitgnal.h.  */
273
#if !defined (HAVE_CONFIG_H) && defined (HAVE_POSIX_SYSTEM)
274
# define HAVE_SIGNAL_H 1
275
#endif
276
277
/* Standard headers.  */
278
#include <stdlib.h>
279
#include <stdio.h>
280
#include <string.h>
281
#include <errno.h>
282
#include <ctype.h>
283
#include <errno.h>
284
#include <unistd.h>
285
#ifdef  HAVE_DOSISH_SYSTEM
286
# define WIN32_LEAN_AND_MEAN  /* We only need the OS core stuff.  */
287
# include <windows.h>
288
#else
289
# include <sys/types.h>
290
# include <sys/stat.h>
291
# include <sys/utsname.h>
292
# include <dirent.h>
293
#endif
294
#include <sys/types.h>
295
#include <sys/time.h>
296
#include <sys/stat.h>
297
#include <fcntl.h>
298
#ifdef HAVE_SIGNAL_H
299
# include <signal.h>
300
#endif
301
#ifdef DOTLOCK_USE_PTHREAD
302
# include <pthread.h>
303
#endif
304
305
#ifdef GNUPG_MAJOR_VERSION
306
# include "util.h"
307
# include "common-defs.h"
308
# include "stringhelp.h"  /* For stpcpy and w32_strerror. */
309
#endif
310
311
#include "dotlock.h"
312
313
314
/* Define constants for file name construction.  */
315
#if !defined(DIRSEP_C) && !defined(EXTSEP_S)
316
# ifdef HAVE_DOSISH_SYSTEM
317
#  define DIRSEP_C '\\'
318
#  define EXTSEP_S "."
319
#else
320
#  define DIRSEP_C '/'
321
#  define EXTSEP_S "."
322
# endif
323
#endif
324
325
/* In GnuPG we use wrappers around the malloc functions.  If they are
326
   not defined we assume that this code is used outside of GnuPG and
327
   fall back to the regular malloc functions.  */
328
#ifndef xtrymalloc
329
# define xtrymalloc(a)     malloc ((a))
330
# define xtrycalloc(a,b)   calloc ((a), (b))
331
# define xfree(a)    free ((a))
332
#endif
333
334
/* Wrapper to set ERRNO.  */
335
#ifdef GPG_ERROR_VERSION
336
3
#  define my_set_errno(e)  gpg_err_set_errno ((e))
337
#else
338
#  define my_set_errno(e)  do { errno = (e); } while (0)
339
#endif
340
341
/* Gettext macro replacement.  */
342
#ifndef _
343
0
# define _(a) (a)
344
#endif
345
346
#ifdef GNUPG_MAJOR_VERSION
347
0
# define my_info_0(a)       log_info ((a))
348
0
# define my_info_1(a,b)     log_info ((a), (b))
349
0
# define my_info_2(a,b,c)   log_info ((a), (b), (c))
350
0
# define my_info_3(a,b,c,d) log_info ((a), (b), (c), (d))
351
0
# define my_error_0(a)      log_error ((a))
352
0
# define my_error_1(a,b)    log_error ((a), (b))
353
0
# define my_error_2(a,b,c)  log_error ((a), (b), (c))
354
# define my_debug_1(a,b)    log_debug ((a), (b))
355
# define my_fatal_0(a)      log_fatal ((a))
356
#else
357
# define my_info_0(a)       fprintf (stderr, (a))
358
# define my_info_1(a,b)     fprintf (stderr, (a), (b))
359
# define my_info_2(a,b,c)   fprintf (stderr, (a), (b), (c))
360
# define my_info_3(a,b,c,d) fprintf (stderr, (a), (b), (c), (d))
361
# define my_error_0(a)      fprintf (stderr, (a))
362
# define my_error_1(a,b)    fprintf (stderr, (a), (b))
363
# define my_error_2(a,b,c)  fprintf (stderr, (a), (b), (c))
364
# define my_debug_1(a,b)    fprintf (stderr, (a), (b))
365
# define my_fatal_0(a)      do { fprintf (stderr,(a)); fflush (stderr); \
366
                                 abort (); } while (0)
367
#endif
368
369
370
371
372

373
/* The object describing a lock.  */
374
struct dotlock_handle
375
{
376
  struct dotlock_handle *next;
377
  char *lockname;             /* Name of the actual lockfile.          */
378
  unsigned int locked     :1; /* Lock status.                          */
379
  unsigned int disable    :1; /* If true, locking is disabled.         */
380
  unsigned int use_o_excl :1; /* Use open (O_EXCL) for locking.        */
381
  unsigned int by_parent  :1; /* Parent does the locking.              */
382
  unsigned int no_write   :1; /* No write to the lockfile.             */
383
384
  int extra_fd;              /* A place for the caller to store an FD.  */
385
386
  /* An optional info callback - see dotlock_set_info_cb.             */
387
  int (*info_cb)(dotlock_t, void *,
388
                 enum dotlock_reasons reason,
389
                 const char *,...);
390
  void *info_cb_value;
391
392
393
#ifdef HAVE_DOSISH_SYSTEM
394
  HANDLE lockhd;       /* The W32 handle of the lock file.      */
395
#else /*!HAVE_DOSISH_SYSTEM */
396
  char *tname;         /* Name of the lockfile template.        */
397
  size_t nodename_off; /* Offset in TNAME of the nodename part. */
398
  size_t nodename_len; /* Length of the nodename part.          */
399
#endif /*!HAVE_DOSISH_SYSTEM */
400
};
401
402
403
/* A list of all lock handles.  The volatile attribute might help
404
   if used in an atexit handler.  Note that [UN]LOCK_all_lockfiles
405
   must not change ERRNO. */
406
static volatile dotlock_t all_lockfiles;
407
#ifdef DOTLOCK_USE_PTHREAD
408
static pthread_mutex_t all_lockfiles_mutex = PTHREAD_MUTEX_INITIALIZER;
409
# define LOCK_all_lockfiles() do {                               \
410
        if (pthread_mutex_lock (&all_lockfiles_mutex))           \
411
          my_fatal_0 ("locking all_lockfiles_mutex failed\n");   \
412
      } while (0)
413
# define UNLOCK_all_lockfiles() do {                             \
414
        if (pthread_mutex_unlock (&all_lockfiles_mutex))         \
415
          my_fatal_0 ("unlocking all_lockfiles_mutex failed\n"); \
416
      } while (0)
417
#else  /*!DOTLOCK_USE_PTHREAD*/
418
10
# define LOCK_all_lockfiles()   do { } while (0)
419
10
# define UNLOCK_all_lockfiles() do { } while (0)
420
#endif /*!DOTLOCK_USE_PTHREAD*/
421
422
/* If this has the value true all locking is disabled.  */
423
static int never_lock;
424
425
426
427

428
#ifdef HAVE_DOSISH_SYSTEM
429
/* FIXME: For use in GnuPG this can be replaced by
430
 *        gnupg_w32_set_errno.  */
431
static int
432
map_w32_to_errno (DWORD w32_err)
433
{
434
  switch (w32_err)
435
    {
436
    case 0:
437
      return 0;
438
439
    case ERROR_FILE_NOT_FOUND:
440
      return ENOENT;
441
442
    case ERROR_PATH_NOT_FOUND:
443
      return ENOENT;
444
445
    case ERROR_ACCESS_DENIED:
446
      return EPERM;
447
448
    case ERROR_INVALID_HANDLE:
449
    case ERROR_INVALID_BLOCK:
450
      return EINVAL;
451
452
    case ERROR_NOT_ENOUGH_MEMORY:
453
      return ENOMEM;
454
455
    case ERROR_NO_DATA:
456
    case ERROR_BROKEN_PIPE:
457
      return EPIPE;
458
459
    default:
460
      return EIO;
461
    }
462
}
463
464
465
static int
466
any8bitchar (const char *string)
467
{
468
  if (string)
469
    for ( ; *string; string++)
470
      if ((*string & 0x80))
471
        return 1;
472
  return 0;
473
}
474
#endif /*HAVE_DOSISH_SYSTEM*/
475
476
477
478

479
/* Entirely disable all locking.  This function should be called
480
   before any locking is done.  It may be called right at startup of
481
   the process as it only sets a global value.  */
482
void
483
dotlock_disable (void)
484
0
{
485
0
  never_lock = 1;
486
0
}
487
488
489
#ifdef HAVE_POSIX_SYSTEM
490
static int
491
maybe_deadlock (dotlock_t h)
492
0
{
493
0
  dotlock_t r;
494
0
  int res = 0;
495
496
0
  LOCK_all_lockfiles ();
497
0
  for (r=all_lockfiles; r; r = r->next)
498
0
    {
499
0
      if ( r != h && r->locked )
500
0
        {
501
0
          res = 1;
502
0
          break;
503
0
        }
504
0
    }
505
0
  UNLOCK_all_lockfiles ();
506
0
  return res;
507
0
}
508
#endif /*HAVE_POSIX_SYSTEM*/
509
510
511
/* Read the lock file and return the pid, returns -1 on error.  True
512
   will be stored in the integer at address SAME_NODE if the lock file
513
   has been created on the same node. */
514
#ifdef HAVE_POSIX_SYSTEM
515
static int
516
read_lockfile (dotlock_t h, int *same_node, int *r_fd)
517
3
{
518
3
  char buffer_space[10+1+70+1]; /* 70 is just an estimated value; node
519
                                   names are usually shorter. */
520
3
  int fd;
521
3
  int pid = -1;
522
3
  char *buffer, *p;
523
3
  size_t expected_len;
524
3
  int res, nread;
525
526
3
  *same_node = 0;
527
3
  expected_len = 10 + 1 + h->nodename_len + 1;
528
3
  if ( expected_len >= sizeof buffer_space)
529
0
    {
530
0
      buffer = xtrymalloc (expected_len);
531
0
      if (!buffer)
532
0
        return -1;
533
0
    }
534
3
  else
535
3
    buffer = buffer_space;
536
537
3
  if ( (fd = open (h->lockname, O_RDONLY)) == -1 )
538
0
    {
539
0
      int e = errno;
540
0
      if (errno != ENOENT)
541
0
        {
542
0
          my_info_2 ("error opening lockfile '%s': %s\n",
543
0
                     h->lockname, strerror(errno) );
544
0
          if (h->info_cb)
545
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
546
0
                        "error opening lockfile '%s': %s\n",
547
0
                        h->lockname, strerror (errno) );
548
0
        }
549
0
      if (buffer != buffer_space)
550
0
        xfree (buffer);
551
0
      my_set_errno (e); /* Need to return ERRNO here. */
552
0
      return -1;
553
0
    }
554
555
3
  p = buffer;
556
3
  nread = 0;
557
3
  do
558
3
    {
559
3
      res = read (fd, p, expected_len - nread);
560
3
      if (res == -1 && errno == EINTR)
561
0
        continue;
562
3
      if (res < 0)
563
0
        {
564
0
          int e = errno;
565
0
          my_info_1 ("error reading lockfile '%s'\n", h->lockname );
566
0
          if (h->info_cb)
567
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
568
0
                        "error reading lockfile '%s': %s\n",
569
0
                        h->lockname, strerror (errno) );
570
0
          close (fd);
571
0
          if (buffer != buffer_space)
572
0
            xfree (buffer);
573
0
          my_set_errno (e);
574
0
          return -1;
575
0
        }
576
3
      p += res;
577
3
      nread += res;
578
3
    }
579
3
  while (res && nread != expected_len);
580
581
3
  if (r_fd)
582
0
    *r_fd = fd;
583
3
  else
584
3
    close(fd);
585
586
3
  if (nread < 11)
587
0
    {
588
0
      my_info_1 ("invalid size of lockfile '%s'\n", h->lockname);
589
0
      if (h->info_cb)
590
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_INV_FILE,
591
0
                    "invalid size of lockfile '%s'\n", h->lockname);
592
0
      if (buffer != buffer_space)
593
0
        xfree (buffer);
594
0
      my_set_errno (EINVAL);
595
0
      return -1;
596
0
    }
597
598
3
  if (buffer[10] != '\n'
599
3
      || (buffer[10] = 0, pid = atoi (buffer)) == -1
600
3
      || !pid )
601
0
    {
602
0
      my_error_2 ("invalid pid %d in lockfile '%s'\n", pid, h->lockname);
603
0
      if (h->info_cb)
604
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_INV_FILE,
605
0
                    "invalid pid %d in lockfile '%s'\n", pid, h->lockname);
606
0
      if (buffer != buffer_space)
607
0
        xfree (buffer);
608
0
      my_set_errno (EINVAL);
609
0
      return -1;
610
0
    }
611
612
3
  if (nread == expected_len
613
3
      && !memcmp (h->tname+h->nodename_off, buffer+11, h->nodename_len)
614
3
      && buffer[11+h->nodename_len] == '\n')
615
3
    *same_node = 1;
616
617
3
  if (buffer != buffer_space)
618
0
    xfree (buffer);
619
3
  return pid;
620
3
}
621
#endif /*HAVE_POSIX_SYSTEM */
622
623
624
/* Check whether the file system which stores TNAME supports
625
   hardlinks.  Instead of using the non-portable statsfs call which
626
   differs between various Unix versions, we do a runtime test.
627
   Returns: 0 supports hardlinks; 1 no hardlink support, -1 unknown
628
   (test error).  */
629
#ifdef HAVE_POSIX_SYSTEM
630
static int
631
use_hardlinks_p (const char *tname)
632
3
{
633
3
  char *lname;
634
3
  struct stat sb;
635
3
  unsigned int nlink;
636
3
  int res;
637
638
3
  if (stat (tname, &sb))
639
0
    return -1;
640
3
  nlink = (unsigned int)sb.st_nlink;
641
642
3
  lname = xtrymalloc (strlen (tname) + 1 + 1);
643
3
  if (!lname)
644
0
    return -1;
645
3
  strcpy (lname, tname);
646
3
  strcat (lname, "x");
647
648
  /* We ignore the return value of link() because it is unreliable.  */
649
3
  (void) link (tname, lname);
650
651
3
  if (stat (tname, &sb))
652
0
    res = -1;  /* Ooops.  */
653
3
  else if (sb.st_nlink == nlink + 1)
654
3
    res = 0;   /* Yeah, hardlinks are supported.  */
655
0
  else
656
0
    res = 1;   /* No hardlink support.  */
657
658
3
  unlink (lname);
659
3
  xfree (lname);
660
3
  return res;
661
3
}
662
#endif /*HAVE_POSIX_SYSTEM */
663
664
665

666
#ifdef  HAVE_POSIX_SYSTEM
667
static int
668
dotlock_get_process_id (dotlock_t h)
669
6
{
670
6
  return h->by_parent? (int)getppid(): (int)getpid();
671
6
}
672
673
static int
674
dotlock_detect_tname (dotlock_t h)
675
0
{
676
0
  struct stat sb;
677
0
  DIR *dir;
678
0
  char *dirname;
679
0
  char *basename;
680
0
  struct dirent *d;
681
0
  int r;
682
683
0
  if (stat (h->lockname, &sb))
684
0
    return -1;
685
686
0
  basename = make_basename (h->lockname, NULL);
687
0
  dirname = make_dirname (h->lockname);
688
689
0
  dir = opendir (dirname);
690
0
  if (dir == NULL)
691
0
    {
692
0
      xfree (basename);
693
0
      xfree (dirname);
694
0
      return -1;
695
0
    }
696
697
0
  while ((d = readdir (dir)))
698
0
    if (sb.st_ino == d->d_ino && strcmp (d->d_name, basename))
699
0
      break;
700
701
0
  if (d)
702
0
    {
703
0
      int len = strlen (h->tname);
704
0
      int dlen = strlen (d->d_name);
705
0
      const char *tname_path;
706
707
0
      if (dlen > len)
708
0
        {
709
0
          closedir (dir);
710
0
          xfree (basename);
711
0
          xfree (dirname);
712
0
          return -1;
713
0
        }
714
715
0
      strcpy (stpcpy (stpcpy (h->tname, dirname), DIRSEP_S), d->d_name);
716
0
      h->use_o_excl = 0;
717
0
      tname_path = strchr (h->tname + strlen (dirname) + 2, '.');
718
0
      if (!tname_path)
719
0
        {
720
0
          closedir (dir);
721
0
          xfree (basename);
722
0
          xfree (dirname);
723
0
          return -1;
724
0
        }
725
0
      h->nodename_off = tname_path - h->tname + 1;
726
0
    }
727
0
  else
728
0
    h->use_o_excl = 1;
729
730
0
  r = closedir (dir);
731
0
  if (r)
732
0
    {
733
0
      xfree (basename);
734
0
      xfree (dirname);
735
0
      return r;
736
0
    }
737
738
0
  xfree (basename);
739
0
  xfree (dirname);
740
0
  return 0;
741
0
}
742
743
/* Locking core for Unix.  It used a temporary file and the link
744
   system call to make locking an atomic operation. */
745
static dotlock_t
746
dotlock_create_unix (dotlock_t h, const char *file_to_lock)
747
3
{
748
3
  int  fd = -1;
749
3
  char pidstr[16];
750
3
  const char *nodename;
751
3
  const char *dirpart;
752
3
  int dirpartlen;
753
3
  struct utsname utsbuf;
754
3
  size_t tnamelen;
755
3
  int pid;
756
757
3
  pid = dotlock_get_process_id (h);
758
3
  snprintf (pidstr, sizeof pidstr, "%10d\n", pid);
759
760
  /* Create a temporary file. */
761
3
  if ( uname ( &utsbuf ) )
762
0
    nodename = "unknown";
763
3
  else
764
3
    nodename = utsbuf.nodename;
765
766
3
  if ( !(dirpart = strrchr (file_to_lock, DIRSEP_C)) )
767
0
    {
768
0
      dirpart = EXTSEP_S;
769
0
      dirpartlen = 1;
770
0
    }
771
3
  else
772
3
    {
773
3
      dirpartlen = dirpart - file_to_lock;
774
3
      dirpart = file_to_lock;
775
3
    }
776
777
3
  LOCK_all_lockfiles ();
778
3
  h->next = all_lockfiles;
779
3
  all_lockfiles = h;
780
781
3
  tnamelen = dirpartlen + 6 + 30 + strlen(nodename) + 10 + 1;
782
3
  h->tname = xtrymalloc (tnamelen + 1);
783
3
  if (!h->tname)
784
0
    {
785
0
      all_lockfiles = h->next;
786
0
      UNLOCK_all_lockfiles ();
787
0
      xfree (h);
788
0
      return NULL;
789
0
    }
790
3
  h->nodename_len = strlen (nodename);
791
792
3
  if (h->no_write)
793
0
    {
794
0
      memset (h->tname, '_', tnamelen);
795
0
      h->tname[tnamelen] = 0;
796
0
      goto skip_write;
797
0
    }
798
799
3
  snprintf (h->tname, tnamelen, "%.*s/.#lk%p.", dirpartlen, dirpart, h );
800
3
  h->nodename_off = strlen (h->tname);
801
3
  snprintf (h->tname+h->nodename_off, tnamelen - h->nodename_off,
802
3
           "%s.%d", nodename, pid);
803
804
3
  do
805
3
    {
806
3
      my_set_errno (0);
807
3
      fd = open (h->tname, O_WRONLY|O_CREAT|O_EXCL,
808
3
                 S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR );
809
3
    }
810
3
  while (fd == -1 && errno == EINTR);
811
812
3
  if ( fd == -1 )
813
0
    {
814
0
      int saveerrno = errno;
815
0
      all_lockfiles = h->next;
816
0
      UNLOCK_all_lockfiles ();
817
0
      my_error_2 (_("failed to create temporary file '%s': %s\n"),
818
0
                  h->tname, strerror (errno));
819
0
      if (h->info_cb)
820
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_WAITING,
821
0
                    _("failed to create temporary file '%s': %s\n"),
822
0
                    h->tname, strerror (errno));
823
0
      xfree (h->tname);
824
0
      xfree (h);
825
0
      my_set_errno (saveerrno);
826
0
      return NULL;
827
0
    }
828
3
  if ( write (fd, pidstr, 11 ) != 11 )
829
0
    goto write_failed;
830
3
  if ( write (fd, nodename, strlen (nodename) ) != strlen (nodename) )
831
0
    goto write_failed;
832
3
  if ( write (fd, "\n", 1 ) != 1 )
833
0
    goto write_failed;
834
3
  if ( close (fd) )
835
0
    {
836
0
      if ( errno == EINTR )
837
0
        fd = -1;
838
0
      goto write_failed;
839
0
    }
840
3
  fd = -1;
841
842
  /* Check whether we support hard links.  */
843
3
  switch (use_hardlinks_p (h->tname))
844
3
    {
845
3
    case 0: /* Yes.  */
846
3
      break;
847
0
    case 1: /* No.  */
848
0
      unlink (h->tname);
849
0
      h->use_o_excl = 1;
850
0
      break;
851
0
    default:
852
0
      {
853
0
        int saveerrno = errno;
854
0
        my_error_2 ("can't check whether hardlinks are supported for '%s': %s\n"
855
0
                    , h->tname, strerror (saveerrno));
856
0
        if (h->info_cb)
857
0
          h->info_cb (h, h->info_cb_value, DOTLOCK_CONFIG_TEST,
858
0
                   "can't check whether hardlinks are supported for '%s': %s\n"
859
0
                   , h->tname, strerror (saveerrno));
860
0
        my_set_errno (saveerrno);
861
0
      }
862
0
      goto write_failed;
863
3
    }
864
865
3
 skip_write:
866
3
  h->lockname = xtrymalloc (strlen (file_to_lock) + 6 );
867
3
  if (!h->lockname)
868
0
    {
869
0
      int saveerrno = errno;
870
0
      all_lockfiles = h->next;
871
0
      UNLOCK_all_lockfiles ();
872
0
      unlink (h->tname);
873
0
      xfree (h->tname);
874
0
      xfree (h);
875
0
      my_set_errno (saveerrno);
876
0
      return NULL;
877
0
    }
878
3
  strcpy (stpcpy (h->lockname, file_to_lock), EXTSEP_S "lock");
879
3
  UNLOCK_all_lockfiles ();
880
881
3
  if (h->no_write)
882
0
    {
883
0
      if (dotlock_detect_tname (h) < 0)
884
0
        {
885
0
          xfree (h->lockname);
886
0
          xfree (h->tname);
887
0
          xfree (h);
888
0
          my_set_errno (EACCES);
889
0
          return NULL;
890
0
        }
891
892
0
      h->locked = 1;
893
0
    }
894
895
3
  return h;
896
897
0
 write_failed:
898
0
  {
899
0
    int saveerrno = errno;
900
0
    all_lockfiles = h->next;
901
0
    UNLOCK_all_lockfiles ();
902
0
    my_error_2 (_("error writing to '%s': %s\n"), h->tname, strerror (errno));
903
0
    if (h->info_cb)
904
0
      h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
905
0
                  _("error writing to '%s': %s\n"),
906
0
                  h->tname, strerror (errno));
907
908
0
    if ( fd != -1 )
909
0
      close (fd);
910
0
    unlink (h->tname);
911
0
    xfree (h->tname);
912
0
    xfree (h);
913
0
    my_set_errno (saveerrno);
914
0
  }
915
0
  return NULL;
916
3
}
917
#endif /*HAVE_POSIX_SYSTEM*/
918
919
920
#ifdef HAVE_DOSISH_SYSTEM
921
/* Locking core for Windows.  This version does not need a temporary
922
   file but uses the plain lock file along with record locking.  We
923
   create this file here so that we later only need to do the file
924
   locking.  For error reporting it is useful to keep the name of the
925
   file in the handle.  */
926
static dotlock_t
927
dotlock_create_w32 (dotlock_t h, const char *file_to_lock)
928
{
929
  LOCK_all_lockfiles ();
930
  h->next = all_lockfiles;
931
  all_lockfiles = h;
932
933
  h->lockname = strconcat (file_to_lock, EXTSEP_S "lock", NULL);
934
  if (!h->lockname)
935
    {
936
      all_lockfiles = h->next;
937
      UNLOCK_all_lockfiles ();
938
      xfree (h);
939
      return NULL;
940
    }
941
942
  /* If would be nice if we would use the FILE_FLAG_DELETE_ON_CLOSE
943
     along with FILE_SHARE_DELETE but that does not work due to a race
944
     condition: Despite the OPEN_ALWAYS flag CreateFile may return an
945
     error and we can't reliable create/open the lock file unless we
946
     would wait here until it works - however there are other valid
947
     reasons why a lock file can't be created and thus the process
948
     would not stop as expected but spin until Windows crashes.  Our
949
     solution is to keep the lock file open; that does not harm. */
950
  if (any8bitchar (h->lockname))
951
    {
952
      wchar_t *wname = utf8_to_wchar (h->lockname);
953
954
      if (wname)
955
        h->lockhd = CreateFileW (wname,
956
                                 GENERIC_READ|GENERIC_WRITE,
957
                                 FILE_SHARE_READ|FILE_SHARE_WRITE,
958
                                 NULL, OPEN_ALWAYS, 0, NULL);
959
      else
960
        h->lockhd = INVALID_HANDLE_VALUE;
961
      xfree (wname);
962
    }
963
  else
964
    h->lockhd = CreateFileA (h->lockname,
965
                             GENERIC_READ|GENERIC_WRITE,
966
                             FILE_SHARE_READ|FILE_SHARE_WRITE,
967
                             NULL, OPEN_ALWAYS, 0, NULL);
968
  if (h->lockhd == INVALID_HANDLE_VALUE)
969
    {
970
      int saveerrno = map_w32_to_errno (GetLastError ());
971
      all_lockfiles = h->next;
972
      UNLOCK_all_lockfiles ();
973
      my_error_2 (_("can't create '%s': %s\n"), h->lockname, w32_strerror (-1));
974
      if (h->info_cb)
975
        h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
976
                    _("can't create '%s': %s\n"),
977
                    h->lockname, w32_strerror (-1));
978
      xfree (h->lockname);
979
      xfree (h);
980
      my_set_errno (saveerrno);
981
      return NULL;
982
    }
983
  UNLOCK_all_lockfiles ();
984
  return h;
985
}
986
#endif /*HAVE_DOSISH_SYSTEM*/
987
988
989
/* Create a lockfile for a file name FILE_TO_LOCK and returns an
990
   object of type dotlock_t which may be used later to actually acquire
991
   the lock.  A cleanup routine gets installed to cleanup left over
992
   locks or other files used internally by the lock mechanism.
993
994
   Calling this function with NULL does only install the atexit
995
   handler and may thus be used to assure that the cleanup is called
996
   after all other atexit handlers.
997
998
   This function creates a lock file in the same directory as
999
   FILE_TO_LOCK using that name and a suffix of ".lock".  Note that on
1000
   POSIX systems a temporary file ".#lk.<hostname>.pid[.threadid] is
1001
   used.
1002
1003
   FLAGS may include DOTLOCK_PREPARE_CREATE bit, which only allocates
1004
   the handle and requires a further call to dotlock_finish_create.
1005
   This can be used to set a callback between these calls.
1006
1007
   FLAGS may include DOTLOCK_LOCK_BY_PARENT bit, when it's the parent
1008
   process controlling the lock.  This is used by dotlock_tool in
1009
   gpgconf.
1010
1011
   FLAGS may include DOTLOCK_LOCKED bit, when it should not create the
1012
   lockfile, but to unlock.  This is used by dotlock_tool in gpgconf.
1013
1014
   The function returns an new handle which needs to be released using
1015
   destroy_dotlock but gets also released at the termination of the
1016
   process.  On error NULL is returned.
1017
 */
1018
1019
dotlock_t
1020
dotlock_create (const char *file_to_lock, unsigned int flags)
1021
3
{
1022
3
  static int initialized;
1023
3
  dotlock_t h;
1024
3
#ifndef HAVE_DOSISH_SYSTEM
1025
3
  int by_parent = 0;
1026
3
  int no_write = 0;
1027
3
#endif
1028
1029
3
  if ( !(flags & DOTLOCK_LOCK_BY_PARENT)
1030
3
       && !initialized )
1031
1
    {
1032
1
      atexit (dotlock_remove_lockfiles);
1033
1
      initialized = 1;
1034
1
    }
1035
1036
3
  if ( !file_to_lock )
1037
0
    return NULL;  /* Only initialization was requested.  */
1038
1039
3
#ifndef HAVE_DOSISH_SYSTEM
1040
3
  if ((flags & DOTLOCK_LOCK_BY_PARENT) || (flags & DOTLOCK_LOCKED))
1041
0
    {
1042
0
      by_parent = !!(flags & DOTLOCK_LOCK_BY_PARENT);
1043
0
      no_write = !!(flags & DOTLOCK_LOCKED);
1044
0
      flags &= ~(DOTLOCK_LOCK_BY_PARENT | DOTLOCK_LOCKED);
1045
0
    }
1046
3
#endif
1047
3
  if ((flags & ~DOTLOCK_PREPARE_CREATE))
1048
0
    {
1049
0
      my_set_errno (EINVAL);
1050
0
      return NULL;
1051
0
    }
1052
1053
3
  h = xtrycalloc (1, sizeof *h);
1054
3
  if (!h)
1055
0
    return NULL;
1056
3
  h->extra_fd = -1;
1057
3
#ifndef HAVE_DOSISH_SYSTEM
1058
3
  h->by_parent = by_parent;
1059
3
  h->no_write = no_write;
1060
3
#endif
1061
1062
3
  if (never_lock)
1063
0
    {
1064
0
      h->disable = 1;
1065
0
      LOCK_all_lockfiles ();
1066
0
      h->next = all_lockfiles;
1067
0
      all_lockfiles = h;
1068
0
      UNLOCK_all_lockfiles ();
1069
0
      return h;
1070
0
    }
1071
1072
3
  if ((flags & DOTLOCK_PREPARE_CREATE))
1073
0
    return h;
1074
3
  else
1075
3
    return dotlock_finish_create (h, file_to_lock);
1076
3
}
1077
1078
1079
/* This function may be used along with dotlock_create (file_name,
1080
 * DOTLOCK_PREPARE_CREATE) to finish the creation call. The given
1081
 * filename shall be the same as passed to dotlock_create.  On success
1082
 * the same handle H is returned, on error NULL is returned and H is
1083
 * released.  */
1084
dotlock_t
1085
dotlock_finish_create (dotlock_t h, const char *file_to_lock)
1086
3
{
1087
3
  if (!h || !file_to_lock)
1088
0
    return NULL;
1089
1090
#ifdef HAVE_DOSISH_SYSTEM
1091
  return dotlock_create_w32 (h, file_to_lock);
1092
#else /*!HAVE_DOSISH_SYSTEM */
1093
3
  return dotlock_create_unix (h, file_to_lock);
1094
3
#endif /*!HAVE_DOSISH_SYSTEM*/
1095
3
}
1096
1097
1098

1099
/* Convenience function to store a file descriptor (or any other
1100
   integer value) in the context of handle H.  */
1101
void
1102
dotlock_set_fd (dotlock_t h, int fd)
1103
0
{
1104
0
  h->extra_fd = fd;
1105
0
}
1106
1107
/* Convenience function to retrieve a file descriptor (or any other
1108
   integer value) stored in the context of handle H.  */
1109
int
1110
dotlock_get_fd (dotlock_t h)
1111
0
{
1112
0
  return h->extra_fd;
1113
0
}
1114
1115
1116
/* Set a callback function for info diagnostics.  The callback
1117
 * function CB is called with the handle, the opaque value OPAQUE, a
1118
 * reason code, and a format string with its arguments.  The callback
1119
 * shall return 0 to continue operation or true in which case the
1120
 * current function will be terminated with an error.  */
1121
void
1122
dotlock_set_info_cb (dotlock_t h,
1123
                     int (*cb)(dotlock_t, void *,
1124
                               enum dotlock_reasons reason,
1125
                               const char *,...),
1126
                     void *opaque)
1127
0
{
1128
0
  h->info_cb = cb;
1129
0
  h->info_cb_value = opaque;
1130
0
}
1131
1132
1133
1134

1135
#ifdef HAVE_POSIX_SYSTEM
1136
/* Unix specific code of destroy_dotlock.  */
1137
static void
1138
dotlock_destroy_unix (dotlock_t h)
1139
3
{
1140
3
  if (h->locked && h->lockname)
1141
0
    unlink (h->lockname);
1142
3
  if (h->tname && !h->use_o_excl)
1143
3
    unlink (h->tname);
1144
3
}
1145
#endif /*HAVE_POSIX_SYSTEM*/
1146
1147
1148
#ifdef HAVE_DOSISH_SYSTEM
1149
/* Windows specific code of destroy_dotlock.  */
1150
static void
1151
dotlock_destroy_w32 (dotlock_t h)
1152
{
1153
  if (h->locked)
1154
    {
1155
      OVERLAPPED ovl;
1156
1157
      memset (&ovl, 0, sizeof ovl);
1158
      UnlockFileEx (h->lockhd, 0, 1, 0, &ovl);
1159
    }
1160
  CloseHandle (h->lockhd);
1161
}
1162
#endif /*HAVE_DOSISH_SYSTEM*/
1163
1164
1165
/* Destroy the lock handle H and release the lock.  */
1166
void
1167
dotlock_destroy (dotlock_t h)
1168
3
{
1169
3
  dotlock_t hprev, htmp;
1170
1171
3
  if ( !h )
1172
0
    return;
1173
1174
  /* First remove the handle from our global list of all locks. */
1175
3
  LOCK_all_lockfiles ();
1176
3
  for (hprev=NULL, htmp=all_lockfiles; htmp; hprev=htmp, htmp=htmp->next)
1177
1
    if (htmp == h)
1178
1
      {
1179
1
        if (hprev)
1180
0
          hprev->next = htmp->next;
1181
1
        else
1182
1
          all_lockfiles = htmp->next;
1183
1
        h->next = NULL;
1184
1
        break;
1185
1
      }
1186
3
  UNLOCK_all_lockfiles ();
1187
1188
  /* Then destroy the lock. */
1189
3
  if (!h->disable
1190
3
      && (!h->by_parent || h->no_write))
1191
3
    {
1192
      /* NOTE: under the condition of (by_parent && !no_write),
1193
         it doesn't come here.  So, the lock file remains.  */
1194
#ifdef HAVE_DOSISH_SYSTEM
1195
      dotlock_destroy_w32 (h);
1196
#else /* !HAVE_DOSISH_SYSTEM */
1197
3
      dotlock_destroy_unix (h);
1198
3
#endif /* HAVE_DOSISH_SYSTEM */
1199
3
    }
1200
1201
3
#ifdef HAVE_POSIX_SYSTEM
1202
  /* When DOTLOCK_LOCK_BY_PARENT and lock fails,
1203
     the temporary file created should be removed.  */
1204
3
  if (h->by_parent && !h->no_write && !h->locked)
1205
0
    if (h->tname && !h->use_o_excl)
1206
0
      unlink (h->tname);
1207
1208
3
  xfree (h->tname);
1209
3
#endif
1210
3
  xfree (h->lockname);
1211
3
  xfree(h);
1212
3
}
1213
1214
1215
/* Return true if H has been taken.  */
1216
int
1217
dotlock_is_locked (dotlock_t h)
1218
0
{
1219
0
  return h && !!h->locked;
1220
0
}
1221
1222
1223
/* Return the next interval to wait.  WTIME and TIMEOUT are pointers
1224
 * to the current state and are updated by this function.  The
1225
 * returned value might be different from the value of WTIME.  */
1226
static int
1227
next_wait_interval (int *wtime, long *timeout)
1228
0
{
1229
0
  int result;
1230
1231
  /* Wait until lock has been released.  We use retry intervals of 4,
1232
   * 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 512, 1024, 2048ms, and
1233
   * so on.  If wait-forever was requested we add a small random value
1234
   * to have different timeouts per process. */
1235
0
  if (!*wtime)
1236
0
    *wtime = 4;
1237
0
  else if (*wtime < 2048)
1238
0
    *wtime *= 2;
1239
0
  else
1240
0
    *wtime = 512;
1241
1242
0
  result = *wtime;
1243
0
  if (*wtime > 8 && *timeout < 0)
1244
0
    result += ((unsigned int)getpid() % 37);
1245
1246
0
  if (*timeout > 0)
1247
0
    {
1248
0
      if (result > *timeout)
1249
0
        result = *timeout;
1250
0
      *timeout -= result;
1251
0
    }
1252
1253
0
  return result;
1254
0
}
1255
1256
1257

1258
#ifdef HAVE_POSIX_SYSTEM
1259
/* Unix specific code of make_dotlock.  Returns 0 on success and -1 on
1260
   error.  */
1261
static int
1262
dotlock_take_unix (dotlock_t h, long timeout)
1263
3
{
1264
3
  int wtime = 0;
1265
3
  int timedout = 0;
1266
3
  int sumtime = 0;
1267
3
  int pid;
1268
3
  int lastpid = -1;
1269
3
  int ownerchanged;
1270
3
  const char *maybe_dead="";
1271
3
  int same_node;
1272
3
  int saveerrno;
1273
3
  int fd;
1274
1275
3
 again:
1276
3
  if (h->use_o_excl)
1277
0
    {
1278
      /* No hardlink support - use open(O_EXCL).  */
1279
0
      do
1280
0
        {
1281
0
          my_set_errno (0);
1282
0
          fd = open (h->lockname, O_WRONLY|O_CREAT|O_EXCL,
1283
0
                     S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR );
1284
0
        }
1285
0
      while (fd == -1 && errno == EINTR);
1286
1287
0
      if (fd == -1 && errno == EEXIST)
1288
0
        ; /* Lock held by another process.  */
1289
0
      else if (fd == -1)
1290
0
        {
1291
0
          saveerrno = errno;
1292
0
          my_error_2 ("lock not made: open(O_EXCL) of '%s' failed: %s\n",
1293
0
                      h->lockname, strerror (saveerrno));
1294
0
          if (h->info_cb)
1295
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1296
0
                        "lock not made: open(O_EXCL) of '%s' failed: %s\n",
1297
0
                        h->lockname, strerror (saveerrno));
1298
0
          my_set_errno (saveerrno);
1299
0
          return -1;
1300
0
        }
1301
0
      else
1302
0
        {
1303
0
          char pidstr[16];
1304
1305
0
          snprintf (pidstr, sizeof pidstr, "%10d\n",
1306
0
                    dotlock_get_process_id (h));
1307
0
          if (write (fd, pidstr, 11 ) == 11
1308
0
              && write (fd, h->tname + h->nodename_off,h->nodename_len)
1309
0
              == h->nodename_len
1310
0
              && write (fd, "\n", 1) == 1
1311
0
              && !close (fd))
1312
0
            {
1313
0
              h->locked = 1;
1314
0
              return 0;
1315
0
            }
1316
          /* Write error.  */
1317
0
          saveerrno = errno;
1318
0
          my_error_2 ("lock not made: writing to '%s' failed: %s\n",
1319
0
                      h->lockname, strerror (errno));
1320
0
          if (h->info_cb)
1321
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1322
0
                        "lock not made: writing to '%s' failed: %s\n",
1323
0
                        h->lockname, strerror (errno));
1324
0
          close (fd);
1325
0
          unlink (h->lockname);
1326
0
          my_set_errno (saveerrno);
1327
0
          return -1;
1328
0
        }
1329
0
    }
1330
3
  else /* Standard method:  Use hardlinks.  */
1331
3
    {
1332
3
      struct stat sb;
1333
1334
      /* We ignore the return value of link() because it is unreliable.  */
1335
3
      (void) link (h->tname, h->lockname);
1336
1337
3
      if (stat (h->tname, &sb))
1338
0
        {
1339
0
          saveerrno = errno;
1340
0
          my_error_1 ("lock not made: Oops: stat of tmp file failed: %s\n",
1341
0
                      strerror (errno));
1342
0
          if (h->info_cb)
1343
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1344
0
                        "lock not made: Oops: stat of tmp file failed: %s\n",
1345
0
                        strerror (errno));
1346
          /* In theory this might be a severe error: It is possible
1347
             that link succeeded but stat failed due to changed
1348
             permissions.  We can't do anything about it, though.  */
1349
0
          my_set_errno (saveerrno);
1350
0
          return -1;
1351
0
        }
1352
1353
3
      if (sb.st_nlink == 2)
1354
3
        {
1355
3
          h->locked = 1;
1356
3
          return 0; /* Okay.  */
1357
3
        }
1358
3
    }
1359
1360
  /* Check for stale lock files.  */
1361
0
  if ( (pid = read_lockfile (h, &same_node, &fd)) == -1 )
1362
0
    {
1363
0
      if ( errno != ENOENT )
1364
0
        {
1365
0
          saveerrno = errno;
1366
0
          my_info_0 ("cannot read lockfile\n");
1367
0
          if (h->info_cb)
1368
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1369
0
                        "cannot read lockfile\n");
1370
0
          my_set_errno (saveerrno);
1371
0
          return -1;
1372
0
        }
1373
0
      my_info_0 ("lockfile disappeared\n");
1374
0
      goto again;
1375
0
    }
1376
0
  else if ( (pid == dotlock_get_process_id (h) && same_node && !h->by_parent)
1377
0
            || (same_node && kill (pid, 0) && errno == ESRCH) )
1378
0
    {
1379
      /* Stale lockfile is detected. */
1380
0
      struct stat sb;
1381
1382
      /* Check if it's unlocked during examining the lockfile.  */
1383
0
      if (fstat (fd, &sb) || sb.st_nlink == 0)
1384
0
        {
1385
          /* It's gone already by another process.  */
1386
0
          close (fd);
1387
0
          goto again;
1388
0
        }
1389
1390
      /*
1391
       * Here, although it's quite _rare_, we have a race condition.
1392
       *
1393
       * When multiple processes race on a stale lockfile, detecting
1394
       * AND removing should be done atomically.  That is, to work
1395
       * correctly, the file to be removed should be the one which is
1396
       * examined for detection.
1397
       *
1398
       * But, when it's not atomic, consider the case for us where it
1399
       * takes some time between the detection and the removal of the
1400
       * lockfile.
1401
       *
1402
       * In this situation, it is possible that the file which was
1403
       * detected as stale is already removed by another process and
1404
       * then new lockfile is created (by that process or other one).
1405
       *
1406
       * And it is newly created valid lockfile which is going to be
1407
       * removed by us.
1408
       *
1409
       * Consider this long comment as it expresses possible (long)
1410
       * time between fstat above and unlink below; Meanwhile, the
1411
       * lockfile in question may be removed and there may be new
1412
       * valid one.
1413
       *
1414
       * In short, when you see the message of removing stale lockfile
1415
       * when there are multiple processes for the work, there is
1416
       * (very) little possibility something went wrong.
1417
       */
1418
1419
0
      unlink (h->lockname);
1420
0
      my_info_1 (_("removing stale lockfile (created by %d)\n"), pid);
1421
0
      close (fd);
1422
0
      if (h->info_cb)
1423
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_STALE_REMOVED,
1424
0
                    _("removing stale lockfile (created by %d)\n"), pid);
1425
0
      goto again;
1426
0
    }
1427
1428
0
  close (fd);
1429
0
  if (lastpid == -1)
1430
0
    lastpid = pid;
1431
0
  ownerchanged = (pid != lastpid);
1432
1433
0
  if (timeout)
1434
0
    {
1435
0
      struct timeval tv;
1436
0
      int wtimereal;
1437
1438
0
      if (ownerchanged)
1439
0
        wtime = 0;  /* Reset because owner changed.  */
1440
1441
0
      wtimereal = next_wait_interval (&wtime, &timeout);
1442
0
      if (!timeout)
1443
0
        timedout = 1;  /* remember.  */
1444
1445
0
      sumtime += wtimereal;
1446
0
      if (sumtime >= 1500)
1447
0
        {
1448
0
          sumtime = 0;
1449
0
          my_info_3 (_("waiting for lock (held by %d%s) %s...\n"),
1450
0
                     pid, maybe_dead, maybe_deadlock(h)? _("(deadlock?) "):"");
1451
0
          if (h->info_cb
1452
0
              && h->info_cb (h, h->info_cb_value, DOTLOCK_WAITING,
1453
0
                             _("waiting for lock (held by %d%s) %s...\n"),
1454
0
                             pid, maybe_dead,
1455
0
                             maybe_deadlock(h)? _("(deadlock?) "):""))
1456
0
            {
1457
0
              my_set_errno (ECANCELED);
1458
0
              return -1;
1459
0
            }
1460
0
        }
1461
1462
0
      tv.tv_sec = wtimereal / 1000;
1463
0
      tv.tv_usec = (wtimereal % 1000) * 1000;
1464
0
      select (0, NULL, NULL, NULL, &tv);
1465
0
      goto again;
1466
0
    }
1467
1468
0
  my_set_errno (timedout? ETIMEDOUT : EACCES);
1469
0
  return -1;
1470
0
}
1471
#endif /*HAVE_POSIX_SYSTEM*/
1472
1473
1474
#ifdef HAVE_DOSISH_SYSTEM
1475
/* Windows specific code of make_dotlock.  Returns 0 on success and -1 on
1476
   error.  */
1477
static int
1478
dotlock_take_w32 (dotlock_t h, long timeout)
1479
{
1480
  int wtime = 0;
1481
  int timedout = 0;
1482
  int w32err;
1483
  OVERLAPPED ovl;
1484
1485
 again:
1486
  /* Lock one byte at offset 0.  The offset is given by OVL.  */
1487
  memset (&ovl, 0, sizeof ovl);
1488
  if (LockFileEx (h->lockhd, (LOCKFILE_EXCLUSIVE_LOCK
1489
                              | LOCKFILE_FAIL_IMMEDIATELY), 0, 1, 0, &ovl))
1490
    {
1491
      h->locked = 1;
1492
      return 0; /* okay */
1493
    }
1494
1495
  w32err = GetLastError ();
1496
  if (w32err != ERROR_LOCK_VIOLATION)
1497
    {
1498
      my_error_2 (_("lock '%s' not made: %s\n"),
1499
                  h->lockname, w32_strerror (w32err));
1500
      if (h->info_cb)
1501
        h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1502
                    _("lock '%s' not made: %s\n"),
1503
                    h->lockname, w32_strerror (w32err));
1504
      my_set_errno (map_w32_to_errno (w32err));
1505
      return -1;
1506
    }
1507
1508
  if (timeout)
1509
    {
1510
      int wtimereal;
1511
1512
      wtimereal = next_wait_interval (&wtime, &timeout);
1513
      if (!timeout)
1514
        timedout = 1;  /* remember.  */
1515
1516
      if (wtime >= 800)
1517
        {
1518
          my_info_1 (_("waiting for lock %s...\n"), h->lockname);
1519
          if (h->info_cb
1520
              && h->info_cb (h, h->info_cb_value, DOTLOCK_WAITING,
1521
                             _("waiting for lock %s...\n"), h->lockname))
1522
            {
1523
              my_set_errno (ECANCELED);
1524
              return -1;
1525
            }
1526
        }
1527
1528
      Sleep (wtimereal);
1529
      goto again;
1530
    }
1531
1532
  my_set_errno (timedout? ETIMEDOUT : EACCES);
1533
  return -1;
1534
}
1535
#endif /*HAVE_DOSISH_SYSTEM*/
1536
1537
1538
/* Take a lock on H.  A value of 0 for TIMEOUT returns immediately if
1539
   the lock can't be taken, -1 waits forever (hopefully not), other
1540
   values wait for TIMEOUT milliseconds.  Returns: 0 on success  */
1541
int
1542
dotlock_take (dotlock_t h, long timeout)
1543
3
{
1544
3
  int ret;
1545
1546
3
  if ( h->disable )
1547
0
    return 0; /* Locks are completely disabled.  Return success. */
1548
1549
3
  if ( h->locked )
1550
0
    {
1551
0
      my_info_1 ("Oops, '%s' is already locked\n", h->lockname);
1552
0
      return 0;
1553
0
    }
1554
1555
#ifdef HAVE_DOSISH_SYSTEM
1556
  ret = dotlock_take_w32 (h, timeout);
1557
#else /*!HAVE_DOSISH_SYSTEM*/
1558
3
  ret = dotlock_take_unix (h, timeout);
1559
3
#endif /*!HAVE_DOSISH_SYSTEM*/
1560
1561
3
  return ret;
1562
3
}
1563
1564
1565

1566
#ifdef HAVE_POSIX_SYSTEM
1567
/* Unix specific code of release_dotlock.  */
1568
static int
1569
dotlock_release_unix (dotlock_t h)
1570
3
{
1571
3
  int pid, same_node;
1572
3
  int saveerrno;
1573
1574
3
  pid = read_lockfile (h, &same_node, NULL);
1575
3
  if ( pid == -1 )
1576
0
    {
1577
0
      saveerrno = errno;
1578
0
      my_error_0 ("release_dotlock: lockfile error\n");
1579
0
      if (h->info_cb)
1580
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1581
0
                    "release_dotlock: lockfile error\n");
1582
0
      my_set_errno (saveerrno);
1583
0
      return -1;
1584
0
    }
1585
3
  if ( pid != dotlock_get_process_id (h) || !same_node )
1586
0
    {
1587
0
      my_error_1 ("release_dotlock: not our lock (pid=%d)\n", pid);
1588
0
      if (h->info_cb)
1589
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_CONFLICT,
1590
0
                    "release_dotlock: not our lock (pid=%d)\n", pid);
1591
0
      my_set_errno (EACCES);
1592
0
      return -1;
1593
0
    }
1594
1595
3
  if ( unlink( h->lockname ) )
1596
0
    {
1597
0
      saveerrno = errno;
1598
0
      my_error_1 ("release_dotlock: error removing lockfile '%s'\n",
1599
0
                  h->lockname);
1600
0
      if (h->info_cb)
1601
0
        h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1602
0
                    "release_dotlock: error removing lockfile '%s'\n",
1603
0
                    h->lockname);
1604
0
      my_set_errno (saveerrno);
1605
0
      return -1;
1606
0
    }
1607
  /* Fixme: As an extra check we could check whether the link count is
1608
     now really at 1. */
1609
3
  return 0;
1610
3
}
1611
#endif /*HAVE_POSIX_SYSTEM */
1612
1613
1614
#ifdef HAVE_DOSISH_SYSTEM
1615
/* Windows specific code of release_dotlock.  */
1616
static int
1617
dotlock_release_w32 (dotlock_t h)
1618
{
1619
  OVERLAPPED ovl;
1620
1621
  memset (&ovl, 0, sizeof ovl);
1622
  if (!UnlockFileEx (h->lockhd, 0, 1, 0, &ovl))
1623
    {
1624
      int ec = (int)GetLastError ();
1625
1626
      my_error_2 ("release_dotlock: error removing lockfile '%s': %s\n",
1627
                  h->lockname, w32_strerror (ec));
1628
      if (h->info_cb)
1629
        h->info_cb (h, h->info_cb_value, DOTLOCK_FILE_ERROR,
1630
                    "release_dotlock: error removing lockfile '%s': %s\n",
1631
                    h->lockname, w32_strerror (ec));
1632
      my_set_errno (map_w32_to_errno (ec));
1633
      return -1;
1634
    }
1635
1636
  return 0;
1637
}
1638
#endif /*HAVE_DOSISH_SYSTEM */
1639
1640
1641
/* Release a lock.  Returns 0 on success.  */
1642
int
1643
dotlock_release (dotlock_t h)
1644
3
{
1645
3
  int ret;
1646
1647
  /* To avoid atexit race conditions we first check whether there are
1648
     any locks left.  It might happen that another atexit handler
1649
     tries to release the lock while the atexit handler of this module
1650
     already ran and thus H is undefined.  */
1651
3
  LOCK_all_lockfiles ();
1652
3
  ret = !all_lockfiles;
1653
3
  UNLOCK_all_lockfiles ();
1654
3
  if (ret)
1655
0
    return 0;
1656
1657
3
  if ( h->disable )
1658
0
    return 0;
1659
1660
3
  if ( !h->locked )
1661
0
    {
1662
0
      if (!never_lock)
1663
0
        {
1664
0
          my_info_1 ("Oops, '%s' is not locked\n", h->lockname);
1665
0
          if (h->info_cb)
1666
0
            h->info_cb (h, h->info_cb_value, DOTLOCK_NOT_LOCKED,
1667
0
                        "Oops, '%s' is not locked\n", h->lockname);
1668
0
        }
1669
0
      return 0;
1670
0
    }
1671
1672
#ifdef HAVE_DOSISH_SYSTEM
1673
  ret = dotlock_release_w32 (h);
1674
#else
1675
3
  ret = dotlock_release_unix (h);
1676
3
#endif
1677
1678
3
  if (!ret)
1679
3
    h->locked = 0;
1680
3
  return ret;
1681
3
}
1682
1683
1684

1685
/* Remove all lockfiles.  This is called by the atexit handler
1686
   installed by this module but may also be called by other
1687
   termination handlers.  */
1688
void
1689
dotlock_remove_lockfiles (void)
1690
1
{
1691
1
  dotlock_t h, h2;
1692
1693
  /* First set the lockfiles list to NULL so that for example
1694
     dotlock_release is aware that this function is currently
1695
     running.  */
1696
1
  LOCK_all_lockfiles ();
1697
1
  h = all_lockfiles;
1698
1
  all_lockfiles = NULL;
1699
1
  UNLOCK_all_lockfiles ();
1700
1701
3
  while ( h )
1702
2
    {
1703
2
      h2 = h->next;
1704
2
      dotlock_destroy (h);
1705
2
      h = h2;
1706
2
    }
1707
1
}