Steve Fung | 6c34c25 | 2015-08-20 00:27:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Steve Fung | 129bea5 | 2015-07-23 13:11:15 -0700 | [diff] [blame] | 17 | #include "crash_collector.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 18 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 20 | #include <fcntl.h> // For file creation modes. |
Alex Vakulenko | 7589aff | 2014-07-30 10:07:35 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 22 | #include <linux/limits.h> // PATH_MAX |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 23 | #include <pwd.h> // For struct passwd. |
| 24 | #include <sys/types.h> // for mode_t. |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 25 | #include <sys/wait.h> // For waitpid. |
| 26 | #include <unistd.h> // For execv and fork. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 27 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 28 | #include <set> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 29 | #include <utility> |
Simon Que | f70060c | 2012-04-09 19:07:07 -0700 | [diff] [blame] | 30 | #include <vector> |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 31 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 32 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 33 | #include <base/logging.h> |
| 34 | #include <base/posix/eintr_wrapper.h> |
| 35 | #include <base/strings/string_split.h> |
| 36 | #include <base/strings/string_util.h> |
| 37 | #include <base/strings/stringprintf.h> |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 38 | #include <brillo/key_value_store.h> |
Steve Fung | b3a48e1 | 2016-02-25 03:37:33 -0800 | [diff] [blame] | 39 | #include <brillo/osrelease_reader.h> |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 40 | #include <brillo/process.h> |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 41 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | const char kCollectChromeFile[] = |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 45 | "/mnt/stateful_partition/etc/collect_chrome_crashes"; |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 46 | const char kCrashTestInProgressPath[] = |
| 47 | "/data/misc/crash_reporter/tmp/crash-test-in-progress"; |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 48 | const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf"; |
| 49 | const char kDefaultUserName[] = "chronos"; |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 50 | const char kLeaveCoreFile[] = "/data/misc/crash_reporter/.leave_core"; |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 51 | const char kShellPath[] = "/system/bin/sh"; |
Steve Fung | ab2ac7d | 2015-08-14 17:58:05 -0700 | [diff] [blame] | 52 | const char kSystemCrashPath[] = "/data/misc/crash_reporter/crash"; |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 53 | const char kUploadVarPrefix[] = "upload_var_"; |
| 54 | const char kUploadFilePrefix[] = "upload_file_"; |
| 55 | |
Steve Fung | b3a48e1 | 2016-02-25 03:37:33 -0800 | [diff] [blame] | 56 | // Product information keys in the /etc/os-release.d folder. |
| 57 | static const char kBdkVersionKey[] = "bdk_version"; |
| 58 | static const char kProductIDKey[] = "product_id"; |
| 59 | static const char kProductVersionKey[] = "product_version"; |
| 60 | |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 61 | // Normally this path is not used. Unfortunately, there are a few edge cases |
| 62 | // where we need this. Any process that runs as kDefaultUserName that crashes |
| 63 | // is consider a "user crash". That includes the initial Chrome browser that |
| 64 | // runs the login screen. If that blows up, there is no logged in user yet, |
| 65 | // so there is no per-user dir for us to stash things in. Instead we fallback |
| 66 | // to this path as it is at least encrypted on a per-system basis. |
| 67 | // |
| 68 | // This also comes up when running autotests. The GUI is sitting at the login |
| 69 | // screen while tests are sshing in, changing users, and triggering crashes as |
| 70 | // the user (purposefully). |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 71 | const char kFallbackUserCrashPath[] = "/home/chronos/crash"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 72 | |
| 73 | // Directory mode of the user crash spool directory. |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 74 | const mode_t kUserCrashPathMode = 0755; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 75 | |
| 76 | // Directory mode of the system crash spool directory. |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 77 | const mode_t kSystemCrashPathMode = 01755; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 78 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 79 | const uid_t kRootOwner = 0; |
| 80 | const uid_t kRootGroup = 0; |
| 81 | |
| 82 | } // namespace |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 83 | |
Ken Mixter | da5db7a | 2010-09-17 13:50:42 -0700 | [diff] [blame] | 84 | // Maximum crash reports per crash spool directory. Note that this is |
| 85 | // a separate maximum from the maximum rate at which we upload these |
| 86 | // diagnostics. The higher this rate is, the more space we allow for |
| 87 | // core files, minidumps, and kcrash logs, and equivalently the more |
| 88 | // processor and I/O bandwidth we dedicate to handling these crashes when |
| 89 | // many occur at once. Also note that if core files are configured to |
| 90 | // be left on the file system, we stop adding crashes when either the |
| 91 | // number of core files or minidumps reaches this number. |
| 92 | const int CrashCollector::kMaxCrashDirectorySize = 32; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 93 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 94 | using base::FilePath; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 95 | using base::StringPrintf; |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 96 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 97 | CrashCollector::CrashCollector() |
Steve Fung | 8ed101b | 2015-12-02 16:18:07 -0800 | [diff] [blame] | 98 | : log_config_path_(kDefaultLogConfig) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | CrashCollector::~CrashCollector() { |
| 102 | } |
| 103 | |
| 104 | void CrashCollector::Initialize( |
| 105 | CrashCollector::CountCrashFunction count_crash_function, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 106 | CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) { |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 107 | CHECK(count_crash_function); |
| 108 | CHECK(is_feedback_allowed_function); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 109 | |
| 110 | count_crash_function_ = count_crash_function; |
| 111 | is_feedback_allowed_function_ = is_feedback_allowed_function; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 114 | int CrashCollector::WriteNewFile(const FilePath &filename, |
| 115 | const char *data, |
| 116 | int size) { |
| 117 | int fd = HANDLE_EINTR(open(filename.value().c_str(), |
| 118 | O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0666)); |
| 119 | if (fd < 0) { |
| 120 | return -1; |
| 121 | } |
| 122 | |
Alex Vakulenko | 859ee45 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 123 | int rv = base::WriteFileDescriptor(fd, data, size) ? size : -1; |
Mike Frysinger | f1a5014 | 2014-05-14 16:05:09 -0400 | [diff] [blame] | 124 | IGNORE_EINTR(close(fd)); |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 125 | return rv; |
| 126 | } |
| 127 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 128 | std::string CrashCollector::Sanitize(const std::string &name) { |
Thiemo Nagel | 9895096 | 2014-05-13 19:48:32 +0200 | [diff] [blame] | 129 | // Make sure the sanitized name does not include any periods. |
| 130 | // The logic in crash_sender relies on this. |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 131 | std::string result = name; |
| 132 | for (size_t i = 0; i < name.size(); ++i) { |
| 133 | if (!isalnum(result[i]) && result[i] != '_') |
| 134 | result[i] = '_'; |
| 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 139 | std::string CrashCollector::FormatDumpBasename(const std::string &exec_name, |
| 140 | time_t timestamp, |
| 141 | pid_t pid) { |
| 142 | struct tm tm; |
| 143 | localtime_r(×tamp, &tm); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 144 | std::string sanitized_exec_name = Sanitize(exec_name); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 145 | return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d", |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 146 | sanitized_exec_name.c_str(), |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 147 | tm.tm_year + 1900, |
| 148 | tm.tm_mon + 1, |
| 149 | tm.tm_mday, |
| 150 | tm.tm_hour, |
| 151 | tm.tm_min, |
| 152 | tm.tm_sec, |
| 153 | pid); |
| 154 | } |
| 155 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 156 | FilePath CrashCollector::GetCrashPath(const FilePath &crash_directory, |
| 157 | const std::string &basename, |
| 158 | const std::string &extension) { |
| 159 | return crash_directory.Append(StringPrintf("%s.%s", |
| 160 | basename.c_str(), |
| 161 | extension.c_str())); |
| 162 | } |
| 163 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 164 | FilePath CrashCollector::GetCrashDirectoryInfo( |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 165 | mode_t *mode, |
| 166 | uid_t *directory_owner, |
| 167 | gid_t *directory_group) { |
Steve Fung | ab2ac7d | 2015-08-14 17:58:05 -0700 | [diff] [blame] | 168 | *mode = kSystemCrashPathMode; |
| 169 | *directory_owner = kRootOwner; |
| 170 | *directory_group = kRootGroup; |
| 171 | return FilePath(kSystemCrashPath); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bool CrashCollector::GetUserInfoFromName(const std::string &name, |
| 175 | uid_t *uid, |
| 176 | gid_t *gid) { |
| 177 | char storage[256]; |
| 178 | struct passwd passwd_storage; |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 179 | struct passwd *passwd_result = nullptr; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 180 | |
| 181 | if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage), |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 182 | &passwd_result) != 0 || passwd_result == nullptr) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 183 | LOG(ERROR) << "Cannot find user named " << name; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
| 187 | *uid = passwd_result->pw_uid; |
| 188 | *gid = passwd_result->pw_gid; |
| 189 | return true; |
| 190 | } |
| 191 | |
James Hawkins | b2de40a | 2016-02-26 15:44:46 -0800 | [diff] [blame] | 192 | bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid __unused, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 193 | FilePath *crash_directory, |
| 194 | bool *out_of_capacity) { |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 195 | if (out_of_capacity) *out_of_capacity = false; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 196 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 197 | // For testing. |
Lei Zhang | 9b1f300 | 2014-04-24 02:10:57 -0700 | [diff] [blame] | 198 | if (!forced_crash_directory_.empty()) { |
| 199 | *crash_directory = forced_crash_directory_; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 200 | return true; |
| 201 | } |
| 202 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 203 | mode_t directory_mode; |
| 204 | uid_t directory_owner; |
| 205 | gid_t directory_group; |
| 206 | *crash_directory = |
Steve Fung | ab2ac7d | 2015-08-14 17:58:05 -0700 | [diff] [blame] | 207 | GetCrashDirectoryInfo(&directory_mode, |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 208 | &directory_owner, |
| 209 | &directory_group); |
| 210 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 211 | if (!base::PathExists(*crash_directory)) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 212 | // Create the spool directory with the appropriate mode (regardless of |
| 213 | // umask) and ownership. |
| 214 | mode_t old_mask = umask(0); |
| 215 | if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 || |
| 216 | chown(crash_directory->value().c_str(), |
| 217 | directory_owner, |
| 218 | directory_group) < 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 219 | LOG(ERROR) << "Unable to create appropriate crash directory"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | umask(old_mask); |
| 223 | } |
| 224 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 225 | if (!base::PathExists(*crash_directory)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 226 | LOG(ERROR) << "Unable to create crash directory " |
| 227 | << crash_directory->value().c_str(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 231 | if (!CheckHasCapacity(*crash_directory)) { |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 232 | if (out_of_capacity) *out_of_capacity = true; |
Steve Fung | ab2ac7d | 2015-08-14 17:58:05 -0700 | [diff] [blame] | 233 | LOG(ERROR) << "Directory " << crash_directory->value() |
| 234 | << " is out of capacity."; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 238 | return true; |
| 239 | } |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 240 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 241 | FilePath CrashCollector::GetProcessPath(pid_t pid) { |
| 242 | return FilePath(StringPrintf("/proc/%d", pid)); |
| 243 | } |
| 244 | |
| 245 | bool CrashCollector::GetSymlinkTarget(const FilePath &symlink, |
Ben Chan | 8563d20 | 2014-01-27 19:30:13 -0800 | [diff] [blame] | 246 | FilePath *target) { |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 247 | ssize_t max_size = 64; |
| 248 | std::vector<char> buffer; |
| 249 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 250 | while (true) { |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 251 | buffer.resize(max_size + 1); |
| 252 | ssize_t size = readlink(symlink.value().c_str(), buffer.data(), max_size); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 253 | if (size < 0) { |
| 254 | int saved_errno = errno; |
| 255 | LOG(ERROR) << "Readlink failed on " << symlink.value() << " with " |
| 256 | << saved_errno; |
| 257 | return false; |
| 258 | } |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 259 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 260 | buffer[size] = 0; |
| 261 | if (size == max_size) { |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 262 | max_size *= 2; |
| 263 | if (max_size > PATH_MAX) { |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 264 | return false; |
| 265 | } |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 266 | continue; |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 267 | } |
| 268 | break; |
| 269 | } |
| 270 | |
Ben Chan | f3811f5 | 2014-08-26 06:46:38 -0700 | [diff] [blame] | 271 | *target = FilePath(buffer.data()); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
| 275 | bool CrashCollector::GetExecutableBaseNameFromPid(pid_t pid, |
| 276 | std::string *base_name) { |
| 277 | FilePath target; |
| 278 | FilePath process_path = GetProcessPath(pid); |
| 279 | FilePath exe_path = process_path.Append("exe"); |
| 280 | if (!GetSymlinkTarget(exe_path, &target)) { |
| 281 | LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value() |
| 282 | << " DirectoryExists: " |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 283 | << base::DirectoryExists(process_path); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 284 | // Try to further diagnose exe readlink failure cause. |
| 285 | struct stat buf; |
| 286 | int stat_result = stat(exe_path.value().c_str(), &buf); |
| 287 | int saved_errno = errno; |
| 288 | if (stat_result < 0) { |
| 289 | LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result |
| 290 | << " " << saved_errno; |
| 291 | } else { |
| 292 | LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode=" |
| 293 | << buf.st_mode; |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | *base_name = target.BaseName().value(); |
| 298 | return true; |
| 299 | } |
| 300 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 301 | // Return true if the given crash directory has not already reached |
| 302 | // maximum capacity. |
| 303 | bool CrashCollector::CheckHasCapacity(const FilePath &crash_directory) { |
| 304 | DIR* dir = opendir(crash_directory.value().c_str()); |
| 305 | if (!dir) { |
Steve Fung | ab2ac7d | 2015-08-14 17:58:05 -0700 | [diff] [blame] | 306 | LOG(WARNING) << "Unable to open crash directory " |
| 307 | << crash_directory.value(); |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | struct dirent ent_buf; |
| 311 | struct dirent* ent; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 312 | bool full = false; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 313 | std::set<std::string> basenames; |
Ben Chan | 262d798 | 2014-09-18 08:05:20 -0700 | [diff] [blame] | 314 | while (readdir_r(dir, &ent_buf, &ent) == 0 && ent) { |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 315 | if ((strcmp(ent->d_name, ".") == 0) || |
| 316 | (strcmp(ent->d_name, "..") == 0)) |
| 317 | continue; |
| 318 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 319 | std::string filename(ent->d_name); |
| 320 | size_t last_dot = filename.rfind("."); |
| 321 | std::string basename; |
| 322 | // If there is a valid looking extension, use the base part of the |
| 323 | // name. If the only dot is the first byte (aka a dot file), treat |
| 324 | // it as unique to avoid allowing a directory full of dot files |
| 325 | // from accumulating. |
| 326 | if (last_dot != std::string::npos && last_dot != 0) |
| 327 | basename = filename.substr(0, last_dot); |
| 328 | else |
| 329 | basename = filename; |
| 330 | basenames.insert(basename); |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 331 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 332 | if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 333 | LOG(WARNING) << "Crash directory " << crash_directory.value() |
| 334 | << " already full with " << kMaxCrashDirectorySize |
| 335 | << " pending reports"; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 336 | full = true; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | closedir(dir); |
| 341 | return !full; |
| 342 | } |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 343 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 344 | bool CrashCollector::GetLogContents(const FilePath &config_path, |
| 345 | const std::string &exec_name, |
| 346 | const FilePath &output_file) { |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 347 | brillo::KeyValueStore store; |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 348 | if (!store.Load(config_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 349 | LOG(INFO) << "Unable to read log configuration file " |
| 350 | << config_path.value(); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 354 | std::string command; |
| 355 | if (!store.GetString(exec_name, &command)) |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 356 | return false; |
| 357 | |
Alex Vakulenko | 74dc624 | 2015-10-13 09:23:34 -0700 | [diff] [blame] | 358 | brillo::ProcessImpl diag_process; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 359 | diag_process.AddArg(kShellPath); |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 360 | diag_process.AddStringOption("-c", command); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 361 | diag_process.RedirectOutput(output_file.value()); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 362 | |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 363 | const int result = diag_process.Run(); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 364 | if (result != 0) { |
Daniel Erat | 731da33 | 2015-01-28 09:48:10 -0700 | [diff] [blame] | 365 | LOG(INFO) << "Log command \"" << command << "\" exited with " << result; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 366 | return false; |
| 367 | } |
| 368 | return true; |
| 369 | } |
| 370 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 371 | void CrashCollector::AddCrashMetaData(const std::string &key, |
| 372 | const std::string &value) { |
| 373 | extra_metadata_.append(StringPrintf("%s=%s\n", key.c_str(), value.c_str())); |
| 374 | } |
| 375 | |
Albert Chaulk | 33dfd47 | 2013-06-19 15:34:13 -0700 | [diff] [blame] | 376 | void CrashCollector::AddCrashMetaUploadFile(const std::string &key, |
| 377 | const std::string &path) { |
| 378 | if (!path.empty()) |
| 379 | AddCrashMetaData(kUploadFilePrefix + key, path); |
| 380 | } |
| 381 | |
| 382 | void CrashCollector::AddCrashMetaUploadData(const std::string &key, |
| 383 | const std::string &value) { |
| 384 | if (!value.empty()) |
| 385 | AddCrashMetaData(kUploadVarPrefix + key, value); |
| 386 | } |
| 387 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 388 | void CrashCollector::WriteCrashMetaData(const FilePath &meta_path, |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 389 | const std::string &exec_name, |
| 390 | const std::string &payload_path) { |
Ben Chan | f84ea21 | 2014-08-06 17:27:48 -0700 | [diff] [blame] | 391 | int64_t payload_size = -1; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 392 | base::GetFileSize(FilePath(payload_path), &payload_size); |
Steve Fung | b3a48e1 | 2016-02-25 03:37:33 -0800 | [diff] [blame] | 393 | |
| 394 | brillo::OsReleaseReader reader; |
| 395 | if (!forced_osreleased_directory_.empty()) { |
| 396 | reader.LoadTestingOnly(forced_osreleased_directory_); |
| 397 | } else { |
| 398 | reader.Load(); |
| 399 | } |
| 400 | std::string bdk_version = "undefined"; |
| 401 | std::string product_id = "undefined"; |
| 402 | std::string product_version = "undefined"; |
| 403 | |
| 404 | if (!reader.GetString(kBdkVersionKey, &bdk_version)) { |
| 405 | LOG(ERROR) << "Could not read " << kBdkVersionKey |
| 406 | << " from /etc/os-release.d/"; |
| 407 | } |
| 408 | |
| 409 | if (!reader.GetString(kProductIDKey, &product_id)) { |
| 410 | LOG(ERROR) << "Could not read " << kProductIDKey |
| 411 | << " from /etc/os-release.d/"; |
| 412 | } |
| 413 | |
| 414 | if (!reader.GetString(kProductVersionKey, &product_version)) { |
| 415 | LOG(ERROR) << "Could not read " << kProductVersionKey |
| 416 | << " from /etc/os-release.d/"; |
| 417 | } |
| 418 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 419 | std::string meta_data = StringPrintf("%sexec_name=%s\n" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 420 | "payload=%s\n" |
Alex Vakulenko | eaf060c | 2014-08-08 09:36:48 -0700 | [diff] [blame] | 421 | "payload_size=%" PRId64 "\n" |
Steve Fung | b3a48e1 | 2016-02-25 03:37:33 -0800 | [diff] [blame] | 422 | "%s=%s\n" |
| 423 | "%s=%s\n" |
| 424 | "%s=%s\n" |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 425 | "done=1\n", |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 426 | extra_metadata_.c_str(), |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 427 | exec_name.c_str(), |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 428 | payload_path.c_str(), |
Steve Fung | b3a48e1 | 2016-02-25 03:37:33 -0800 | [diff] [blame] | 429 | payload_size, |
| 430 | kBdkVersionKey, |
| 431 | bdk_version.c_str(), |
| 432 | kProductIDKey, |
| 433 | product_id.c_str(), |
| 434 | kProductVersionKey, |
| 435 | product_version.c_str()); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 436 | // We must use WriteNewFile instead of base::WriteFile as we |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 437 | // do not want to write with root access to a symlink that an attacker |
| 438 | // might have created. |
| 439 | if (WriteNewFile(meta_path, meta_data.c_str(), meta_data.size()) < 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 440 | LOG(ERROR) << "Unable to write " << meta_path.value(); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
Thieu Le | 1652fb2 | 2011-03-03 12:14:43 -0800 | [diff] [blame] | 443 | |
| 444 | bool CrashCollector::IsCrashTestInProgress() { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 445 | return base::PathExists(FilePath(kCrashTestInProgressPath)); |
Thieu Le | 1652fb2 | 2011-03-03 12:14:43 -0800 | [diff] [blame] | 446 | } |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 447 | |
| 448 | bool CrashCollector::IsDeveloperImage() { |
| 449 | // If we're testing crash reporter itself, we don't want to special-case |
| 450 | // for developer images. |
| 451 | if (IsCrashTestInProgress()) |
| 452 | return false; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 453 | return base::PathExists(FilePath(kLeaveCoreFile)); |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 454 | } |