Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 22 | #include "android-base/stringprintf.h" |
| 23 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 24 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 25 | #include "base/unix_file/fd_file.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame^] | 29 | using android::base::StringPrintf; |
| 30 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 31 | bool ScopedFlock::Init(const char* filename, std::string* error_msg) { |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 32 | return Init(filename, O_CREAT | O_RDWR, true, error_msg); |
| 33 | } |
| 34 | |
| 35 | bool ScopedFlock::Init(const char* filename, int flags, bool block, std::string* error_msg) { |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 36 | while (true) { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 37 | if (file_.get() != nullptr) { |
| 38 | UNUSED(file_->FlushCloseOrErase()); // Ignore result. |
| 39 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 40 | file_.reset(OS::OpenFileWithFlags(filename, flags)); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 41 | if (file_.get() == nullptr) { |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 42 | *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno)); |
| 43 | return false; |
| 44 | } |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 45 | int operation = block ? LOCK_EX : (LOCK_EX | LOCK_NB); |
| 46 | int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), operation)); |
| 47 | if (flock_result == EWOULDBLOCK) { |
| 48 | // File is locked by someone else and we are required not to block; |
| 49 | return false; |
| 50 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 51 | if (flock_result != 0) { |
| 52 | *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno)); |
| 53 | return false; |
| 54 | } |
| 55 | struct stat fstat_stat; |
| 56 | int fstat_result = TEMP_FAILURE_RETRY(fstat(file_->Fd(), &fstat_stat)); |
| 57 | if (fstat_result != 0) { |
| 58 | *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno)); |
| 59 | return false; |
| 60 | } |
| 61 | struct stat stat_stat; |
| 62 | int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat)); |
| 63 | if (stat_result != 0) { |
| 64 | PLOG(WARNING) << "Failed to stat, will retry: " << filename; |
| 65 | // ENOENT can happen if someone racing with us unlinks the file we created so just retry. |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 66 | if (block) { |
| 67 | continue; |
| 68 | } else { |
| 69 | // Note that in theory we could race with someone here for a long time and end up retrying |
| 70 | // over and over again. This potential behavior does not fit well in the non-blocking |
| 71 | // semantics. Thus, if we are not require to block return failure when racing. |
| 72 | return false; |
| 73 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 74 | } |
| 75 | if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) { |
| 76 | LOG(WARNING) << "File changed while locking, will retry: " << filename; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 77 | if (block) { |
| 78 | continue; |
| 79 | } else { |
| 80 | // See comment above. |
| 81 | return false; |
| 82 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | } |
| 87 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 88 | bool ScopedFlock::Init(File* file, std::string* error_msg) { |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 89 | file_.reset(new File(dup(file->Fd()), file->GetPath(), file->CheckUsage(), file->ReadOnlyMode())); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 90 | if (file_->Fd() == -1) { |
| 91 | file_.reset(); |
| 92 | *error_msg = StringPrintf("Failed to duplicate open file '%s': %s", |
| 93 | file->GetPath().c_str(), strerror(errno)); |
| 94 | return false; |
| 95 | } |
| 96 | if (0 != TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX))) { |
| 97 | file_.reset(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 98 | *error_msg = StringPrintf( |
| 99 | "Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno)); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 105 | File* ScopedFlock::GetFile() const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 106 | CHECK(file_.get() != nullptr); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 107 | return file_.get(); |
| 108 | } |
| 109 | |
Andreas Gampe | 833a485 | 2014-05-21 18:46:59 -0700 | [diff] [blame] | 110 | bool ScopedFlock::HasFile() { |
| 111 | return file_.get() != nullptr; |
| 112 | } |
| 113 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 114 | ScopedFlock::ScopedFlock() { } |
| 115 | |
| 116 | ScopedFlock::~ScopedFlock() { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 117 | if (file_.get() != nullptr) { |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 118 | int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN)); |
| 119 | CHECK_EQ(0, flock_result); |
Calin Juravle | 02416085 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 120 | int close_result = -1; |
| 121 | if (file_->ReadOnlyMode()) { |
| 122 | close_result = file_->Close(); |
| 123 | } else { |
| 124 | close_result = file_->FlushCloseOrErase(); |
| 125 | } |
| 126 | if (close_result != 0) { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 127 | PLOG(WARNING) << "Could not close scoped file lock file."; |
| 128 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | } // namespace art |