blob: 0e8031f4f2bff825b1af07252c7cfca7d7e23fa8 [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
22#include "base/logging.h"
23#include "base/stringprintf.h"
24#include "base/unix_file/fd_file.h"
25
26namespace art {
27
28bool ScopedFlock::Init(const char* filename, std::string* error_msg) {
Calin Juravle877fd962016-01-05 14:29:29 +000029 return Init(filename, O_CREAT | O_RDWR, true, error_msg);
30}
31
32bool ScopedFlock::Init(const char* filename, int flags, bool block, std::string* error_msg) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010033 while (true) {
Andreas Gampe4303ba92014-11-06 01:00:46 -080034 if (file_.get() != nullptr) {
35 UNUSED(file_->FlushCloseOrErase()); // Ignore result.
36 }
Calin Juravle877fd962016-01-05 14:29:29 +000037 file_.reset(OS::OpenFileWithFlags(filename, flags));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070038 if (file_.get() == nullptr) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010039 *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
40 return false;
41 }
Calin Juravle877fd962016-01-05 14:29:29 +000042 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 Kamathd1c606f2014-06-09 16:50:19 +010048 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 Juravle877fd962016-01-05 14:29:29 +000063 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 Kamathd1c606f2014-06-09 16:50:19 +010071 }
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 Juravle877fd962016-01-05 14:29:29 +000074 if (block) {
75 continue;
76 } else {
77 // See comment above.
78 return false;
79 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010080 }
81 return true;
82 }
83}
84
Alex Lighta59dd802014-07-02 16:28:08 -070085bool ScopedFlock::Init(File* file, std::string* error_msg) {
Calin Juravle2e2db782016-02-23 12:00:03 +000086 file_.reset(new File(dup(file->Fd()), file->GetPath(), file->CheckUsage(), file->ReadOnlyMode()));
Alex Lighta59dd802014-07-02 16:28:08 -070087 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 Chartier2cebb242015-04-21 16:50:40 -070095 *error_msg = StringPrintf(
96 "Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
Alex Lighta59dd802014-07-02 16:28:08 -070097 return false;
98 }
99 return true;
100}
101
Calin Juravle877fd962016-01-05 14:29:29 +0000102File* ScopedFlock::GetFile() const {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700103 CHECK(file_.get() != nullptr);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100104 return file_.get();
105}
106
Andreas Gampe833a4852014-05-21 18:46:59 -0700107bool ScopedFlock::HasFile() {
108 return file_.get() != nullptr;
109}
110
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100111ScopedFlock::ScopedFlock() { }
112
113ScopedFlock::~ScopedFlock() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700114 if (file_.get() != nullptr) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100115 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
116 CHECK_EQ(0, flock_result);
Calin Juravle2e2db782016-02-23 12:00:03 +0000117 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 Gampe4303ba92014-11-06 01:00:46 -0800124 PLOG(WARNING) << "Could not close scoped file lock file.";
125 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100126 }
127}
128
129} // namespace art