blob: 152aec32300d4b19e5fe92556b74f3fa1bda7671 [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.
Ben Chan7e776902014-06-18 13:19:51 -070013#define __STDC_FORMAT_MACROS // PRId64
Mike Frysinger65b4c1e2011-09-21 12:41:29 -040014#include <inttypes.h>
Ken Mixter03403162010-08-18 15:23:16 -070015
Ken Mixteree849c52010-09-30 15:30:10 -070016#include <set>
Ben Chan7e776902014-06-18 13:19:51 -070017#include <utility>
Simon Quef70060c2012-04-09 19:07:07 -070018#include <vector>
Ken Mixteree849c52010-09-30 15:30:10 -070019
Mike Frysingerf19b5182013-05-17 19:36:47 -040020#include <dbus/dbus-glib-lowlevel.h>
21#include <glib.h>
22
Ben Chan7e776902014-06-18 13:19:51 -070023#include <base/file_util.h>
24#include <base/logging.h>
25#include <base/posix/eintr_wrapper.h>
26#include <base/strings/string_split.h>
27#include <base/strings/string_util.h>
28#include <base/strings/stringprintf.h>
29#include <chromeos/cryptohome.h>
30#include <chromeos/dbus/dbus.h>
31#include <chromeos/dbus/service_constants.h>
32#include <chromeos/process.h>
Ken Mixter03403162010-08-18 15:23:16 -070033
Michael Krebs4fe30db2011-08-05 13:54:52 -070034static const char kCollectChromeFile[] =
35 "/mnt/stateful_partition/etc/collect_chrome_crashes";
36static const char kCrashTestInProgressPath[] = "/tmp/crash-test-in-progress";
Simon Quef70060c2012-04-09 19:07:07 -070037static const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf";
Ken Mixter03403162010-08-18 15:23:16 -070038static const char kDefaultUserName[] = "chronos";
Michael Krebs4fe30db2011-08-05 13:54:52 -070039static const char kLeaveCoreFile[] = "/root/.leave_core";
Ken Mixteree849c52010-09-30 15:30:10 -070040static const char kLsbRelease[] = "/etc/lsb-release";
Ken Mixterc49dbd42010-12-14 17:44:11 -080041static const char kShellPath[] = "/bin/sh";
Ken Mixter03403162010-08-18 15:23:16 -070042static const char kSystemCrashPath[] = "/var/spool/crash";
Albert Chaulk33dfd472013-06-19 15:34:13 -070043static const char kUploadVarPrefix[] = "upload_var_";
44static const char kUploadFilePrefix[] = "upload_file_";
Mike Frysingerf19b5182013-05-17 19:36:47 -040045// Normally this path is not used. Unfortunately, there are a few edge cases
46// where we need this. Any process that runs as kDefaultUserName that crashes
47// is consider a "user crash". That includes the initial Chrome browser that
48// runs the login screen. If that blows up, there is no logged in user yet,
49// so there is no per-user dir for us to stash things in. Instead we fallback
50// to this path as it is at least encrypted on a per-system basis.
51//
52// This also comes up when running autotests. The GUI is sitting at the login
53// screen while tests are sshing in, changing users, and triggering crashes as
54// the user (purposefully).
55static const char kFallbackUserCrashPath[] = "/home/chronos/crash";
Ken Mixter03403162010-08-18 15:23:16 -070056
57// Directory mode of the user crash spool directory.
58static const mode_t kUserCrashPathMode = 0755;
59
60// Directory mode of the system crash spool directory.
61static const mode_t kSystemCrashPathMode = 01755;
62
63static const uid_t kRootOwner = 0;
64static const uid_t kRootGroup = 0;
65
Ken Mixterda5db7a2010-09-17 13:50:42 -070066// Maximum crash reports per crash spool directory. Note that this is
67// a separate maximum from the maximum rate at which we upload these
68// diagnostics. The higher this rate is, the more space we allow for
69// core files, minidumps, and kcrash logs, and equivalently the more
70// processor and I/O bandwidth we dedicate to handling these crashes when
71// many occur at once. Also note that if core files are configured to
72// be left on the file system, we stop adding crashes when either the
73// number of core files or minidumps reaches this number.
74const int CrashCollector::kMaxCrashDirectorySize = 32;
Ken Mixter04ec10f2010-08-26 16:02:02 -070075
Simon Que9f90aca2013-02-19 17:19:52 -080076using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050077using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080078
Ken Mixterafcf8082010-10-26 14:45:01 -070079CrashCollector::CrashCollector()
Lei Zhang9b1f3002014-04-24 02:10:57 -070080 : lsb_release_(kLsbRelease),
Simon Queacc79382012-05-04 18:10:09 -070081 log_config_path_(kDefaultLogConfig) {
Ken Mixter03403162010-08-18 15:23:16 -070082}
83
84CrashCollector::~CrashCollector() {
85}
86
87void CrashCollector::Initialize(
88 CrashCollector::CountCrashFunction count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080089 CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) {
Ken Mixter03403162010-08-18 15:23:16 -070090 CHECK(count_crash_function != NULL);
91 CHECK(is_feedback_allowed_function != NULL);
Ken Mixter03403162010-08-18 15:23:16 -070092
93 count_crash_function_ = count_crash_function;
94 is_feedback_allowed_function_ = is_feedback_allowed_function;
Ken Mixter03403162010-08-18 15:23:16 -070095}
96
Ken Mixter9b346472010-11-07 13:45:45 -080097int CrashCollector::WriteNewFile(const FilePath &filename,
98 const char *data,
99 int size) {
100 int fd = HANDLE_EINTR(open(filename.value().c_str(),
101 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666));
102 if (fd < 0) {
103 return -1;
104 }
105
Ben Chanf30c6412014-05-22 23:09:01 -0700106 int rv = base::WriteFileDescriptor(fd, data, size);
Mike Frysingerf1a50142014-05-14 16:05:09 -0400107 IGNORE_EINTR(close(fd));
Ken Mixter9b346472010-11-07 13:45:45 -0800108 return rv;
109}
110
Ken Mixteree849c52010-09-30 15:30:10 -0700111std::string CrashCollector::Sanitize(const std::string &name) {
Thiemo Nagel98950962014-05-13 19:48:32 +0200112 // Make sure the sanitized name does not include any periods.
113 // The logic in crash_sender relies on this.
Ken Mixteree849c52010-09-30 15:30:10 -0700114 std::string result = name;
115 for (size_t i = 0; i < name.size(); ++i) {
116 if (!isalnum(result[i]) && result[i] != '_')
117 result[i] = '_';
118 }
119 return result;
120}
121
Ken Mixter03403162010-08-18 15:23:16 -0700122std::string CrashCollector::FormatDumpBasename(const std::string &exec_name,
123 time_t timestamp,
124 pid_t pid) {
125 struct tm tm;
126 localtime_r(&timestamp, &tm);
Ken Mixteree849c52010-09-30 15:30:10 -0700127 std::string sanitized_exec_name = Sanitize(exec_name);
Ken Mixter03403162010-08-18 15:23:16 -0700128 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d",
Ken Mixteree849c52010-09-30 15:30:10 -0700129 sanitized_exec_name.c_str(),
Ken Mixter03403162010-08-18 15:23:16 -0700130 tm.tm_year + 1900,
131 tm.tm_mon + 1,
132 tm.tm_mday,
133 tm.tm_hour,
134 tm.tm_min,
135 tm.tm_sec,
136 pid);
137}
138
Ken Mixter207694d2010-10-28 15:42:37 -0700139FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory,
140 const std::string &basename,
141 const std::string &extension) {
142 return crash_directory.Append(StringPrintf("%s.%s",
143 basename.c_str(),
144 extension.c_str()));
145}
146
Mike Frysingerf19b5182013-05-17 19:36:47 -0400147namespace {
148
149const char *GetGErrorMessage(const GError *error) {
150 if (!error)
151 return "Unknown error.";
152 return error->message;
153}
154
155}
156
157GHashTable *CrashCollector::GetActiveUserSessions(void) {
158 GHashTable *active_sessions = NULL;
159
160 chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection();
161 if (!dbus.HasConnection()) {
162 LOG(ERROR) << "Error connecting to system D-Bus";
163 return active_sessions;
164 }
165 chromeos::dbus::Proxy proxy(dbus,
166 login_manager::kSessionManagerServiceName,
167 login_manager::kSessionManagerServicePath,
168 login_manager::kSessionManagerInterface);
169 if (!proxy) {
170 LOG(ERROR) << "Error creating D-Bus proxy to interface "
171 << "'" << login_manager::kSessionManagerServiceName << "'";
172 return active_sessions;
173 }
174
175 // Request all the active sessions.
176 GError *gerror = NULL;
177 if (!dbus_g_proxy_call(proxy.gproxy(),
178 login_manager::kSessionManagerRetrieveActiveSessions,
179 &gerror, G_TYPE_INVALID,
180 DBUS_TYPE_G_STRING_STRING_HASHTABLE, &active_sessions,
181 G_TYPE_INVALID)) {
182 LOG(ERROR) << "Error performing D-Bus proxy call "
183 << "'"
184 << login_manager::kSessionManagerRetrieveActiveSessions << "'"
185 << ": " << GetGErrorMessage(gerror);
186 return active_sessions;
187 }
188
189 return active_sessions;
190}
191
192FilePath CrashCollector::GetUserCrashPath(void) {
193 // In this multiprofile world, there is no one-specific user dir anymore.
194 // Ask the session manager for the active ones, then just run with the
195 // first result we get back.
196 FilePath user_path = FilePath(kFallbackUserCrashPath);
197 GHashTable *active_sessions = GetActiveUserSessions();
198 if (!active_sessions)
199 return user_path;
200
201 GList *list = g_hash_table_get_values(active_sessions);
202 if (list) {
203 const char *salted_path = static_cast<const char *>(list->data);
Mike Frysinger37843a92013-06-11 17:03:59 -0400204 user_path = chromeos::cryptohome::home::GetHashedUserPath(salted_path)
205 .Append("crash");
Mike Frysingerf19b5182013-05-17 19:36:47 -0400206 g_list_free(list);
207 }
208
209 g_hash_table_destroy(active_sessions);
210
211 return user_path;
212}
213
Ken Mixter03403162010-08-18 15:23:16 -0700214FilePath CrashCollector::GetCrashDirectoryInfo(
215 uid_t process_euid,
216 uid_t default_user_id,
217 gid_t default_user_group,
218 mode_t *mode,
219 uid_t *directory_owner,
220 gid_t *directory_group) {
Michael Krebs4fe30db2011-08-05 13:54:52 -0700221 // TODO(mkrebs): This can go away once Chrome crashes are handled
222 // normally (see crosbug.com/5872).
223 // Check if the user crash directory should be used. If we are
224 // collecting chrome crashes during autotesting, we want to put them in
225 // the system crash directory so they are outside the cryptohome -- in
226 // case we are being run during logout (see crosbug.com/18637).
227 if (process_euid == default_user_id && IsUserSpecificDirectoryEnabled()) {
Ken Mixter03403162010-08-18 15:23:16 -0700228 *mode = kUserCrashPathMode;
229 *directory_owner = default_user_id;
230 *directory_group = default_user_group;
Mike Frysingerf19b5182013-05-17 19:36:47 -0400231 return GetUserCrashPath();
Ken Mixter03403162010-08-18 15:23:16 -0700232 } else {
233 *mode = kSystemCrashPathMode;
234 *directory_owner = kRootOwner;
235 *directory_group = kRootGroup;
236 return FilePath(kSystemCrashPath);
237 }
238}
239
240bool CrashCollector::GetUserInfoFromName(const std::string &name,
241 uid_t *uid,
242 gid_t *gid) {
243 char storage[256];
244 struct passwd passwd_storage;
245 struct passwd *passwd_result = NULL;
246
247 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage),
248 &passwd_result) != 0 || passwd_result == NULL) {
Ken Mixtera3249322011-03-03 08:47:38 -0800249 LOG(ERROR) << "Cannot find user named " << name;
Ken Mixter03403162010-08-18 15:23:16 -0700250 return false;
251 }
252
253 *uid = passwd_result->pw_uid;
254 *gid = passwd_result->pw_gid;
255 return true;
256}
257
258bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid,
Ken Mixter207694d2010-10-28 15:42:37 -0700259 FilePath *crash_directory,
260 bool *out_of_capacity) {
Ken Mixter03403162010-08-18 15:23:16 -0700261 uid_t default_user_id;
262 gid_t default_user_group;
263
Ken Mixter207694d2010-10-28 15:42:37 -0700264 if (out_of_capacity != NULL) *out_of_capacity = false;
265
Ken Mixter03403162010-08-18 15:23:16 -0700266 // For testing.
Lei Zhang9b1f3002014-04-24 02:10:57 -0700267 if (!forced_crash_directory_.empty()) {
268 *crash_directory = forced_crash_directory_;
Ken Mixter03403162010-08-18 15:23:16 -0700269 return true;
270 }
271
272 if (!GetUserInfoFromName(kDefaultUserName,
273 &default_user_id,
274 &default_user_group)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800275 LOG(ERROR) << "Could not find default user info";
Ken Mixter03403162010-08-18 15:23:16 -0700276 return false;
277 }
278 mode_t directory_mode;
279 uid_t directory_owner;
280 gid_t directory_group;
281 *crash_directory =
282 GetCrashDirectoryInfo(euid,
283 default_user_id,
284 default_user_group,
285 &directory_mode,
286 &directory_owner,
287 &directory_group);
288
Mike Frysingera557c112014-02-05 22:55:39 -0500289 if (!base::PathExists(*crash_directory)) {
Ken Mixter03403162010-08-18 15:23:16 -0700290 // Create the spool directory with the appropriate mode (regardless of
291 // umask) and ownership.
292 mode_t old_mask = umask(0);
293 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 ||
294 chown(crash_directory->value().c_str(),
295 directory_owner,
296 directory_group) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800297 LOG(ERROR) << "Unable to create appropriate crash directory";
Ken Mixter03403162010-08-18 15:23:16 -0700298 return false;
299 }
300 umask(old_mask);
301 }
302
Mike Frysingera557c112014-02-05 22:55:39 -0500303 if (!base::PathExists(*crash_directory)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800304 LOG(ERROR) << "Unable to create crash directory "
305 << crash_directory->value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700306 return false;
307 }
308
Ken Mixter04ec10f2010-08-26 16:02:02 -0700309 if (!CheckHasCapacity(*crash_directory)) {
Ken Mixter207694d2010-10-28 15:42:37 -0700310 if (out_of_capacity != NULL) *out_of_capacity = true;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700311 return false;
312 }
313
Ken Mixter03403162010-08-18 15:23:16 -0700314 return true;
315}
Ken Mixter04ec10f2010-08-26 16:02:02 -0700316
Albert Chaulk426fcc02013-05-02 15:38:31 -0700317FilePath CrashCollector::GetProcessPath(pid_t pid) {
318 return FilePath(StringPrintf("/proc/%d", pid));
319}
320
321bool CrashCollector::GetSymlinkTarget(const FilePath &symlink,
Ben Chan8563d202014-01-27 19:30:13 -0800322 FilePath *target) {
Albert Chaulk426fcc02013-05-02 15:38:31 -0700323 int max_size = 32;
Ben Chan8563d202014-01-27 19:30:13 -0800324 scoped_ptr<char[]> buffer;
Albert Chaulk426fcc02013-05-02 15:38:31 -0700325 while (true) {
326 buffer.reset(new char[max_size + 1]);
327 ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size);
328 if (size < 0) {
329 int saved_errno = errno;
330 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
331 << saved_errno;
332 return false;
333 }
334 buffer[size] = 0;
335 if (size == max_size) {
336 // Avoid overflow when doubling.
337 if (max_size * 2 > max_size) {
338 max_size *= 2;
339 continue;
340 } else {
341 return false;
342 }
343 }
344 break;
345 }
346
347 *target = FilePath(buffer.get());
348 return true;
349}
350
351bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid,
352 std::string *base_name) {
353 FilePath target;
354 FilePath process_path = GetProcessPath(pid);
355 FilePath exe_path = process_path.Append("exe");
356 if (!GetSymlinkTarget(exe_path, &target)) {
357 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
358 << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500359 << base::DirectoryExists(process_path);
Albert Chaulk426fcc02013-05-02 15:38:31 -0700360 // Try to further diagnose exe readlink failure cause.
361 struct stat buf;
362 int stat_result = stat(exe_path.value().c_str(), &buf);
363 int saved_errno = errno;
364 if (stat_result < 0) {
365 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
366 << " " << saved_errno;
367 } else {
368 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
369 << buf.st_mode;
370 }
371 return false;
372 }
373 *base_name = target.BaseName().value();
374 return true;
375}
376
Ken Mixter04ec10f2010-08-26 16:02:02 -0700377// Return true if the given crash directory has not already reached
378// maximum capacity.
379bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) {
380 DIR* dir = opendir(crash_directory.value().c_str());
381 if (!dir) {
382 return false;
383 }
384 struct dirent ent_buf;
385 struct dirent* ent;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700386 bool full = false;
Ken Mixteree849c52010-09-30 15:30:10 -0700387 std::set<std::string> basenames;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700388 while (readdir_r(dir, &ent_buf, &ent) == 0 && ent != NULL) {
389 if ((strcmp(ent->d_name, ".") == 0) ||
390 (strcmp(ent->d_name, "..") == 0))
391 continue;
392
Ken Mixteree849c52010-09-30 15:30:10 -0700393 std::string filename(ent->d_name);
394 size_t last_dot = filename.rfind(".");
395 std::string basename;
396 // If there is a valid looking extension, use the base part of the
397 // name. If the only dot is the first byte (aka a dot file), treat
398 // it as unique to avoid allowing a directory full of dot files
399 // from accumulating.
400 if (last_dot != std::string::npos && last_dot != 0)
401 basename = filename.substr(0, last_dot);
402 else
403 basename = filename;
404 basenames.insert(basename);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700405
Ken Mixteree849c52010-09-30 15:30:10 -0700406 if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800407 LOG(WARNING) << "Crash directory " << crash_directory.value()
408 << " already full with " << kMaxCrashDirectorySize
409 << " pending reports";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700410 full = true;
411 break;
412 }
413 }
414 closedir(dir);
415 return !full;
416}
Ken Mixteree849c52010-09-30 15:30:10 -0700417
Ken Mixterc49dbd42010-12-14 17:44:11 -0800418bool CrashCollector::IsCommentLine(const std::string &line) {
419 size_t found = line.find_first_not_of(" ");
420 return found != std::string::npos && line[found] == '#';
421}
422
Ken Mixteree849c52010-09-30 15:30:10 -0700423bool CrashCollector::ReadKeyValueFile(
424 const FilePath &path,
425 const char separator,
426 std::map<std::string, std::string> *dictionary) {
427 std::string contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500428 if (!base::ReadFileToString(path, &contents)) {
Ken Mixteree849c52010-09-30 15:30:10 -0700429 return false;
430 }
431 typedef std::vector<std::string> StringVector;
432 StringVector lines;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700433 base::SplitString(contents, '\n', &lines);
Ken Mixteree849c52010-09-30 15:30:10 -0700434 bool any_errors = false;
435 for (StringVector::iterator line = lines.begin(); line != lines.end();
436 ++line) {
437 // Allow empty strings.
438 if (line->empty())
439 continue;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800440 // Allow comment lines.
441 if (IsCommentLine(*line))
442 continue;
Ken Mixteree849c52010-09-30 15:30:10 -0700443 StringVector sides;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700444 base::SplitString(*line, separator, &sides);
Ken Mixteree849c52010-09-30 15:30:10 -0700445 if (sides.size() != 2) {
446 any_errors = true;
447 continue;
448 }
449 dictionary->insert(std::pair<std::string, std::string>(sides[0], sides[1]));
450 }
451 return !any_errors;
452}
453
Ken Mixterc49dbd42010-12-14 17:44:11 -0800454bool CrashCollector::GetLogContents(const FilePath &config_path,
455 const std::string &exec_name,
456 const FilePath &output_file) {
457 std::map<std::string, std::string> log_commands;
458 if (!ReadKeyValueFile(config_path, ':', &log_commands)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800459 LOG(INFO) << "Unable to read log configuration file "
460 << config_path.value();
Ken Mixterc49dbd42010-12-14 17:44:11 -0800461 return false;
462 }
463
464 if (log_commands.find(exec_name) == log_commands.end())
465 return false;
466
Ken Mixtera3249322011-03-03 08:47:38 -0800467 chromeos::ProcessImpl diag_process;
468 diag_process.AddArg(kShellPath);
Ken Mixterc49dbd42010-12-14 17:44:11 -0800469 std::string shell_command = log_commands[exec_name];
Ken Mixtera3249322011-03-03 08:47:38 -0800470 diag_process.AddStringOption("-c", shell_command);
471 diag_process.RedirectOutput(output_file.value());
Ken Mixterc49dbd42010-12-14 17:44:11 -0800472
Ken Mixtera3249322011-03-03 08:47:38 -0800473 int result = diag_process.Run();
474 if (result != 0) {
475 LOG(INFO) << "Running shell command " << shell_command << "failed with: "
476 << result;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800477 return false;
478 }
479 return true;
480}
481
Ken Mixterafcf8082010-10-26 14:45:01 -0700482void CrashCollector::AddCrashMetaData(const std::string &key,
483 const std::string &value) {
484 extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str()));
485}
486
Albert Chaulk33dfd472013-06-19 15:34:13 -0700487void CrashCollector::AddCrashMetaUploadFile(const std::string &key,
488 const std::string &path) {
489 if (!path.empty())
490 AddCrashMetaData(kUploadFilePrefix + key, path);
491}
492
493void CrashCollector::AddCrashMetaUploadData(const std::string &key,
494 const std::string &value) {
495 if (!value.empty())
496 AddCrashMetaData(kUploadVarPrefix + key, value);
497}
498
Ken Mixteree849c52010-09-30 15:30:10 -0700499void CrashCollector::WriteCrashMetaData(const FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700500 const std::string &exec_name,
501 const std::string &payload_path) {
Ken Mixteree849c52010-09-30 15:30:10 -0700502 std::map<std::string, std::string> contents;
Lei Zhang9b1f3002014-04-24 02:10:57 -0700503 if (!ReadKeyValueFile(FilePath(lsb_release_), '=', &contents)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800504 LOG(ERROR) << "Problem parsing " << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700505 // Even though there was some failure, take as much as we could read.
506 }
507 std::string version("unknown");
508 std::map<std::string, std::string>::iterator i;
509 if ((i = contents.find("CHROMEOS_RELEASE_VERSION")) != contents.end()) {
510 version = i->second;
511 }
Ken Mixterc909b692010-10-18 12:26:05 -0700512 int64 payload_size = -1;
Mike Frysingera557c112014-02-05 22:55:39 -0500513 base::GetFileSize(FilePath(payload_path), &payload_size);
Ken Mixterafcf8082010-10-26 14:45:01 -0700514 std::string meta_data = StringPrintf("%sexec_name=%s\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700515 "ver=%s\n"
Ken Mixter207694d2010-10-28 15:42:37 -0700516 "payload=%s\n"
Mike Frysinger65b4c1e2011-09-21 12:41:29 -0400517 "payload_size=%"PRId64"\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700518 "done=1\n",
Ken Mixterafcf8082010-10-26 14:45:01 -0700519 extra_metadata_.c_str(),
Ken Mixteree849c52010-09-30 15:30:10 -0700520 exec_name.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700521 version.c_str(),
Ken Mixter207694d2010-10-28 15:42:37 -0700522 payload_path.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700523 payload_size);
Ben Chanf30c6412014-05-22 23:09:01 -0700524 // We must use WriteNewFile instead of base::WriteFile as we
Ken Mixter9b346472010-11-07 13:45:45 -0800525 // do not want to write with root access to a symlink that an attacker
526 // might have created.
527 if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800528 LOG(ERROR) << "Unable to write " << meta_path.value();
Ken Mixteree849c52010-09-30 15:30:10 -0700529 }
530}
Thieu Le1652fb22011-03-03 12:14:43 -0800531
532bool CrashCollector::IsCrashTestInProgress() {
Mike Frysingera557c112014-02-05 22:55:39 -0500533 return base::PathExists(FilePath(kCrashTestInProgressPath));
Thieu Le1652fb22011-03-03 12:14:43 -0800534}
Michael Krebs4fe30db2011-08-05 13:54:52 -0700535
536bool CrashCollector::IsDeveloperImage() {
537 // If we're testing crash reporter itself, we don't want to special-case
538 // for developer images.
539 if (IsCrashTestInProgress())
540 return false;
Mike Frysingera557c112014-02-05 22:55:39 -0500541 return base::PathExists(FilePath(kLeaveCoreFile));
Michael Krebs4fe30db2011-08-05 13:54:52 -0700542}
543
544bool CrashCollector::ShouldHandleChromeCrashes() {
545 // If we're testing crash reporter itself, we don't want to allow an
546 // override for chrome crashes. And, let's be conservative and only
547 // allow an override for developer images.
548 if (!IsCrashTestInProgress() && IsDeveloperImage()) {
549 // Check if there's an override to indicate we should indeed collect
550 // chrome crashes. This allows the crashes to still be tracked when
551 // they occur in autotests. See "crosbug.com/17987".
Mike Frysingera557c112014-02-05 22:55:39 -0500552 if (base::PathExists(FilePath(kCollectChromeFile)))
Michael Krebs4fe30db2011-08-05 13:54:52 -0700553 return true;
554 }
555 // We default to ignoring chrome crashes.
556 return false;
557}
558
559bool CrashCollector::IsUserSpecificDirectoryEnabled() {
560 return !ShouldHandleChromeCrashes();
561}