blob: d4bb56b62a775cbfad2521df1e1a1f7273f70575 [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 Gampe46ee31b2016-12-14 10:11:49 -080022#include "android-base/stringprintf.h"
23
Narayan Kamathd1c606f2014-06-09 16:50:19 +010024#include "base/logging.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010025#include "base/unix_file/fd_file.h"
26
27namespace art {
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029using android::base::StringPrintf;
30
Narayan Kamathd1c606f2014-06-09 16:50:19 +010031bool ScopedFlock::Init(const char* filename, std::string* error_msg) {
Calin Juravle877fd962016-01-05 14:29:29 +000032 return Init(filename, O_CREAT | O_RDWR, true, error_msg);
33}
34
35bool ScopedFlock::Init(const char* filename, int flags, bool block, std::string* error_msg) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010036 while (true) {
Andreas Gampe4303ba92014-11-06 01:00:46 -080037 if (file_.get() != nullptr) {
38 UNUSED(file_->FlushCloseOrErase()); // Ignore result.
39 }
Calin Juravle877fd962016-01-05 14:29:29 +000040 file_.reset(OS::OpenFileWithFlags(filename, flags));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070041 if (file_.get() == nullptr) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010042 *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
43 return false;
44 }
Calin Juravle877fd962016-01-05 14:29:29 +000045 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 Kamathd1c606f2014-06-09 16:50:19 +010051 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 Juravle877fd962016-01-05 14:29:29 +000066 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 Kamathd1c606f2014-06-09 16:50:19 +010074 }
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 Juravle877fd962016-01-05 14:29:29 +000077 if (block) {
78 continue;
79 } else {
80 // See comment above.
81 return false;
82 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010083 }
84 return true;
85 }
86}
87
Alex Lighta59dd802014-07-02 16:28:08 -070088bool ScopedFlock::Init(File* file, std::string* error_msg) {
Calin Juravle024160852016-02-23 12:00:03 +000089 file_.reset(new File(dup(file->Fd()), file->GetPath(), file->CheckUsage(), file->ReadOnlyMode()));
Alex Lighta59dd802014-07-02 16:28:08 -070090 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 Chartier2cebb242015-04-21 16:50:40 -070098 *error_msg = StringPrintf(
99 "Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
Alex Lighta59dd802014-07-02 16:28:08 -0700100 return false;
101 }
102 return true;
103}
104
Calin Juravle877fd962016-01-05 14:29:29 +0000105File* ScopedFlock::GetFile() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 CHECK(file_.get() != nullptr);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100107 return file_.get();
108}
109
Andreas Gampe833a4852014-05-21 18:46:59 -0700110bool ScopedFlock::HasFile() {
111 return file_.get() != nullptr;
112}
113
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100114ScopedFlock::ScopedFlock() { }
115
116ScopedFlock::~ScopedFlock() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700117 if (file_.get() != nullptr) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100118 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
119 CHECK_EQ(0, flock_result);
Calin Juravle024160852016-02-23 12:00:03 +0000120 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 Gampe4303ba92014-11-06 01:00:46 -0800127 PLOG(WARNING) << "Could not close scoped file lock file.";
128 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100129 }
130}
131
132} // namespace art