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 | #ifndef ART_RUNTIME_BASE_SCOPED_FLOCK_H_ |
| 18 | #define ART_RUNTIME_BASE_SCOPED_FLOCK_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 23 | #include "android-base/unique_fd.h" |
| 24 | |
| 25 | #include "base/logging.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 26 | #include "base/macros.h" |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 28 | #include "os.h" |
| 29 | |
| 30 | namespace art { |
| 31 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 32 | class LockedFile; |
| 33 | class LockedFileCloseNoFlush; |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 34 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 35 | // A scoped File object that calls Close without flushing. |
| 36 | typedef std::unique_ptr<LockedFile, LockedFileCloseNoFlush> ScopedFlock; |
| 37 | |
| 38 | class LockedFile : public unix_file::FdFile { |
| 39 | public: |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 40 | // Attempts to acquire an exclusive file lock (see flock(2)) on the file |
| 41 | // at filename, and blocks until it can do so. |
| 42 | // |
Calin Juravle | 877fd96 | 2016-01-05 14:29:29 +0000 | [diff] [blame] | 43 | // It is an error if its inode changed (usually due to a new file being |
| 44 | // created at the same path) between attempts to lock it. In blocking mode, |
| 45 | // locking will be retried if the file changed. In non-blocking mode, false |
| 46 | // is returned and no attempt is made to re-acquire the lock. |
| 47 | // |
| 48 | // The file is opened with the provided flags. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 49 | static ScopedFlock Open(const char* filename, int flags, bool block, |
| 50 | std::string* error_msg); |
| 51 | |
| 52 | // Calls Open(filename, O_CREAT | O_RDWR, true, errror_msg) |
| 53 | static ScopedFlock Open(const char* filename, std::string* error_msg); |
| 54 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 55 | // Attempt to acquire an exclusive file lock (see flock(2)) on 'file'. |
| 56 | // Returns true if the lock could be acquired or false if an error |
| 57 | // occured. |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 58 | static ScopedFlock DupOf(const int fd, const std::string& path, |
| 59 | const bool read_only_mode, std::string* error_message); |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 60 | |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 61 | // Release a lock held on this file, if any. |
| 62 | void ReleaseLock(); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 63 | |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 64 | private: |
Narayan Kamath | a3d27eb | 2017-05-11 13:50:59 +0100 | [diff] [blame] | 65 | // Constructors should not be invoked directly, use one of the factory |
| 66 | // methods instead. |
| 67 | explicit LockedFile(FdFile&& other) : FdFile(std::move(other)) { |
| 68 | } |
| 69 | |
| 70 | // Constructors should not be invoked directly, use one of the factory |
| 71 | // methods instead. |
| 72 | LockedFile(int fd, const std::string& path, bool check_usage, bool read_only_mode) |
| 73 | : FdFile(fd, path, check_usage, read_only_mode) { |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | class LockedFileCloseNoFlush { |
| 78 | public: |
| 79 | void operator()(LockedFile* ptr) { |
| 80 | ptr->ReleaseLock(); |
| 81 | UNUSED(ptr->Close()); |
| 82 | |
| 83 | delete ptr; |
| 84 | } |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | } // namespace art |
| 88 | |
| 89 | #endif // ART_RUNTIME_BASE_SCOPED_FLOCK_H_ |