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 | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 24 | |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 25 | #include "file_utils.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 26 | #include "unix_file/fd_file.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 30 | using android::base::StringPrintf; |
| 31 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 32 | /* static */ ScopedFlock LockedFile::Open(const char* filename, std::string* error_msg) { |
| 33 | return Open(filename, O_CREAT | O_RDWR, true, error_msg); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 36 | /* static */ ScopedFlock LockedFile::Open(const char* filename, int flags, bool block, |
| 37 | std::string* error_msg) { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 38 | #ifdef _WIN32 |
| 39 | // TODO: implement file locking for Windows. |
| 40 | UNUSED(filename); |
| 41 | UNUSED(flags); |
| 42 | UNUSED(block); |
| 43 | *error_msg = "flock is unsupported on Windows"; |
| 44 | return nullptr; |
| 45 | #else |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 46 | while (true) { |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 47 | // NOTE: We don't check usage here because the ScopedFlock should *never* be |
| 48 | // responsible for flushing its underlying FD. Its only purpose should be |
| 49 | // to acquire a lock, and the unlock / close in the corresponding |
| 50 | // destructor. Callers should explicitly flush files they're writing to if |
| 51 | // that is the desired behaviour. |
Andreas Gampe | 0de385f | 2018-10-11 11:11:13 -0700 | [diff] [blame] | 52 | std::unique_ptr<File> file(OS::OpenFileWithFlags(filename, flags, /* auto_flush= */ false)); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 53 | if (file.get() == nullptr) { |
| 54 | *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno)); |
| 55 | return nullptr; |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 56 | } |
Calin Juravle | df674c4 | 2017-04-27 19:30:16 -0700 | [diff] [blame] | 57 | |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 58 | int operation = block ? LOCK_EX : (LOCK_EX | LOCK_NB); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 59 | int flock_result = TEMP_FAILURE_RETRY(flock(file->Fd(), operation)); |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 60 | if (flock_result == EWOULDBLOCK) { |
| 61 | // File is locked by someone else and we are required not to block; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 62 | return nullptr; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 63 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 64 | if (flock_result != 0) { |
| 65 | *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno)); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 66 | return nullptr; |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 67 | } |
| 68 | struct stat fstat_stat; |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 69 | int fstat_result = TEMP_FAILURE_RETRY(fstat(file->Fd(), &fstat_stat)); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 70 | if (fstat_result != 0) { |
| 71 | *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno)); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 72 | return nullptr; |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 73 | } |
| 74 | struct stat stat_stat; |
| 75 | int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat)); |
| 76 | if (stat_result != 0) { |
| 77 | PLOG(WARNING) << "Failed to stat, will retry: " << filename; |
| 78 | // 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] | 79 | if (block) { |
| 80 | continue; |
| 81 | } else { |
| 82 | // Note that in theory we could race with someone here for a long time and end up retrying |
| 83 | // over and over again. This potential behavior does not fit well in the non-blocking |
| 84 | // semantics. Thus, if we are not require to block return failure when racing. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 85 | return nullptr; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 86 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 87 | } |
| 88 | if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) { |
| 89 | LOG(WARNING) << "File changed while locking, will retry: " << filename; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 90 | if (block) { |
| 91 | continue; |
| 92 | } else { |
| 93 | // See comment above. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 94 | return nullptr; |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 95 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 96 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 97 | |
| 98 | return ScopedFlock(new LockedFile(std::move((*file.get())))); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 99 | } |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 100 | #endif |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 103 | ScopedFlock LockedFile::DupOf(const int fd, const std::string& path, |
| 104 | const bool read_only_mode, std::string* error_msg) { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 105 | #ifdef _WIN32 |
| 106 | // TODO: implement file locking for Windows. |
| 107 | UNUSED(fd); |
| 108 | UNUSED(path); |
| 109 | UNUSED(read_only_mode); |
| 110 | *error_msg = "flock is unsupported on Windows."; |
| 111 | return nullptr; |
| 112 | #else |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 113 | // NOTE: We don't check usage here because the ScopedFlock should *never* be |
| 114 | // responsible for flushing its underlying FD. Its only purpose should be |
| 115 | // to acquire a lock, and the unlock / close in the corresponding |
| 116 | // destructor. Callers should explicitly flush files they're writing to if |
| 117 | // that is the desired behaviour. |
| 118 | ScopedFlock locked_file( |
Andreas Gampe | dfcd82c | 2018-10-16 20:22:37 -0700 | [diff] [blame] | 119 | new LockedFile(DupCloexec(fd), path, /* check_usage= */ false, read_only_mode)); |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 120 | if (locked_file->Fd() == -1) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 121 | *error_msg = StringPrintf("Failed to duplicate open file '%s': %s", |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 122 | locked_file->GetPath().c_str(), strerror(errno)); |
| 123 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 124 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 125 | if (0 != TEMP_FAILURE_RETRY(flock(locked_file->Fd(), LOCK_EX))) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 126 | *error_msg = StringPrintf( |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 127 | "Failed to lock file '%s': %s", locked_file->GetPath().c_str(), strerror(errno)); |
| 128 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 129 | } |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 130 | |
| 131 | return locked_file; |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 132 | #endif |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 135 | void LockedFile::ReleaseLock() { |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 136 | #ifndef _WIN32 |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 137 | if (this->Fd() != -1) { |
| 138 | int flock_result = TEMP_FAILURE_RETRY(flock(this->Fd(), LOCK_UN)); |
Calin Juravle | f7ada95 | 2017-03-21 20:02:07 -0700 | [diff] [blame] | 139 | if (flock_result != 0) { |
Alex Light | 9c48ee5 | 2017-05-08 10:18:47 -0700 | [diff] [blame] | 140 | // Only printing a warning is okay since this is only used with either: |
| 141 | // 1) a non-blocking Init call, or |
| 142 | // 2) as a part of a seperate binary (eg dex2oat) which has it's own timeout logic to prevent |
| 143 | // deadlocks. |
| 144 | // This means we can be sure that the warning won't cause a deadlock. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 145 | PLOG(WARNING) << "Unable to unlock file " << this->GetPath(); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 146 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 147 | } |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 148 | #endif |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace art |