blob: b81a936cf58c62100ff61097bc1a65ff5b397fa3 [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ken Mixter03403162010-08-18 15:23:16 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "crash_collector.h"
Ken Mixter03403162010-08-18 15:23:16 -070018
Ken Mixter04ec10f2010-08-26 16:02:02 -070019#include <dirent.h>
Ken Mixter9b346472010-11-07 13:45:45 -080020#include <fcntl.h> // For file creation modes.
Alex Vakulenko7589aff2014-07-30 10:07:35 -070021#include <inttypes.h>
Ben Chanf3811f52014-08-26 06:46:38 -070022#include <linux/limits.h> // PATH_MAX
Ken Mixter03403162010-08-18 15:23:16 -070023#include <pwd.h> // For struct passwd.
24#include <sys/types.h> // for mode_t.
Ken Mixter9b346472010-11-07 13:45:45 -080025#include <sys/wait.h> // For waitpid.
26#include <unistd.h> // For execv and fork.
Ken Mixter03403162010-08-18 15:23:16 -070027
Ken Mixteree849c52010-09-30 15:30:10 -070028#include <set>
Ben Chan7e776902014-06-18 13:19:51 -070029#include <utility>
Simon Quef70060c2012-04-09 19:07:07 -070030#include <vector>
Ken Mixteree849c52010-09-30 15:30:10 -070031
Ben Chanab6cc902014-09-05 08:21:06 -070032#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070033#include <base/logging.h>
34#include <base/posix/eintr_wrapper.h>
35#include <base/strings/string_split.h>
36#include <base/strings/string_util.h>
37#include <base/strings/stringprintf.h>
Daniel Erat731da332015-01-28 09:48:10 -070038#include <chromeos/key_value_store.h>
Ben Chan7e776902014-06-18 13:19:51 -070039#include <chromeos/process.h>
Ken Mixter03403162010-08-18 15:23:16 -070040
Daniel Erat731da332015-01-28 09:48:10 -070041namespace {
42
43const char kCollectChromeFile[] =
Michael Krebs4fe30db2011-08-05 13:54:52 -070044 "/mnt/stateful_partition/etc/collect_chrome_crashes";
Steve Fung0e8746d2015-08-20 17:07:50 -070045const char kCrashTestInProgressPath[] =
46 "/data/misc/crash_reporter/tmp/crash-test-in-progress";
Daniel Erat731da332015-01-28 09:48:10 -070047const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf";
48const char kDefaultUserName[] = "chronos";
Steve Fung0e8746d2015-08-20 17:07:50 -070049const char kLeaveCoreFile[] = "/data/misc/crash_reporter/.leave_core";
Daniel Erat731da332015-01-28 09:48:10 -070050const char kLsbRelease[] = "/etc/lsb-release";
Steve Fung0e8746d2015-08-20 17:07:50 -070051const char kShellPath[] = "/system/bin/sh";
Steve Fungab2ac7d2015-08-14 17:58:05 -070052const char kSystemCrashPath[] = "/data/misc/crash_reporter/crash";
Daniel Erat731da332015-01-28 09:48:10 -070053const char kUploadVarPrefix[] = "upload_var_";
54const char kUploadFilePrefix[] = "upload_file_";
55
56// Key of the lsb-release entry containing the OS version.
57const char kLsbVersionKey[] = "CHROMEOS_RELEASE_VERSION";
58
Mike Frysingerf19b5182013-05-17 19:36:47 -040059// Normally this path is not used. Unfortunately, there are a few edge cases
60// where we need this. Any process that runs as kDefaultUserName that crashes
61// is consider a "user crash". That includes the initial Chrome browser that
62// runs the login screen. If that blows up, there is no logged in user yet,
63// so there is no per-user dir for us to stash things in. Instead we fallback
64// to this path as it is at least encrypted on a per-system basis.
65//
66// This also comes up when running autotests. The GUI is sitting at the login
67// screen while tests are sshing in, changing users, and triggering crashes as
68// the user (purposefully).
Daniel Erat731da332015-01-28 09:48:10 -070069const char kFallbackUserCrashPath[] = "/home/chronos/crash";
Ken Mixter03403162010-08-18 15:23:16 -070070
71// Directory mode of the user crash spool directory.
Daniel Erat731da332015-01-28 09:48:10 -070072const mode_t kUserCrashPathMode = 0755;
Ken Mixter03403162010-08-18 15:23:16 -070073
74// Directory mode of the system crash spool directory.
Daniel Erat731da332015-01-28 09:48:10 -070075const mode_t kSystemCrashPathMode = 01755;
Ken Mixter03403162010-08-18 15:23:16 -070076
Daniel Erat731da332015-01-28 09:48:10 -070077const uid_t kRootOwner = 0;
78const uid_t kRootGroup = 0;
79
80} // namespace
Ken Mixter03403162010-08-18 15:23:16 -070081
Ken Mixterda5db7a2010-09-17 13:50:42 -070082// Maximum crash reports per crash spool directory. Note that this is
83// a separate maximum from the maximum rate at which we upload these
84// diagnostics. The higher this rate is, the more space we allow for
85// core files, minidumps, and kcrash logs, and equivalently the more
86// processor and I/O bandwidth we dedicate to handling these crashes when
87// many occur at once. Also note that if core files are configured to
88// be left on the file system, we stop adding crashes when either the
89// number of core files or minidumps reaches this number.
90const int CrashCollector::kMaxCrashDirectorySize = 32;
Ken Mixter04ec10f2010-08-26 16:02:02 -070091
Simon Que9f90aca2013-02-19 17:19:52 -080092using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050093using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080094
Ken Mixterafcf8082010-10-26 14:45:01 -070095CrashCollector::CrashCollector()
Lei Zhang9b1f3002014-04-24 02:10:57 -070096 : lsb_release_(kLsbRelease),
Simon Queacc79382012-05-04 18:10:09 -070097 log_config_path_(kDefaultLogConfig) {
Ken Mixter03403162010-08-18 15:23:16 -070098}
99
100CrashCollector::~CrashCollector() {
101}
102
103void CrashCollector::Initialize(
104 CrashCollector::CountCrashFunction count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -0800105 CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) {
Ben Chan262d7982014-09-18 08:05:20 -0700106 CHECK(count_crash_function);
107 CHECK(is_feedback_allowed_function);
Ken Mixter03403162010-08-18 15:23:16 -0700108
109 count_crash_function_ = count_crash_function;
110 is_feedback_allowed_function_ = is_feedback_allowed_function;
Ken Mixter03403162010-08-18 15:23:16 -0700111}
112
Ken Mixter9b346472010-11-07 13:45:45 -0800113int CrashCollector::WriteNewFile(const FilePath &filename,
114 const char *data,
115 int size) {
116 int fd = HANDLE_EINTR(open(filename.value().c_str(),
117 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666));
118 if (fd < 0) {
119 return -1;
120 }
121
Alex Vakulenko859ee452014-12-10 12:52:31 -0800122 int rv = base::WriteFileDescriptor(fd, data, size) ? size : -1;
Mike Frysingerf1a50142014-05-14 16:05:09 -0400123 IGNORE_EINTR(close(fd));
Ken Mixter9b346472010-11-07 13:45:45 -0800124 return rv;
125}
126
Ken Mixteree849c52010-09-30 15:30:10 -0700127std::string CrashCollector::Sanitize(const std::string &name) {
Thiemo Nagel98950962014-05-13 19:48:32 +0200128 // Make sure the sanitized name does not include any periods.
129 // The logic in crash_sender relies on this.
Ken Mixteree849c52010-09-30 15:30:10 -0700130 std::string result = name;
131 for (size_t i = 0; i < name.size(); ++i) {
132 if (!isalnum(result[i]) && result[i] != '_')
133 result[i] = '_';
134 }
135 return result;
136}
137
Ken Mixter03403162010-08-18 15:23:16 -0700138std::string CrashCollector::FormatDumpBasename(const std::string &exec_name,
139 time_t timestamp,
140 pid_t pid) {
141 struct tm tm;
142 localtime_r(&timestamp, &tm);
Ken Mixteree849c52010-09-30 15:30:10 -0700143 std::string sanitized_exec_name = Sanitize(exec_name);
Ken Mixter03403162010-08-18 15:23:16 -0700144 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d",
Ken Mixteree849c52010-09-30 15:30:10 -0700145 sanitized_exec_name.c_str(),
Ken Mixter03403162010-08-18 15:23:16 -0700146 tm.tm_year + 1900,
147 tm.tm_mon + 1,
148 tm.tm_mday,
149 tm.tm_hour,
150 tm.tm_min,
151 tm.tm_sec,
152 pid);
153}
154
Ken Mixter207694d2010-10-28 15:42:37 -0700155FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory,
156 const std::string &basename,
157 const std::string &extension) {
158 return crash_directory.Append(StringPrintf("%s.%s",
159 basename.c_str(),
160 extension.c_str()));
161}
162
Ken Mixter03403162010-08-18 15:23:16 -0700163FilePath CrashCollector::GetCrashDirectoryInfo(
Ken Mixter03403162010-08-18 15:23:16 -0700164 mode_t *mode,
165 uid_t *directory_owner,
166 gid_t *directory_group) {
Steve Fungab2ac7d2015-08-14 17:58:05 -0700167 *mode = kSystemCrashPathMode;
168 *directory_owner = kRootOwner;
169 *directory_group = kRootGroup;
170 return FilePath(kSystemCrashPath);
Ken Mixter03403162010-08-18 15:23:16 -0700171}
172
173bool CrashCollector::GetUserInfoFromName(const std::string &name,
174 uid_t *uid,
175 gid_t *gid) {
176 char storage[256];
177 struct passwd passwd_storage;
Ben Chan262d7982014-09-18 08:05:20 -0700178 struct passwd *passwd_result = nullptr;
Ken Mixter03403162010-08-18 15:23:16 -0700179
180 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage),
Ben Chan262d7982014-09-18 08:05:20 -0700181 &passwd_result) != 0 || passwd_result == nullptr) {
Ken Mixtera3249322011-03-03 08:47:38 -0800182 LOG(ERROR) << "Cannot find user named " << name;
Ken Mixter03403162010-08-18 15:23:16 -0700183 return false;
184 }
185
186 *uid = passwd_result->pw_uid;
187 *gid = passwd_result->pw_gid;
188 return true;
189}
190
191bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid,
Ken Mixter207694d2010-10-28 15:42:37 -0700192 FilePath *crash_directory,
193 bool *out_of_capacity) {
Ben Chan262d7982014-09-18 08:05:20 -0700194 if (out_of_capacity) *out_of_capacity = false;
Ken Mixter207694d2010-10-28 15:42:37 -0700195
Ken Mixter03403162010-08-18 15:23:16 -0700196 // For testing.
Lei Zhang9b1f3002014-04-24 02:10:57 -0700197 if (!forced_crash_directory_.empty()) {
198 *crash_directory = forced_crash_directory_;
Ken Mixter03403162010-08-18 15:23:16 -0700199 return true;
200 }
201
Ken Mixter03403162010-08-18 15:23:16 -0700202 mode_t directory_mode;
203 uid_t directory_owner;
204 gid_t directory_group;
205 *crash_directory =
Steve Fungab2ac7d2015-08-14 17:58:05 -0700206 GetCrashDirectoryInfo(&directory_mode,
Ken Mixter03403162010-08-18 15:23:16 -0700207 &directory_owner,
208 &directory_group);
209
Mike Frysingera557c112014-02-05 22:55:39 -0500210 if (!base::PathExists(*crash_directory)) {
Ken Mixter03403162010-08-18 15:23:16 -0700211 // Create the spool directory with the appropriate mode (regardless of
212 // umask) and ownership.
213 mode_t old_mask = umask(0);
214 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 ||
215 chown(crash_directory->value().c_str(),
216 directory_owner,
217 directory_group) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800218 LOG(ERROR) << "Unable to create appropriate crash directory";
Ken Mixter03403162010-08-18 15:23:16 -0700219 return false;
220 }
221 umask(old_mask);
222 }
223
Mike Frysingera557c112014-02-05 22:55:39 -0500224 if (!base::PathExists(*crash_directory)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800225 LOG(ERROR) << "Unable to create crash directory "
226 << crash_directory->value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700227 return false;
228 }
229
Ken Mixter04ec10f2010-08-26 16:02:02 -0700230 if (!CheckHasCapacity(*crash_directory)) {
Ben Chan262d7982014-09-18 08:05:20 -0700231 if (out_of_capacity) *out_of_capacity = true;
Steve Fungab2ac7d2015-08-14 17:58:05 -0700232 LOG(ERROR) << "Directory " << crash_directory->value()
233 << " is out of capacity.";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700234 return false;
235 }
236
Ken Mixter03403162010-08-18 15:23:16 -0700237 return true;
238}
Ken Mixter04ec10f2010-08-26 16:02:02 -0700239
Albert Chaulk426fcc02013-05-02 15:38:31 -0700240FilePath CrashCollector::GetProcessPath(pid_t pid) {
241 return FilePath(StringPrintf("/proc/%d", pid));
242}
243
244bool CrashCollector::GetSymlinkTarget(const FilePath &symlink,
Ben Chan8563d202014-01-27 19:30:13 -0800245 FilePath *target) {
Ben Chanf3811f52014-08-26 06:46:38 -0700246 ssize_t max_size = 64;
247 std::vector<char> buffer;
248
Albert Chaulk426fcc02013-05-02 15:38:31 -0700249 while (true) {
Ben Chanf3811f52014-08-26 06:46:38 -0700250 buffer.resize(max_size + 1);
251 ssize_t size = readlink(symlink.value().c_str(), buffer.data(), max_size);
Albert Chaulk426fcc02013-05-02 15:38:31 -0700252 if (size < 0) {
253 int saved_errno = errno;
254 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
255 << saved_errno;
256 return false;
257 }
Ben Chanf3811f52014-08-26 06:46:38 -0700258
Albert Chaulk426fcc02013-05-02 15:38:31 -0700259 buffer[size] = 0;
260 if (size == max_size) {
Ben Chanf3811f52014-08-26 06:46:38 -0700261 max_size *= 2;
262 if (max_size > PATH_MAX) {
Albert Chaulk426fcc02013-05-02 15:38:31 -0700263 return false;
264 }
Ben Chanf3811f52014-08-26 06:46:38 -0700265 continue;
Albert Chaulk426fcc02013-05-02 15:38:31 -0700266 }
267 break;
268 }
269
Ben Chanf3811f52014-08-26 06:46:38 -0700270 *target = FilePath(buffer.data());
Albert Chaulk426fcc02013-05-02 15:38:31 -0700271 return true;
272}
273
274bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid,
275 std::string *base_name) {
276 FilePath target;
277 FilePath process_path = GetProcessPath(pid);
278 FilePath exe_path = process_path.Append("exe");
279 if (!GetSymlinkTarget(exe_path, &target)) {
280 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
281 << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500282 << base::DirectoryExists(process_path);
Albert Chaulk426fcc02013-05-02 15:38:31 -0700283 // Try to further diagnose exe readlink failure cause.
284 struct stat buf;
285 int stat_result = stat(exe_path.value().c_str(), &buf);
286 int saved_errno = errno;
287 if (stat_result < 0) {
288 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
289 << " " << saved_errno;
290 } else {
291 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
292 << buf.st_mode;
293 }
294 return false;
295 }
296 *base_name = target.BaseName().value();
297 return true;
298}
299
Ken Mixter04ec10f2010-08-26 16:02:02 -0700300// Return true if the given crash directory has not already reached
301// maximum capacity.
302bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) {
303 DIR* dir = opendir(crash_directory.value().c_str());
304 if (!dir) {
Steve Fungab2ac7d2015-08-14 17:58:05 -0700305 LOG(WARNING) << "Unable to open crash directory "
306 << crash_directory.value();
Ken Mixter04ec10f2010-08-26 16:02:02 -0700307 return false;
308 }
309 struct dirent ent_buf;
310 struct dirent* ent;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700311 bool full = false;
Ken Mixteree849c52010-09-30 15:30:10 -0700312 std::set<std::string> basenames;
Ben Chan262d7982014-09-18 08:05:20 -0700313 while (readdir_r(dir, &ent_buf, &ent) == 0 && ent) {
Ken Mixter04ec10f2010-08-26 16:02:02 -0700314 if ((strcmp(ent->d_name, ".") == 0) ||
315 (strcmp(ent->d_name, "..") == 0))
316 continue;
317
Ken Mixteree849c52010-09-30 15:30:10 -0700318 std::string filename(ent->d_name);
319 size_t last_dot = filename.rfind(".");
320 std::string basename;
321 // If there is a valid looking extension, use the base part of the
322 // name. If the only dot is the first byte (aka a dot file), treat
323 // it as unique to avoid allowing a directory full of dot files
324 // from accumulating.
325 if (last_dot != std::string::npos && last_dot != 0)
326 basename = filename.substr(0, last_dot);
327 else
328 basename = filename;
329 basenames.insert(basename);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700330
Ken Mixteree849c52010-09-30 15:30:10 -0700331 if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800332 LOG(WARNING) << "Crash directory " << crash_directory.value()
333 << " already full with " << kMaxCrashDirectorySize
334 << " pending reports";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700335 full = true;
336 break;
337 }
338 }
339 closedir(dir);
340 return !full;
341}
Ken Mixteree849c52010-09-30 15:30:10 -0700342
Ken Mixterc49dbd42010-12-14 17:44:11 -0800343bool CrashCollector::GetLogContents(const FilePath &config_path,
344 const std::string &exec_name,
345 const FilePath &output_file) {
Daniel Erat731da332015-01-28 09:48:10 -0700346 chromeos::KeyValueStore store;
347 if (!store.Load(config_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800348 LOG(INFO) << "Unable to read log configuration file "
349 << config_path.value();
Ken Mixterc49dbd42010-12-14 17:44:11 -0800350 return false;
351 }
352
Daniel Erat731da332015-01-28 09:48:10 -0700353 std::string command;
354 if (!store.GetString(exec_name, &command))
Ken Mixterc49dbd42010-12-14 17:44:11 -0800355 return false;
356
Ken Mixtera3249322011-03-03 08:47:38 -0800357 chromeos::ProcessImpl diag_process;
358 diag_process.AddArg(kShellPath);
Daniel Erat731da332015-01-28 09:48:10 -0700359 diag_process.AddStringOption("-c", command);
Ken Mixtera3249322011-03-03 08:47:38 -0800360 diag_process.RedirectOutput(output_file.value());
Ken Mixterc49dbd42010-12-14 17:44:11 -0800361
Daniel Erat731da332015-01-28 09:48:10 -0700362 const int result = diag_process.Run();
Ken Mixtera3249322011-03-03 08:47:38 -0800363 if (result != 0) {
Daniel Erat731da332015-01-28 09:48:10 -0700364 LOG(INFO) << "Log command \"" << command << "\" exited with " << result;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800365 return false;
366 }
367 return true;
368}
369
Ken Mixterafcf8082010-10-26 14:45:01 -0700370void CrashCollector::AddCrashMetaData(const std::string &key,
371 const std::string &value) {
372 extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str()));
373}
374
Albert Chaulk33dfd472013-06-19 15:34:13 -0700375void CrashCollector::AddCrashMetaUploadFile(const std::string &key,
376 const std::string &path) {
377 if (!path.empty())
378 AddCrashMetaData(kUploadFilePrefix + key, path);
379}
380
381void CrashCollector::AddCrashMetaUploadData(const std::string &key,
382 const std::string &value) {
383 if (!value.empty())
384 AddCrashMetaData(kUploadVarPrefix + key, value);
385}
386
Ken Mixteree849c52010-09-30 15:30:10 -0700387void CrashCollector::WriteCrashMetaData(const FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700388 const std::string &exec_name,
389 const std::string &payload_path) {
Daniel Erat731da332015-01-28 09:48:10 -0700390 chromeos::KeyValueStore store;
391 if (!store.Load(FilePath(lsb_release_))) {
Ken Mixtera3249322011-03-03 08:47:38 -0800392 LOG(ERROR) << "Problem parsing " << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700393 // Even though there was some failure, take as much as we could read.
394 }
Daniel Erat731da332015-01-28 09:48:10 -0700395
Ken Mixteree849c52010-09-30 15:30:10 -0700396 std::string version("unknown");
Daniel Erat731da332015-01-28 09:48:10 -0700397 if (!store.GetString(kLsbVersionKey, &version)) {
398 LOG(ERROR) << "Unable to read " << kLsbVersionKey << " from "
399 << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700400 }
Ben Chanf84ea212014-08-06 17:27:48 -0700401 int64_t payload_size = -1;
Mike Frysingera557c112014-02-05 22:55:39 -0500402 base::GetFileSize(FilePath(payload_path), &payload_size);
Ken Mixterafcf8082010-10-26 14:45:01 -0700403 std::string meta_data = StringPrintf("%sexec_name=%s\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700404 "ver=%s\n"
Ken Mixter207694d2010-10-28 15:42:37 -0700405 "payload=%s\n"
Alex Vakulenkoeaf060c2014-08-08 09:36:48 -0700406 "payload_size=%" PRId64 "\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700407 "done=1\n",
Ken Mixterafcf8082010-10-26 14:45:01 -0700408 extra_metadata_.c_str(),
Ken Mixteree849c52010-09-30 15:30:10 -0700409 exec_name.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700410 version.c_str(),
Ken Mixter207694d2010-10-28 15:42:37 -0700411 payload_path.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700412 payload_size);
Ben Chanf30c6412014-05-22 23:09:01 -0700413 // We must use WriteNewFile instead of base::WriteFile as we
Ken Mixter9b346472010-11-07 13:45:45 -0800414 // do not want to write with root access to a symlink that an attacker
415 // might have created.
416 if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800417 LOG(ERROR) << "Unable to write " << meta_path.value();
Ken Mixteree849c52010-09-30 15:30:10 -0700418 }
419}
Thieu Le1652fb22011-03-03 12:14:43 -0800420
421bool CrashCollector::IsCrashTestInProgress() {
Mike Frysingera557c112014-02-05 22:55:39 -0500422 return base::PathExists(FilePath(kCrashTestInProgressPath));
Thieu Le1652fb22011-03-03 12:14:43 -0800423}
Michael Krebs4fe30db2011-08-05 13:54:52 -0700424
425bool CrashCollector::IsDeveloperImage() {
426 // If we're testing crash reporter itself, we don't want to special-case
427 // for developer images.
428 if (IsCrashTestInProgress())
429 return false;
Mike Frysingera557c112014-02-05 22:55:39 -0500430 return base::PathExists(FilePath(kLeaveCoreFile));
Michael Krebs4fe30db2011-08-05 13:54:52 -0700431}