Simon Que | f70060c | 2012-04-09 19:07:07 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 2 | // 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 Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 7 | #include <dirent.h> |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 8 | #include <fcntl.h> // For file creation modes. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 9 | #include <pwd.h> // For struct passwd. |
| 10 | #include <sys/types.h> // for mode_t. |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 11 | #include <sys/wait.h> // For waitpid. |
| 12 | #include <unistd.h> // For execv and fork. |
Mike Frysinger | 65b4c1e | 2011-09-21 12:41:29 -0400 | [diff] [blame] | 13 | #define __STDC_FORMAT_MACROS // PRId64 |
| 14 | #include <inttypes.h> |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 15 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 16 | #include <set> |
Simon Que | f70060c | 2012-04-09 19:07:07 -0700 | [diff] [blame] | 17 | #include <vector> |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 18 | |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 19 | #include <dbus/dbus-glib-lowlevel.h> |
| 20 | #include <glib.h> |
| 21 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 22 | #include "base/file_util.h" |
| 23 | #include "base/logging.h" |
Mike Frysinger | 1a8780d | 2013-02-14 22:39:57 -0500 | [diff] [blame] | 24 | #include "base/posix/eintr_wrapper.h" |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 25 | #include "base/strings/string_split.h" |
| 26 | #include "base/strings/string_util.h" |
| 27 | #include "base/strings/stringprintf.h" |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 28 | #include "chromeos/cryptohome.h" |
| 29 | #include "chromeos/dbus/dbus.h" |
| 30 | #include "chromeos/dbus/service_constants.h" |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 31 | #include "chromeos/process.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 32 | |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 33 | static const char kCollectChromeFile[] = |
| 34 | "/mnt/stateful_partition/etc/collect_chrome_crashes"; |
| 35 | static const char kCrashTestInProgressPath[] = "/tmp/crash-test-in-progress"; |
Simon Que | f70060c | 2012-04-09 19:07:07 -0700 | [diff] [blame] | 36 | static const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 37 | static const char kDefaultUserName[] = "chronos"; |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 38 | static const char kLeaveCoreFile[] = "/root/.leave_core"; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 39 | static const char kLsbRelease[] = "/etc/lsb-release"; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 40 | static const char kShellPath[] = "/bin/sh"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 41 | static const char kSystemCrashPath[] = "/var/spool/crash"; |
Albert Chaulk | 33dfd47 | 2013-06-19 15:34:13 -0700 | [diff] [blame] | 42 | static const char kUploadVarPrefix[] = "upload_var_"; |
| 43 | static const char kUploadFilePrefix[] = "upload_file_"; |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 44 | // 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). |
| 54 | static const char kFallbackUserCrashPath[] = "/home/chronos/crash"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 55 | |
| 56 | // Directory mode of the user crash spool directory. |
| 57 | static const mode_t kUserCrashPathMode = 0755; |
| 58 | |
| 59 | // Directory mode of the system crash spool directory. |
| 60 | static const mode_t kSystemCrashPathMode = 01755; |
| 61 | |
| 62 | static const uid_t kRootOwner = 0; |
| 63 | static const uid_t kRootGroup = 0; |
| 64 | |
Ken Mixter | da5db7a | 2010-09-17 13:50:42 -0700 | [diff] [blame] | 65 | // 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. |
| 73 | const int CrashCollector::kMaxCrashDirectorySize = 32; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 74 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 75 | using base::FilePath; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 76 | using base::StringPrintf; |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 77 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 78 | CrashCollector::CrashCollector() |
| 79 | : forced_crash_directory_(NULL), |
Simon Que | acc7938 | 2012-05-04 18:10:09 -0700 | [diff] [blame] | 80 | lsb_release_(kLsbRelease), |
| 81 | log_config_path_(kDefaultLogConfig) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | CrashCollector::~CrashCollector() { |
| 85 | } |
| 86 | |
| 87 | void CrashCollector::Initialize( |
| 88 | CrashCollector::CountCrashFunction count_crash_function, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 89 | CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 90 | CHECK(count_crash_function != NULL); |
| 91 | CHECK(is_feedback_allowed_function != NULL); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 92 | |
| 93 | count_crash_function_ = count_crash_function; |
| 94 | is_feedback_allowed_function_ = is_feedback_allowed_function; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 97 | int 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 Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 111 | std::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 Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 120 | std::string CrashCollector::FormatDumpBasename(const std::string &exec_name, |
| 121 | time_t timestamp, |
| 122 | pid_t pid) { |
| 123 | struct tm tm; |
| 124 | localtime_r(×tamp, &tm); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 125 | std::string sanitized_exec_name = Sanitize(exec_name); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 126 | return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d", |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 127 | sanitized_exec_name.c_str(), |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 128 | 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 Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 137 | FilePath 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 Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 145 | namespace { |
| 146 | |
| 147 | const char *GetGErrorMessage(const GError *error) { |
| 148 | if (!error) |
| 149 | return "Unknown error."; |
| 150 | return error->message; |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | GHashTable *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 | |
| 190 | FilePath 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 Frysinger | 37843a9 | 2013-06-11 17:03:59 -0400 | [diff] [blame] | 202 | user_path = chromeos::cryptohome::home::GetHashedUserPath(salted_path) |
| 203 | .Append("crash"); |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 204 | g_list_free(list); |
| 205 | } |
| 206 | |
| 207 | g_hash_table_destroy(active_sessions); |
| 208 | |
| 209 | return user_path; |
| 210 | } |
| 211 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 212 | FilePath 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 Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 219 | // 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 Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 226 | *mode = kUserCrashPathMode; |
| 227 | *directory_owner = default_user_id; |
| 228 | *directory_group = default_user_group; |
Mike Frysinger | f19b518 | 2013-05-17 19:36:47 -0400 | [diff] [blame] | 229 | return GetUserCrashPath(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 230 | } else { |
| 231 | *mode = kSystemCrashPathMode; |
| 232 | *directory_owner = kRootOwner; |
| 233 | *directory_group = kRootGroup; |
| 234 | return FilePath(kSystemCrashPath); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool 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 Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 247 | LOG(ERROR) << "Cannot find user named " << name; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | |
| 251 | *uid = passwd_result->pw_uid; |
| 252 | *gid = passwd_result->pw_gid; |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 257 | FilePath *crash_directory, |
| 258 | bool *out_of_capacity) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 259 | uid_t default_user_id; |
| 260 | gid_t default_user_group; |
| 261 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 262 | if (out_of_capacity != NULL) *out_of_capacity = false; |
| 263 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 264 | // 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 Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 273 | LOG(ERROR) << "Could not find default user info"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 274 | 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 Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 287 | if (!base::PathExists(*crash_directory)) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 288 | // 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 Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 295 | LOG(ERROR) << "Unable to create appropriate crash directory"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | umask(old_mask); |
| 299 | } |
| 300 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 301 | if (!base::PathExists(*crash_directory)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 302 | LOG(ERROR) << "Unable to create crash directory " |
| 303 | << crash_directory->value().c_str(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 304 | return false; |
| 305 | } |
| 306 | |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 307 | if (!CheckHasCapacity(*crash_directory)) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 308 | if (out_of_capacity != NULL) *out_of_capacity = true; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 312 | return true; |
| 313 | } |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 314 | |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 315 | FilePath CrashCollector::GetProcessPath(pid_t pid) { |
| 316 | return FilePath(StringPrintf("/proc/%d", pid)); |
| 317 | } |
| 318 | |
| 319 | bool CrashCollector::GetSymlinkTarget(const FilePath &symlink, |
Ben Chan | 8563d20 | 2014-01-27 19:30:13 -0800 | [diff] [blame] | 320 | FilePath *target) { |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 321 | int max_size = 32; |
Ben Chan | 8563d20 | 2014-01-27 19:30:13 -0800 | [diff] [blame] | 322 | scoped_ptr<char[]> buffer; |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 323 | 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 | |
| 349 | bool 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 Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 357 | << base::DirectoryExists(process_path); |
Albert Chaulk | 426fcc0 | 2013-05-02 15:38:31 -0700 | [diff] [blame] | 358 | // 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 Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 375 | // Return true if the given crash directory has not already reached |
| 376 | // maximum capacity. |
| 377 | bool 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 Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 384 | bool full = false; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 385 | std::set<std::string> basenames; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 386 | 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 Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 391 | 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 Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 403 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 404 | if (basenames.size() >= static_cast<size_t>(kMaxCrashDirectorySize)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 405 | LOG(WARNING) << "Crash directory " << crash_directory.value() |
| 406 | << " already full with " << kMaxCrashDirectorySize |
| 407 | << " pending reports"; |
Ken Mixter | 04ec10f | 2010-08-26 16:02:02 -0700 | [diff] [blame] | 408 | full = true; |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | closedir(dir); |
| 413 | return !full; |
| 414 | } |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 415 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 416 | bool 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 Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 421 | bool CrashCollector::ReadKeyValueFile( |
| 422 | const FilePath &path, |
| 423 | const char separator, |
| 424 | std::map<std::string, std::string> *dictionary) { |
| 425 | std::string contents; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 426 | if (!base::ReadFileToString(path, &contents)) { |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 427 | return false; |
| 428 | } |
| 429 | typedef std::vector<std::string> StringVector; |
| 430 | StringVector lines; |
Chris Masone | 3ba6c5b | 2011-05-13 16:57:09 -0700 | [diff] [blame] | 431 | base::SplitString(contents, '\n', &lines); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 432 | 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 Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 438 | // Allow comment lines. |
| 439 | if (IsCommentLine(*line)) |
| 440 | continue; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 441 | StringVector sides; |
Chris Masone | 3ba6c5b | 2011-05-13 16:57:09 -0700 | [diff] [blame] | 442 | base::SplitString(*line, separator, &sides); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 443 | 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 Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 452 | bool 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 Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 457 | LOG(INFO) << "Unable to read log configuration file " |
| 458 | << config_path.value(); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 459 | return false; |
| 460 | } |
| 461 | |
| 462 | if (log_commands.find(exec_name) == log_commands.end()) |
| 463 | return false; |
| 464 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 465 | chromeos::ProcessImpl diag_process; |
| 466 | diag_process.AddArg(kShellPath); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 467 | std::string shell_command = log_commands[exec_name]; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 468 | diag_process.AddStringOption("-c", shell_command); |
| 469 | diag_process.RedirectOutput(output_file.value()); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 470 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 471 | int result = diag_process.Run(); |
| 472 | if (result != 0) { |
| 473 | LOG(INFO) << "Running shell command " << shell_command << "failed with: " |
| 474 | << result; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 480 | void 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 Chaulk | 33dfd47 | 2013-06-19 15:34:13 -0700 | [diff] [blame] | 485 | void CrashCollector::AddCrashMetaUploadFile(const std::string &key, |
| 486 | const std::string &path) { |
| 487 | if (!path.empty()) |
| 488 | AddCrashMetaData(kUploadFilePrefix + key, path); |
| 489 | } |
| 490 | |
| 491 | void CrashCollector::AddCrashMetaUploadData(const std::string &key, |
| 492 | const std::string &value) { |
| 493 | if (!value.empty()) |
| 494 | AddCrashMetaData(kUploadVarPrefix + key, value); |
| 495 | } |
| 496 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 497 | void CrashCollector::WriteCrashMetaData(const FilePath &meta_path, |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 498 | const std::string &exec_name, |
| 499 | const std::string &payload_path) { |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 500 | std::map<std::string, std::string> contents; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 501 | if (!ReadKeyValueFile(FilePath(std::string(lsb_release_)), '=', &contents)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 502 | LOG(ERROR) << "Problem parsing " << lsb_release_; |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 503 | // 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 Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 510 | int64 payload_size = -1; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 511 | base::GetFileSize(FilePath(payload_path), &payload_size); |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 512 | std::string meta_data = StringPrintf("%sexec_name=%s\n" |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 513 | "ver=%s\n" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 514 | "payload=%s\n" |
Mike Frysinger | 65b4c1e | 2011-09-21 12:41:29 -0400 | [diff] [blame] | 515 | "payload_size=%"PRId64"\n" |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 516 | "done=1\n", |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 517 | extra_metadata_.c_str(), |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 518 | exec_name.c_str(), |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 519 | version.c_str(), |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 520 | payload_path.c_str(), |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 521 | payload_size); |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 522 | // 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 Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 526 | LOG(ERROR) << "Unable to write " << meta_path.value(); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 527 | } |
| 528 | } |
Thieu Le | 1652fb2 | 2011-03-03 12:14:43 -0800 | [diff] [blame] | 529 | |
| 530 | bool CrashCollector::IsCrashTestInProgress() { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 531 | return base::PathExists(FilePath(kCrashTestInProgressPath)); |
Thieu Le | 1652fb2 | 2011-03-03 12:14:43 -0800 | [diff] [blame] | 532 | } |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 533 | |
| 534 | bool 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 Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 539 | return base::PathExists(FilePath(kLeaveCoreFile)); |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | bool 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 Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame^] | 550 | if (base::PathExists(FilePath(kCollectChromeFile))) |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 551 | return true; |
| 552 | } |
| 553 | // We default to ignoring chrome crashes. |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | bool CrashCollector::IsUserSpecificDirectoryEnabled() { |
| 558 | return !ShouldHandleChromeCrashes(); |
| 559 | } |