aboutsummaryrefslogtreecommitdiff
path: root/gcc/lockfile.cc
blob: cecbb86491da2f3db10482f628787bec68e6fe48 (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
/* File locking.
   Copyright (C) 2023-2025 Free Software Foundation, Inc.

This file is part of GCC.

GCC 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, or (at your option) any later
version.

GCC 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 GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#define INCLUDE_STRING
#include "config.h"
#include "system.h"
#include "lockfile.h"

/* fcntl.h may exist without expected contents.  */
#if HAVE_FCNTL_H && HOST_HAS_F_SETLKW
#define LOCKFILE_USE_FCNTL 1
#endif

/* Unique write lock.  No other lock can be held on this lockfile.
   Blocking call.  */
int
lockfile::lock_write ()
{
  fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    return -1;

#ifdef LOCKFILE_USE_FCNTL
  struct flock s_flock;

  s_flock.l_whence = SEEK_SET;
  s_flock.l_start = 0;
  s_flock.l_len = 0;
  s_flock.l_pid = getpid ();
  s_flock.l_type = F_WRLCK;

  while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
    continue;
#endif
  return 0;
}

/* Unique write lock.  No other lock can be held on this lockfile.
   Only locks if this filelock is not locked by any other process.
   Return whether locking was successful.  */
int
lockfile::try_lock_write ()
{
  fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    return -1;

#ifdef LOCKFILE_USE_FCNTL
  struct flock s_flock;

  s_flock.l_whence = SEEK_SET;
  s_flock.l_start = 0;
  s_flock.l_len = 0;
  s_flock.l_pid = getpid ();
  s_flock.l_type = F_WRLCK;

  if (fcntl (fd, F_SETLK, &s_flock) == -1)
    {
      close (fd);
      fd = -1;
      return 1;
    }
#endif
  return 0;
}

/* Shared read lock.  Only read lock can be held concurrently.
   If write lock is already held by this process, it will be
   changed to read lock.
   Blocking call.  */
int
lockfile::lock_read ()
{
  fd = open (filename.c_str (), O_RDWR | O_CREAT, 0666);
  if (fd < 0)
    return -1;

#ifdef LOCKFILE_USE_FCNTL
  struct flock s_flock;

  s_flock.l_whence = SEEK_SET;
  s_flock.l_start = 0;
  s_flock.l_len = 0;
  s_flock.l_pid = getpid ();
  s_flock.l_type = F_RDLCK;

  while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
    continue;
#endif
  return 0;
}

/* Unlock all previously placed locks.  */
void
lockfile::unlock ()
{
  if (fd < 0)
    {
#ifdef LOCKFILE_USE_FCNTL
      struct flock s_flock;

      s_flock.l_whence = SEEK_SET;
      s_flock.l_start = 0;
      s_flock.l_len = 0;
      s_flock.l_pid = getpid ();
      s_flock.l_type = F_UNLCK;

      fcntl (fd, F_SETLK, &s_flock);
#endif
      close (fd);
      fd = -1;
    }
}

/* Are lockfiles supported?  */
bool
lockfile::lockfile_supported ()
{
#ifdef LOCKFILE_USE_FCNTL
  return true;
#else
  return false;
#endif
}