blob: 5ddff085cf6ae7c404e887f789212d5d700b41f7 [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"
Mike Frysingera557c112014-02-05 22:55:39 -050025#include "base/strings/string_split.h"
26#include "base/strings/string_util.h"
27#include "base/strings/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";
Albert Chaulk33dfd472013-06-19 15:34:13 -070042static const char kUploadVarPrefix[] = "upload_var_";
43static const char kUploadFilePrefix[] = "upload_file_";
Mike Frysingerf19b5182013-05-17 19:36:47 -040044// Normally this path is not used. Unfortunately, there are a few edge cases
45// where we need this. Any process that runs as kDefaultUserName that crashes
46// is consider a "user crash". That includes the initial Chrome browser that
47// runs the login screen. If that blows up, there is no logged in user yet,
48// so there is no per-user dir for us to stash things in. Instead we fallback
49// to this path as it is at least encrypted on a per-system basis.
50//
51// This also comes up when running autotests. The GUI is sitting at the login
52// screen while tests are sshing in, changing users, and triggering crashes as
53// the user (purposefully).
54static const char kFallbackUserCrashPath[] = "/home/chronos/crash";
Ken Mixter03403162010-08-18 15:23:16 -070055
56// Directory mode of the user crash spool directory.
57static const mode_t kUserCrashPathMode = 0755;
58
59// Directory mode of the system crash spool directory.
60static const mode_t kSystemCrashPathMode = 01755;
61
62static const uid_t kRootOwner = 0;
63static const uid_t kRootGroup = 0;
64
Ken Mixterda5db7a2010-09-17 13:50:42 -070065// Maximum crash reports per crash spool directory. Note that this is
66// a separate maximum from the maximum rate at which we upload these
67// diagnostics. The higher this rate is, the more space we allow for
68// core files, minidumps, and kcrash logs, and equivalently the more
69// processor and I/O bandwidth we dedicate to handling these crashes when
70// many occur at once. Also note that if core files are configured to
71// be left on the file system, we stop adding crashes when either the
72// number of core files or minidumps reaches this number.
73const int CrashCollector::kMaxCrashDirectorySize = 32;
Ken Mixter04ec10f2010-08-26 16:02:02 -070074
Simon Que9f90aca2013-02-19 17:19:52 -080075using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050076using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080077
Ken Mixterafcf8082010-10-26 14:45:01 -070078CrashCollector::CrashCollector()
Lei Zhang9b1f3002014-04-24 02:10:57 -070079 : lsb_release_(kLsbRelease),
Simon Queacc79382012-05-04 18:10:09 -070080 log_config_path_(kDefaultLogConfig) {
Ken Mixter03403162010-08-18 15:23:16 -070081}
82
83CrashCollector::~CrashCollector() {
84}
85
86void CrashCollector::Initialize(
87 CrashCollector::CountCrashFunction count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080088 CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) {
Ken Mixter03403162010-08-18 15:23:16 -070089 CHECK(count_crash_function != NULL);
90 CHECK(is_feedback_allowed_function != NULL);
Ken Mixter03403162010-08-18 15:23:16 -070091
92 count_crash_function_ = count_crash_function;
93 is_feedback_allowed_function_ = is_feedback_allowed_function;
Ken Mixter03403162010-08-18 15:23:16 -070094}
95
Ken Mixter9b346472010-11-07 13:45:45 -080096int CrashCollector::WriteNewFile(const FilePath &filename,
97 const char *data,
98 int size) {
99 int fd = HANDLE_EINTR(open(filename.value().c_str(),
100 O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666));
101 if (fd < 0) {
102 return -1;
103 }
104
105 int rv = file_util::WriteFileDescriptor(fd, data, size);
Mike Frysingerf1a50142014-05-14 16:05:09 -0400106 IGNORE_EINTR(close(fd));
Ken Mixter9b346472010-11-07 13:45:45 -0800107 return rv;
108}
109
Ken Mixteree849c52010-09-30 15:30:10 -0700110std::string CrashCollector::Sanitize(const std::string &name) {
Thiemo Nagel98950962014-05-13 19:48:32 +0200111 // Make sure the sanitized name does not include any periods.
112 // The logic in crash_sender relies on this.
Ken Mixteree849c52010-09-30 15:30:10 -0700113 std::string result = name;
114 for (size_t i = 0; i < name.size(); ++i) {
115 if (!isalnum(result[i]) && result[i] != '_')
116 result[i] = '_';
117 }
118 return result;
119}
120
Ken Mixter03403162010-08-18 15:23:16 -0700121std::string CrashCollector::FormatDumpBasename(const std::string &exec_name,
122 time_t timestamp,
123 pid_t pid) {
124 struct tm tm;
125 localtime_r(&timestamp, &tm);
Ken Mixteree849c52010-09-30 15:30:10 -0700126 std::string sanitized_exec_name = Sanitize(exec_name);
Ken Mixter03403162010-08-18 15:23:16 -0700127 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d",
Ken Mixteree849c52010-09-30 15:30:10 -0700128 sanitized_exec_name.c_str(),
Ken Mixter03403162010-08-18 15:23:16 -0700129 tm.tm_year + 1900,
130 tm.tm_mon + 1,
131 tm.tm_mday,
132 tm.tm_hour,
133 tm.tm_min,
134 tm.tm_sec,
135 pid);
136}
137
Ken Mixter207694d2010-10-28 15:42:37 -0700138FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory,
139 const std::string &basename,
140 const std::string &extension) {
141 return crash_directory.Append(StringPrintf("%s.%s",
142 basename.c_str(),
143 extension.c_str()));
144}
145
Mike Frysingerf19b5182013-05-17 19:36:47 -0400146namespace {
147
148const char *GetGErrorMessage(const GError *error) {
149 if (!error)
150 return "Unknown error.";
151 return error->message;
152}
153
154}
155
156GHashTable *CrashCollector::GetActiveUserSessions(void) {
157 GHashTable *active_sessions = NULL;
158
159 chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection();
160 if (!dbus.HasConnection()) {
161 LOG(ERROR) << "Error connecting to system D-Bus";
162 return active_sessions;
163 }
164 chromeos::dbus::Proxy proxy(dbus,
165 login_manager::kSessionManagerServiceName,
166 login_manager::kSessionManagerServicePath,
167 login_manager::kSessionManagerInterface);
168 if (!proxy) {
169 LOG(ERROR) << "Error creating D-Bus proxy to interface "
170 << "'" << login_manager::kSessionManagerServiceName << "'";
171 return active_sessions;
172 }
173
174 // Request all the active sessions.
175 GError *gerror = NULL;
176 if (!dbus_g_proxy_call(proxy.gproxy(),
177 login_manager::kSessionManagerRetrieveActiveSessions,
178 &gerror, G_TYPE_INVALID,
179 DBUS_TYPE_G_STRING_STRING_HASHTABLE, &active_sessions,
180 G_TYPE_INVALID)) {
181 LOG(ERROR) << "Error performing D-Bus proxy call "
182 << "'"
183 << login_manager::kSessionManagerRetrieveActiveSessions << "'"
184 << ": " << GetGErrorMessage(gerror);
185 return active_sessions;
186 }
187
188 return active_sessions;
189}
190
191FilePath CrashCollector::GetUserCrashPath(void) {
192 // In this multiprofile world, there is no one-specific user dir anymore.
193 // Ask the session manager for the active ones, then just run with the
194 // first result we get back.
195 FilePath user_path = FilePath(kFallbackUserCrashPath);
196 GHashTable *active_sessions = GetActiveUserSessions();
197 if (!active_sessions)
198 return user_path;
199
200 GList *list = g_hash_table_get_values(active_sessions);
201 if (list) {
202 const char *salted_path = static_cast<const char *>(list->data);
Mike Frysinger37843a92013-06-11 17:03:59 -0400203 user_path = chromeos::cryptohome::home::GetHashedUserPath(salted_path)
204 .Append("crash");
Mike Frysingerf19b5182013-05-17 19:36:47 -0400205 g_list_free(list);
206 }
207
208 g_hash_table_destroy(active_sessions);
209
210 return user_path;
211}
212
Ken Mixter03403162010-08-18 15:23:16 -0700213FilePath CrashCollector::GetCrashDirectoryInfo(
214 uid_t process_euid,
215 uid_t default_user_id,
216 gid_t default_user_group,
217 mode_t *mode,
218 uid_t *directory_owner,
219 gid_t *directory_group) {
Michael Krebs4fe30db2011-08-05 13:54:52 -0700220 // TODO(mkrebs): This can go away once Chrome crashes are handled
221 // normally (see crosbug.com/5872).
222 // Check if the user crash directory should be used. If we are
223 // collecting chrome crashes during autotesting, we want to put them in
224 // the system crash directory so they are outside the cryptohome -- in
225 // case we are being run during logout (see crosbug.com/18637).
226 if (process_euid == default_user_id && IsUserSpecificDirectoryEnabled()) {
Ken Mixter03403162010-08-18 15:23:16 -0700227 *mode = kUserCrashPathMode;
228 *directory_owner = default_user_id;
229 *directory_group = default_user_group;
Mike Frysingerf19b5182013-05-17 19:36:47 -0400230 return GetUserCrashPath();
Ken Mixter03403162010-08-18 15:23:16 -0700231 } else {
232 *mode = kSystemCrashPathMode;
233 *directory_owner = kRootOwner;
234 *directory_group = kRootGroup;
235 return FilePath(kSystemCrashPath);
236 }
237}
238
239bool CrashCollector::GetUserInfoFromName(const std::string &name,
240 uid_t *uid,
241 gid_t *gid) {
242 char storage[256];
243 struct passwd passwd_storage;
244 struct passwd *passwd_result = NULL;
245
246 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage),
247 &passwd_result) != 0 || passwd_result == NULL) {
Ken Mixtera3249322011-03-03 08:47:38 -0800248 LOG(ERROR) << "Cannot find user named " << name;
Ken Mixter03403162010-08-18 15:23:16 -0700249 return false;
250 }
251
252 *uid = passwd_result->pw_uid;
253 *gid = passwd_result->pw_gid;
254 return true;
255}
256
257bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid,
Ken Mixter207694d2010-10-28 15:42:37 -0700258 FilePath *crash_directory,
259 bool *out_of_capacity) {
Ken Mixter03403162010-08-18 15:23:16 -0700260 uid_t default_user_id;
261 gid_t default_user_group;
262
Ken Mixter207694d2010-10-28 15:42:37 -0700263 if (out_of_capacity != NULL) *out_of_capacity = false;
264
Ken Mixter03403162010-08-18 15:23:16 -0700265 // For testing.
Lei Zhang9b1f3002014-04-24 02:10:57 -0700266 if (!forced_crash_directory_.empty()) {
267 *crash_directory = forced_crash_directory_;
Ken Mixter03403162010-08-18 15:23:16 -0700268 return true;
269 }
270
271 if (!GetUserInfoFromName(kDefaultUserName,
272 &default_user_id,
273 &default_user_group)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800274 LOG(ERROR) << "Could not find default user info";
Ken Mixter03403162010-08-18 15:23:16 -0700275 return false;
276 }
277 mode_t directory_mode;
278 uid_t directory_owner;
279 gid_t directory_group;
280 *crash_directory =
281 GetCrashDirectoryInfo(euid,
282 default_user_id,
283 default_user_group,
284 &directory_mode,
285 &directory_owner,
286 &directory_group);
287
Mike Frysingera557c112014-02-05 22:55:39 -0500288 if (!base::PathExists(*crash_directory)) {
Ken Mixter03403162010-08-18 15:23:16 -0700289 // Create the spool directory with the appropriate mode (regardless of
290 // umask) and ownership.
291 mode_t old_mask = umask(0);
292 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 ||
293 chown(crash_directory->value().c_str(),
294 directory_owner,
295 directory_group) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800296 LOG(ERROR) << "Unable to create appropriate crash directory";
Ken Mixter03403162010-08-18 15:23:16 -0700297 return false;
298 }
299 umask(old_mask);
300 }
301
Mike Frysingera557c112014-02-05 22:55:39 -0500302 if (!base::PathExists(*crash_directory)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800303 LOG(ERROR) << "Unable to create crash directory "
304 << crash_directory->value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700305 return false;
306 }
307
Ken Mixter04ec10f2010-08-26 16:02:02 -0700308 if (!CheckHasCapacity(*crash_directory)) {
Ken Mixter207694d2010-10-28 15:42:37 -0700309 if (out_of_capacity != NULL) *out_of_capacity = true;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700310 return false;
311 }
312
Ken Mixter03403162010-08-18 15:23:16 -0700313 return true;
314}
Ken Mixter04ec10f2010-08-26 16:02:02 -0700315
Albert Chaulk426fcc02013-05-02 15:38:31 -0700316FilePath CrashCollector::GetProcessPath(pid_t pid) {
317 return FilePath(StringPrintf("/proc/%d", pid));
318}
319
320bool CrashCollector::GetSymlinkTarget(const FilePath &symlink,
Ben Chan8563d202014-01-27 19:30:13 -0800321 FilePath *target) {
Albert Chaulk426fcc02013-05-02 15:38:31 -0700322 int max_size = 32;
Ben Chan8563d202014-01-27 19:30:13 -0800323 scoped_ptr<char[]> buffer;
Albert Chaulk426fcc02013-05-02 15:38:31 -0700324 while (true) {
325 buffer.reset(new char[max_size + 1]);
326 ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size);
327 if (size < 0) {
328 int saved_errno = errno;
329 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
330 << saved_errno;
331 return false;
332 }
333 buffer[size] = 0;
334 if (size == max_size) {
335 // Avoid overflow when doubling.
336 if (max_size * 2 > max_size) {
337 max_size *= 2;
338 continue;
339 } else {
340 return false;
341 }
342 }
343 break;
344 }
345
346 *target = FilePath(buffer.get());
347 return true;
348}
349
350bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid,
351 std::string *base_name) {
352 FilePath target;
353 FilePath process_path = GetProcessPath(pid);
354 FilePath exe_path = process_path.Append("exe");
355 if (!GetSymlinkTarget(exe_path, &target)) {
356 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
357 << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500358 << base::DirectoryExists(process_path);
Albert Chaulk426fcc02013-05-02 15:38:31 -0700359 // Try to further diagnose exe readlink failure cause.
360 struct stat buf;
361 int stat_result = stat(exe_path.value().c_str(), &buf);
362 int saved_errno = errno;
363 if (stat_result < 0) {
364 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
365 << " " << saved_errno;
366 } else {
367 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
368 << buf.st_mode;
369 }
370 return false;
371 }
372 *base_name = target.BaseName().value();
373 return true;
374}
375
Ken Mixter04ec10f2010-08-26 16:02:02 -0700376// Return true if the given crash directory has not already reached
377// maximum capacity.
378bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) {
379 DIR* dir = opendir(crash_directory.value().c_str());
380 if (!dir) {
381 return false;
382 }
383 struct dirent ent_buf;
384 struct dirent* ent;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700385 bool full = false;
Ken Mixteree849c52010-09-30 15:30:10 -0700386 std::set<std::string> basenames;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700387 while (readdir_r(dir, &ent_buf, &ent) == 0 && ent != NULL) {
388 if ((strcmp(ent->d_name, ".") == 0) ||
389 (strcmp(ent->d_name, "..") == 0))
390 continue;
391
Ken Mixteree849c52010-09-30 15:30:10 -0700392 std::string filename(ent->d_name);
393 size_t last_dot = filename.rfind(".");
394 std::string basename;
395 // If there is a valid looking extension, use the base part of the
396 // name. If the only dot is the first byte (aka a dot file), treat
397 // it as unique to avoid allowing a directory full of dot files
398 // from accumulating.
399 if (last_dot != std::string::npos && last_dot != 0)
400 basename = filename.substr(0, last_dot);
401 else
402 basename = filename;
403 basenames.insert(basename);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700404
Ken Mixteree849c52010-09-30 15:30:10 -0700405 if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800406 LOG(WARNING) << "Crash directory " << crash_directory.value()
407 << " already full with " << kMaxCrashDirectorySize
408 << " pending reports";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700409 full = true;
410 break;
411 }
412 }
413 closedir(dir);
414 return !full;
415}
Ken Mixteree849c52010-09-30 15:30:10 -0700416
Ken Mixterc49dbd42010-12-14 17:44:11 -0800417bool CrashCollector::IsCommentLine(const std::string &line) {
418 size_t found = line.find_first_not_of(" ");
419 return found != std::string::npos && line[found] == '#';
420}
421
Ken Mixteree849c52010-09-30 15:30:10 -0700422bool CrashCollector::ReadKeyValueFile(
423 const FilePath &path,
424 const char separator,
425 std::map<std::string, std::string> *dictionary) {
426 std::string contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500427 if (!base::ReadFileToString(path, &contents)) {
Ken Mixteree849c52010-09-30 15:30:10 -0700428 return false;
429 }
430 typedef std::vector<std::string> StringVector;
431 StringVector lines;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700432 base::SplitString(contents, '\n', &lines);
Ken Mixteree849c52010-09-30 15:30:10 -0700433 bool any_errors = false;
434 for (StringVector::iterator line = lines.begin(); line != lines.end();
435 ++line) {
436 // Allow empty strings.
437 if (line->empty())
438 continue;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800439 // Allow comment lines.
440 if (IsCommentLine(*line))
441 continue;
Ken Mixteree849c52010-09-30 15:30:10 -0700442 StringVector sides;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700443 base::SplitString(*line, separator, &sides);
Ken Mixteree849c52010-09-30 15:30:10 -0700444 if (sides.size() != 2) {
445 any_errors = true;
446 continue;
447 }
448 dictionary->insert(std::pair<std::string, std::string>(sides[0], sides[1]));
449 }
450 return !any_errors;
451}
452
Ken Mixterc49dbd42010-12-14 17:44:11 -0800453bool CrashCollector::GetLogContents(const FilePath &config_path,
454 const std::string &exec_name,
455 const FilePath &output_file) {
456 std::map<std::string, std::string> log_commands;
457 if (!ReadKeyValueFile(config_path, ':', &log_commands)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800458 LOG(INFO) << "Unable to read log configuration file "
459 << config_path.value();
Ken Mixterc49dbd42010-12-14 17:44:11 -0800460 return false;
461 }
462
463 if (log_commands.find(exec_name) == log_commands.end())
464 return false;
465
Ken Mixtera3249322011-03-03 08:47:38 -0800466 chromeos::ProcessImpl diag_process;
467 diag_process.AddArg(kShellPath);
Ken Mixterc49dbd42010-12-14 17:44:11 -0800468 std::string shell_command = log_commands[exec_name];
Ken Mixtera3249322011-03-03 08:47:38 -0800469 diag_process.AddStringOption("-c", shell_command);
470 diag_process.RedirectOutput(output_file.value());
Ken Mixterc49dbd42010-12-14 17:44:11 -0800471
Ken Mixtera3249322011-03-03 08:47:38 -0800472 int result = diag_process.Run();
473 if (result != 0) {
474 LOG(INFO) << "Running shell command " << shell_command << "failed with: "
475 << result;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800476 return false;
477 }
478 return true;
479}
480
Ken Mixterafcf8082010-10-26 14:45:01 -0700481void CrashCollector::AddCrashMetaData(const std::string &key,
482 const std::string &value) {
483 extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str()));
484}
485
Albert Chaulk33dfd472013-06-19 15:34:13 -0700486void CrashCollector::AddCrashMetaUploadFile(const std::string &key,
487 const std::string &path) {
488 if (!path.empty())
489 AddCrashMetaData(kUploadFilePrefix + key, path);
490}
491
492void CrashCollector::AddCrashMetaUploadData(const std::string &key,
493 const std::string &value) {
494 if (!value.empty())
495 AddCrashMetaData(kUploadVarPrefix + key, value);
496}
497
Ken Mixteree849c52010-09-30 15:30:10 -0700498void CrashCollector::WriteCrashMetaData(const FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700499 const std::string &exec_name,
500 const std::string &payload_path) {
Ken Mixteree849c52010-09-30 15:30:10 -0700501 std::map<std::string, std::string> contents;
Lei Zhang9b1f3002014-04-24 02:10:57 -0700502 if (!ReadKeyValueFile(FilePath(lsb_release_), '=', &contents)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800503 LOG(ERROR) << "Problem parsing " << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700504 // Even though there was some failure, take as much as we could read.
505 }
506 std::string version("unknown");
507 std::map<std::string, std::string>::iterator i;
508 if ((i = contents.find("CHROMEOS_RELEASE_VERSION")) != contents.end()) {
509 version = i->second;
510 }
Ken Mixterc909b692010-10-18 12:26:05 -0700511 int64 payload_size = -1;
Mike Frysingera557c112014-02-05 22:55:39 -0500512 base::GetFileSize(FilePath(payload_path), &payload_size);
Ken Mixterafcf8082010-10-26 14:45:01 -0700513 std::string meta_data = StringPrintf("%sexec_name=%s\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700514 "ver=%s\n"
Ken Mixter207694d2010-10-28 15:42:37 -0700515 "payload=%s\n"
Mike Frysinger65b4c1e2011-09-21 12:41:29 -0400516 "payload_size=%"PRId64"\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700517 "done=1\n",
Ken Mixterafcf8082010-10-26 14:45:01 -0700518 extra_metadata_.c_str(),
Ken Mixteree849c52010-09-30 15:30:10 -0700519 exec_name.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700520 version.c_str(),
Ken Mixter207694d2010-10-28 15:42:37 -0700521 payload_path.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700522 payload_size);
Ken Mixter9b346472010-11-07 13:45:45 -0800523 // We must use WriteNewFile instead of file_util::WriteFile as we
524 // do not want to write with root access to a symlink that an attacker
525 // might have created.
526 if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800527 LOG(ERROR) << "Unable to write " << meta_path.value();
Ken Mixteree849c52010-09-30 15:30:10 -0700528 }
529}
Thieu Le1652fb22011-03-03 12:14:43 -0800530
531bool CrashCollector::IsCrashTestInProgress() {
Mike Frysingera557c112014-02-05 22:55:39 -0500532 return base::PathExists(FilePath(kCrashTestInProgressPath));
Thieu Le1652fb22011-03-03 12:14:43 -0800533}
Michael Krebs4fe30db2011-08-05 13:54:52 -0700534
535bool CrashCollector::IsDeveloperImage() {
536 // If we're testing crash reporter itself, we don't want to special-case
537 // for developer images.
538 if (IsCrashTestInProgress())
539 return false;
Mike Frysingera557c112014-02-05 22:55:39 -0500540 return base::PathExists(FilePath(kLeaveCoreFile));
Michael Krebs4fe30db2011-08-05 13:54:52 -0700541}
542
543bool CrashCollector::ShouldHandleChromeCrashes() {
544 // If we're testing crash reporter itself, we don't want to allow an
545 // override for chrome crashes. And, let's be conservative and only
546 // allow an override for developer images.
547 if (!IsCrashTestInProgress() && IsDeveloperImage()) {
548 // Check if there's an override to indicate we should indeed collect
549 // chrome crashes. This allows the crashes to still be tracked when
550 // they occur in autotests. See "crosbug.com/17987".
Mike Frysingera557c112014-02-05 22:55:39 -0500551 if (base::PathExists(FilePath(kCollectChromeFile)))
Michael Krebs4fe30db2011-08-05 13:54:52 -0700552 return true;
553 }
554 // We default to ignoring chrome crashes.
555 return false;
556}
557
558bool CrashCollector::IsUserSpecificDirectoryEnabled() {
559 return !ShouldHandleChromeCrashes();
560}