blob: beee501986739fcdab2221ebf16fb6ce8c89acad [file] [log] [blame]
Narayan Kamathd1c606f2014-06-09 16:50:19 +01001/*
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 Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080024
David Sehr1979c642018-04-26 14:41:18 -070025#include "unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010026
27namespace art {
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029using android::base::StringPrintf;
30
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010031/* static */ ScopedFlock LockedFile::Open(const char* filename, std::string* error_msg) {
32 return Open(filename, O_CREAT | O_RDWR, true, error_msg);
Calin Juravle877fd962016-01-05 14:29:29 +000033}
34
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010035/* static */ ScopedFlock LockedFile::Open(const char* filename, int flags, bool block,
36 std::string* error_msg) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010037 while (true) {
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010038 // NOTE: We don't check usage here because the ScopedFlock should *never* be
39 // responsible for flushing its underlying FD. Its only purpose should be
40 // to acquire a lock, and the unlock / close in the corresponding
41 // destructor. Callers should explicitly flush files they're writing to if
42 // that is the desired behaviour.
Andreas Gampe0de385f2018-10-11 11:11:13 -070043 std::unique_ptr<File> file(OS::OpenFileWithFlags(filename, flags, /* auto_flush= */ false));
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010044 if (file.get() == nullptr) {
45 *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
46 return nullptr;
Andreas Gampe4303ba92014-11-06 01:00:46 -080047 }
Calin Juravledf674c42017-04-27 19:30:16 -070048
Calin Juravle877fd962016-01-05 14:29:29 +000049 int operation = block ? LOCK_EX : (LOCK_EX | LOCK_NB);
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010050 int flock_result = TEMP_FAILURE_RETRY(flock(file->Fd(), operation));
Calin Juravle877fd962016-01-05 14:29:29 +000051 if (flock_result == EWOULDBLOCK) {
52 // File is locked by someone else and we are required not to block;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010053 return nullptr;
Calin Juravle877fd962016-01-05 14:29:29 +000054 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010055 if (flock_result != 0) {
56 *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno));
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010057 return nullptr;
Narayan Kamathd1c606f2014-06-09 16:50:19 +010058 }
59 struct stat fstat_stat;
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010060 int fstat_result = TEMP_FAILURE_RETRY(fstat(file->Fd(), &fstat_stat));
Narayan Kamathd1c606f2014-06-09 16:50:19 +010061 if (fstat_result != 0) {
62 *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno));
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010063 return nullptr;
Narayan Kamathd1c606f2014-06-09 16:50:19 +010064 }
65 struct stat stat_stat;
66 int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat));
67 if (stat_result != 0) {
68 PLOG(WARNING) << "Failed to stat, will retry: " << filename;
69 // ENOENT can happen if someone racing with us unlinks the file we created so just retry.
Calin Juravle877fd962016-01-05 14:29:29 +000070 if (block) {
71 continue;
72 } else {
73 // Note that in theory we could race with someone here for a long time and end up retrying
74 // over and over again. This potential behavior does not fit well in the non-blocking
75 // semantics. Thus, if we are not require to block return failure when racing.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010076 return nullptr;
Calin Juravle877fd962016-01-05 14:29:29 +000077 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010078 }
79 if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) {
80 LOG(WARNING) << "File changed while locking, will retry: " << filename;
Calin Juravle877fd962016-01-05 14:29:29 +000081 if (block) {
82 continue;
83 } else {
84 // See comment above.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010085 return nullptr;
Calin Juravle877fd962016-01-05 14:29:29 +000086 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010087 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010088
89 return ScopedFlock(new LockedFile(std::move((*file.get()))));
Narayan Kamathd1c606f2014-06-09 16:50:19 +010090 }
91}
92
Narayan Kamatha3d27eb2017-05-11 13:50:59 +010093ScopedFlock LockedFile::DupOf(const int fd, const std::string& path,
94 const bool read_only_mode, std::string* error_msg) {
95 // NOTE: We don't check usage here because the ScopedFlock should *never* be
96 // responsible for flushing its underlying FD. Its only purpose should be
97 // to acquire a lock, and the unlock / close in the corresponding
98 // destructor. Callers should explicitly flush files they're writing to if
99 // that is the desired behaviour.
100 ScopedFlock locked_file(
Andreas Gampe0de385f2018-10-11 11:11:13 -0700101 new LockedFile(dup(fd), path, /* check_usage= */ false, read_only_mode));
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100102 if (locked_file->Fd() == -1) {
Alex Lighta59dd802014-07-02 16:28:08 -0700103 *error_msg = StringPrintf("Failed to duplicate open file '%s': %s",
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100104 locked_file->GetPath().c_str(), strerror(errno));
105 return nullptr;
Alex Lighta59dd802014-07-02 16:28:08 -0700106 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100107 if (0 != TEMP_FAILURE_RETRY(flock(locked_file->Fd(), LOCK_EX))) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700108 *error_msg = StringPrintf(
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100109 "Failed to lock file '%s': %s", locked_file->GetPath().c_str(), strerror(errno));
110 return nullptr;
Alex Lighta59dd802014-07-02 16:28:08 -0700111 }
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100112
113 return locked_file;
Alex Lighta59dd802014-07-02 16:28:08 -0700114}
115
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100116void LockedFile::ReleaseLock() {
117 if (this->Fd() != -1) {
118 int flock_result = TEMP_FAILURE_RETRY(flock(this->Fd(), LOCK_UN));
Calin Juravlef7ada952017-03-21 20:02:07 -0700119 if (flock_result != 0) {
Alex Light9c48ee52017-05-08 10:18:47 -0700120 // Only printing a warning is okay since this is only used with either:
121 // 1) a non-blocking Init call, or
122 // 2) as a part of a seperate binary (eg dex2oat) which has it's own timeout logic to prevent
123 // deadlocks.
124 // This means we can be sure that the warning won't cause a deadlock.
Narayan Kamatha3d27eb2017-05-11 13:50:59 +0100125 PLOG(WARNING) << "Unable to unlock file " << this->GetPath();
Andreas Gampe4303ba92014-11-06 01:00:46 -0800126 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100127 }
128}
129
130} // namespace art