OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "crash-reporter/crash_collector.h" |
| 6 |
| 7 #include <pwd.h> // For struct passwd. |
| 8 #include <sys/types.h> // for mode_t. |
| 9 |
| 10 #include "base/file_util.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/string_util.h" |
| 13 #include "crash-reporter/system_logging.h" |
| 14 |
| 15 static const char kDefaultUserName[] = "chronos"; |
| 16 static const char kSystemCrashPath[] = "/var/spool/crash"; |
| 17 static const char kUserCrashPath[] = "/home/chronos/user/crash"; |
| 18 |
| 19 // Directory mode of the user crash spool directory. |
| 20 static const mode_t kUserCrashPathMode = 0755; |
| 21 |
| 22 // Directory mode of the system crash spool directory. |
| 23 static const mode_t kSystemCrashPathMode = 01755; |
| 24 |
| 25 static const uid_t kRootOwner = 0; |
| 26 static const uid_t kRootGroup = 0; |
| 27 |
| 28 CrashCollector::CrashCollector() : forced_crash_directory_(NULL) { |
| 29 } |
| 30 |
| 31 CrashCollector::~CrashCollector() { |
| 32 } |
| 33 |
| 34 void CrashCollector::Initialize( |
| 35 CrashCollector::CountCrashFunction count_crash_function, |
| 36 CrashCollector::IsFeedbackAllowedFunction is_feedback_allowed_function, |
| 37 SystemLogging *logger) { |
| 38 CHECK(count_crash_function != NULL); |
| 39 CHECK(is_feedback_allowed_function != NULL); |
| 40 CHECK(logger != NULL); |
| 41 |
| 42 count_crash_function_ = count_crash_function; |
| 43 is_feedback_allowed_function_ = is_feedback_allowed_function; |
| 44 logger_ = logger; |
| 45 } |
| 46 |
| 47 std::string CrashCollector::FormatDumpBasename(const std::string &exec_name, |
| 48 time_t timestamp, |
| 49 pid_t pid) { |
| 50 struct tm tm; |
| 51 localtime_r(×tamp, &tm); |
| 52 return StringPrintf("%s.%04d%02d%02d.%02d%02d%02d.%d", |
| 53 exec_name.c_str(), |
| 54 tm.tm_year + 1900, |
| 55 tm.tm_mon + 1, |
| 56 tm.tm_mday, |
| 57 tm.tm_hour, |
| 58 tm.tm_min, |
| 59 tm.tm_sec, |
| 60 pid); |
| 61 } |
| 62 |
| 63 FilePath CrashCollector::GetCrashDirectoryInfo( |
| 64 uid_t process_euid, |
| 65 uid_t default_user_id, |
| 66 gid_t default_user_group, |
| 67 mode_t *mode, |
| 68 uid_t *directory_owner, |
| 69 gid_t *directory_group) { |
| 70 if (process_euid == default_user_id) { |
| 71 *mode = kUserCrashPathMode; |
| 72 *directory_owner = default_user_id; |
| 73 *directory_group = default_user_group; |
| 74 return FilePath(kUserCrashPath); |
| 75 } else { |
| 76 *mode = kSystemCrashPathMode; |
| 77 *directory_owner = kRootOwner; |
| 78 *directory_group = kRootGroup; |
| 79 return FilePath(kSystemCrashPath); |
| 80 } |
| 81 } |
| 82 |
| 83 bool CrashCollector::GetUserInfoFromName(const std::string &name, |
| 84 uid_t *uid, |
| 85 gid_t *gid) { |
| 86 char storage[256]; |
| 87 struct passwd passwd_storage; |
| 88 struct passwd *passwd_result = NULL; |
| 89 |
| 90 if (getpwnam_r(name.c_str(), &passwd_storage, storage, sizeof(storage), |
| 91 &passwd_result) != 0 || passwd_result == NULL) { |
| 92 logger_->LogError("Cannot find user named %s", name.c_str()); |
| 93 return false; |
| 94 } |
| 95 |
| 96 *uid = passwd_result->pw_uid; |
| 97 *gid = passwd_result->pw_gid; |
| 98 return true; |
| 99 } |
| 100 |
| 101 bool CrashCollector::GetCreatedCrashDirectoryByEuid(uid_t euid, |
| 102 FilePath *crash_directory) { |
| 103 uid_t default_user_id; |
| 104 gid_t default_user_group; |
| 105 |
| 106 // For testing. |
| 107 if (forced_crash_directory_ != NULL) { |
| 108 *crash_directory = FilePath(forced_crash_directory_); |
| 109 return true; |
| 110 } |
| 111 |
| 112 if (!GetUserInfoFromName(kDefaultUserName, |
| 113 &default_user_id, |
| 114 &default_user_group)) { |
| 115 logger_->LogError("Could not find default user info"); |
| 116 return false; |
| 117 } |
| 118 mode_t directory_mode; |
| 119 uid_t directory_owner; |
| 120 gid_t directory_group; |
| 121 *crash_directory = |
| 122 GetCrashDirectoryInfo(euid, |
| 123 default_user_id, |
| 124 default_user_group, |
| 125 &directory_mode, |
| 126 &directory_owner, |
| 127 &directory_group); |
| 128 |
| 129 if (!file_util::PathExists(*crash_directory)) { |
| 130 // Create the spool directory with the appropriate mode (regardless of |
| 131 // umask) and ownership. |
| 132 mode_t old_mask = umask(0); |
| 133 if (mkdir(crash_directory->value().c_str(), directory_mode) < 0 || |
| 134 chown(crash_directory->value().c_str(), |
| 135 directory_owner, |
| 136 directory_group) < 0) { |
| 137 logger_->LogError("Unable to create appropriate crash directory"); |
| 138 return false; |
| 139 } |
| 140 umask(old_mask); |
| 141 } |
| 142 |
| 143 if (!file_util::PathExists(*crash_directory)) { |
| 144 logger_->LogError("Unable to create crash directory %s", |
| 145 crash_directory->value().c_str()); |
| 146 return false; |
| 147 } |
| 148 |
| 149 return true; |
| 150 } |
OLD | NEW |