Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 5 | #include "crash-reporter/user_collector.h" |
| 6 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 7 | #include <grp.h> // For struct group. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 8 | #include <pcrecpp.h> |
| 9 | #include <pcrecpp.h> |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 10 | #include <pwd.h> // For struct passwd. |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 11 | #include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 12 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 13 | #include <string> |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 14 | #include <vector> |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 15 | |
| 16 | #include "base/file_util.h" |
| 17 | #include "base/logging.h" |
| 18 | #include "base/string_util.h" |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 19 | #include "chromeos/process.h" |
| 20 | #include "chromeos/syslog_logging.h" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 21 | #include "gflags/gflags.h" |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 22 | |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 23 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 24 | DEFINE_bool(core2md_failure, false, "Core2md failure test"); |
| 25 | DEFINE_bool(directory_failure, false, "Spool directory failure test"); |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 26 | DEFINE_string(filter_in, "", |
| 27 | "Ignore all crashes but this for testing"); |
| 28 | #pragma GCC diagnostic error "-Wstrict-aliasing" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 29 | |
| 30 | static const char kCollectionErrorSignature[] = |
| 31 | "crash_reporter-user-collection"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 32 | // This procfs file is used to cause kernel core file writing to |
| 33 | // instead pipe the core file into a user space process. See |
| 34 | // core(5) man page. |
| 35 | static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern"; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 36 | static const char kCorePipeLimitFile[] = "/proc/sys/kernel/core_pipe_limit"; |
| 37 | // Set core_pipe_limit to 4 so that we can catch a few unrelated concurrent |
| 38 | // crashes, but finite to avoid infinitely recursing on crash handling. |
| 39 | static const char kCorePipeLimit[] = "4"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 40 | static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md"; |
Ken Mixter | 7ac7a70 | 2010-08-13 15:43:34 -0700 | [diff] [blame] | 41 | static const char kLeaveCoreFile[] = "/root/.leave_core"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 42 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 43 | static const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf"; |
| 44 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 45 | const char *UserCollector::kUserId = "Uid:\t"; |
| 46 | const char *UserCollector::kGroupId = "Gid:\t"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 47 | |
| 48 | UserCollector::UserCollector() |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 49 | : generate_diagnostics_(false), |
| 50 | core_pattern_file_(kCorePatternFile), |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 51 | core_pipe_limit_file_(kCorePipeLimitFile), |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 52 | initialized_(false) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void UserCollector::Initialize( |
| 56 | UserCollector::CountCrashFunction count_crash_function, |
| 57 | const std::string &our_path, |
| 58 | UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function, |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 59 | bool generate_diagnostics) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 60 | CrashCollector::Initialize(count_crash_function, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 61 | is_feedback_allowed_function); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 62 | our_path_ = our_path; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 63 | initialized_ = true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 64 | generate_diagnostics_ = generate_diagnostics; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | UserCollector::~UserCollector() { |
| 68 | } |
| 69 | |
| 70 | std::string UserCollector::GetPattern(bool enabled) const { |
| 71 | if (enabled) { |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 72 | // Combine the three crash attributes into one parameter to try to reduce |
| 73 | // the size of the invocation line for crash_reporter since the kernel |
| 74 | // has a fixed-sized (128B) buffer that it will truncate into. Note that |
| 75 | // the kernel does not support quoted arguments in core_pattern. |
| 76 | return StringPrintf("|%s --user=%%p:%%s:%%e", our_path_.c_str()); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 77 | } else { |
| 78 | return "core"; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool UserCollector::SetUpInternal(bool enabled) { |
| 83 | CHECK(initialized_); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 84 | LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling"; |
| 85 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 86 | if (file_util::WriteFile(FilePath(core_pipe_limit_file_), |
| 87 | kCorePipeLimit, |
| 88 | strlen(kCorePipeLimit)) != |
| 89 | static_cast<int>(strlen(kCorePipeLimit))) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 90 | LOG(ERROR) << "Unable to write " << core_pipe_limit_file_; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 91 | return false; |
| 92 | } |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 93 | std::string pattern = GetPattern(enabled); |
| 94 | if (file_util::WriteFile(FilePath(core_pattern_file_), |
| 95 | pattern.c_str(), |
| 96 | pattern.length()) != |
| 97 | static_cast<int>(pattern.length())) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 98 | LOG(ERROR) << "Unable to write " << core_pattern_file_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 104 | FilePath UserCollector::GetProcessPath(pid_t pid) { |
| 105 | return FilePath(StringPrintf("/proc/%d", pid)); |
| 106 | } |
| 107 | |
| 108 | bool UserCollector::GetSymlinkTarget(const FilePath &symlink, |
| 109 | FilePath *target) { |
| 110 | int max_size = 32; |
| 111 | scoped_array<char> buffer; |
| 112 | while (true) { |
| 113 | buffer.reset(new char[max_size + 1]); |
| 114 | ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size); |
| 115 | if (size < 0) { |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 116 | int saved_errno = errno; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 117 | LOG(ERROR) << "Readlink failed on " << symlink.value() << " with " |
| 118 | << saved_errno; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | buffer[size] = 0; |
| 122 | if (size == max_size) { |
| 123 | // Avoid overflow when doubling. |
| 124 | if (max_size * 2 > max_size) { |
| 125 | max_size *= 2; |
| 126 | continue; |
| 127 | } else { |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | *target = FilePath(buffer.get()); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool UserCollector::GetExecutableBaseNameFromPid(uid_t pid, |
| 139 | std::string *base_name) { |
| 140 | FilePath target; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 141 | FilePath process_path = GetProcessPath(pid); |
| 142 | FilePath exe_path = process_path.Append("exe"); |
| 143 | if (!GetSymlinkTarget(exe_path, &target)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 144 | LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value() |
| 145 | << " DirectoryExists: " |
| 146 | << file_util::DirectoryExists(process_path); |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 147 | // Try to further diagnose exe readlink failure cause. |
| 148 | struct stat buf; |
| 149 | int stat_result = stat(exe_path.value().c_str(), &buf); |
| 150 | int saved_errno = errno; |
| 151 | if (stat_result < 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 152 | LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result |
| 153 | << " " << saved_errno; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 154 | } else { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 155 | LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode=" |
| 156 | << buf.st_mode; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 157 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 158 | return false; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 159 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 160 | *base_name = target.BaseName().value(); |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | bool UserCollector::GetIdFromStatus(const char *prefix, |
| 165 | IdKind kind, |
| 166 | const std::string &status_contents, |
| 167 | int *id) { |
| 168 | // From fs/proc/array.c:task_state(), this file contains: |
| 169 | // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n |
| 170 | std::vector<std::string> status_lines; |
| 171 | SplitString(status_contents, '\n', &status_lines); |
| 172 | std::vector<std::string>::iterator line_iterator; |
| 173 | for (line_iterator = status_lines.begin(); |
| 174 | line_iterator != status_lines.end(); |
| 175 | ++line_iterator) { |
| 176 | if (line_iterator->find(prefix) == 0) |
| 177 | break; |
| 178 | } |
| 179 | if (line_iterator == status_lines.end()) { |
| 180 | return false; |
| 181 | } |
| 182 | std::string id_substring = line_iterator->substr(strlen(prefix), |
| 183 | std::string::npos); |
| 184 | std::vector<std::string> ids; |
| 185 | SplitString(id_substring, '\t', &ids); |
| 186 | if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) { |
| 187 | return false; |
| 188 | } |
| 189 | const char *number = ids[kind].c_str(); |
| 190 | char *end_number = NULL; |
| 191 | *id = strtol(number, &end_number, 10); |
| 192 | if (*end_number != '\0') |
| 193 | return false; |
| 194 | return true; |
| 195 | } |
| 196 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 197 | void UserCollector::EnqueueCollectionErrorLog(pid_t pid, |
| 198 | const std::string &exec) { |
| 199 | FilePath crash_path; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 200 | LOG(INFO) << "Writing conversion problems as separate crash report."; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 201 | if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 202 | LOG(ERROR) << "Could not even get log directory; out of space?"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 206 | std::string error_log = chromeos::GetLog(); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 207 | FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog"); |
| 208 | if (GetLogContents(FilePath(kDefaultLogConfig), kCollectionErrorSignature, |
| 209 | diag_log_path)) { |
| 210 | // We load the contents of diag_log into memory and append it to |
| 211 | // the error log. We cannot just append to files because we need |
| 212 | // to always create new files to prevent attack. |
| 213 | std::string diag_log_contents; |
| 214 | file_util::ReadFileToString(diag_log_path, &diag_log_contents); |
| 215 | error_log.append(diag_log_contents); |
| 216 | file_util::Delete(diag_log_path, false); |
| 217 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 218 | FilePath log_path = GetCrashPath(crash_path, dump_basename, "log"); |
| 219 | FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta"); |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 220 | // We must use WriteNewFile instead of file_util::WriteFile as we do |
| 221 | // not want to write with root access to a symlink that an attacker |
| 222 | // might have created. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 223 | WriteNewFile(log_path, error_log.data(), error_log.length()); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 224 | AddCrashMetaData("sig", kCollectionErrorSignature); |
| 225 | WriteCrashMetaData(meta_path, exec, log_path.value()); |
| 226 | } |
| 227 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 228 | bool UserCollector::CopyOffProcFiles(pid_t pid, |
| 229 | const FilePath &container_dir) { |
| 230 | if (!file_util::CreateDirectory(container_dir)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 231 | LOG(ERROR) << "Could not create " << container_dir.value().c_str(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | FilePath process_path = GetProcessPath(pid); |
| 235 | if (!file_util::PathExists(process_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 236 | LOG(ERROR) << "Path " << process_path.value() << " does not exist"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | static const char *proc_files[] = { |
| 240 | "auxv", |
| 241 | "cmdline", |
| 242 | "environ", |
| 243 | "maps", |
| 244 | "status" |
| 245 | }; |
| 246 | for (unsigned i = 0; i < arraysize(proc_files); ++i) { |
| 247 | if (!file_util::CopyFile(process_path.Append(proc_files[i]), |
| 248 | container_dir.Append(proc_files[i]))) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 249 | LOG(ERROR) << "Could not copy " << proc_files[i] << " file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 256 | bool UserCollector::GetCreatedCrashDirectory(pid_t pid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 257 | FilePath *crash_file_path, |
| 258 | bool *out_of_capacity) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 259 | FilePath process_path = GetProcessPath(pid); |
| 260 | std::string status; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 261 | if (FLAGS_directory_failure) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 262 | LOG(ERROR) << "Purposefully failing to create spool directory"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 263 | return false; |
| 264 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 265 | if (!file_util::ReadFileToString(process_path.Append("status"), |
| 266 | &status)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 267 | LOG(ERROR) << "Could not read status file"; |
| 268 | LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: " |
| 269 | << file_util::DirectoryExists(process_path); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | int process_euid; |
| 273 | if (!GetIdFromStatus(kUserId, kIdEffective, status, &process_euid)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 274 | LOG(ERROR) << "Could not find euid in status file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 275 | return false; |
| 276 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 277 | if (!GetCreatedCrashDirectoryByEuid(process_euid, |
| 278 | crash_file_path, |
| 279 | out_of_capacity)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 280 | LOG(ERROR) << "Could not create crash directory"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | return true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) { |
| 287 | // Copy off all stdin to a core file. |
| 288 | FilePath stdin_path("/dev/fd/0"); |
| 289 | if (file_util::CopyFile(stdin_path, core_path)) { |
| 290 | return true; |
| 291 | } |
| 292 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 293 | LOG(ERROR) << "Could not write core file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 294 | // If the file system was full, make sure we remove any remnants. |
| 295 | file_util::Delete(core_path, false); |
| 296 | return false; |
| 297 | } |
| 298 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 299 | bool UserCollector::RunCoreToMinidump(const FilePath &core_path, |
| 300 | const FilePath &procfs_directory, |
| 301 | const FilePath &minidump_path, |
| 302 | const FilePath &temp_directory) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 303 | FilePath output_path = temp_directory.Append("output"); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 304 | chromeos::ProcessImpl core2md; |
| 305 | core2md.RedirectOutput(output_path.value()); |
| 306 | core2md.AddArg(kCoreToMinidumpConverterPath); |
| 307 | core2md.AddArg(core_path.value()); |
| 308 | core2md.AddArg(procfs_directory.value()); |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 309 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 310 | if (!FLAGS_core2md_failure) { |
| 311 | core2md.AddArg(minidump_path.value()); |
| 312 | } else { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 313 | // To test how core2md errors are propagaged, cause an error |
| 314 | // by forgetting a required argument. |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 317 | int errorlevel = core2md.Run(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 318 | |
| 319 | std::string output; |
| 320 | file_util::ReadFileToString(output_path, &output); |
| 321 | if (errorlevel != 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 322 | LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath |
| 323 | << " [result=" << errorlevel << "]: " << output; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
| 327 | if (!file_util::PathExists(minidump_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 328 | LOG(ERROR) << "Minidump file " << minidump_path.value() |
| 329 | << " was not created"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | return true; |
| 333 | } |
| 334 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 335 | bool UserCollector::ConvertCoreToMinidump(pid_t pid, |
| 336 | const FilePath &container_dir, |
| 337 | const FilePath &core_path, |
| 338 | const FilePath &minidump_path) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 339 | if (!CopyOffProcFiles(pid, container_dir)) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 343 | if (!CopyStdinToCoreFile(core_path)) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 347 | bool conversion_result = RunCoreToMinidump( |
| 348 | core_path, |
| 349 | container_dir, // procfs directory |
| 350 | minidump_path, |
| 351 | container_dir); // temporary directory |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 352 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 353 | if (conversion_result) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 354 | LOG(INFO) << "Stored minidump to " << minidump_path.value(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 357 | return conversion_result; |
| 358 | } |
| 359 | |
| 360 | bool UserCollector::ConvertAndEnqueueCrash(int pid, |
| 361 | const std::string &exec, |
| 362 | bool *out_of_capacity) { |
| 363 | FilePath crash_path; |
| 364 | if (!GetCreatedCrashDirectory(pid, &crash_path, out_of_capacity)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 365 | LOG(ERROR) << "Unable to find/create process-specific crash path"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | |
| 369 | // Directory like /tmp/crash_reporter.1234 which contains the |
| 370 | // procfs entries and other temporary files used during conversion. |
| 371 | FilePath container_dir = FilePath("/tmp").Append( |
| 372 | StringPrintf("crash_reporter.%d", pid)); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 373 | // Delete a pre-existing directory from crash reporter that may have |
| 374 | // been left around for diagnostics from a failed conversion attempt. |
| 375 | // If we don't, existing files can cause forking to fail. |
| 376 | file_util::Delete(container_dir, true); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 377 | std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid); |
| 378 | FilePath core_path = GetCrashPath(crash_path, dump_basename, "core"); |
| 379 | FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta"); |
| 380 | FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp"); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 381 | FilePath log_path = GetCrashPath(crash_path, dump_basename, "log"); |
| 382 | |
| 383 | if (GetLogContents(FilePath(kDefaultLogConfig), exec, log_path)) |
| 384 | AddCrashMetaData("log", log_path.value()); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 385 | |
| 386 | if (!ConvertCoreToMinidump(pid, container_dir, core_path, |
| 387 | minidump_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 388 | LOG(INFO) << "Leaving core file at " << core_path.value() |
| 389 | << " due to conversion error"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | |
| 393 | // Here we commit to sending this file. We must not return false |
| 394 | // after this point or we will generate a log report as well as a |
| 395 | // crash report. |
| 396 | WriteCrashMetaData(meta_path, |
| 397 | exec, |
| 398 | minidump_path.value()); |
| 399 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 400 | if (!file_util::PathExists(FilePath(kLeaveCoreFile))) { |
| 401 | file_util::Delete(core_path, false); |
| 402 | } else { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 403 | LOG(INFO) << "Leaving core file at " << core_path.value() |
| 404 | << " due to developer image"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 407 | file_util::Delete(container_dir, true); |
| 408 | return true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 411 | bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes, |
| 412 | pid_t *pid, int *signal, |
| 413 | std::string *kernel_supplied_name) { |
| 414 | pcrecpp::RE re("(\\d+):(\\d+):(.*)"); |
| 415 | return re.FullMatch(crash_attributes, pid, signal, kernel_supplied_name); |
| 416 | } |
| 417 | |
| 418 | bool UserCollector::HandleCrash(const std::string &crash_attributes, |
| 419 | const char *force_exec) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 420 | CHECK(initialized_); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 421 | int pid = 0; |
| 422 | int signal = 0; |
| 423 | std::string kernel_supplied_name; |
| 424 | |
| 425 | if (!ParseCrashAttributes(crash_attributes, &pid, &signal, |
| 426 | &kernel_supplied_name)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 427 | LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 431 | std::string exec; |
| 432 | if (force_exec) { |
| 433 | exec.assign(force_exec); |
| 434 | } else if (!GetExecutableBaseNameFromPid(pid, &exec)) { |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 435 | // If we cannot find the exec name, use the kernel supplied name. |
| 436 | // We don't always use the kernel's since it truncates the name to |
| 437 | // 16 characters. |
| 438 | exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str()); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 439 | } |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 440 | |
| 441 | // Allow us to test the crash reporting mechanism successfully even if |
| 442 | // other parts of the system crash. |
| 443 | if (!FLAGS_filter_in.empty() && |
| 444 | (FLAGS_filter_in == "none" || |
| 445 | FLAGS_filter_in != exec)) { |
| 446 | // We use a different format message to make it more obvious in tests |
| 447 | // which crashes are test generated and which are real. |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 448 | LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while " |
| 449 | << "filter_in=" << FLAGS_filter_in << "."; |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 450 | return true; |
| 451 | } |
| 452 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 453 | bool feedback = is_feedback_allowed_function_(); |
Ken Mixter | 2105b49 | 2010-11-09 16:14:38 -0800 | [diff] [blame] | 454 | const char *handling_string = "handling"; |
| 455 | if (!feedback) { |
| 456 | handling_string = "ignoring - no consent"; |
| 457 | } |
| 458 | |
| 459 | // Treat Chrome crashes as if the user opted-out. We stop counting Chrome |
| 460 | // crashes towards user crashes, so user crashes really mean non-Chrome |
| 461 | // user-space crashes. |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 462 | if (exec == "chrome" || exec == "supplied_chrome") { |
Ken Mixter | 2105b49 | 2010-11-09 16:14:38 -0800 | [diff] [blame] | 463 | feedback = false; |
| 464 | handling_string = "ignoring - chrome crash"; |
| 465 | } |
| 466 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame^] | 467 | LOG(WARNING) << "Received crash notification for " << exec << "[" << pid |
| 468 | << "] sig " << signal << " (" << handling_string << ")"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 469 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 470 | if (feedback) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 471 | count_crash_function_(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 472 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 473 | if (generate_diagnostics_) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 474 | bool out_of_capacity = false; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 475 | bool convert_and_enqueue_result = |
| 476 | ConvertAndEnqueueCrash(pid, exec, &out_of_capacity); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 477 | if (!convert_and_enqueue_result) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 478 | if (!out_of_capacity) |
| 479 | EnqueueCollectionErrorLog(pid, exec); |
| 480 | return false; |
| 481 | } |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 482 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 483 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 484 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 485 | return true; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 486 | } |