OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/crash/linux/synchronized_minidump_manager.h" | 5 #include "chromecast/crash/linux/synchronized_minidump_manager.h" |
6 | 6 |
7 #include <dirent.h> | |
8 #include <errno.h> | |
9 #include <fcntl.h> | |
10 #include <stddef.h> | |
11 #include <stdint.h> | 7 #include <stdint.h> |
12 #include <string.h> | 8 #include <string.h> |
13 #include <sys/file.h> | |
14 #include <sys/stat.h> | |
15 #include <sys/types.h> | |
16 #include <time.h> | |
17 #include <unistd.h> | |
18 | 9 |
19 #include <utility> | 10 #include <utility> |
20 | 11 |
21 #include "base/files/dir_reader_posix.h" | 12 #include "base/files/dir_reader_posix.h" |
22 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
23 #include "base/logging.h" | 14 #include "base/logging.h" |
24 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/strings/string_number_conversions.h" |
25 #include "base/strings/string_split.h" | 17 #include "base/strings/string_split.h" |
26 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "chromecast/base/file_utils.h" |
27 #include "chromecast/base/path_utils.h" | 20 #include "chromecast/base/path_utils.h" |
28 #include "chromecast/base/serializers.h" | 21 #include "chromecast/base/serializers.h" |
29 #include "chromecast/crash/linux/dump_info.h" | 22 #include "chromecast/crash/linux/dump_info.h" |
30 | 23 |
31 // if |cond| is false, returns |retval|. | 24 // if |cond| is false, returns |retval|. |
32 #define RCHECK(cond, retval) \ | 25 #define RCHECK(cond, retval) \ |
33 do { \ | 26 do { \ |
34 if (!(cond)) { \ | 27 if (!(cond)) { \ |
35 return (retval); \ | 28 return (retval); \ |
36 } \ | 29 } \ |
37 } while (0) | 30 } while (0) |
38 | 31 |
39 namespace chromecast { | 32 namespace chromecast { |
40 | 33 |
41 namespace { | 34 namespace { |
42 | 35 |
43 const mode_t kDirMode = 0770; | |
44 const mode_t kFileMode = 0660; | |
45 const char kLockfileName[] = "lockfile"; | 36 const char kLockfileName[] = "lockfile"; |
46 const char kMetadataName[] = "metadata"; | 37 const char kMetadataName[] = "metadata"; |
47 const char kMinidumpsDir[] = "minidumps"; | 38 const char kMinidumpsDir[] = "minidumps"; |
48 | 39 |
49 const char kLockfileRatelimitKey[] = "ratelimit"; | 40 const char kLockfileRatelimitKey[] = "ratelimit"; |
50 const char kLockfileRatelimitPeriodStartKey[] = "period_start"; | 41 const char kLockfileRatelimitPeriodStartKey[] = "period_start"; |
51 const char kLockfileRatelimitPeriodDumpsKey[] = "period_dumps"; | 42 const char kLockfileRatelimitPeriodDumpsKey[] = "period_dumps"; |
52 const std::size_t kLockfileNumRatelimitParams = 2; | 43 const uint64_t kLockfileNumRatelimitParams = 2; |
53 | 44 |
54 // Gets the ratelimit parameter dictionary given a deserialized |metadata|. | 45 // Gets the ratelimit parameter dictionary given a deserialized |metadata|. |
55 // Returns nullptr if invalid. | 46 // Returns nullptr if invalid. |
56 base::DictionaryValue* GetRatelimitParams(base::Value* metadata) { | 47 base::DictionaryValue* GetRatelimitParams(base::Value* metadata) { |
57 base::DictionaryValue* dict; | 48 base::DictionaryValue* dict; |
58 base::DictionaryValue* ratelimit_params; | 49 base::DictionaryValue* ratelimit_params; |
59 if (!metadata || !metadata->GetAsDictionary(&dict) || | 50 if (!metadata || !metadata->GetAsDictionary(&dict) || |
60 !dict->GetDictionary(kLockfileRatelimitKey, &ratelimit_params)) { | 51 !dict->GetDictionary(kLockfileRatelimitKey, &ratelimit_params)) { |
61 return nullptr; | 52 return nullptr; |
62 } | 53 } |
63 | 54 |
64 return ratelimit_params; | 55 return ratelimit_params; |
65 } | 56 } |
66 | 57 |
67 // Returns the time of the current ratelimit period's start in |metadata|. | 58 // Returns the time of the current ratelimit period's start in |metadata|. |
68 // Returns (time_t)-1 if an error occurs. | 59 // Returns base::Time() if an error occurs. |
69 time_t GetRatelimitPeriodStart(base::Value* metadata) { | 60 base::Time GetRatelimitPeriodStart(base::Value* metadata) { |
70 time_t result = static_cast<time_t>(-1); | |
71 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); | 61 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); |
72 RCHECK(ratelimit_params, result); | 62 RCHECK(ratelimit_params, base::Time()); |
73 | 63 |
74 std::string period_start_str; | 64 double seconds = 0.0; |
75 RCHECK(ratelimit_params->GetString(kLockfileRatelimitPeriodStartKey, | 65 RCHECK( |
76 &period_start_str), | 66 ratelimit_params->GetDouble(kLockfileRatelimitPeriodStartKey, &seconds), |
77 result); | 67 base::Time()); |
78 | 68 |
79 errno = 0; | 69 // Return value of 0 indicates "not initialized", so we need to explicitly |
80 result = static_cast<time_t>(strtoll(period_start_str.c_str(), nullptr, 0)); | 70 // check for it and return time_t = 0 equivalent. |
81 if (errno != 0) { | 71 return seconds ? base::Time::FromDoubleT(seconds) : base::Time::UnixEpoch(); |
82 return static_cast<time_t>(-1); | |
83 } | |
84 | |
85 return result; | |
86 } | 72 } |
87 | 73 |
88 // Sets the time of the current ratelimit period's start in |metadata| to | 74 // Sets the time of the current ratelimit period's start in |metadata| to |
89 // |period_start|. Returns 0 on success, < 0 on error. | 75 // |period_start|. Returns true on success, false on error. |
90 int SetRatelimitPeriodStart(base::Value* metadata, time_t period_start) { | 76 bool SetRatelimitPeriodStart(base::Value* metadata, base::Time period_start) { |
91 DCHECK_GE(period_start, 0); | 77 DCHECK(!period_start.is_null()); |
92 | 78 |
93 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); | 79 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); |
94 RCHECK(ratelimit_params, -1); | 80 RCHECK(ratelimit_params, false); |
95 | 81 |
96 std::string period_start_str = | 82 ratelimit_params->SetDouble(kLockfileRatelimitPeriodStartKey, |
97 base::StringPrintf("%lld", static_cast<long long>(period_start)); | 83 period_start.ToDoubleT()); |
98 ratelimit_params->SetString(kLockfileRatelimitPeriodStartKey, | 84 return true; |
99 period_start_str); | |
100 return 0; | |
101 } | 85 } |
102 | 86 |
103 // Gets the number of dumps added to |metadata| in the current ratelimit | 87 // Gets the number of dumps added to |metadata| in the current ratelimit |
104 // period. Returns < 0 on error. | 88 // period. Returns < 0 on error. |
105 int GetRatelimitPeriodDumps(base::Value* metadata) { | 89 int GetRatelimitPeriodDumps(base::Value* metadata) { |
106 int period_dumps = -1; | 90 int period_dumps = -1; |
107 | 91 |
108 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); | 92 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); |
109 if (!ratelimit_params || | 93 if (!ratelimit_params || |
110 !ratelimit_params->GetInteger(kLockfileRatelimitPeriodDumpsKey, | 94 !ratelimit_params->GetInteger(kLockfileRatelimitPeriodDumpsKey, |
111 &period_dumps)) { | 95 &period_dumps)) { |
112 return -1; | 96 return -1; |
113 } | 97 } |
114 | 98 |
115 return period_dumps; | 99 return period_dumps; |
116 } | 100 } |
117 | 101 |
118 // Sets the current ratelimit period's number of dumps in |metadata| to | 102 // Sets the current ratelimit period's number of dumps in |metadata| to |
119 // |period_dumps|. Returns 0 on success, < 0 on error. | 103 // |period_dumps|. Returns true on success, false on error. |
120 int SetRatelimitPeriodDumps(base::Value* metadata, int period_dumps) { | 104 bool SetRatelimitPeriodDumps(base::Value* metadata, int period_dumps) { |
121 DCHECK_GE(period_dumps, 0); | 105 DCHECK_GE(period_dumps, 0); |
122 | 106 |
123 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); | 107 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); |
124 RCHECK(ratelimit_params, -1); | 108 RCHECK(ratelimit_params, false); |
125 | 109 |
126 ratelimit_params->SetInteger(kLockfileRatelimitPeriodDumpsKey, period_dumps); | 110 ratelimit_params->SetInteger(kLockfileRatelimitPeriodDumpsKey, period_dumps); |
127 | 111 |
128 return 0; | 112 return true; |
129 } | 113 } |
130 | 114 |
131 // Returns true if |metadata| contains valid metadata, false otherwise. | 115 // Returns true if |metadata| contains valid metadata, false otherwise. |
132 bool ValidateMetadata(base::Value* metadata) { | 116 bool ValidateMetadata(base::Value* metadata) { |
133 RCHECK(metadata, false); | 117 RCHECK(metadata, false); |
134 | 118 |
135 // Validate ratelimit params | 119 // Validate ratelimit params |
136 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); | 120 base::DictionaryValue* ratelimit_params = GetRatelimitParams(metadata); |
137 | 121 |
138 return ratelimit_params && | 122 return ratelimit_params && |
139 ratelimit_params->size() == kLockfileNumRatelimitParams && | 123 ratelimit_params->size() == kLockfileNumRatelimitParams && |
140 GetRatelimitPeriodStart(metadata) >= 0 && | 124 !GetRatelimitPeriodStart(metadata).is_null() && |
141 GetRatelimitPeriodDumps(metadata) >= 0; | 125 GetRatelimitPeriodDumps(metadata) >= 0; |
142 } | 126 } |
143 | 127 |
144 } // namespace | 128 } // namespace |
145 | 129 |
146 // One day | 130 // One day |
147 const int SynchronizedMinidumpManager::kRatelimitPeriodSeconds = 24 * 3600; | 131 const int SynchronizedMinidumpManager::kRatelimitPeriodSeconds = 24 * 3600; |
148 const int SynchronizedMinidumpManager::kRatelimitPeriodMaxDumps = 100; | 132 const int SynchronizedMinidumpManager::kRatelimitPeriodMaxDumps = 100; |
149 | 133 |
150 SynchronizedMinidumpManager::SynchronizedMinidumpManager() | 134 SynchronizedMinidumpManager::SynchronizedMinidumpManager() |
151 : non_blocking_(false), | 135 : dump_path_(GetHomePathASCII(kMinidumpsDir)), |
152 dump_path_(GetHomePathASCII(kMinidumpsDir)), | |
153 lockfile_path_(dump_path_.Append(kLockfileName).value()), | 136 lockfile_path_(dump_path_.Append(kLockfileName).value()), |
154 metadata_path_(dump_path_.Append(kMetadataName).value()), | 137 metadata_path_(dump_path_.Append(kMetadataName).value()), |
155 lockfile_fd_(-1) { | 138 lockfile_fd_(-1) {} |
156 } | |
157 | 139 |
158 SynchronizedMinidumpManager::~SynchronizedMinidumpManager() { | 140 SynchronizedMinidumpManager::~SynchronizedMinidumpManager() { |
159 // Release the lock if held. | 141 // Release the lock if held. |
160 ReleaseLockFile(); | 142 ReleaseLockFile(); |
161 } | 143 } |
162 | 144 |
163 // TODO(slan): Move some of this pruning logic to ReleaseLockFile? | 145 // TODO(slan): Move some of this pruning logic to ReleaseLockFile? |
164 int SynchronizedMinidumpManager::GetNumDumps(bool delete_all_dumps) { | 146 int SynchronizedMinidumpManager::GetNumDumps(bool delete_all_dumps) { |
165 DIR* dirp; | |
166 struct dirent* dptr; | |
167 int num_dumps = 0; | 147 int num_dumps = 0; |
168 | 148 |
169 // folder does not exist | 149 base::DirReaderPosix reader(dump_path_.value().c_str()); |
170 dirp = opendir(dump_path_.value().c_str()); | 150 if (!reader.IsValid()) { |
171 if (dirp == NULL) { | |
172 LOG(ERROR) << "Unable to open directory " << dump_path_.value(); | 151 LOG(ERROR) << "Unable to open directory " << dump_path_.value(); |
173 return 0; | 152 return 0; |
174 } | 153 } |
175 | 154 |
176 while ((dptr = readdir(dirp)) != NULL) { | 155 while (reader.Next()) { |
177 struct stat buf; | 156 if (strcmp(reader.name(), ".") == 0 || strcmp(reader.name(), "..") == 0) |
178 const std::string file_fullname = dump_path_.value() + "/" + dptr->d_name; | |
179 if (lstat(file_fullname.c_str(), &buf) == -1 || !S_ISREG(buf.st_mode)) { | |
180 // if we cannot lstat this file, it is probably bad, so skip | |
181 // if the file is not regular, skip | |
182 continue; | 157 continue; |
183 } | |
184 | 158 |
185 // 'lockfile' and 'metadata' is not counted | 159 const base::FilePath dump_file(dump_path_.Append(reader.name())); |
186 if (lockfile_path_ != file_fullname && metadata_path_ != file_fullname) { | 160 // If file cannot be found, skip. |
| 161 if (!base::PathExists(dump_file)) |
| 162 continue; |
| 163 |
| 164 // Do not count |lockfile_path_| and |metadata_path_|. |
| 165 if (lockfile_path_ != dump_file && metadata_path_ != dump_file) { |
187 ++num_dumps; | 166 ++num_dumps; |
188 if (delete_all_dumps) { | 167 if (delete_all_dumps) { |
189 LOG(INFO) << "Removing " << dptr->d_name | 168 LOG(INFO) << "Removing " << reader.name() |
190 << "which was not in the lockfile"; | 169 << "which was not in the lockfile"; |
191 if (remove(file_fullname.c_str()) < 0) { | 170 if (!base::DeleteFile(dump_file, false)) |
192 LOG(INFO) << "remove failed. error " << strerror(errno); | 171 PLOG(INFO) << "Removing " << dump_file.value() << " failed"; |
193 } | |
194 } | 172 } |
195 } | 173 } |
196 } | 174 } |
197 | 175 |
198 closedir(dirp); | |
199 return num_dumps; | 176 return num_dumps; |
200 } | 177 } |
201 | 178 |
202 int SynchronizedMinidumpManager::AcquireLockAndDoWork() { | 179 bool SynchronizedMinidumpManager::AcquireLockAndDoWork() { |
203 int success = -1; | 180 bool success = false; |
204 if (AcquireLockFile() >= 0) { | 181 if (AcquireLockFile()) { |
205 success = DoWork(); | 182 success = DoWork(); |
206 ReleaseLockFile(); | 183 ReleaseLockFile(); |
207 } | 184 } |
208 return success; | 185 return success; |
209 } | 186 } |
210 | 187 |
211 int SynchronizedMinidumpManager::AcquireLockFile() { | 188 bool SynchronizedMinidumpManager::AcquireLockFile() { |
212 DCHECK_LT(lockfile_fd_, 0); | 189 DCHECK_LT(lockfile_fd_, 0); |
213 // Make the directory for the minidumps if it does not exist. | 190 // Make the directory for the minidumps if it does not exist. |
214 if (mkdir(dump_path_.value().c_str(), kDirMode) < 0 && errno != EEXIST) { | 191 base::File::Error error; |
215 LOG(ERROR) << "mkdir for " << dump_path_.value().c_str() | 192 if (!CreateDirectoryAndGetError(dump_path_, &error)) { |
216 << " failed. error = " << strerror(errno); | 193 LOG(ERROR) << "Failed to create directory " << dump_path_.value() |
217 return -1; | 194 << ". error = " << error; |
| 195 return false; |
218 } | 196 } |
219 | 197 |
220 // Open the lockfile. Create it if it does not exist. | 198 // Open the lockfile. Create it if it does not exist. |
221 lockfile_fd_ = open(lockfile_path_.c_str(), O_RDWR | O_CREAT, kFileMode); | 199 base::File lockfile(lockfile_path_, base::File::FLAG_OPEN_ALWAYS); |
222 | 200 |
223 // If opening or creating the lockfile failed, we don't want to proceed | 201 // If opening or creating the lockfile failed, we don't want to proceed |
224 // with dump writing for fear of exhausting up system resources. | 202 // with dump writing for fear of exhausting up system resources. |
225 if (lockfile_fd_ < 0) { | 203 if (!lockfile.IsValid()) { |
226 LOG(ERROR) << "open lockfile failed " << lockfile_path_; | 204 LOG(ERROR) << "open lockfile failed " << lockfile_path_.value(); |
227 return -1; | 205 return false; |
228 } | 206 } |
229 | 207 |
230 // Acquire the lock on the file. Whether or not we are in non-blocking mode, | 208 if ((lockfile_fd_ = OpenAndLockFile(lockfile_path_, false)) < 0) { |
231 // flock failure means that we did not acquire it and this method should fail. | |
232 int operation_mode = non_blocking_ ? (LOCK_EX | LOCK_NB) : LOCK_EX; | |
233 if (flock(lockfile_fd_, operation_mode) < 0) { | |
234 ReleaseLockFile(); | 209 ReleaseLockFile(); |
235 LOG(INFO) << "flock lockfile failed, error = " << strerror(errno); | 210 return false; |
236 return -1; | |
237 } | 211 } |
238 | 212 |
239 // The lockfile is open and locked. Parse it to provide subclasses with a | 213 // The lockfile is open and locked. Parse it to provide subclasses with a |
240 // record of all the current dumps. | 214 // record of all the current dumps. |
241 if (ParseFiles() < 0) { | 215 if (!ParseFiles()) { |
242 LOG(ERROR) << "Lockfile did not parse correctly. "; | 216 LOG(ERROR) << "Lockfile did not parse correctly. "; |
243 if (InitializeFiles() < 0 || ParseFiles() < 0) { | 217 if (!InitializeFiles() || !ParseFiles()) { |
244 LOG(ERROR) << "Failed to create a new lock file!"; | 218 LOG(ERROR) << "Failed to create a new lock file!"; |
245 return -1; | 219 return false; |
246 } | 220 } |
247 } | 221 } |
248 | 222 |
249 DCHECK(dumps_); | 223 DCHECK(dumps_); |
250 DCHECK(metadata_); | 224 DCHECK(metadata_); |
251 | 225 |
252 // We successfully have acquired the lock. | 226 // We successfully have acquired the lock. |
253 return 0; | 227 return true; |
254 } | 228 } |
255 | 229 |
256 int SynchronizedMinidumpManager::ParseFiles() { | 230 bool SynchronizedMinidumpManager::ParseFiles() { |
257 DCHECK_GE(lockfile_fd_, 0); | 231 DCHECK_GE(lockfile_fd_, 0); |
258 DCHECK(!dumps_); | 232 DCHECK(!dumps_); |
259 DCHECK(!metadata_); | 233 DCHECK(!metadata_); |
260 | 234 |
261 std::string lockfile; | 235 std::string lockfile; |
262 RCHECK(ReadFileToString(base::FilePath(lockfile_path_), &lockfile), -1); | 236 RCHECK(ReadFileToString(lockfile_path_, &lockfile), false); |
263 | 237 |
264 std::vector<std::string> lines = base::SplitString( | 238 std::vector<std::string> lines = base::SplitString( |
265 lockfile, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 239 lockfile, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
266 | 240 |
267 std::unique_ptr<base::ListValue> dumps = | 241 std::unique_ptr<base::ListValue> dumps = |
268 base::WrapUnique(new base::ListValue()); | 242 base::WrapUnique(new base::ListValue()); |
269 | 243 |
270 // Validate dumps | 244 // Validate dumps |
271 for (const std::string& line : lines) { | 245 for (const std::string& line : lines) { |
272 if (line.size() == 0) | 246 if (line.size() == 0) |
273 continue; | 247 continue; |
274 std::unique_ptr<base::Value> dump_info = DeserializeFromJson(line); | 248 std::unique_ptr<base::Value> dump_info = DeserializeFromJson(line); |
275 DumpInfo info(dump_info.get()); | 249 DumpInfo info(dump_info.get()); |
276 RCHECK(info.valid(), -1); | 250 RCHECK(info.valid(), false); |
277 dumps->Append(std::move(dump_info)); | 251 dumps->Append(std::move(dump_info)); |
278 } | 252 } |
279 | 253 |
280 std::unique_ptr<base::Value> metadata = | 254 std::unique_ptr<base::Value> metadata = |
281 DeserializeJsonFromFile(base::FilePath(metadata_path_)); | 255 DeserializeJsonFromFile(metadata_path_); |
282 RCHECK(ValidateMetadata(metadata.get()), -1); | 256 RCHECK(ValidateMetadata(metadata.get()), false); |
283 | 257 |
284 dumps_ = std::move(dumps); | 258 dumps_ = std::move(dumps); |
285 metadata_ = std::move(metadata); | 259 metadata_ = std::move(metadata); |
286 return 0; | 260 return true; |
287 } | 261 } |
288 | 262 |
289 int SynchronizedMinidumpManager::WriteFiles(const base::ListValue* dumps, | 263 bool SynchronizedMinidumpManager::WriteFiles(const base::ListValue* dumps, |
290 const base::Value* metadata) { | 264 const base::Value* metadata) { |
291 DCHECK(dumps); | 265 DCHECK(dumps); |
292 DCHECK(metadata); | 266 DCHECK(metadata); |
293 std::string lockfile; | 267 std::string lockfile; |
294 | 268 |
295 for (const auto& elem : *dumps) { | 269 for (const auto& elem : *dumps) { |
296 std::unique_ptr<std::string> dump_info = SerializeToJson(*elem); | 270 std::unique_ptr<std::string> dump_info = SerializeToJson(*elem); |
297 RCHECK(dump_info, -1); | 271 RCHECK(dump_info, false); |
298 lockfile += *dump_info; | 272 lockfile += *dump_info; |
299 lockfile += "\n"; // Add line seperatators | 273 lockfile += "\n"; // Add line seperatators |
300 } | 274 } |
301 | 275 |
302 if (WriteFile(base::FilePath(lockfile_path_), | 276 if (WriteFile(lockfile_path_, lockfile.c_str(), lockfile.size()) < 0) { |
303 lockfile.c_str(), | 277 return false; |
304 lockfile.size()) < 0) { | |
305 return -1; | |
306 } | 278 } |
307 | 279 |
308 return SerializeJsonToFile(base::FilePath(metadata_path_), *metadata) ? 0 | 280 return SerializeJsonToFile(metadata_path_, *metadata); |
309 : -1; | |
310 } | 281 } |
311 | 282 |
312 int SynchronizedMinidumpManager::InitializeFiles() { | 283 bool SynchronizedMinidumpManager::InitializeFiles() { |
313 std::unique_ptr<base::DictionaryValue> metadata = | 284 std::unique_ptr<base::DictionaryValue> metadata = |
314 base::WrapUnique(new base::DictionaryValue()); | 285 base::WrapUnique(new base::DictionaryValue()); |
315 | 286 |
316 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue(); | 287 base::DictionaryValue* ratelimit_fields = new base::DictionaryValue(); |
317 metadata->Set(kLockfileRatelimitKey, base::WrapUnique(ratelimit_fields)); | 288 metadata->Set(kLockfileRatelimitKey, base::WrapUnique(ratelimit_fields)); |
318 ratelimit_fields->SetString(kLockfileRatelimitPeriodStartKey, "0"); | 289 ratelimit_fields->SetDouble(kLockfileRatelimitPeriodStartKey, 0.0); |
319 ratelimit_fields->SetInteger(kLockfileRatelimitPeriodDumpsKey, 0); | 290 ratelimit_fields->SetInteger(kLockfileRatelimitPeriodDumpsKey, 0); |
320 | 291 |
321 std::unique_ptr<base::ListValue> dumps = | 292 std::unique_ptr<base::ListValue> dumps = |
322 base::WrapUnique(new base::ListValue()); | 293 base::WrapUnique(new base::ListValue()); |
323 | 294 |
324 return WriteFiles(dumps.get(), metadata.get()); | 295 return WriteFiles(dumps.get(), metadata.get()); |
325 } | 296 } |
326 | 297 |
327 int SynchronizedMinidumpManager::AddEntryToLockFile(const DumpInfo& dump_info) { | 298 bool SynchronizedMinidumpManager::AddEntryToLockFile( |
328 DCHECK_LE(0, lockfile_fd_); | 299 const DumpInfo& dump_info) { |
| 300 DCHECK_GE(lockfile_fd_, 0); |
329 DCHECK(dumps_); | 301 DCHECK(dumps_); |
330 | 302 |
331 // Make sure dump_info is valid. | 303 // Make sure dump_info is valid. |
332 if (!dump_info.valid()) { | 304 if (!dump_info.valid()) { |
333 LOG(ERROR) << "Entry to be added is invalid"; | 305 LOG(ERROR) << "Entry to be added is invalid"; |
334 return -1; | 306 return false; |
335 } | 307 } |
336 | 308 |
337 dumps_->Append(dump_info.GetAsValue()); | 309 dumps_->Append(dump_info.GetAsValue()); |
338 | 310 return true; |
339 return 0; | |
340 } | 311 } |
341 | 312 |
342 int SynchronizedMinidumpManager::RemoveEntryFromLockFile(int index) { | 313 bool SynchronizedMinidumpManager::RemoveEntryFromLockFile(int index) { |
343 return dumps_->Remove(static_cast<size_t>(index), nullptr) ? 0 : -1; | 314 return dumps_->Remove(static_cast<uint64_t>(index), nullptr); |
344 } | 315 } |
345 | 316 |
346 void SynchronizedMinidumpManager::ReleaseLockFile() { | 317 void SynchronizedMinidumpManager::ReleaseLockFile() { |
347 // flock is associated with the fd entry in the open fd table, so closing | 318 // flock is associated with the fd entry in the open fd table, so closing |
348 // all fd's will release the lock. To be safe, we explicitly unlock. | 319 // all fd's will release the lock. To be safe, we explicitly unlock. |
349 if (lockfile_fd_ >= 0) { | 320 if (lockfile_fd_ >= 0) { |
350 if (dumps_) | 321 if (dumps_) |
351 WriteFiles(dumps_.get(), metadata_.get()); | 322 WriteFiles(dumps_.get(), metadata_.get()); |
352 | 323 |
353 flock(lockfile_fd_, LOCK_UN); | 324 UnlockAndCloseFile(lockfile_fd_); |
354 close(lockfile_fd_); | |
355 | |
356 // We may use this object again, so we should reset this. | |
357 lockfile_fd_ = -1; | 325 lockfile_fd_ = -1; |
358 } | 326 } |
359 | 327 |
360 dumps_.reset(); | 328 dumps_.reset(); |
361 metadata_.reset(); | 329 metadata_.reset(); |
362 } | 330 } |
363 | 331 |
364 ScopedVector<DumpInfo> SynchronizedMinidumpManager::GetDumps() { | 332 ScopedVector<DumpInfo> SynchronizedMinidumpManager::GetDumps() { |
365 ScopedVector<DumpInfo> dumps; | 333 ScopedVector<DumpInfo> dumps; |
366 | 334 |
367 for (const auto& elem : *dumps_) { | 335 for (const auto& elem : *dumps_) { |
368 dumps.push_back(new DumpInfo(elem.get())); | 336 dumps.push_back(new DumpInfo(elem.get())); |
369 } | 337 } |
370 | 338 |
371 return dumps; | 339 return dumps; |
372 } | 340 } |
373 | 341 |
374 int SynchronizedMinidumpManager::SetCurrentDumps( | 342 bool SynchronizedMinidumpManager::SetCurrentDumps( |
375 const ScopedVector<DumpInfo>& dumps) { | 343 const ScopedVector<DumpInfo>& dumps) { |
376 dumps_->Clear(); | 344 dumps_->Clear(); |
377 | 345 |
378 for (DumpInfo* dump : dumps) | 346 for (DumpInfo* dump : dumps) |
379 dumps_->Append(dump->GetAsValue()); | 347 dumps_->Append(dump->GetAsValue()); |
380 | 348 |
381 return 0; | 349 return true; |
382 } | 350 } |
383 | 351 |
384 int SynchronizedMinidumpManager::IncrementNumDumpsInCurrentPeriod() { | 352 bool SynchronizedMinidumpManager::IncrementNumDumpsInCurrentPeriod() { |
385 DCHECK(metadata_); | 353 DCHECK(metadata_); |
386 int last_dumps = GetRatelimitPeriodDumps(metadata_.get()); | 354 int last_dumps = GetRatelimitPeriodDumps(metadata_.get()); |
387 RCHECK(last_dumps >= 0, -1); | 355 RCHECK(last_dumps >= 0, false); |
388 | 356 |
389 return SetRatelimitPeriodDumps(metadata_.get(), last_dumps + 1); | 357 return SetRatelimitPeriodDumps(metadata_.get(), last_dumps + 1); |
390 } | 358 } |
391 | 359 |
392 bool SynchronizedMinidumpManager::CanUploadDump() { | 360 bool SynchronizedMinidumpManager::CanUploadDump() { |
393 time_t cur_time = time(nullptr); | 361 base::Time cur_time = base::Time::Now(); |
394 time_t period_start = GetRatelimitPeriodStart(metadata_.get()); | 362 base::Time period_start = GetRatelimitPeriodStart(metadata_.get()); |
395 int period_dumps_count = GetRatelimitPeriodDumps(metadata_.get()); | 363 int period_dumps_count = GetRatelimitPeriodDumps(metadata_.get()); |
396 | 364 |
397 // If we're in invalid state, or we passed the period, reset the ratelimit. | 365 // If we're in invalid state, or we passed the period, reset the ratelimit. |
398 // When the device reboots, |cur_time| may be incorrectly reported to be a | 366 // When the device reboots, |cur_time| may be incorrectly reported to be a |
399 // very small number for a short period of time. So only consider | 367 // very small number for a short period of time. So only consider |
400 // |period_start| invalid when |cur_time| is less if |cur_time| is not very | 368 // |period_start| invalid when |cur_time| is less if |cur_time| is not very |
401 // close to 0. | 369 // close to 0. |
402 if (period_dumps_count < 0 || | 370 if (period_dumps_count < 0 || |
403 (cur_time < period_start && cur_time > kRatelimitPeriodSeconds) || | 371 (cur_time < period_start && |
404 difftime(cur_time, period_start) >= kRatelimitPeriodSeconds) { | 372 cur_time.ToDoubleT() > kRatelimitPeriodSeconds) || |
| 373 (cur_time - period_start).InSeconds() >= kRatelimitPeriodSeconds) { |
405 period_start = cur_time; | 374 period_start = cur_time; |
406 period_dumps_count = 0; | 375 period_dumps_count = 0; |
407 SetRatelimitPeriodStart(metadata_.get(), period_start); | 376 SetRatelimitPeriodStart(metadata_.get(), period_start); |
408 SetRatelimitPeriodDumps(metadata_.get(), period_dumps_count); | 377 SetRatelimitPeriodDumps(metadata_.get(), period_dumps_count); |
409 } | 378 } |
410 | 379 |
411 return period_dumps_count < kRatelimitPeriodMaxDumps; | 380 return period_dumps_count < kRatelimitPeriodMaxDumps; |
412 } | 381 } |
413 | 382 |
414 bool SynchronizedMinidumpManager::HasDumps() { | 383 bool SynchronizedMinidumpManager::HasDumps() { |
415 // Check if lockfile has entries. | 384 // Check if lockfile has entries. |
416 int64_t size = 0; | 385 int64_t size = 0; |
417 if (GetFileSize(base::FilePath(lockfile_path_), &size) && size > 0) | 386 if (base::GetFileSize(lockfile_path_, &size) && size > 0) |
418 return true; | 387 return true; |
419 | 388 |
420 // Check if any files are in minidump directory | 389 // Check if any files are in minidump directory |
421 base::DirReaderPosix reader(dump_path_.value().c_str()); | 390 base::DirReaderPosix reader(dump_path_.value().c_str()); |
422 if (!reader.IsValid()) { | 391 if (!reader.IsValid()) { |
423 DLOG(FATAL) << "Could not open minidump dir: " << dump_path_.value(); | 392 DLOG(FATAL) << "Could not open minidump dir: " << dump_path_.value(); |
424 return false; | 393 return false; |
425 } | 394 } |
426 | 395 |
427 while (reader.Next()) { | 396 while (reader.Next()) { |
428 if (strcmp(reader.name(), ".") == 0 || strcmp(reader.name(), "..") == 0) | 397 if (strcmp(reader.name(), ".") == 0 || strcmp(reader.name(), "..") == 0) |
429 continue; | 398 continue; |
430 | 399 |
431 const std::string file_path = dump_path_.Append(reader.name()).value(); | 400 const base::FilePath file_path = dump_path_.Append(reader.name()); |
432 if (file_path != lockfile_path_ && file_path != metadata_path_) | 401 if (file_path != lockfile_path_ && file_path != metadata_path_) |
433 return true; | 402 return true; |
434 } | 403 } |
435 | 404 |
436 return false; | 405 return false; |
437 } | 406 } |
438 | 407 |
439 } // namespace chromecast | 408 } // namespace chromecast |
OLD | NEW |