blob: 0521a064521bed46194466e53cf4da7e660f8830 [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()
79 : forced_crash_directory_(NULL),
Simon Queacc79382012-05-04 18:10:09 -070080 lsb_release_(kLsbRelease),
81 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
106 int rv = file_util::WriteFileDescriptor(fd, data, size);
107 HANDLE_EINTR(close(fd));
108 return rv;
109}
110
Ken Mixteree849c52010-09-30 15:30:10 -0700111std::string CrashCollector::Sanitize(const std::string &name) {
112 std::string result = name;
113 for (size_t i = 0; i < name.size(); ++i) {
114 if (!isalnum(result[i]) && result[i] != '_')
115 result[i] = '_';
116 }
117 return result;
118}
119
Ken Mixter03403162010-08-18 15:23:16 -0700120std::string CrashCollector::FormatDumpBasename(const std::string &exec_name,
121 time_t timestamp,
122 pid_t pid) {
123 struct tm tm;
124 localtime_r(&timestamp, &tm);
Ken Mixteree849c52010-09-30 15:30:10 -0700125 std::string sanitized_exec_name = Sanitize(exec_name);
Ken Mixter03403162010-08-18 15:23:16 -0700126 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d",
Ken Mixteree849c52010-09-30 15:30:10 -0700127 sanitized_exec_name.c_str(),
Ken Mixter03403162010-08-18 15:23:16 -0700128 tm.tm_year + 1900,
129 tm.tm_mon + 1,
130 tm.tm_mday,
131 tm.tm_hour,
132 tm.tm_min,
133 tm.tm_sec,
134 pid);
135}
136
Ken Mixter207694d2010-10-28 15:42:37 -0700137FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory,
138 const std::string &basename,
139 const std::string &extension) {
140 return crash_directory.Append(StringPrintf("%s.%s",
141 basename.c_str(),
142 extension.c_str()));
143}
144
Mike Frysingerf19b5182013-05-17 19:36:47 -0400145namespace {
146
147const char *GetGErrorMessage(const GError *error) {
148 if (!error)
149 return "Unknown error.";
150 return error->message;
151}
152
153}
154
155GHashTable *CrashCollector::GetActiveUserSessions(void) {
156 GHashTable *active_sessions = NULL;
157
158 chromeos::dbus::BusConnection dbus = chromeos::dbus::GetSystemBusConnection();
159 if (!dbus.HasConnection()) {
160 LOG(ERROR) << "Error connecting to system D-Bus";
161 return active_sessions;
162 }
163 chromeos::dbus::Proxy proxy(dbus,
164 login_manager::kSessionManagerServiceName,
165 login_manager::kSessionManagerServicePath,
166 login_manager::kSessionManagerInterface);
167 if (!proxy) {
168 LOG(ERROR) << "Error creating D-Bus proxy to interface "
169 << "'" << login_manager::kSessionManagerServiceName << "'";
170 return active_sessions;
171 }
172
173 // Request all the active sessions.
174 GError *gerror = NULL;
175 if (!dbus_g_proxy_call(proxy.gproxy(),
176 login_manager::kSessionManagerRetrieveActiveSessions,
177 &gerror, G_TYPE_INVALID,
178 DBUS_TYPE_G_STRING_STRING_HASHTABLE, &active_sessions,
179 G_TYPE_INVALID)) {
180 LOG(ERROR) << "Error performing D-Bus proxy call "
181 << "'"
182 << login_manager::kSessionManagerRetrieveActiveSessions << "'"
183 << ": " << GetGErrorMessage(gerror);
184 return active_sessions;
185 }
186
187 return active_sessions;
188}
189
190FilePath CrashCollector::GetUserCrashPath(void) {
191 // In this multiprofile world, there is no one-specific user dir anymore.
192 // Ask the session manager for the active ones, then just run with the
193 // first result we get back.
194 FilePath user_path = FilePath(kFallbackUserCrashPath);
195 GHashTable *active_sessions = GetActiveUserSessions();
196 if (!active_sessions)
197 return user_path;
198
199 GList *list = g_hash_table_get_values(active_sessions);
200 if (list) {
201 const char *salted_path = static_cast<const char *>(list->data);
Mike Frysinger37843a92013-06-11 17:03:59 -0400202 user_path = chromeos::cryptohome::home::GetHashedUserPath(salted_path)
203 .Append("crash");
Mike Frysingerf19b5182013-05-17 19:36:47 -0400204 g_list_free(list);
205 }
206
207 g_hash_table_destroy(active_sessions);
208
209 return user_path;
210}
211
Ken Mixter03403162010-08-18 15:23:16 -0700212FilePath CrashCollector::GetCrashDirectoryInfo(
213 uid_t process_euid,
214 uid_t default_user_id,
215 gid_t default_user_group,
216 mode_t *mode,
217 uid_t *directory_owner,
218 gid_t *directory_group) {
Michael Krebs4fe30db2011-08-05 13:54:52 -0700219 // TODO(mkrebs): This can go away once Chrome crashes are handled
220 // normally (see crosbug.com/5872).
221 // Check if the user crash directory should be used. If we are
222 // collecting chrome crashes during autotesting, we want to put them in
223 // the system crash directory so they are outside the cryptohome -- in
224 // case we are being run during logout (see crosbug.com/18637).
225 if (process_euid == default_user_id && IsUserSpecificDirectoryEnabled()) {
Ken Mixter03403162010-08-18 15:23:16 -0700226 *mode = kUserCrashPathMode;
227 *directory_owner = default_user_id;
228 *directory_group = default_user_group;
Mike Frysingerf19b5182013-05-17 19:36:47 -0400229 return GetUserCrashPath();
Ken Mixter03403162010-08-18 15:23:16 -0700230 } else {
231 *mode = kSystemCrashPathMode;
232 *directory_owner = kRootOwner;
233 *directory_group = kRootGroup;
234 return FilePath(kSystemCrashPath);
235 }
236}
237
238bool CrashCollector::GetUserInfoFromName(const std::string &name,
239 uid_t *uid,
240 gid_t *gid) {
241 char storage[256];
242 struct passwd passwd_storage;
243 struct passwd *passwd_result = NULL;
244
245 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage),
246 &passwd_result) != 0 || passwd_result == NULL) {
Ken Mixtera3249322011-03-03 08:47:38 -0800247 LOG(ERROR) << "Cannot find user named " << name;
Ken Mixter03403162010-08-18 15:23:16 -0700248 return false;
249 }
250
251 *uid = passwd_result->pw_uid;
252 *gid = passwd_result->pw_gid;
253 return true;
254}
255
256bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid,
Ken Mixter207694d2010-10-28 15:42:37 -0700257 FilePath *crash_directory,
258 bool *out_of_capacity) {
Ken Mixter03403162010-08-18 15:23:16 -0700259 uid_t default_user_id;
260 gid_t default_user_group;
261
Ken Mixter207694d2010-10-28 15:42:37 -0700262 if (out_of_capacity != NULL) *out_of_capacity = false;
263
Ken Mixter03403162010-08-18 15:23:16 -0700264 // For testing.
265 if (forced_crash_directory_ != NULL) {
266 *crash_directory = FilePath(forced_crash_directory_);
267 return true;
268 }
269
270 if (!GetUserInfoFromName(kDefaultUserName,
271 &default_user_id,
272 &default_user_group)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800273 LOG(ERROR) << "Could not find default user info";
Ken Mixter03403162010-08-18 15:23:16 -0700274 return false;
275 }
276 mode_t directory_mode;
277 uid_t directory_owner;
278 gid_t directory_group;
279 *crash_directory =
280 GetCrashDirectoryInfo(euid,
281 default_user_id,
282 default_user_group,
283 &directory_mode,
284 &directory_owner,
285 &directory_group);
286
Mike Frysingera557c112014-02-05 22:55:39 -0500287 if (!base::PathExists(*crash_directory)) {
Ken Mixter03403162010-08-18 15:23:16 -0700288 // Create the spool directory with the appropriate mode (regardless of
289 // umask) and ownership.
290 mode_t old_mask = umask(0);
291 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 ||
292 chown(crash_directory->value().c_str(),
293 directory_owner,
294 directory_group) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800295 LOG(ERROR) << "Unable to create appropriate crash directory";
Ken Mixter03403162010-08-18 15:23:16 -0700296 return false;
297 }
298 umask(old_mask);
299 }
300
Mike Frysingera557c112014-02-05 22:55:39 -0500301 if (!base::PathExists(*crash_directory)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800302 LOG(ERROR) << "Unable to create crash directory "
303 << crash_directory->value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700304 return false;
305 }
306
Ken Mixter04ec10f2010-08-26 16:02:02 -0700307 if (!CheckHasCapacity(*crash_directory)) {
Ken Mixter207694d2010-10-28 15:42:37 -0700308 if (out_of_capacity != NULL) *out_of_capacity = true;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700309 return false;
310 }
311
Ken Mixter03403162010-08-18 15:23:16 -0700312 return true;
313}
Ken Mixter04ec10f2010-08-26 16:02:02 -0700314
Albert Chaulk426fcc02013-05-02 15:38:31 -0700315FilePath CrashCollector::GetProcessPath(pid_t pid) {
316 return FilePath(StringPrintf("/proc/%d", pid));
317}
318
319bool CrashCollector::GetSymlinkTarget(const FilePath &symlink,
Ben Chan8563d202014-01-27 19:30:13 -0800320 FilePath *target) {
Albert Chaulk426fcc02013-05-02 15:38:31 -0700321 int max_size = 32;
Ben Chan8563d202014-01-27 19:30:13 -0800322 scoped_ptr<char[]> buffer;
Albert Chaulk426fcc02013-05-02 15:38:31 -0700323 while (true) {
324 buffer.reset(new char[max_size + 1]);
325 ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size);
326 if (size < 0) {
327 int saved_errno = errno;
328 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
329 << saved_errno;
330 return false;
331 }
332 buffer[size] = 0;
333 if (size == max_size) {
334 // Avoid overflow when doubling.
335 if (max_size * 2 > max_size) {
336 max_size *= 2;
337 continue;
338 } else {
339 return false;
340 }
341 }
342 break;
343 }
344
345 *target = FilePath(buffer.get());
346 return true;
347}
348
349bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid,
350 std::string *base_name) {
351 FilePath target;
352 FilePath process_path = GetProcessPath(pid);
353 FilePath exe_path = process_path.Append("exe");
354 if (!GetSymlinkTarget(exe_path, &target)) {
355 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
356 << " DirectoryExists: "
Mike Frysingera557c112014-02-05 22:55:39 -0500357 << base::DirectoryExists(process_path);
Albert Chaulk426fcc02013-05-02 15:38:31 -0700358 // Try to further diagnose exe readlink failure cause.
359 struct stat buf;
360 int stat_result = stat(exe_path.value().c_str(), &buf);
361 int saved_errno = errno;
362 if (stat_result < 0) {
363 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
364 << " " << saved_errno;
365 } else {
366 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
367 << buf.st_mode;
368 }
369 return false;
370 }
371 *base_name = target.BaseName().value();
372 return true;
373}
374
Ken Mixter04ec10f2010-08-26 16:02:02 -0700375// Return true if the given crash directory has not already reached
376// maximum capacity.
377bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) {
378 DIR* dir = opendir(crash_directory.value().c_str());
379 if (!dir) {
380 return false;
381 }
382 struct dirent ent_buf;
383 struct dirent* ent;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700384 bool full = false;
Ken Mixteree849c52010-09-30 15:30:10 -0700385 std::set<std::string> basenames;
Ken Mixter04ec10f2010-08-26 16:02:02 -0700386 while (readdir_r(dir, &ent_buf, &ent) == 0 && ent != NULL) {
387 if ((strcmp(ent->d_name, ".") == 0) ||
388 (strcmp(ent->d_name, "..") == 0))
389 continue;
390
Ken Mixteree849c52010-09-30 15:30:10 -0700391 std::string filename(ent->d_name);
392 size_t last_dot = filename.rfind(".");
393 std::string basename;
394 // If there is a valid looking extension, use the base part of the
395 // name. If the only dot is the first byte (aka a dot file), treat
396 // it as unique to avoid allowing a directory full of dot files
397 // from accumulating.
398 if (last_dot != std::string::npos && last_dot != 0)
399 basename = filename.substr(0, last_dot);
400 else
401 basename = filename;
402 basenames.insert(basename);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700403
Ken Mixteree849c52010-09-30 15:30:10 -0700404 if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800405 LOG(WARNING) << "Crash directory " << crash_directory.value()
406 << " already full with " << kMaxCrashDirectorySize
407 << " pending reports";
Ken Mixter04ec10f2010-08-26 16:02:02 -0700408 full = true;
409 break;
410 }
411 }
412 closedir(dir);
413 return !full;
414}
Ken Mixteree849c52010-09-30 15:30:10 -0700415
Ken Mixterc49dbd42010-12-14 17:44:11 -0800416bool CrashCollector::IsCommentLine(const std::string &line) {
417 size_t found = line.find_first_not_of(" ");
418 return found != std::string::npos && line[found] == '#';
419}
420
Ken Mixteree849c52010-09-30 15:30:10 -0700421bool CrashCollector::ReadKeyValueFile(
422 const FilePath &path,
423 const char separator,
424 std::map<std::string, std::string> *dictionary) {
425 std::string contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500426 if (!base::ReadFileToString(path, &contents)) {
Ken Mixteree849c52010-09-30 15:30:10 -0700427 return false;
428 }
429 typedef std::vector<std::string> StringVector;
430 StringVector lines;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700431 base::SplitString(contents, '\n', &lines);
Ken Mixteree849c52010-09-30 15:30:10 -0700432 bool any_errors = false;
433 for (StringVector::iterator line = lines.begin(); line != lines.end();
434 ++line) {
435 // Allow empty strings.
436 if (line->empty())
437 continue;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800438 // Allow comment lines.
439 if (IsCommentLine(*line))
440 continue;
Ken Mixteree849c52010-09-30 15:30:10 -0700441 StringVector sides;
Chris Masone3ba6c5b2011-05-13 16:57:09 -0700442 base::SplitString(*line, separator, &sides);
Ken Mixteree849c52010-09-30 15:30:10 -0700443 if (sides.size() != 2) {
444 any_errors = true;
445 continue;
446 }
447 dictionary->insert(std::pair<std::string, std::string>(sides[0], sides[1]));
448 }
449 return !any_errors;
450}
451
Ken Mixterc49dbd42010-12-14 17:44:11 -0800452bool CrashCollector::GetLogContents(const FilePath &config_path,
453 const std::string &exec_name,
454 const FilePath &output_file) {
455 std::map<std::string, std::string> log_commands;
456 if (!ReadKeyValueFile(config_path, ':', &log_commands)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800457 LOG(INFO) << "Unable to read log configuration file "
458 << config_path.value();
Ken Mixterc49dbd42010-12-14 17:44:11 -0800459 return false;
460 }
461
462 if (log_commands.find(exec_name) == log_commands.end())
463 return false;
464
Ken Mixtera3249322011-03-03 08:47:38 -0800465 chromeos::ProcessImpl diag_process;
466 diag_process.AddArg(kShellPath);
Ken Mixterc49dbd42010-12-14 17:44:11 -0800467 std::string shell_command = log_commands[exec_name];
Ken Mixtera3249322011-03-03 08:47:38 -0800468 diag_process.AddStringOption("-c", shell_command);
469 diag_process.RedirectOutput(output_file.value());
Ken Mixterc49dbd42010-12-14 17:44:11 -0800470
Ken Mixtera3249322011-03-03 08:47:38 -0800471 int result = diag_process.Run();
472 if (result != 0) {
473 LOG(INFO) << "Running shell command " << shell_command << "failed with: "
474 << result;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800475 return false;
476 }
477 return true;
478}
479
Ken Mixterafcf8082010-10-26 14:45:01 -0700480void CrashCollector::AddCrashMetaData(const std::string &key,
481 const std::string &value) {
482 extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str()));
483}
484
Albert Chaulk33dfd472013-06-19 15:34:13 -0700485void CrashCollector::AddCrashMetaUploadFile(const std::string &key,
486 const std::string &path) {
487 if (!path.empty())
488 AddCrashMetaData(kUploadFilePrefix + key, path);
489}
490
491void CrashCollector::AddCrashMetaUploadData(const std::string &key,
492 const std::string &value) {
493 if (!value.empty())
494 AddCrashMetaData(kUploadVarPrefix + key, value);
495}
496
Ken Mixteree849c52010-09-30 15:30:10 -0700497void CrashCollector::WriteCrashMetaData(const FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700498 const std::string &exec_name,
499 const std::string &payload_path) {
Ken Mixteree849c52010-09-30 15:30:10 -0700500 std::map<std::string, std::string> contents;
Ken Mixterafcf8082010-10-26 14:45:01 -0700501 if (!ReadKeyValueFile(FilePath(std::string(lsb_release_)), '=', &contents)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800502 LOG(ERROR) << "Problem parsing " << lsb_release_;
Ken Mixteree849c52010-09-30 15:30:10 -0700503 // Even though there was some failure, take as much as we could read.
504 }
505 std::string version("unknown");
506 std::map<std::string, std::string>::iterator i;
507 if ((i = contents.find("CHROMEOS_RELEASE_VERSION")) != contents.end()) {
508 version = i->second;
509 }
Ken Mixterc909b692010-10-18 12:26:05 -0700510 int64 payload_size = -1;
Mike Frysingera557c112014-02-05 22:55:39 -0500511 base::GetFileSize(FilePath(payload_path), &payload_size);
Ken Mixterafcf8082010-10-26 14:45:01 -0700512 std::string meta_data = StringPrintf("%sexec_name=%s\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700513 "ver=%s\n"
Ken Mixter207694d2010-10-28 15:42:37 -0700514 "payload=%s\n"
Mike Frysinger65b4c1e2011-09-21 12:41:29 -0400515 "payload_size=%"PRId64"\n"
Ken Mixteree849c52010-09-30 15:30:10 -0700516 "done=1\n",
Ken Mixterafcf8082010-10-26 14:45:01 -0700517 extra_metadata_.c_str(),
Ken Mixteree849c52010-09-30 15:30:10 -0700518 exec_name.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700519 version.c_str(),
Ken Mixter207694d2010-10-28 15:42:37 -0700520 payload_path.c_str(),
Ken Mixterc909b692010-10-18 12:26:05 -0700521 payload_size);
Ken Mixter9b346472010-11-07 13:45:45 -0800522 // We must use WriteNewFile instead of file_util::WriteFile as we
523 // do not want to write with root access to a symlink that an attacker
524 // might have created.
525 if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800526 LOG(ERROR) << "Unable to write " << meta_path.value();
Ken Mixteree849c52010-09-30 15:30:10 -0700527 }
528}
Thieu Le1652fb22011-03-03 12:14:43 -0800529
530bool CrashCollector::IsCrashTestInProgress() {
Mike Frysingera557c112014-02-05 22:55:39 -0500531 return base::PathExists(FilePath(kCrashTestInProgressPath));
Thieu Le1652fb22011-03-03 12:14:43 -0800532}
Michael Krebs4fe30db2011-08-05 13:54:52 -0700533
534bool CrashCollector::IsDeveloperImage() {
535 // If we're testing crash reporter itself, we don't want to special-case
536 // for developer images.
537 if (IsCrashTestInProgress())
538 return false;
Mike Frysingera557c112014-02-05 22:55:39 -0500539 return base::PathExists(FilePath(kLeaveCoreFile));
Michael Krebs4fe30db2011-08-05 13:54:52 -0700540}
541
542bool CrashCollector::ShouldHandleChromeCrashes() {
543 // If we're testing crash reporter itself, we don't want to allow an
544 // override for chrome crashes. And, let's be conservative and only
545 // allow an override for developer images.
546 if (!IsCrashTestInProgress() && IsDeveloperImage()) {
547 // Check if there's an override to indicate we should indeed collect
548 // chrome crashes. This allows the crashes to still be tracked when
549 // they occur in autotests. See "crosbug.com/17987".
Mike Frysingera557c112014-02-05 22:55:39 -0500550 if (base::PathExists(FilePath(kCollectChromeFile)))
Michael Krebs4fe30db2011-08-05 13:54:52 -0700551 return true;
552 }
553 // We default to ignoring chrome crashes.
554 return false;
555}
556
557bool CrashCollector::IsUserSpecificDirectoryEnabled() {
558 return !ShouldHandleChromeCrashes();
559}