1 /*
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 
26 namespace art {
27 
Init(const char * filename,std::string * error_msg)28 bool ScopedFlock::Init(const char* filename, std::string* error_msg) {
29   while (true) {
30     if (file_.get() != nullptr) {
31       UNUSED(file_->FlushCloseOrErase());  // Ignore result.
32     }
33     file_.reset(OS::OpenFileWithFlags(filename, O_CREAT | O_RDWR));
34     if (file_.get() == nullptr) {
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 
Init(File * file,std::string * error_msg)64 bool ScopedFlock::Init(File* file, std::string* error_msg) {
65   file_.reset(new File(dup(file->Fd()), true));
66   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(
75         "Failed to lock file '%s': %s", file->GetPath().c_str(), strerror(errno));
76     return false;
77   }
78   return true;
79 }
80 
GetFile()81 File* ScopedFlock::GetFile() {
82   CHECK(file_.get() != nullptr);
83   return file_.get();
84 }
85 
HasFile()86 bool ScopedFlock::HasFile() {
87   return file_.get() != nullptr;
88 }
89 
ScopedFlock()90 ScopedFlock::ScopedFlock() { }
91 
~ScopedFlock()92 ScopedFlock::~ScopedFlock() {
93   if (file_.get() != nullptr) {
94     int flock_result = TEMP_FAILURE_RETRY(flock(file_->Fd(), LOCK_UN));
95     CHECK_EQ(0, flock_result);
96     if (file_->FlushCloseOrErase() != 0) {
97       PLOG(WARNING) << "Could not close scoped file lock file.";
98     }
99   }
100 }
101 
102 }  // namespace art
103