blob: 71e0590272e568f784ff75b80e1ff50a4d3ac3a9 [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) {
29 while (true) {
Andreas Gampe4303ba92014-11-06 01:00:46 -080030 if (file_.get() != nullptr) {
31 UNUSED(file_->FlushCloseOrErase()); // Ignore result.
32 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010033 file_.reset(OS::OpenFileWithFlags(filename, O_CREAT | O_RDWR));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070034 if (file_.get() == nullptr) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010035 *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
36 return false;
37 }
38 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX));
39 if (flock_result != 0) {
40 *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno));
41 return false;
42 }
43 struct stat fstat_stat;
44 int fstat_result = TEMP_FAILURE_RETRY(fstat(file_->Fd(), &fstat_stat));
45 if (fstat_result != 0) {
46 *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno));
47 return false;
48 }
49 struct stat stat_stat;
50 int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat));
51 if (stat_result != 0) {
52 PLOG(WARNING) << "Failed to stat, will retry: " << filename;
53 // ENOENT can happen if someone racing with us unlinks the file we created so just retry.
54 continue;
55 }
56 if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) {
57 LOG(WARNING) << "File changed while locking, will retry: " << filename;
58 continue;
59 }
60 return true;
61 }
62}
63
Alex Lighta59dd802014-07-02 16:28:08 -070064bool ScopedFlock::Init(File* file, std::string* error_msg) {
Andreas Gampe4303ba92014-11-06 01:00:46 -080065 file_.reset(new File(dup(file->Fd()), true));
Alex Lighta59dd802014-07-02 16:28:08 -070066 if (file_->Fd() == -1) {
67 file_.reset();
68 *error_msg = StringPrintf("Failed to duplicate open file '%s': %s",
69 file->GetPath().c_str(), strerror(errno));
70 return false;
71 }
72 if (0 != TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_EX))) {
73 file_.reset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070074 *error_msg = StringPrintf(
75 "Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
Alex Lighta59dd802014-07-02 16:28:08 -070076 return false;
77 }
78 return true;
79}
80
Narayan Kamathd1c606f2014-06-09 16:50:19 +010081File* ScopedFlock::GetFile() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 CHECK(file_.get() != nullptr);
Narayan Kamathd1c606f2014-06-09 16:50:19 +010083 return file_.get();
84}
85
Andreas Gampe833a4852014-05-21 18:46:59 -070086bool ScopedFlock::HasFile() {
87 return file_.get() != nullptr;
88}
89
Narayan Kamathd1c606f2014-06-09 16:50:19 +010090ScopedFlock::ScopedFlock() { }
91
92ScopedFlock::~ScopedFlock() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070093 if (file_.get() != nullptr) {
Narayan Kamathd1c606f2014-06-09 16:50:19 +010094 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
95 CHECK_EQ(0, flock_result);
Andreas Gampe4303ba92014-11-06 01:00:46 -080096 if (file_->FlushCloseOrErase() != 0) {
97 PLOG(WARNING) << "Could not close scoped file lock file.";
98 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010099 }
100}
101
102} // namespace art