OLD | NEW |
---|---|
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 Loading... | |
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( | 60 ScopedLockedFileHandle handle( |
scottmg
2015/04/01 00:49:14
I think the/a case that has changed here:
- X ent
Robert Sesek
2015/04/01 15:08:26
I think RecoverSettings won't call InitializeSetti
| |
65 open(file_path().value().c_str(), | 61 LoggingOpenFileForWrite(file_path(), |
66 O_CREAT | O_EXCL | O_WRONLY | O_EXLOCK, | 62 FileWriteMode::kCreateOrFail, |
67 0644))); | 63 FilePermissions::kWorldReadable), |
64 FileLocking::kExclusive); | |
68 | 65 |
69 // The file was created, so this is a new database that needs to be | 66 // The file was created, so this is a new database that needs to be |
70 // initialized with a client ID. | 67 // initialized with a client ID. |
71 if (handle.is_valid()) { | 68 if (handle.is_valid()) { |
72 bool initialized = InitializeSettings(handle.get()); | 69 bool initialized = InitializeSettings(handle.get()); |
73 if (initialized) | 70 if (initialized) |
74 INITIALIZATION_STATE_SET_VALID(initialized_); | 71 INITIALIZATION_STATE_SET_VALID(initialized_); |
75 return initialized; | 72 return initialized; |
76 } | 73 } |
77 | 74 |
(...skipping 27 matching lines...) Expand all Loading... | |
105 return false; | 102 return false; |
106 | 103 |
107 *enabled = (settings.options & Data::Options::kUploadsEnabled) != 0; | 104 *enabled = (settings.options & Data::Options::kUploadsEnabled) != 0; |
108 return true; | 105 return true; |
109 } | 106 } |
110 | 107 |
111 bool Settings::SetUploadsEnabled(bool enabled) { | 108 bool Settings::SetUploadsEnabled(bool enabled) { |
112 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 109 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
113 | 110 |
114 Data settings; | 111 Data settings; |
115 ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings); | 112 ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings); |
116 if (!handle.is_valid()) | 113 if (!handle.is_valid()) |
117 return false; | 114 return false; |
118 | 115 |
119 if (enabled) | 116 if (enabled) |
120 settings.options |= Data::Options::kUploadsEnabled; | 117 settings.options |= Data::Options::kUploadsEnabled; |
121 else | 118 else |
122 settings.options &= ~Data::Options::kUploadsEnabled; | 119 settings.options &= ~Data::Options::kUploadsEnabled; |
123 | 120 |
124 return WriteSettings(handle.get(), settings); | 121 return WriteSettings(handle.get(), settings); |
125 } | 122 } |
126 | 123 |
127 bool Settings::GetLastUploadAttemptTime(time_t* time) { | 124 bool Settings::GetLastUploadAttemptTime(time_t* time) { |
128 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 125 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
129 | 126 |
130 Data settings; | 127 Data settings; |
131 if (!OpenAndReadSettings(&settings)) | 128 if (!OpenAndReadSettings(&settings)) |
132 return false; | 129 return false; |
133 | 130 |
134 *time = InRangeCast<time_t>(settings.last_upload_attempt_time, | 131 *time = InRangeCast<time_t>(settings.last_upload_attempt_time, |
135 std::numeric_limits<time_t>::max()); | 132 std::numeric_limits<time_t>::max()); |
136 return true; | 133 return true; |
137 } | 134 } |
138 | 135 |
139 bool Settings::SetLastUploadAttemptTime(time_t time) { | 136 bool Settings::SetLastUploadAttemptTime(time_t time) { |
140 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 137 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
141 | 138 |
142 Data settings; | 139 Data settings; |
143 ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings); | 140 ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings); |
144 if (!handle.is_valid()) | 141 if (!handle.is_valid()) |
145 return false; | 142 return false; |
146 | 143 |
147 settings.last_upload_attempt_time = InRangeCast<uint64_t>(time, 0); | 144 settings.last_upload_attempt_time = InRangeCast<uint64_t>(time, 0); |
148 | 145 |
149 return WriteSettings(handle.get(), settings); | 146 return WriteSettings(handle.get(), settings); |
150 } | 147 } |
151 | 148 |
152 ScopedFileHandle Settings::OpenForReading() { | 149 Settings::ScopedLockedFileHandle::ScopedLockedFileHandle() : handle_() { |
153 ScopedFileHandle handle(HANDLE_EINTR( | 150 } |
154 open(file_path().value().c_str(), O_RDONLY | O_SHLOCK))); | 151 |
155 PLOG_IF(ERROR, !handle.is_valid()) << "open for reading"; | 152 Settings::ScopedLockedFileHandle::ScopedLockedFileHandle(FileHandle file, |
153 FileLocking locking) | |
154 : handle_(file) { | |
155 if (handle_.is_valid()) { | |
156 if (!LoggingLockFile(handle_.get(), locking)) | |
157 handle_.reset(); | |
158 } | |
159 } | |
160 | |
161 Settings::ScopedLockedFileHandle::~ScopedLockedFileHandle() { | |
162 FreeIfNecessary(); | |
163 } | |
164 | |
165 Settings::ScopedLockedFileHandle Settings::OpenForReading() { | |
166 ScopedLockedFileHandle handle(LoggingOpenFileForRead(file_path()), | |
167 FileLocking::kShared); | |
156 return handle.Pass(); | 168 return handle.Pass(); |
157 } | 169 } |
158 | 170 |
159 ScopedFileHandle Settings::OpenForReadingAndWriting() { | 171 Settings::ScopedLockedFileHandle Settings::OpenForReadingAndWriting() { |
160 ScopedFileHandle handle(HANDLE_EINTR( | 172 ScopedLockedFileHandle handle( |
161 open(file_path().value().c_str(), O_RDWR | O_EXLOCK | O_CREAT, 0644))); | 173 LoggingOpenFileForReadAndWrite(file_path(), |
162 PLOG_IF(ERROR, !handle.is_valid()) << "open for writing"; | 174 FileWriteMode::kReuseOrCreate, |
175 FilePermissions::kWorldReadable), | |
176 FileLocking::kExclusive); | |
163 return handle.Pass(); | 177 return handle.Pass(); |
164 } | 178 } |
165 | 179 |
180 void Settings::ScopedLockedFileHandle::reset() { | |
181 FreeIfNecessary(); | |
182 handle_.reset(); | |
183 } | |
184 | |
185 void Settings::ScopedLockedFileHandle::FreeIfNecessary() { | |
186 if (is_valid()) | |
187 LoggingUnlockFile(handle_.get()); | |
188 } | |
189 | |
166 bool Settings::OpenAndReadSettings(Data* out_data) { | 190 bool Settings::OpenAndReadSettings(Data* out_data) { |
167 ScopedFileHandle handle = OpenForReading(); | 191 ScopedLockedFileHandle handle = OpenForReading(); |
168 if (!handle.is_valid()) | 192 if (!handle.is_valid()) |
169 return false; | 193 return false; |
170 | 194 |
171 if (ReadSettings(handle.get(), out_data)) | 195 if (ReadSettings(handle.get(), out_data)) |
172 return true; | 196 return true; |
173 | 197 |
174 // The settings file is corrupt, so reinitialize it. | 198 // The settings file is corrupt, so reinitialize it. |
175 handle.reset(); | 199 handle.reset(); |
176 | 200 |
177 // The settings failed to be read, so re-initialize them. | 201 // The settings failed to be read, so re-initialize them. |
178 return RecoverSettings(kInvalidFileHandle, out_data); | 202 return RecoverSettings(kInvalidFileHandle, out_data); |
179 } | 203 } |
180 | 204 |
181 ScopedFileHandle Settings::OpenForWritingAndReadSettings(Data* out_data) { | 205 Settings::ScopedLockedFileHandle Settings::OpenForWritingAndReadSettings( |
182 ScopedFileHandle handle = OpenForReadingAndWriting(); | 206 Data* out_data) { |
207 ScopedLockedFileHandle handle = OpenForReadingAndWriting(); | |
183 if (!handle.is_valid()) | 208 if (!handle.is_valid()) |
184 return ScopedFileHandle(); | 209 return ScopedLockedFileHandle(); |
185 | 210 |
186 if (!ReadSettings(handle.get(), out_data)) { | 211 if (!ReadSettings(handle.get(), out_data)) { |
187 if (!RecoverSettings(handle.get(), out_data)) | 212 if (!RecoverSettings(handle.get(), out_data)) |
188 return ScopedFileHandle(); | 213 return ScopedLockedFileHandle(); |
189 } | 214 } |
190 | 215 |
191 return handle.Pass(); | 216 return handle.Pass(); |
192 } | 217 } |
193 | 218 |
194 bool Settings::ReadSettings(FileHandle handle, Data* out_data) { | 219 bool Settings::ReadSettings(FileHandle handle, Data* out_data) { |
195 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0) | 220 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0) |
196 return false; | 221 return false; |
197 | 222 |
198 if (!LoggingReadFile(handle, out_data, sizeof(*out_data))) | 223 if (!LoggingReadFile(handle, out_data, sizeof(*out_data))) |
199 return false; | 224 return false; |
200 | 225 |
201 if (out_data->magic != Data::kSettingsMagic) { | 226 if (out_data->magic != Data::kSettingsMagic) { |
202 LOG(ERROR) << "Settings magic is not " << Data::kSettingsMagic; | 227 LOG(ERROR) << "Settings magic is not " << Data::kSettingsMagic; |
203 return false; | 228 return false; |
204 } | 229 } |
205 | 230 |
206 if (out_data->version != Data::kSettingsVersion) { | 231 if (out_data->version != Data::kSettingsVersion) { |
207 LOG(ERROR) << "Settings version is not " << Data::kSettingsVersion; | 232 LOG(ERROR) << "Settings version is not " << Data::kSettingsVersion; |
208 return false; | 233 return false; |
209 } | 234 } |
210 | 235 |
211 return true; | 236 return true; |
212 } | 237 } |
213 | 238 |
214 bool Settings::WriteSettings(FileHandle handle, const Data& data) { | 239 bool Settings::WriteSettings(FileHandle handle, const Data& data) { |
215 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0) | 240 if (LoggingSeekFile(handle, 0, SEEK_SET) != 0) |
216 return false; | 241 return false; |
217 | 242 |
218 if (HANDLE_EINTR(ftruncate(handle, 0)) != 0) { | 243 if (!LoggingTruncateFile(handle)) |
219 PLOG(ERROR) << "ftruncate settings file"; | |
220 return false; | 244 return false; |
221 } | |
222 | 245 |
223 return LoggingWriteFile(handle, &data, sizeof(Data)); | 246 return LoggingWriteFile(handle, &data, sizeof(Data)); |
224 } | 247 } |
225 | 248 |
226 bool Settings::RecoverSettings(FileHandle handle, Data* out_data) { | 249 bool Settings::RecoverSettings(FileHandle handle, Data* out_data) { |
227 ScopedFileHandle scoped_handle; | 250 ScopedLockedFileHandle scoped_handle; |
228 if (handle == kInvalidFileHandle) { | 251 if (handle == kInvalidFileHandle) { |
229 scoped_handle = OpenForReadingAndWriting(); | 252 scoped_handle = OpenForReadingAndWriting(); |
230 handle = scoped_handle.get(); | 253 handle = scoped_handle.get(); |
231 | 254 |
232 // Test if the file has already been recovered now that the exclusive lock | 255 // Test if the file has already been recovered now that the exclusive lock |
233 // is held. | 256 // is held. |
234 if (ReadSettings(handle, out_data)) | 257 if (ReadSettings(handle, out_data)) |
235 return true; | 258 return true; |
236 } | 259 } |
237 | 260 |
238 LOG(INFO) << "Recovering settings file " << file_path().value(); | 261 LOG(INFO) << "Recovering settings file " << file_path().value().c_str(); |
239 | 262 |
240 if (handle == kInvalidFileHandle) { | 263 if (handle == kInvalidFileHandle) { |
241 LOG(ERROR) << "Invalid file handle"; | 264 LOG(ERROR) << "Invalid file handle"; |
242 return false; | 265 return false; |
243 } | 266 } |
244 | 267 |
245 if (!InitializeSettings(handle)) | 268 if (!InitializeSettings(handle)) |
246 return false; | 269 return false; |
247 | 270 |
248 return ReadSettings(handle, out_data); | 271 return ReadSettings(handle, out_data); |
249 } | 272 } |
250 | 273 |
251 bool Settings::InitializeSettings(FileHandle handle) { | 274 bool Settings::InitializeSettings(FileHandle handle) { |
252 uuid_t uuid; | |
253 uuid_generate(uuid); | |
254 | |
255 Data settings; | 275 Data settings; |
256 settings.client_id.InitializeFromBytes(uuid); | 276 if (!UUID::GenerateFromSystem(&settings.client_id)) |
277 return false; | |
257 | 278 |
258 return WriteSettings(handle, settings); | 279 return WriteSettings(handle, settings); |
259 } | 280 } |
260 | 281 |
261 } // namespace crashpad | 282 } // namespace crashpad |
OLD | NEW |