Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: client/settings.cc

Issue 999953002: Use LockFile/UnlockFile for Settings to port to Windows (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@lock-fileio-2
Patch Set: init Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Crashpad Authors. All rights reserved. 1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "client/settings.h" 15 #include "client/settings.h"
16 16
17 #include <limits> 17 #include <limits>
18 18
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <uuid/uuid.h>
22
23 #include "base/compiler_specific.h" 19 #include "base/compiler_specific.h"
24 #include "base/logging.h" 20 #include "base/logging.h"
25 #include "base/posix/eintr_wrapper.h" 21 #include "base/posix/eintr_wrapper.h"
26 #include "util/numeric/in_range_cast.h" 22 #include "util/numeric/in_range_cast.h"
27 23
28 namespace crashpad { 24 namespace crashpad {
29 25
30 struct ALIGNAS(4) Settings::Data { 26 struct ALIGNAS(4) Settings::Data {
31 static const uint32_t kSettingsMagic = 'CPds'; 27 static const uint32_t kSettingsMagic = 'CPds';
32 static const uint32_t kSettingsVersion = 1; 28 static const uint32_t kSettingsVersion = 1;
(...skipping 21 matching lines...) Expand all
54 : file_path_(file_path), 50 : file_path_(file_path),
55 initialized_() { 51 initialized_() {
56 } 52 }
57 53
58 Settings::~Settings() { 54 Settings::~Settings() {
59 } 55 }
60 56
61 bool Settings::Initialize() { 57 bool Settings::Initialize() {
62 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); 58 INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
63 59
64 ScopedFileHandle handle(HANDLE_EINTR(
65 open(file_path().value().c_str(),
66 O_CREAT | O_EXCL | O_WRONLY | O_EXLOCK,
67 0644)));
68
69 // The file was created, so this is a new database that needs to be
70 // initialized with a client ID.
71 if (handle.is_valid()) {
72 bool initialized = InitializeSettings(handle.get());
73 if (initialized)
74 INITIALIZATION_STATE_SET_VALID(initialized_);
75 return initialized;
76 }
77
78 // The file wasn't created, try opening it for a write operation. If the file
79 // needs to be recovered, writing is necessary. This also ensures that the
80 // process has permission to write the file.
81 Data settings; 60 Data settings;
82 if (!OpenForWritingAndReadSettings(&settings).is_valid()) 61 if (!OpenForWritingAndReadSettings(&settings).is_valid())
83 return false; 62 return false;
84 63
85 INITIALIZATION_STATE_SET_VALID(initialized_); 64 INITIALIZATION_STATE_SET_VALID(initialized_);
86 return true; 65 return true;
87 } 66 }
88 67
89 bool Settings::GetClientID(UUID* client_id) { 68 bool Settings::GetClientID(UUID* client_id) {
90 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 69 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
(...skipping 14 matching lines...) Expand all
105 return false; 84 return false;
106 85
107 *enabled = (settings.options & Data::Options::kUploadsEnabled) != 0; 86 *enabled = (settings.options & Data::Options::kUploadsEnabled) != 0;
108 return true; 87 return true;
109 } 88 }
110 89
111 bool Settings::SetUploadsEnabled(bool enabled) { 90 bool Settings::SetUploadsEnabled(bool enabled) {
112 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 91 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
113 92
114 Data settings; 93 Data settings;
115 ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings); 94 ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings);
116 if (!handle.is_valid()) 95 if (!handle.is_valid())
117 return false; 96 return false;
118 97
119 if (enabled) 98 if (enabled)
120 settings.options |= Data::Options::kUploadsEnabled; 99 settings.options |= Data::Options::kUploadsEnabled;
121 else 100 else
122 settings.options &= ~Data::Options::kUploadsEnabled; 101 settings.options &= ~Data::Options::kUploadsEnabled;
123 102
124 return WriteSettings(handle.get(), settings); 103 return WriteSettings(handle.get(), settings);
125 } 104 }
126 105
127 bool Settings::GetLastUploadAttemptTime(time_t* time) { 106 bool Settings::GetLastUploadAttemptTime(time_t* time) {
128 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 107 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
129 108
130 Data settings; 109 Data settings;
131 if (!OpenAndReadSettings(&settings)) 110 if (!OpenAndReadSettings(&settings))
132 return false; 111 return false;
133 112
134 *time = InRangeCast<time_t>(settings.last_upload_attempt_time, 113 *time = InRangeCast<time_t>(settings.last_upload_attempt_time,
135 std::numeric_limits<time_t>::max()); 114 std::numeric_limits<time_t>::max());
136 return true; 115 return true;
137 } 116 }
138 117
139 bool Settings::SetLastUploadAttemptTime(time_t time) { 118 bool Settings::SetLastUploadAttemptTime(time_t time) {
140 INITIALIZATION_STATE_DCHECK_VALID(initialized_); 119 INITIALIZATION_STATE_DCHECK_VALID(initialized_);
141 120
142 Data settings; 121 Data settings;
143 ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings); 122 ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings);
144 if (!handle.is_valid()) 123 if (!handle.is_valid())
145 return false; 124 return false;
146 125
147 settings.last_upload_attempt_time = InRangeCast<uint64_t>(time, 0); 126 settings.last_upload_attempt_time = InRangeCast<uint64_t>(time, 0);
148 127
149 return WriteSettings(handle.get(), settings); 128 return WriteSettings(handle.get(), settings);
150 } 129 }
151 130
152 ScopedFileHandle Settings::OpenForReading() { 131 Settings::ScopedLockedFileHandle::ScopedLockedFileHandle() : handle_() {
153 ScopedFileHandle handle(HANDLE_EINTR( 132 }
154 open(file_path().value().c_str(), O_RDONLY | O_SHLOCK))); 133
155 PLOG_IF(ERROR, !handle.is_valid()) << "open for reading"; 134 Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(FileHandle file,
135 FileLocking locking)
136 : handle_(file) {
137 if (handle_.is_valid()) {
138 if (!LoggingLockFile(handle_.get(), locking))
139 handle_.reset();
140 }
141 }
142
143 Settings::ScopedLockedFileHandle::~ScopedLockedFileHandle() {
144 FreeIfNecessary();
145 }
146
147 Settings::ScopedLockedFileHandle Settings::OpenForReading() {
148 ScopedLockedFileHandle handle(LoggingOpenFileForRead(file_path()),
149 FileLocking::kShared);
156 return handle.Pass(); 150 return handle.Pass();
157 } 151 }
158 152
159 ScopedFileHandle Settings::OpenForReadingAndWriting() { 153 Settings::ScopedLockedFileHandle Settings::OpenForReadingAndWriting() {
160 ScopedFileHandle handle(HANDLE_EINTR( 154 ScopedLockedFileHandle handle(
161 open(file_path().value().c_str(), O_RDWR | O_EXLOCK | O_CREAT, 0644))); 155 LoggingOpenFileForReadAndWrite(file_path(),
162 PLOG_IF(ERROR, !handle.is_valid()) << "open for writing"; 156 FileWriteMode::kReuseOrCreate,
157 FilePermissions::kWorldReadable),
158 FileLocking::kExclusive);
163 return handle.Pass(); 159 return handle.Pass();
164 } 160 }
165 161
162 void Settings::ScopedLockedFileHandle::reset() {
Robert Sesek 2015/04/01 15:08:26 nit: Implementation order should match header orde
scottmg 2015/04/01 19:32:10 Removed.
163 FreeIfNecessary();
164 handle_.reset();
165 }
166
167 void Settings::ScopedLockedFileHandle::FreeIfNecessary() {
168 if (is_valid())
169 LoggingUnlockFile(handle_.get());
170 }
171
166 bool Settings::OpenAndReadSettings(Data* out_data) { 172 bool Settings::OpenAndReadSettings(Data* out_data) {
167 ScopedFileHandle handle = OpenForReading(); 173 ScopedLockedFileHandle handle = OpenForReading();
168 if (!handle.is_valid()) 174 if (!handle.is_valid())
169 return false; 175 return false;
170 176
171 if (ReadSettings(handle.get(), out_data)) 177 if (ReadSettings(handle.get(), out_data))
172 return true; 178 return true;
173 179
174 // The settings file is corrupt, so reinitialize it. 180 // The settings file is corrupt, so reinitialize it.
175 handle.reset(); 181 handle.reset();
176 182
177 // The settings failed to be read, so re-initialize them. 183 // The settings failed to be read, so re-initialize them.
178 return RecoverSettings(kInvalidFileHandle, out_data); 184 return RecoverSettings(kInvalidFileHandle, out_data);
179 } 185 }
180 186
181 ScopedFileHandle Settings::OpenForWritingAndReadSettings(Data* out_data) { 187 Settings::ScopedLockedFileHandle Settings::OpenForWritingAndReadSettings(
182 ScopedFileHandle handle = OpenForReadingAndWriting(); 188 Data* out_data) {
189 ScopedLockedFileHandle handle = OpenForReadingAndWriting();
183 if (!handle.is_valid()) 190 if (!handle.is_valid())
184 return ScopedFileHandle(); 191 return ScopedLockedFileHandle();
185 192
186 if (!ReadSettings(handle.get(), out_data)) { 193 if (!ReadSettings(handle.get(), out_data)) {
187 if (!RecoverSettings(handle.get(), out_data)) 194 if (!RecoverSettings(handle.get(), out_data))
188 return ScopedFileHandle(); 195 return ScopedLockedFileHandle();
189 } 196 }
190 197
191 return handle.Pass(); 198 return handle.Pass();
192 } 199 }
193 200
194 bool Settings::ReadSettings(FileHandle handle, Data* out_data) { 201 bool Settings::ReadSettings(FileHandle handle, Data* out_data) {
195 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0) 202 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0)
196 return false; 203 return false;
197 204
198 if (!LoggingReadFile(handle, out_data, sizeof(*out_data))) 205 if (!LoggingReadFile(handle, out_data, sizeof(*out_data)))
199 return false; 206 return false;
200 207
201 if (out_data->magic != Data::kSettingsMagic) { 208 if (out_data->magic != Data::kSettingsMagic) {
202 LOG(ERROR) << "Settings magic is not " << Data::kSettingsMagic; 209 LOG(ERROR) << "Settings magic is not " << Data::kSettingsMagic;
203 return false; 210 return false;
204 } 211 }
205 212
206 if (out_data->version != Data::kSettingsVersion) { 213 if (out_data->version != Data::kSettingsVersion) {
207 LOG(ERROR) << "Settings version is not " << Data::kSettingsVersion; 214 LOG(ERROR) << "Settings version is not " << Data::kSettingsVersion;
208 return false; 215 return false;
209 } 216 }
210 217
211 return true; 218 return true;
212 } 219 }
213 220
214 bool Settings::WriteSettings(FileHandle handle, const Data& data) { 221 bool Settings::WriteSettings(FileHandle handle, const Data& data) {
215 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0) 222 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0)
216 return false; 223 return false;
217 224
218 if (HANDLE_EINTR(ftruncate(handle, 0)) != 0) { 225 if (!LoggingTruncateFile(handle))
219 PLOG(ERROR) << "ftruncate settings file";
220 return false; 226 return false;
221 }
222 227
223 return LoggingWriteFile(handle, &data, sizeof(Data)); 228 return LoggingWriteFile(handle, &data, sizeof(Data));
224 } 229 }
225 230
226 bool Settings::RecoverSettings(FileHandle handle, Data* out_data) { 231 bool Settings::RecoverSettings(FileHandle handle, Data* out_data) {
227 ScopedFileHandle scoped_handle; 232 ScopedLockedFileHandle scoped_handle;
228 if (handle == kInvalidFileHandle) { 233 if (handle == kInvalidFileHandle) {
229 scoped_handle = OpenForReadingAndWriting(); 234 scoped_handle = OpenForReadingAndWriting();
230 handle = scoped_handle.get(); 235 handle = scoped_handle.get();
231 236
232 // Test if the file has already been recovered now that the exclusive lock 237 // Test if the file has already been recovered now that the exclusive lock
233 // is held. 238 // is held.
234 if (ReadSettings(handle, out_data)) 239 if (ReadSettings(handle, out_data))
235 return true; 240 return true;
236 } 241 }
237 242
238 LOG(INFO) << "Recovering settings file " << file_path().value(); 243 LOG(INFO) << "Recovering settings file " << file_path().value().c_str();
239 244
240 if (handle == kInvalidFileHandle) { 245 if (handle == kInvalidFileHandle) {
241 LOG(ERROR) << "Invalid file handle"; 246 LOG(ERROR) << "Invalid file handle";
242 return false; 247 return false;
243 } 248 }
244 249
245 if (!InitializeSettings(handle)) 250 if (!InitializeSettings(handle))
246 return false; 251 return false;
247 252
248 return ReadSettings(handle, out_data); 253 return ReadSettings(handle, out_data);
249 } 254 }
250 255
251 bool Settings::InitializeSettings(FileHandle handle) { 256 bool Settings::InitializeSettings(FileHandle handle) {
252 uuid_t uuid;
253 uuid_generate(uuid);
254
255 Data settings; 257 Data settings;
256 settings.client_id.InitializeFromBytes(uuid); 258 if (!UUID::GenerateFromSystem(&settings.client_id))
259 return false;
257 260
258 return WriteSettings(handle, settings); 261 return WriteSettings(handle, settings);
259 } 262 }
260 263
261 } // namespace crashpad 264 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698