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