xref: /aosp_15_r20/art/libartbase/base/scoped_flock.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "scoped_flock.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <sys/file.h>
20*795d594fSAndroid Build Coastguard Worker #include <sys/stat.h>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
23*795d594fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include "file_utils.h"
26*795d594fSAndroid Build Coastguard Worker #include "unix_file/fd_file.h"
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker namespace art {
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker using android::base::StringPrintf;  // NOLINT - StringPrintf is actually used
31*795d594fSAndroid Build Coastguard Worker 
Open(const char * filename,std::string * error_msg)32*795d594fSAndroid Build Coastguard Worker /* static */ ScopedFlock LockedFile::Open(const char* filename, std::string* error_msg) {
33*795d594fSAndroid Build Coastguard Worker   return Open(filename, O_CREAT | O_RDWR, true, error_msg);
34*795d594fSAndroid Build Coastguard Worker }
35*795d594fSAndroid Build Coastguard Worker 
Open(const char * filename,int flags,bool block,std::string * error_msg)36*795d594fSAndroid Build Coastguard Worker /* static */ ScopedFlock LockedFile::Open(const char* filename, int flags, bool block,
37*795d594fSAndroid Build Coastguard Worker                                           std::string* error_msg) {
38*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
39*795d594fSAndroid Build Coastguard Worker   // TODO: implement file locking for Windows.
40*795d594fSAndroid Build Coastguard Worker   UNUSED(filename);
41*795d594fSAndroid Build Coastguard Worker   UNUSED(flags);
42*795d594fSAndroid Build Coastguard Worker   UNUSED(block);
43*795d594fSAndroid Build Coastguard Worker   *error_msg = "flock is unsupported on Windows";
44*795d594fSAndroid Build Coastguard Worker   return nullptr;
45*795d594fSAndroid Build Coastguard Worker #else
46*795d594fSAndroid Build Coastguard Worker   while (true) {
47*795d594fSAndroid Build Coastguard Worker     // NOTE: We don't check usage here because the ScopedFlock should *never* be
48*795d594fSAndroid Build Coastguard Worker     // responsible for flushing its underlying FD. Its only purpose should be
49*795d594fSAndroid Build Coastguard Worker     // to acquire a lock, and the unlock / close in the corresponding
50*795d594fSAndroid Build Coastguard Worker     // destructor. Callers should explicitly flush files they're writing to if
51*795d594fSAndroid Build Coastguard Worker     // that is the desired behaviour.
52*795d594fSAndroid Build Coastguard Worker     std::unique_ptr<File> file(OS::OpenFileWithFlags(filename, flags, /* auto_flush= */ false));
53*795d594fSAndroid Build Coastguard Worker     if (file.get() == nullptr) {
54*795d594fSAndroid Build Coastguard Worker       *error_msg = StringPrintf("Failed to open file '%s': %s", filename, strerror(errno));
55*795d594fSAndroid Build Coastguard Worker       return nullptr;
56*795d594fSAndroid Build Coastguard Worker     }
57*795d594fSAndroid Build Coastguard Worker 
58*795d594fSAndroid Build Coastguard Worker     int operation = block ? LOCK_EX : (LOCK_EX | LOCK_NB);
59*795d594fSAndroid Build Coastguard Worker     int flock_result = TEMP_FAILURE_RETRY(flock(file->Fd(), operation));
60*795d594fSAndroid Build Coastguard Worker     if (flock_result == EWOULDBLOCK) {
61*795d594fSAndroid Build Coastguard Worker       // File is locked by someone else and we are required not to block;
62*795d594fSAndroid Build Coastguard Worker       return nullptr;
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker     if (flock_result != 0) {
65*795d594fSAndroid Build Coastguard Worker       *error_msg = StringPrintf("Failed to lock file '%s': %s", filename, strerror(errno));
66*795d594fSAndroid Build Coastguard Worker       return nullptr;
67*795d594fSAndroid Build Coastguard Worker     }
68*795d594fSAndroid Build Coastguard Worker     struct stat fstat_stat;
69*795d594fSAndroid Build Coastguard Worker     int fstat_result = TEMP_FAILURE_RETRY(fstat(file->Fd(), &fstat_stat));
70*795d594fSAndroid Build Coastguard Worker     if (fstat_result != 0) {
71*795d594fSAndroid Build Coastguard Worker       *error_msg = StringPrintf("Failed to fstat file '%s': %s", filename, strerror(errno));
72*795d594fSAndroid Build Coastguard Worker       return nullptr;
73*795d594fSAndroid Build Coastguard Worker     }
74*795d594fSAndroid Build Coastguard Worker     struct stat stat_stat;
75*795d594fSAndroid Build Coastguard Worker     int stat_result = TEMP_FAILURE_RETRY(stat(filename, &stat_stat));
76*795d594fSAndroid Build Coastguard Worker     if (stat_result != 0) {
77*795d594fSAndroid Build Coastguard Worker       PLOG(WARNING) << "Failed to stat, will retry: " << filename;
78*795d594fSAndroid Build Coastguard Worker       // ENOENT can happen if someone racing with us unlinks the file we created so just retry.
79*795d594fSAndroid Build Coastguard Worker       if (block) {
80*795d594fSAndroid Build Coastguard Worker         continue;
81*795d594fSAndroid Build Coastguard Worker       } else {
82*795d594fSAndroid Build Coastguard Worker         // Note that in theory we could race with someone here for a long time and end up retrying
83*795d594fSAndroid Build Coastguard Worker         // over and over again. This potential behavior does not fit well in the non-blocking
84*795d594fSAndroid Build Coastguard Worker         // semantics. Thus, if we are not require to block return failure when racing.
85*795d594fSAndroid Build Coastguard Worker         return nullptr;
86*795d594fSAndroid Build Coastguard Worker       }
87*795d594fSAndroid Build Coastguard Worker     }
88*795d594fSAndroid Build Coastguard Worker     if (fstat_stat.st_dev != stat_stat.st_dev || fstat_stat.st_ino != stat_stat.st_ino) {
89*795d594fSAndroid Build Coastguard Worker       LOG(WARNING) << "File changed while locking, will retry: " << filename;
90*795d594fSAndroid Build Coastguard Worker       if (block) {
91*795d594fSAndroid Build Coastguard Worker         continue;
92*795d594fSAndroid Build Coastguard Worker       } else {
93*795d594fSAndroid Build Coastguard Worker         // See comment above.
94*795d594fSAndroid Build Coastguard Worker         return nullptr;
95*795d594fSAndroid Build Coastguard Worker       }
96*795d594fSAndroid Build Coastguard Worker     }
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker     return ScopedFlock(new LockedFile(std::move((*file.get()))));
99*795d594fSAndroid Build Coastguard Worker   }
100*795d594fSAndroid Build Coastguard Worker #endif
101*795d594fSAndroid Build Coastguard Worker }
102*795d594fSAndroid Build Coastguard Worker 
DupOf(const int fd,const std::string & path,const bool read_only_mode,std::string * error_msg)103*795d594fSAndroid Build Coastguard Worker ScopedFlock LockedFile::DupOf(const int fd, const std::string& path,
104*795d594fSAndroid Build Coastguard Worker                               const bool read_only_mode, std::string* error_msg) {
105*795d594fSAndroid Build Coastguard Worker #ifdef _WIN32
106*795d594fSAndroid Build Coastguard Worker   // TODO: implement file locking for Windows.
107*795d594fSAndroid Build Coastguard Worker   UNUSED(fd);
108*795d594fSAndroid Build Coastguard Worker   UNUSED(path);
109*795d594fSAndroid Build Coastguard Worker   UNUSED(read_only_mode);
110*795d594fSAndroid Build Coastguard Worker   *error_msg = "flock is unsupported on Windows.";
111*795d594fSAndroid Build Coastguard Worker   return nullptr;
112*795d594fSAndroid Build Coastguard Worker #else
113*795d594fSAndroid Build Coastguard Worker   // NOTE: We don't check usage here because the ScopedFlock should *never* be
114*795d594fSAndroid Build Coastguard Worker   // responsible for flushing its underlying FD. Its only purpose should be
115*795d594fSAndroid Build Coastguard Worker   // to acquire a lock, and the unlock / close in the corresponding
116*795d594fSAndroid Build Coastguard Worker   // destructor. Callers should explicitly flush files they're writing to if
117*795d594fSAndroid Build Coastguard Worker   // that is the desired behaviour.
118*795d594fSAndroid Build Coastguard Worker   ScopedFlock locked_file(
119*795d594fSAndroid Build Coastguard Worker       new LockedFile(DupCloexec(fd), path, /* check_usage= */ false, read_only_mode));
120*795d594fSAndroid Build Coastguard Worker   if (locked_file->Fd() == -1) {
121*795d594fSAndroid Build Coastguard Worker     *error_msg = StringPrintf("Failed to duplicate open file '%s': %s",
122*795d594fSAndroid Build Coastguard Worker                               locked_file->GetPath().c_str(), strerror(errno));
123*795d594fSAndroid Build Coastguard Worker     return nullptr;
124*795d594fSAndroid Build Coastguard Worker   }
125*795d594fSAndroid Build Coastguard Worker   if (0 != TEMP_FAILURE_RETRY(flock(locked_file->Fd(), LOCK_EX))) {
126*795d594fSAndroid Build Coastguard Worker     *error_msg = StringPrintf(
127*795d594fSAndroid Build Coastguard Worker         "Failed to lock file '%s': %s", locked_file->GetPath().c_str(), strerror(errno));
128*795d594fSAndroid Build Coastguard Worker     return nullptr;
129*795d594fSAndroid Build Coastguard Worker   }
130*795d594fSAndroid Build Coastguard Worker 
131*795d594fSAndroid Build Coastguard Worker   return locked_file;
132*795d594fSAndroid Build Coastguard Worker #endif
133*795d594fSAndroid Build Coastguard Worker }
134*795d594fSAndroid Build Coastguard Worker 
ReleaseLock()135*795d594fSAndroid Build Coastguard Worker void LockedFile::ReleaseLock() {
136*795d594fSAndroid Build Coastguard Worker #ifndef _WIN32
137*795d594fSAndroid Build Coastguard Worker   if (this->Fd() != -1) {
138*795d594fSAndroid Build Coastguard Worker     int flock_result = TEMP_FAILURE_RETRY(flock(this->Fd(), LOCK_UN));
139*795d594fSAndroid Build Coastguard Worker     if (flock_result != 0) {
140*795d594fSAndroid Build Coastguard Worker       // Only printing a warning is okay since this is only used with either:
141*795d594fSAndroid Build Coastguard Worker       // 1) a non-blocking Init call, or
142*795d594fSAndroid Build Coastguard Worker       // 2) as a part of a seperate binary (eg dex2oat) which has it's own timeout logic to prevent
143*795d594fSAndroid Build Coastguard Worker       //    deadlocks.
144*795d594fSAndroid Build Coastguard Worker       // This means we can be sure that the warning won't cause a deadlock.
145*795d594fSAndroid Build Coastguard Worker       PLOG(WARNING) << "Unable to unlock file " << this->GetPath();
146*795d594fSAndroid Build Coastguard Worker     }
147*795d594fSAndroid Build Coastguard Worker   }
148*795d594fSAndroid Build Coastguard Worker #endif
149*795d594fSAndroid Build Coastguard Worker }
150*795d594fSAndroid Build Coastguard Worker 
151*795d594fSAndroid Build Coastguard Worker }  // namespace art
152