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