-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_lock.cc
59 lines (47 loc) · 1.21 KB
/
process_lock.cc
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
// Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "hammerd/process_lock.h"
#include <sys/file.h>
#include <utility>
#include <base/logging.h>
namespace hammerd {
ProcessLock::ProcessLock(const base::FilePath& lock_file)
: lock_file_(lock_file) {}
ProcessLock::~ProcessLock() {
Release();
}
bool ProcessLock::IsLocked() const {
return fd_.is_valid();
}
bool ProcessLock::Acquire() {
if (IsLocked()) {
return true;
}
base::ScopedFD fd(
open(lock_file_.value().c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0666));
if (!fd.is_valid()) {
PLOG(ERROR) << "Failed to open the file";
return false;
}
if (flock(fd.get(), LOCK_EX | LOCK_NB) != 0) {
PLOG(ERROR) << "Failed to lock the file";
return false;
}
LOG(INFO) << "Locked the file";
fd_ = std::move(fd);
return true;
}
bool ProcessLock::Release() {
if (!IsLocked()) {
return true;
}
if (flock(fd_.get(), LOCK_UN | LOCK_NB) != 0) {
PLOG(ERROR) << "Failed to unlock the file";
return false;
}
LOG(INFO) << "Unlocked the file";
fd_.reset();
return true;
}
} // namespace hammerd