1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "scoped_flock.h"
18
19 #include <sys/file.h>
20 #include <sys/stat.h>
21
22 #include "android-base/stringprintf.h"
23
24 #include "base/logging.h"
25 #include "base/unix_file/fd_file.h"
26
27 namespace art {
28
29 using android::base::StringPrintf;
30
Init(const char * filename,std::string * error_msg)31 bool ScopedFlock::Init(const char* filename, std::string* error_msg) {
32 return Init(filename, O_CREAT | O_RDWR, true, error_msg);
33 }
34
Init(const char * filename,int flags,bool block,std::string * error_msg)35 bool ScopedFlock::Init(const char* filename, int flags, bool block, std::string* error_msg) {
36 return Init(filename, flags, block, true, error_msg);
37 }
38
Init(const char * filename,int flags,bool block,bool flush_on_close,std::string * error_msg)39 bool ScopedFlock::Init(const char* filename,
40 int flags,
41 bool block,
42 bool flush_on_close,
43 std::string* error_msg) {
44 flush_on_close_ = flush_on_close;
45 while (true) {
46 if (file_.get() != nullptr) {
47 UNUSED(file_->FlushCloseOrErase()); // Ignore result.
48 }
49
50 bool check_usage = flush_on_close; // Check usage only if we need to flush on close.
51 file_.reset(OS::OpenFileWithFlags(filename, flags, check_usage));
52 if (file_.get() == nullptr) {
53 *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
54 return false;
55 }
56 int operation = block ? LOCK_EX : (LOCK_EX | LOCK_NB);
57 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), operation));
58 if (flock_result == EWOULDBLOCK) {
59 // File is locked by someone else and we are required not to block;
60 return false;
61 }
62 if (flock_result != 0) {
63 *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno));
64 return false;
65 }
66 struct stat fstat_stat;
67 int fstat_result = TEMP_FAILURE_RETRY(fstat(file_->Fd(), &fstat_stat));
68 if (fstat_result != 0) {
69 *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno));
70 return false;
71 }
72 struct stat stat_stat;
73 int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat));
74 if (stat_result != 0) {
75 PLOG(WARNING) << "Failed to stat, will retry: " << filename;
76 // ENOENT can happen if someone racing with us unlinks the file we created so just retry.
77 if (block) {
78 continue;
79 } else {
80 // Note that in theory we could race with someone here for a long time and end up retrying
81 // over and over again. This potential behavior does not fit well in the non-blocking
82 // semantics. Thus, if we are not require to block return failure when racing.
83 return false;
84 }
85 }
86 if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) {
87 LOG(WARNING) << "File changed while locking, will retry: " << filename;
88 if (block) {
89 continue;
90 } else {
91 // See comment above.
92 return false;
93 }
94 }
95 return true;
96 }
97 }
98
Init(File * file,std::string * error_msg)99 bool ScopedFlock::Init(File* file, std::string* error_msg) {
100 flush_on_close_ = true;
101 file_.reset(new File(dup(file->Fd()), file->GetPath(), file->CheckUsage(), file->ReadOnlyMode()));
102 if (file_->Fd() == -1) {
103 file_.reset();
104 *error_msg = StringPrintf("Failed to duplicate open file '%s': %s",
105 file->GetPath().c_str(), strerror(errno));
106 return false;
107 }
108 if (0 != TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX))) {
109 file_.reset();
110 *error_msg = StringPrintf(
111 "Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
112 return false;
113 }
114 return true;
115 }
116
GetFile() const117 File* ScopedFlock::GetFile() const {
118 CHECK(file_.get() != nullptr);
119 return file_.get();
120 }
121
HasFile()122 bool ScopedFlock::HasFile() {
123 return file_.get() != nullptr;
124 }
125
ScopedFlock()126 ScopedFlock::ScopedFlock() : flush_on_close_(true) { }
127
~ScopedFlock()128 ScopedFlock::~ScopedFlock() {
129 if (file_.get() != nullptr) {
130 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
131 if (flock_result != 0) {
132 // Only printing a warning is okay since this is only used with either:
133 // 1) a non-blocking Init call, or
134 // 2) as a part of a seperate binary (eg dex2oat) which has it's own timeout logic to prevent
135 // deadlocks.
136 // This means we can be sure that the warning won't cause a deadlock.
137 PLOG(WARNING) << "Unable to unlock file " << file_->GetPath();
138 }
139 int close_result = -1;
140 if (file_->ReadOnlyMode() || !flush_on_close_) {
141 close_result = file_->Close();
142 } else {
143 close_result = file_->FlushCloseOrErase();
144 }
145 if (close_result != 0) {
146 PLOG(WARNING) << "Could not close scoped file lock file.";
147 }
148 }
149 }
150
151 } // namespace art
152