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 |
26 namespace internal { | |
27 | |
28 // static | |
29 FileHandle ScopedLockedFileHandleTraits::InvalidValue() { | |
30 return kInvalidFileHandle; | |
31 } | |
32 | |
33 // static | |
34 void ScopedLockedFileHandleTraits::Free(FileHandle handle) { | |
35 if (handle != kInvalidFileHandle) | |
36 LoggingUnlockFile(handle); | |
37 CheckedCloseFile(handle); | |
Robert Sesek
2015/04/17 21:33:36
Shouldn't this be protected with the condition too
scottmg
2015/04/20 18:20:41
Done.
| |
38 } | |
39 | |
40 } // namespace internal | |
41 | |
30 struct ALIGNAS(4) Settings::Data { | 42 struct ALIGNAS(4) Settings::Data { |
31 static const uint32_t kSettingsMagic = 'CPds'; | 43 static const uint32_t kSettingsMagic = 'CPds'; |
32 static const uint32_t kSettingsVersion = 1; | 44 static const uint32_t kSettingsVersion = 1; |
33 | 45 |
34 enum Options : uint32_t { | 46 enum Options : uint32_t { |
35 kUploadsEnabled = 1 << 0, | 47 kUploadsEnabled = 1 << 0, |
36 }; | 48 }; |
37 | 49 |
38 Data() : magic(kSettingsMagic), | 50 Data() : magic(kSettingsMagic), |
39 version(kSettingsVersion), | 51 version(kSettingsVersion), |
(...skipping 14 matching lines...) Expand all Loading... | |
54 : file_path_(file_path), | 66 : file_path_(file_path), |
55 initialized_() { | 67 initialized_() { |
56 } | 68 } |
57 | 69 |
58 Settings::~Settings() { | 70 Settings::~Settings() { |
59 } | 71 } |
60 | 72 |
61 bool Settings::Initialize() { | 73 bool Settings::Initialize() { |
62 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); | 74 INITIALIZATION_STATE_SET_INITIALIZING(initialized_); |
63 | 75 |
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; | 76 Data settings; |
82 if (!OpenForWritingAndReadSettings(&settings).is_valid()) | 77 if (!OpenForWritingAndReadSettings(&settings).is_valid()) |
83 return false; | 78 return false; |
84 | 79 |
85 INITIALIZATION_STATE_SET_VALID(initialized_); | 80 INITIALIZATION_STATE_SET_VALID(initialized_); |
86 return true; | 81 return true; |
87 } | 82 } |
88 | 83 |
89 bool Settings::GetClientID(UUID* client_id) { | 84 bool Settings::GetClientID(UUID* client_id) { |
90 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 85 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
(...skipping 14 matching lines...) Expand all Loading... | |
105 return false; | 100 return false; |
106 | 101 |
107 *enabled = (settings.options & Data::Options::kUploadsEnabled) != 0; | 102 *enabled = (settings.options & Data::Options::kUploadsEnabled) != 0; |
108 return true; | 103 return true; |
109 } | 104 } |
110 | 105 |
111 bool Settings::SetUploadsEnabled(bool enabled) { | 106 bool Settings::SetUploadsEnabled(bool enabled) { |
112 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 107 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
113 | 108 |
114 Data settings; | 109 Data settings; |
115 ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings); | 110 ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings); |
116 if (!handle.is_valid()) | 111 if (!handle.is_valid()) |
117 return false; | 112 return false; |
118 | 113 |
119 if (enabled) | 114 if (enabled) |
120 settings.options |= Data::Options::kUploadsEnabled; | 115 settings.options |= Data::Options::kUploadsEnabled; |
121 else | 116 else |
122 settings.options &= ~Data::Options::kUploadsEnabled; | 117 settings.options &= ~Data::Options::kUploadsEnabled; |
123 | 118 |
124 return WriteSettings(handle.get(), settings); | 119 return WriteSettings(handle.get(), settings); |
125 } | 120 } |
126 | 121 |
127 bool Settings::GetLastUploadAttemptTime(time_t* time) { | 122 bool Settings::GetLastUploadAttemptTime(time_t* time) { |
128 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 123 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
129 | 124 |
130 Data settings; | 125 Data settings; |
131 if (!OpenAndReadSettings(&settings)) | 126 if (!OpenAndReadSettings(&settings)) |
132 return false; | 127 return false; |
133 | 128 |
134 *time = InRangeCast<time_t>(settings.last_upload_attempt_time, | 129 *time = InRangeCast<time_t>(settings.last_upload_attempt_time, |
135 std::numeric_limits<time_t>::max()); | 130 std::numeric_limits<time_t>::max()); |
136 return true; | 131 return true; |
137 } | 132 } |
138 | 133 |
139 bool Settings::SetLastUploadAttemptTime(time_t time) { | 134 bool Settings::SetLastUploadAttemptTime(time_t time) { |
140 INITIALIZATION_STATE_DCHECK_VALID(initialized_); | 135 INITIALIZATION_STATE_DCHECK_VALID(initialized_); |
141 | 136 |
142 Data settings; | 137 Data settings; |
143 ScopedFileHandle handle = OpenForWritingAndReadSettings(&settings); | 138 ScopedLockedFileHandle handle = OpenForWritingAndReadSettings(&settings); |
144 if (!handle.is_valid()) | 139 if (!handle.is_valid()) |
145 return false; | 140 return false; |
146 | 141 |
147 settings.last_upload_attempt_time = InRangeCast<uint64_t>(time, 0); | 142 settings.last_upload_attempt_time = InRangeCast<uint64_t>(time, 0); |
148 | 143 |
149 return WriteSettings(handle.get(), settings); | 144 return WriteSettings(handle.get(), settings); |
150 } | 145 } |
151 | 146 |
152 ScopedFileHandle Settings::OpenForReading() { | 147 // static |
153 ScopedFileHandle handle(HANDLE_EINTR( | 148 Settings::ScopedLockedFileHandle Settings::MakeScopedLockedFileHandle( |
154 open(file_path().value().c_str(), O_RDONLY | O_SHLOCK))); | 149 FileHandle file, |
155 PLOG_IF(ERROR, !handle.is_valid()) << "open for reading"; | 150 FileLocking locking) { |
156 return handle.Pass(); | 151 ScopedFileHandle scoped(file); |
152 if (scoped.is_valid()) { | |
153 if (!LoggingLockFile(scoped.get(), locking)) | |
154 scoped.reset(); | |
155 } | |
156 return ScopedLockedFileHandle(scoped.release()); | |
157 } | 157 } |
158 | 158 |
159 ScopedFileHandle Settings::OpenForReadingAndWriting() { | 159 Settings::ScopedLockedFileHandle Settings::OpenForReading() { |
160 ScopedFileHandle handle(HANDLE_EINTR( | 160 return MakeScopedLockedFileHandle(LoggingOpenFileForRead(file_path()), |
161 open(file_path().value().c_str(), O_RDWR | O_EXLOCK | O_CREAT, 0644))); | 161 FileLocking::kShared); |
162 PLOG_IF(ERROR, !handle.is_valid()) << "open for writing"; | 162 } |
163 return handle.Pass(); | 163 |
164 Settings::ScopedLockedFileHandle Settings::OpenForReadingAndWriting() { | |
165 return MakeScopedLockedFileHandle( | |
166 LoggingOpenFileForReadAndWrite(file_path(), | |
167 FileWriteMode::kReuseOrCreate, | |
168 FilePermissions::kWorldReadable), | |
169 FileLocking::kExclusive); | |
164 } | 170 } |
165 | 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(); | |
239 | |
240 if (handle == kInvalidFileHandle) { | 243 if (handle == kInvalidFileHandle) { |
241 LOG(ERROR) << "Invalid file handle"; | 244 LOG(ERROR) << "Invalid file handle"; |
242 return false; | 245 return false; |
243 } | 246 } |
244 | 247 |
248 LOG(INFO) << "Initializing settings file " << file_path().value().c_str(); | |
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 (!settings.client_id.InitializeWithNew()) |
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 |
OLD | NEW |