blob: 0e93eee6271c46b4ef6c35f593a57097e40f0041 [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));
34 if (file_.get() == NULL) {
35 *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();
74 *error_msg = StringPrintf("Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
75 return false;
76 }
77 return true;
78}
79
Narayan Kamathd1c606f2014-06-09 16:50:19 +010080File* ScopedFlock::GetFile() {
81 CHECK(file_.get() != NULL);
82 return file_.get();
83}
84
Andreas Gampe833a4852014-05-21 18:46:59 -070085bool ScopedFlock::HasFile() {
86 return file_.get() != nullptr;
87}
88
Narayan Kamathd1c606f2014-06-09 16:50:19 +010089ScopedFlock::ScopedFlock() { }
90
91ScopedFlock::~ScopedFlock() {
92 if (file_.get() != NULL) {
93 int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
94 CHECK_EQ(0, flock_result);
Andreas Gampe4303ba92014-11-06 01:00:46 -080095 if (file_->FlushCloseOrErase() != 0) {
96 PLOG(WARNING) << "Could not close scoped file lock file.";
97 }
Narayan Kamathd1c606f2014-06-09 16:50:19 +010098 }
99}
100
101} // namespace art