Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/LockFileManager.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- LockFileManager.cpp - File-level Locking Utility------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/Support/LockFileManager.h"
10
#include "llvm/ADT/None.h"
11
#include "llvm/ADT/SmallVector.h"
12
#include "llvm/ADT/StringExtras.h"
13
#include "llvm/Support/Errc.h"
14
#include "llvm/Support/ErrorOr.h"
15
#include "llvm/Support/FileSystem.h"
16
#include "llvm/Support/MemoryBuffer.h"
17
#include "llvm/Support/Signals.h"
18
#include "llvm/Support/raw_ostream.h"
19
#include <cerrno>
20
#include <ctime>
21
#include <memory>
22
#include <sys/stat.h>
23
#include <sys/types.h>
24
#include <system_error>
25
#include <tuple>
26
#ifdef _WIN32
27
#include <windows.h>
28
#endif
29
#if LLVM_ON_UNIX
30
#include <unistd.h>
31
#endif
32
33
#if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (__MAC_OS_X_VERSION_MIN_REQUIRED > 1050)
34
#define USE_OSX_GETHOSTUUID 1
35
#else
36
#define USE_OSX_GETHOSTUUID 0
37
#endif
38
39
#if USE_OSX_GETHOSTUUID
40
#include <uuid/uuid.h>
41
#endif
42
43
using namespace llvm;
44
45
/// Attempt to read the lock file with the given name, if it exists.
46
///
47
/// \param LockFileName The name of the lock file to read.
48
///
49
/// \returns The process ID of the process that owns this lock file
50
Optional<std::pair<std::string, int> >
51
1.84k
LockFileManager::readLockFile(StringRef LockFileName) {
52
1.84k
  // Read the owning host and PID out of the lock file. If it appears that the
53
1.84k
  // owning process is dead, the lock file is invalid.
54
1.84k
  ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
55
1.84k
      MemoryBuffer::getFile(LockFileName);
56
1.84k
  if (!MBOrErr) {
57
1.84k
    sys::fs::remove(LockFileName);
58
1.84k
    return None;
59
1.84k
  }
60
1
  MemoryBuffer &MB = *MBOrErr.get();
61
1
62
1
  StringRef Hostname;
63
1
  StringRef PIDStr;
64
1
  std::tie(Hostname, PIDStr) = getToken(MB.getBuffer(), " ");
65
1
  PIDStr = PIDStr.substr(PIDStr.find_first_not_of(" "));
66
1
  int PID;
67
1
  if (!PIDStr.getAsInteger(10, PID)) {
68
1
    auto Owner = std::make_pair(std::string(Hostname), PID);
69
1
    if (processStillExecuting(Owner.first, Owner.second))
70
1
      return Owner;
71
0
  }
72
0
73
0
  // Delete the lock file. It's invalid anyway.
74
0
  sys::fs::remove(LockFileName);
75
0
  return None;
76
0
}
77
78
1.83k
static std::error_code getHostID(SmallVectorImpl<char> &HostID) {
79
1.83k
  HostID.clear();
80
1.83k
81
1.83k
#if USE_OSX_GETHOSTUUID
82
1.83k
  // On OS X, use the more stable hardware UUID instead of hostname.
83
1.83k
  struct timespec wait = {1, 0}; // 1 second.
84
1.83k
  uuid_t uuid;
85
1.83k
  if (gethostuuid(uuid, &wait) != 0)
86
0
    return std::error_code(errno, std::system_category());
87
1.83k
88
1.83k
  uuid_string_t UUIDStr;
89
1.83k
  uuid_unparse(uuid, UUIDStr);
90
1.83k
  StringRef UUIDRef(UUIDStr);
91
1.83k
  HostID.append(UUIDRef.begin(), UUIDRef.end());
92
1.83k
93
#elif LLVM_ON_UNIX
94
  char HostName[256];
95
  HostName[255] = 0;
96
  HostName[0] = 0;
97
  gethostname(HostName, 255);
98
  StringRef HostNameRef(HostName);
99
  HostID.append(HostNameRef.begin(), HostNameRef.end());
100
101
#else
102
  StringRef Dummy("localhost");
103
  HostID.append(Dummy.begin(), Dummy.end());
104
#endif
105
106
1.83k
  return std::error_code();
107
1.83k
}
108
109
1
bool LockFileManager::processStillExecuting(StringRef HostID, int PID) {
110
1
#if LLVM_ON_UNIX && !defined(__ANDROID__)
111
1
  SmallString<256> StoredHostID;
112
1
  if (getHostID(StoredHostID))
113
0
    return true; // Conservatively assume it's executing on error.
114
1
115
1
  // Check whether the process is dead. If so, we're done.
116
1
  if (StoredHostID == HostID && getsid(PID) == -1 && errno
== ESRCH0
)
117
1
    
return false0
;
118
1
#endif
119
1
120
1
  return true;
121
1
}
122
123
namespace {
124
125
/// An RAII helper object ensure that the unique lock file is removed.
126
///
127
/// Ensures that if there is an error or a signal before we finish acquiring the
128
/// lock, the unique file will be removed. And if we successfully take the lock,
129
/// the signal handler is left in place so that signals while the lock is held
130
/// will remove the unique lock file. The caller should ensure there is a
131
/// matching call to sys::DontRemoveFileOnSignal when the lock is released.
132
class RemoveUniqueLockFileOnSignal {
133
  StringRef Filename;
134
  bool RemoveImmediately;
135
public:
136
  RemoveUniqueLockFileOnSignal(StringRef Name)
137
1.83k
  : Filename(Name), RemoveImmediately(true) {
138
1.83k
    sys::RemoveFileOnSignal(Filename, nullptr);
139
1.83k
  }
140
141
1.83k
  ~RemoveUniqueLockFileOnSignal() {
142
1.83k
    if (!RemoveImmediately) {
143
1.83k
      // Leave the signal handler enabled. It will be removed when the lock is
144
1.83k
      // released.
145
1.83k
      return;
146
1.83k
    }
147
0
    sys::fs::remove(Filename);
148
0
    sys::DontRemoveFileOnSignal(Filename);
149
0
  }
150
151
1.83k
  void lockAcquired() { RemoveImmediately = false; }
152
};
153
154
} // end anonymous namespace
155
156
LockFileManager::LockFileManager(StringRef FileName)
157
1.84k
{
158
1.84k
  this->FileName = FileName;
159
1.84k
  if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
160
0
    std::string S("failed to obtain absolute path for ");
161
0
    S.append(this->FileName.str());
162
0
    setError(EC, S);
163
0
    return;
164
0
  }
165
1.84k
  LockFileName = this->FileName;
166
1.84k
  LockFileName += ".lock";
167
1.84k
168
1.84k
  // If the lock file already exists, don't bother to try to create our own
169
1.84k
  // lock file; it won't work anyway. Just figure out who owns this lock file.
170
1.84k
  if ((Owner = readLockFile(LockFileName)))
171
1
    return;
172
1.84k
173
1.84k
  // Create a lock file that is unique to this instance.
174
1.84k
  UniqueLockFileName = LockFileName;
175
1.84k
  UniqueLockFileName += "-%%%%%%%%";
176
1.84k
  int UniqueLockFileID;
177
1.84k
  if (std::error_code EC = sys::fs::createUniqueFile(
178
10
          UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
179
10
    std::string S("failed to create unique file ");
180
10
    S.append(UniqueLockFileName.str());
181
10
    setError(EC, S);
182
10
    return;
183
10
  }
184
1.83k
185
1.83k
  // Write our process ID to our unique lock file.
186
1.83k
  {
187
1.83k
    SmallString<256> HostID;
188
1.83k
    if (auto EC = getHostID(HostID)) {
189
0
      setError(EC, "failed to get host id");
190
0
      return;
191
0
    }
192
1.83k
193
1.83k
    raw_fd_ostream Out(UniqueLockFileID, /*shouldClose=*/true);
194
1.83k
    Out << HostID << ' ';
195
1.83k
#if LLVM_ON_UNIX
196
1.83k
    Out << getpid();
197
#else
198
    Out << "1";
199
#endif
200
    Out.close();
201
1.83k
202
1.83k
    if (Out.has_error()) {
203
0
      // We failed to write out PID, so report the error, remove the
204
0
      // unique lock file, and fail.
205
0
      std::string S("failed to write to ");
206
0
      S.append(UniqueLockFileName.str());
207
0
      setError(Out.error(), S);
208
0
      sys::fs::remove(UniqueLockFileName);
209
0
      return;
210
0
    }
211
1.83k
  }
212
1.83k
213
1.83k
  // Clean up the unique file on signal, which also releases the lock if it is
214
1.83k
  // held since the .lock symlink will point to a nonexistent file.
215
1.83k
  RemoveUniqueLockFileOnSignal RemoveUniqueFile(UniqueLockFileName);
216
1.83k
217
1.83k
  while (true) {
218
1.83k
    // Create a link from the lock file name. If this succeeds, we're done.
219
1.83k
    std::error_code EC =
220
1.83k
        sys::fs::create_link(UniqueLockFileName, LockFileName);
221
1.83k
    if (!EC) {
222
1.83k
      RemoveUniqueFile.lockAcquired();
223
1.83k
      return;
224
1.83k
    }
225
0
226
0
    if (EC != errc::file_exists) {
227
0
      std::string S("failed to create link ");
228
0
      raw_string_ostream OSS(S);
229
0
      OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
230
0
      setError(EC, OSS.str());
231
0
      return;
232
0
    }
233
0
234
0
    // Someone else managed to create the lock file first. Read the process ID
235
0
    // from the lock file.
236
0
    if ((Owner = readLockFile(LockFileName))) {
237
0
      // Wipe out our unique lock file (it's useless now)
238
0
      sys::fs::remove(UniqueLockFileName);
239
0
      return;
240
0
    }
241
0
242
0
    if (!sys::fs::exists(LockFileName)) {
243
0
      // The previous owner released the lock file before we could read it.
244
0
      // Try to get ownership again.
245
0
      continue;
246
0
    }
247
0
248
0
    // There is a lock file that nobody owns; try to clean it up and get
249
0
    // ownership.
250
0
    if ((EC = sys::fs::remove(LockFileName))) {
251
0
      std::string S("failed to remove lockfile ");
252
0
      S.append(UniqueLockFileName.str());
253
0
      setError(EC, S);
254
0
      return;
255
0
    }
256
0
  }
257
1.83k
}
258
259
3.69k
LockFileManager::LockFileState LockFileManager::getState() const {
260
3.69k
  if (Owner)
261
2
    return LFS_Shared;
262
3.69k
263
3.69k
  if (ErrorCode)
264
20
    return LFS_Error;
265
3.67k
266
3.67k
  return LFS_Owned;
267
3.67k
}
268
269
0
std::string LockFileManager::getErrorMessage() const {
270
0
  if (ErrorCode) {
271
0
    std::string Str(ErrorDiagMsg);
272
0
    std::string ErrCodeMsg = ErrorCode.message();
273
0
    raw_string_ostream OSS(Str);
274
0
    if (!ErrCodeMsg.empty())
275
0
      OSS << ": " << ErrCodeMsg;
276
0
    return OSS.str();
277
0
  }
278
0
  return "";
279
0
}
280
281
1.84k
LockFileManager::~LockFileManager() {
282
1.84k
  if (getState() != LFS_Owned)
283
11
    return;
284
1.83k
285
1.83k
  // Since we own the lock, remove the lock file and our own unique lock file.
286
1.83k
  sys::fs::remove(LockFileName);
287
1.83k
  sys::fs::remove(UniqueLockFileName);
288
1.83k
  // The unique file is now gone, so remove it from the signal handler. This
289
1.83k
  // matches a sys::RemoveFileOnSignal() in LockFileManager().
290
1.83k
  sys::DontRemoveFileOnSignal(UniqueLockFileName);
291
1.83k
}
292
293
0
LockFileManager::WaitForUnlockResult LockFileManager::waitForUnlock() {
294
0
  if (getState() != LFS_Shared)
295
0
    return Res_Success;
296
0
297
#ifdef _WIN32
298
  unsigned long Interval = 1;
299
#else
300
0
  struct timespec Interval;
301
0
  Interval.tv_sec = 0;
302
0
  Interval.tv_nsec = 1000000;
303
0
#endif
304
0
  // Don't wait more than 40s per iteration. Total timeout for the file
305
0
  // to appear is ~1.5 minutes.
306
0
  const unsigned MaxSeconds = 40;
307
0
  do {
308
0
    // Sleep for the designated interval, to allow the owning process time to
309
0
    // finish up and remove the lock file.
310
0
    // FIXME: Should we hook in to system APIs to get a notification when the
311
0
    // lock file is deleted?
312
#ifdef _WIN32
313
    Sleep(Interval);
314
#else
315
    nanosleep(&Interval, nullptr);
316
0
#endif
317
0
318
0
    if (sys::fs::access(LockFileName.c_str(), sys::fs::AccessMode::Exist) ==
319
0
        errc::no_such_file_or_directory) {
320
0
      // If the original file wasn't created, somone thought the lock was dead.
321
0
      if (!sys::fs::exists(FileName))
322
0
        return Res_OwnerDied;
323
0
      return Res_Success;
324
0
    }
325
0
326
0
    // If the process owning the lock died without cleaning up, just bail out.
327
0
    if (!processStillExecuting((*Owner).first, (*Owner).second))
328
0
      return Res_OwnerDied;
329
0
330
0
    // Exponentially increase the time we wait for the lock to be removed.
331
#ifdef _WIN32
332
    Interval *= 2;
333
#else
334
0
    Interval.tv_sec *= 2;
335
0
    Interval.tv_nsec *= 2;
336
0
    if (Interval.tv_nsec >= 1000000000) {
337
0
      ++Interval.tv_sec;
338
0
      Interval.tv_nsec -= 1000000000;
339
0
    }
340
0
#endif
341
0
  } while (
342
#ifdef _WIN32
343
           Interval < MaxSeconds * 1000
344
#else
345
0
           Interval.tv_sec < (time_t)MaxSeconds
346
0
#endif
347
0
           );
348
0
349
0
  // Give up.
350
0
  return Res_Timeout;
351
0
}
352
353
0
std::error_code LockFileManager::unsafeRemoveLockFile() {
354
0
  return sys::fs::remove(LockFileName);
355
0
}