blob: 55fdd57f8ab7810b7f49f8b4f6f297d6e6c2d75f [file] [log] [blame]
Simon Quef70060c2012-04-09 19:07:07 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Ken Mixter03403162010-08-18 15:23:16 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "crash-reporter/crash_collector.h"
6
Ken Mixter04ec10f2010-08-26 16:02:02 -07007#include <dirent.h>
Ken Mixter9b346472010-11-07 13:45:45 -08008#include <fcntl.h> // For file creation modes.
Ken Mixter03403162010-08-18 15:23:16 -07009#include <pwd.h> // For struct passwd.
10#include <sys/types.h> // for mode_t.
Ken Mixter9b346472010-11-07 13:45:45 -080011#include <sys/wait.h> // For waitpid.
12#include <unistd.h> // For execv and fork.
Mike Frysinger65b4c1e2011-09-21 12:41:29 -040013#define __STDC_FORMAT_MACROS // PRId64
14#include <inttypes.h>
Ken Mixter03403162010-08-18 15:23:16 -070015
Ken Mixteree849c52010-09-30 15:30:10 -070016#include <set>
Simon Quef70060c2012-04-09 19:07:07 -070017#include <vector>
Ken Mixteree849c52010-09-30 15:30:10 -070018
Mike Frysingerf19b5182013-05-17 19:36:47 -040019#include <dbus/dbus-glib-lowlevel.h>
20#include <glib.h>
21
Ken Mixter03403162010-08-18 15:23:16 -070022#include "base/file_util.h"
23#include "base/logging.h"
Mike Frysinger1a8780d2013-02-14 22:39:57 -050024#include "base/posix/eintr_wrapper.h"
Chris Masone8a68c7c2011-05-14 11:44:04 -070025#include "base/string_split.h"
Ken Mixter03403162010-08-18 15:23:16 -070026#include "base/string_util.h"
Mike Frysinger57b261c2012-04-11 14:47:09 -040027#include "base/stringprintf.h"
Mike Frysingerf19b5182013-05-17 19:36:47 -040028#include "chromeos/cryptohome.h"
29#include "chromeos/dbus/dbus.h"
30#include "chromeos/dbus/service_constants.h"
Ken Mixtera3249322011-03-03 08:47:38 -080031#include "chromeos/process.h"
Ken Mixter03403162010-08-18 15:23:16 -070032
Michael Krebs4fe30db2011-08-05 13:54:52 -070033static const char kCollectChromeFile[] =
34 "/mnt/stateful_partition/etc/collect_chrome_crashes";
35static const char kCrashTestInProgressPath[] = "/tmp/crash-test-in-progress";
Simon Quef70060c2012-04-09 19:07:07 -070036static const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf";
Ken Mixter03403162010-08-18 15:23:16 -070037static const char kDefaultUserName[] = "chronos";
Michael Krebs4fe30db2011-08-05 13:54:52 -070038static const char kLeaveCoreFile[] = "/root/.leave_core";
Ken Mixteree849c52010-09-30 15:30:10 -070039static const char kLsbRelease[] = "/etc/lsb-release";
Ken Mixterc49dbd42010-12-14 17:44:11 -080040static const char kShellPath[] = "/bin/sh";
Ken Mixter03403162010-08-18 15:23:16 -070041static const char kSystemCrashPath[] = "/var/spool/crash";
Mike Frysingerf19b5182013-05-17 19:36:47 -040042// Normally this path is not used. Unfortunately, there are a few edge cases
43// where we need this. Any process that runs as kDefaultUserName that crashes
44// is consider a "user crash". That includes the initial Chrome browser that
45// runs the login screen. If that blows up, there is no logged in user yet,
46// so there is no per-user dir for us to stash things in. Instead we fallback
47// to this path as it is at least encrypted on a per-system basis.
48//
49// This also comes up when running autotests. The GUI is sitting at the login
50// screen while tests are sshing in, changing users, and triggering crashes as
51// the user (purposefully).
52static const char kFallbackUserCrashPath[] = "/home/chronos/crash";
Ken Mixter03403162010-08-18 15:23:16 -070053
54// Directory mode of the user crash spool directory.
55static const mode_t kUserCrashPathMode = 0755;
56
57// Directory mode of the system crash spool directory.
58static const mode_t kSystemCrashPathMode = 01755;
59
60static const uid_t kRootOwner = 0;
61static const uid_t kRootGroup = 0;
62
Ken Mixterda5db7a2010-09-17 13:50:42 -070063// Maximum crash reports per crash spool directory. Note that this is
64// a separate maximum from the maximum rate at which we upload these
65// diagnostics. The higher this rate is, the more space we allow for
66// core files, minidumps, and kcrash logs, and equivalently the more
67// processor and I/O bandwidth we dedicate to handling these crashes when
68// many occur at once. Also note that if core files are configured to
69// be left on the file system, we stop adding crashes when either the
70// number of core files or minidumps reaches this number.
71const int CrashCollector::kMaxCrashDirectorySize = 32;
Ken Mixter04ec10f2010-08-26 16:02:02 -070072
Simon Que9f90aca2013-02-19 17:19:52 -080073using base::FilePath;
74
Ken Mixterafcf8082010-10-26 14:45:01 -070075CrashCollector::CrashCollector()
76 : forced_crash_directory_(NULL),
Simon Queacc79382012-05-04 18:10:09 -070077 lsb_release_(kLsbRelease),
78 log_config_path_(kDefaultLogConfig) {
Ken Mixter03403162010-08-18 15:23:16 -070079}
80
81CrashCollector::~CrashCollector() {
82}
83
84void CrashCollector::Initialize(
85 CrashCollector::CountCrashFunction count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080086 CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) {
Ken Mixter03403162010-08-18 15:23:16 -070087 CHECK(count_crash_function != NULL);
88 CHECK(is_feedback_allowed_function != NULL);
Ken Mixter03403162010-08-18 15:23:16 -070089
90 count_crash_function_ = count_crash_function;
91 is_feedback_allowed_function_ = is_feedback_allowed_function;
Ken Mixter03403162010-08-18 15:23:16 -070092}
93
Ken Mixter9b346472010-11-07 13:45:45 -080094int CrashCollector::WriteNewFile(const FilePath &filename,
95 const char *data,
96 int size) {
97 int fd = HANDLE_EINTR(open(filename.value().c_str(),
98 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666));
99 if (fd < 0) {
100 return -1;
101 }
102
103 int rv = file_util::WriteFileDescriptor(fd, data, size);
104 HANDLE_EINTR(close(fd));
105 return rv;
106}
107
Ken Mixteree849c52010-09-30 15:30:10 -0700108std::string CrashCollector::Sanitize(const std::string &name) {
109 std::string result = name;
110 for (size_t i = 0; i < name.size(); ++i) {
111 if (!isalnum(result[i]) && result[i] != '_')
112 result[i] = '_';
113 }
114 return result;
115}
116
Ken Mixter03403162010-08-18 15:23:16 -0700117std::string CrashCollector::FormatDumpBasename(const std::string &exec_name,
118 time_t timestamp,
119 pid_t pid) {
120 struct tm tm;
121 localtime_r(&timestamp, &tm);
Ken Mixteree849c52010-09-30 15:30:10 -0700122 std::string sanitized_exec_name = Sanitize(exec_name);
Ken Mixter03403162010-08-18 15:23:16 -0700123 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d",
Ken Mixteree849c52010-09-30 15:30:10 -0700124 sanitized_exec_name.c_str(),
Ken Mixter03403162010-08-18 15:23:16 -0700125 tm.tm_year + 1900,
126 tm.tm_mon + 1,
127 tm.tm_mday,
128 tm.tm_hour,
129 tm.tm_min,
130 tm.tm_sec,
131 pid);
132}
133
Ken Mixter207694d2010-10-28 15:42:37 -0700134FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory,
135 const std::string &basename,
136 const std::string &extension) {
137 return crash_directory.Append(StringPrintf("%s.%s",
138 basename.c_str(),
139 extension.c_str()));
140}
141
Mike Frysingerf19b5182013-05-17 19:36:47 -0400142namespace {
143
144const char *GetGErrorMessage(const GError *error) {
145 if (!error)
146 return "Unknown error.";
147 return error->message;
148}
149
150}
151
152GHashTable *CrashCollector::GetActiveUserSessions(void) {
153 GHashTable *active_sessions = NULL;
154
155 chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection();
156 if (!dbus.HasConnection()) {
157 LOG(ERROR) << "Error connecting to system D-Bus";
158 return active_sessions;
159 }
160 chromeos::dbus::Proxy proxy(dbus,
161 login_manager::kSessionManagerServiceName,
162 login_manager::kSessionManagerServicePath,
163 login_manager::kSessionManagerInterface);
164 if (!proxy) {
165 LOG(ERROR) << "Error creating D-Bus proxy to interface "
166 << "'" << login_manager::kSessionManagerServiceName << "'";
167 return active_sessions;
168 }
169
170 // Request all the active sessions.
171 GError *gerror = NULL;
172 if (!dbus_g_proxy_call(proxy.gproxy(),
173 login_manager::kSessionManagerRetrieveActiveSessions,
174 &gerror, G_TYPE_INVALID,
175 DBUS_TYPE_G_STRING_STRING_HASHTABLE, &active_sessions,
176 G_TYPE_INVALID)) {
177 LOG(ERROR) << "Error performing D-Bus proxy call "
178 << "'"
179 << login_manager::kSessionManagerRetrieveActiveSessions << "'"
180 << ": " << GetGErrorMessage(gerror);
181 return active_sessions;
182 }
183
184 return active_sessions;
185}
186
187FilePath CrashCollector::GetUserCrashPath(void) {
188 // In this multiprofile world, there is no one-specific user dir anymore.
189 // Ask the session manager for the active ones, then just run with the
190 // first result we get back.
191 FilePath user_path = FilePath(kFallbackUserCrashPath);
192 GHashTable *active_sessions = GetActiveUserSessions();
193 if (!active_sessions)
194 return user_path;
195
196 GList *list = g_hash_table_get_values(active_sessions);
197 if (list) {
198 const char *salted_path = static_cast<const char *>(list->data);
199 user_path = chromeos::cryptohome::home::GetHashedUserPath(salted_path);
200 g_list_free(list);
201 }
202
203 g_hash_table_destroy(active_sessions);
204
205 return user_path;
206}
207
Ken Mixter03403162010-08-18 15:23:16 -0700208FilePath CrashCollector::GetCrashDirectoryInfo(
209 uid_t process_euid,
210 uid_t default_user_id,
211 gid_t default_user_group,
212 mode_t *mode,
213 uid_t *directory_owner,
214 gid_t *directory_group) {
Michael Krebs4fe30db2011-08-05 13:54:52 -0700215 // TODO(mkrebs): This can go away once Chrome crashes are handled
216 // normally (see crosbug.com/5872).
217 // Check if the user crash directory should be used. If we are
218 // collecting chrome crashes during autotesting, we want to put them in
219 // the system crash directory so they are outside the cryptohome -- in
220 // case we are being run during logout (see crosbug.com/18637).
221 if (process_euid == default_user_id && IsUserSpecificDirectoryEnabled()) {
Ken Mixter03403162010-08-18 15:23:16 -0700222 *mode = kUserCrashPathMode;
223 *directory_owner = default_user_id;
224 *directory_group = default_user_group;
Mike Frysingerf19b5182013-05-17 19:36:47 -0400225 return GetUserCrashPath();
Ken Mixter03403162010-08-18 15:23:16 -0700226 } else {
227 *mode = kSystemCrashPathMode;
228 *directory_owner = kRootOwner;
229 *directory_group = kRootGroup;
230 return FilePath(kSystemCrashPath);
231 }
232}
233
234bool CrashCollector::GetUserInfoFromName(const std::string &name,
235 uid_t *uid,
236 gid_t *gid) {
237 char storage[256];
238 struct passwd passwd_storage;
239 struct passwd *passwd_result = NULL;
240
241 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage),
242 &passwd_result) != 0 || passwd_result == NULL) {
Ken Mixtera3249322011-03-03 08:47:38 -0800243 LOG(ERROR) << "Cannot find user named " << name;
Ken Mixter03403162010-08-18 15:23:16 -0700244 return false;
245 }
246
247 *uid = passwd_result->pw_uid;
248 *gid = passwd_result->pw_gid;
249 return true;
250}
251
252bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid,
Ken Mixter207694d2010-10-28 15:42:37 -0700253 FilePath *crash_directory,
254 bool *out_of_capacity) {
Ken Mixter03403162010-08-18 15:23:16 -0700255 uid_t default_user_id;
256 gid_t default_user_group;
257
Ken Mixter207694d2010-10-28 15:42:37 -0700258 if (out_of_capacity != NULL) *out_of_capacity = false;
259
Ken Mixter03403162010-08-18 15:23:16 -0700260 // For testing.
261 if (forced_crash_directory_ != NULL) {
262 *crash_directory = FilePath(forced_crash_directory_);
263 return true;
264 }
265
266 if (!GetUserInfoFromName(kDefaultUserName,
267 &default_user_id,
268 &default_user_group)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800269 LOG(ERROR) << "Could not find default user info";
Ken Mixter03403162010-08-18 15:23:16 -0700270 return false;
271 }
272 mode_t directory_mode;
273 uid_t directory_owner;
274 gid_t directory_group;
275 *crash_directory =
276 GetCrashDirectoryInfo(euid,
277 default_user_id,
278 default_user_group,
279 &directory_mode,
280 &directory_owner,
281 &directory_group);
282
283 if (!file_util::PathExists(*crash_directory)) {
284 // Create the spool directory with the appropriate mode (regardless of
285 // umask) and ownership.
286 mode_t old_mask = umask(0);
287 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 ||
288 chown(crash_directory->value().c_str(),
289 directory_owner,
290 directory_group) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800291 LOG(ERROR) << "Unable to create appropriate crash directory";
Ken Mixter03403162010-08-18 15:23:16 -0700292 return false;
293 }
294 umask(old_mask);
295 }
296
297 if (!file_util::PathExists(*crash_directory)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800298 LOG(ERROR) << "Unable to create crash directory "
299 << crash_directory->value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700300 return false;
301 }
302
Ken Mixter04ec10f2010-08-26 16:02:02 -0700303 if (!CheckHasCapacity(*crash_directory)) {
Ken Mixter207694d2010-10-28 15:42:37 -0700304 if (out_of_capacity != NULL) *out_of_capacity = true;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700305 return false;
306 }
307
Ken Mixter03403162010-08-18 15:23:16 -0700308 return true;
309}
Ken Mixter04ec10f2010-08-26 16:02:02 -0700310
Albert Chaulk426fcc02013-05-02 15:38:31 -0700311FilePath CrashCollector::GetProcessPath(pid_t pid) {
312 return FilePath(StringPrintf("/proc/%d", pid));
313}
314
315bool CrashCollector::GetSymlinkTarget(const FilePath &symlink,
316 FilePath *target) {
317 int max_size = 32;
318 scoped_array<char> buffer;
319 while (true) {
320 buffer.reset(new char[max_size + 1]);
321 ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size);
322 if (size < 0) {
323 int saved_errno = errno;
324 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
325 << saved_errno;
326 return false;
327 }
328 buffer[size] = 0;
329 if (size == max_size) {
330 // Avoid overflow when doubling.
331 if (max_size * 2 > max_size) {
332 max_size *= 2;
333 continue;
334 } else {
335 return false;
336 }
337 }
338 break;
339 }
340
341 *target = FilePath(buffer.get());
342 return true;
343}
344
345bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid,
346 std::string *base_name) {
347 FilePath target;
348 FilePath process_path = GetProcessPath(pid);
349 FilePath exe_path = process_path.Append("exe");
350 if (!GetSymlinkTarget(exe_path, &target)) {
351 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
352 << " DirectoryExists: "
353 << file_util::DirectoryExists(process_path);
354 // Try to further diagnose exe readlink failure cause.
355 struct stat buf;
356 int stat_result = stat(exe_path.value().c_str(), &buf);
357 int saved_errno = errno;
358 if (stat_result < 0) {
359 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
360 << " " << saved_errno;
361 } else {
362 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
363 << buf.st_mode;
364 }
365 return false;
366 }
367 *base_name = target.BaseName().value();
368 return true;
369}
370
Ken Mixter04ec10f2010-08-26 16:02:02 -0700371// Return true if the given crash directory has not already reached
372// maximum capacity.
373bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) {
374 DIR* dir = opendir(crash_directory.value().c_str());
375 if (!dir) {
376 return false;
377 }
378 struct dirent ent_buf;
379 struct dirent* ent;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700380 bool full = false;
Ken Mixteree849c52010-09-30 15:30:10 -0700381 std::set<std::string> basenames;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700382 while (readdir_r(dir, &ent_buf, &ent) == 0 && ent != NULL) {
383 if ((strcmp(ent->d_name, ".") == 0) ||
384 (strcmp(ent->d_name, "..") == 0))
385 continue;
386
Ken Mixteree849c52010-09-30 15:30:10 -0700387 std::string filename(ent->d_name);
388 size_t last_dot = filename.rfind(".");
389 std::string basename;
390 // If there is a valid looking extension, use the base part of the
391 // name. If the only dot is the first byte (aka a dot file), treat
392 // it as unique to avoid allowing a directory full of dot files
393 // from accumulating.
394 if (last_dot != std::string::npos && last_dot != 0)
395 basename = filename.substr(0, last_dot);
396 else
397 basename = filename;
398 basenames.insert(basename);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700399
Ken Mixteree849c52010-09-30 15:30:10 -0700400 if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800401 LOG(WARNING) << "Crash directory " << crash_directory.value()
402 << " already full with " << kMaxCrashDirectorySize
403 << " pending reports";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700404 full = true;
405 break;
406 }
407 }
408 closedir(dir);
409 return !full;
410}
Ken Mixteree849c52010-09-30 15:30:10 -0700411
Ken Mixterc49dbd42010-12-14 17:44:11 -0800412bool CrashCollector::IsCommentLine(const std::string &line) {
413 size_t found = line.find_first_not_of(" ");
414 return found != std::string::npos && line[found] == '#';
415}
416
Ken Mixteree849c52010-09-30 15:30:10 -0700417bool CrashCollector::ReadKeyValueFile(
418 const FilePath &path,
419 const char separator,
420 std::map<std::string, std::string> *dictionary) {
421 std::string contents;
422 if (!file_util::ReadFileToString(path, &contents)) {
423 return false;
424 }
425 typedef std::vector<std::string> StringVector;
426 StringVector lines;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700427 base::SplitString(contents, '\n', &lines);
Ken Mixteree849c52010-09-30 15:30:10 -0700428 bool any_errors = false;
429 for (StringVector::iterator line = lines.begin(); line != lines.end();
430 ++line) {
431 // Allow empty strings.
432 if (line->empty())
433 continue;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800434 // Allow comment lines.
435 if (IsCommentLine(*line))
436 continue;
Ken Mixteree849c52010-09-30 15:30:10 -0700437 StringVector sides;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700438 base::SplitString(*line, separator, &sides);
Ken Mixteree849c52010-09-30 15:30:10 -0700439 if (sides.size() != 2) {
440 any_errors = true;
441 continue;
442 }
443 dictionary->insert(std::pair<std::string, std::string>(sides[0], sides[1]));
444 }
445 return !any_errors;
446}
447
Ken Mixterc49dbd42010-12-14 17:44:11 -0800448bool CrashCollector::GetLogContents(const FilePath &config_path,
449 const std::string &exec_name,
450 const FilePath &output_file) {
451 std::map<std::string, std::string> log_commands;
452 if (!ReadKeyValueFile(config_path, ':', &log_commands)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800453 LOG(INFO) << "Unable to read log configuration file "
454 << config_path.value();
Ken Mixterc49dbd42010-12-14 17:44:11 -0800455 return false;
456 }
457
458 if (log_commands.find(exec_name) == log_commands.end())
459 return false;
460
Ken Mixtera3249322011-03-03 08:47:38 -0800461 chromeos::ProcessImpl diag_process;
462 diag_process.AddArg(kShellPath);
Ken Mixterc49dbd42010-12-14 17:44:11 -0800463 std::string shell_command = log_commands[exec_name];
Ken Mixtera3249322011-03-03 08:47:38 -0800464 diag_process.AddStringOption("-c", shell_command);
465 diag_process.RedirectOutput(output_file.value());
Ken Mixterc49dbd42010-12-14 17:44:11 -0800466
Ken Mixtera3249322011-03-03 08:47:38 -0800467 int result = diag_process.Run();
468 if (result != 0) {
469 LOG(INFO) << "Running shell command " << shell_command << "failed with: "
470 << result;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800471 return false;
472 }
473 return true;
474}
475
Ken Mixterafcf8082010-10-26 14:45:01 -0700476void CrashCollector::AddCrashMetaData(const std::string &key,
477 const std::string &value) {
478 extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str()));
479}
480
Ken Mixteree849c52010-09-30 15:30:10 -0700481void CrashCollector::WriteCrashMetaData(const FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700482 const std::string &exec_name,
483 const std::string &payload_path) {
Ken Mixteree849c52010-09-30 15:30:10 -0700484 std::map<std::string, std::string> contents;
Ken Mixterafcf8082010-10-26 14:45:01 -0700485 if (!ReadKeyValueFile(FilePath(std::string(lsb_release_)), '=', &contents)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800486 LOG(ERROR) << "Problem parsing " << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700487 // Even though there was some failure, take as much as we could read.
488 }
489 std::string version("unknown");
490 std::map<std::string, std::string>::iterator i;
491 if ((i = contents.find("CHROMEOS_RELEASE_VERSION")) != contents.end()) {
492 version = i->second;
493 }
Ken Mixterc909b692010-10-18 12:26:05 -0700494 int64 payload_size = -1;
495 file_util::GetFileSize(FilePath(payload_path), &payload_size);
Ken Mixterafcf8082010-10-26 14:45:01 -0700496 std::string meta_data = StringPrintf("%sexec_name=%s\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700497 "ver=%s\n"
Ken Mixter207694d2010-10-28 15:42:37 -0700498 "payload=%s\n"
Mike Frysinger65b4c1e2011-09-21 12:41:29 -0400499 "payload_size=%"PRId64"\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700500 "done=1\n",
Ken Mixterafcf8082010-10-26 14:45:01 -0700501 extra_metadata_.c_str(),
Ken Mixteree849c52010-09-30 15:30:10 -0700502 exec_name.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700503 version.c_str(),
Ken Mixter207694d2010-10-28 15:42:37 -0700504 payload_path.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700505 payload_size);
Ken Mixter9b346472010-11-07 13:45:45 -0800506 // We must use WriteNewFile instead of file_util::WriteFile as we
507 // do not want to write with root access to a symlink that an attacker
508 // might have created.
509 if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800510 LOG(ERROR) << "Unable to write " << meta_path.value();
Ken Mixteree849c52010-09-30 15:30:10 -0700511 }
512}
Thieu Le1652fb22011-03-03 12:14:43 -0800513
514bool CrashCollector::IsCrashTestInProgress() {
515 return file_util::PathExists(FilePath(kCrashTestInProgressPath));
516}
Michael Krebs4fe30db2011-08-05 13:54:52 -0700517
518bool CrashCollector::IsDeveloperImage() {
519 // If we're testing crash reporter itself, we don't want to special-case
520 // for developer images.
521 if (IsCrashTestInProgress())
522 return false;
523 return file_util::PathExists(FilePath(kLeaveCoreFile));
524}
525
526bool CrashCollector::ShouldHandleChromeCrashes() {
527 // If we're testing crash reporter itself, we don't want to allow an
528 // override for chrome crashes. And, let's be conservative and only
529 // allow an override for developer images.
530 if (!IsCrashTestInProgress() && IsDeveloperImage()) {
531 // Check if there's an override to indicate we should indeed collect
532 // chrome crashes. This allows the crashes to still be tracked when
533 // they occur in autotests. See "crosbug.com/17987".
534 if (file_util::PathExists(FilePath(kCollectChromeFile)))
535 return true;
536 }
537 // We default to ignoring chrome crashes.
538 return false;
539}
540
541bool CrashCollector::IsUserSpecificDirectoryEnabled() {
542 return !ShouldHandleChromeCrashes();
543}