Steve Fung | 6c34c25 | 2015-08-20 00:27:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 16 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 17 | #ifndef CRASH_REPORTER_USER_COLLECTOR_H_ |
| 18 | #define CRASH_REPORTER_USER_COLLECTOR_H_ |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 19 | |
| 20 | #include <string> |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 21 | #include <vector> |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 22 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 23 | #include <base/files/file_path.h> |
Ben Chan | 895fa5d | 2014-09-02 20:58:20 -0700 | [diff] [blame] | 24 | #include <base/macros.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 25 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 26 | |
Steve Fung | 129bea5 | 2015-07-23 13:11:15 -0700 | [diff] [blame] | 27 | #include "crash_collector.h" |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 28 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 29 | class SystemLogging; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 30 | |
| 31 | // User crash collector. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 32 | class UserCollector : public CrashCollector { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 33 | public: |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 34 | UserCollector(); |
| 35 | |
| 36 | // Initialize the user crash collector for detection of crashes, |
| 37 | // given a crash counting function, the path to this executable, |
| 38 | // metrics collection enabled oracle, and system logger facility. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 39 | // Crash detection/reporting is not enabled until Enable is called. |
| 40 | // |generate_diagnostics| is used to indicate whether or not to try |
| 41 | // to generate a minidump from crashes. |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 42 | void Initialize(CountCrashFunction count_crash, |
| 43 | const std::string &our_path, |
| 44 | IsFeedbackAllowedFunction is_metrics_allowed, |
Steve Fung | d6169a2 | 2014-08-11 15:52:23 -0700 | [diff] [blame] | 45 | bool generate_diagnostics, |
| 46 | bool core2md_failure, |
| 47 | bool directory_failure, |
| 48 | const std::string &filter_in); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 49 | |
Ben Chan | efec0b3 | 2014-08-12 08:52:11 -0700 | [diff] [blame] | 50 | ~UserCollector() override; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 51 | |
| 52 | // Enable collection. |
| 53 | bool Enable() { return SetUpInternal(true); } |
| 54 | |
| 55 | // Disable collection. |
| 56 | bool Disable() { return SetUpInternal(false); } |
| 57 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 58 | // Handle a specific user crash. Returns true on success. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 59 | bool HandleCrash(const std::string &crash_attributes, |
| 60 | const char *force_exec); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 61 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 62 | private: |
| 63 | friend class UserCollectorTest; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 64 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath); |
| 65 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid); |
| 66 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK); |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 67 | FRIEND_TEST(UserCollectorTest, GetExecutableBaseNameFromPid); |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 68 | FRIEND_TEST(UserCollectorTest, GetFirstLineWithPrefix); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 69 | FRIEND_TEST(UserCollectorTest, GetIdFromStatus); |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 70 | FRIEND_TEST(UserCollectorTest, GetStateFromStatus); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 71 | FRIEND_TEST(UserCollectorTest, GetProcessPath); |
| 72 | FRIEND_TEST(UserCollectorTest, GetSymlinkTarget); |
| 73 | FRIEND_TEST(UserCollectorTest, GetUserInfoFromName); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 74 | FRIEND_TEST(UserCollectorTest, ParseCrashAttributes); |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 75 | FRIEND_TEST(UserCollectorTest, ShouldDumpChromeOverridesDeveloperImage); |
| 76 | FRIEND_TEST(UserCollectorTest, ShouldDumpDeveloperImageOverridesConsent); |
| 77 | FRIEND_TEST(UserCollectorTest, ShouldDumpUseConsentProductionImage); |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 78 | FRIEND_TEST(UserCollectorTest, ValidateProcFiles); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 79 | FRIEND_TEST(UserCollectorTest, ValidateCoreFile); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 80 | |
| 81 | // Enumeration to pass to GetIdFromStatus. Must match the order |
| 82 | // that the kernel lists IDs in the status file. |
| 83 | enum IdKind { |
| 84 | kIdReal = 0, // uid and gid |
| 85 | kIdEffective = 1, // euid and egid |
| 86 | kIdSet = 2, // suid and sgid |
| 87 | kIdFileSystem = 3, // fsuid and fsgid |
| 88 | kIdMax |
| 89 | }; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 90 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 91 | enum ErrorType { |
| 92 | kErrorNone, |
| 93 | kErrorSystemIssue, |
| 94 | kErrorReadCoreData, |
| 95 | kErrorUnusableProcFiles, |
| 96 | kErrorInvalidCoreFile, |
| 97 | kErrorUnsupported32BitCoreFile, |
| 98 | kErrorCore2MinidumpConversion, |
| 99 | }; |
| 100 | |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 101 | static const int kForkProblem = 255; |
| 102 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 103 | // Returns an error type signature for a given |error_type| value, |
| 104 | // which is reported to the crash server along with the |
| 105 | // crash_reporter-user-collection signature. |
| 106 | std::string GetErrorTypeSignature(ErrorType error_type) const; |
| 107 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 108 | std::string GetPattern(bool enabled) const; |
| 109 | bool SetUpInternal(bool enabled); |
| 110 | |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 111 | // Returns, via |line|, the first line in |lines| that starts with |prefix|. |
| 112 | // Returns true if a line is found, or false otherwise. |
| 113 | bool GetFirstLineWithPrefix(const std::vector<std::string> &lines, |
| 114 | const char *prefix, std::string *line); |
| 115 | |
| 116 | // Returns the identifier of |kind|, via |id|, found in |status_lines| on |
| 117 | // the line starting with |prefix|. |status_lines| contains the lines in |
| 118 | // the status file. Returns true if the identifier can be determined. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 119 | bool GetIdFromStatus(const char *prefix, |
| 120 | IdKind kind, |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 121 | const std::vector<std::string> &status_lines, |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 122 | int *id); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 123 | |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 124 | // Returns the process state, via |state|, found in |status_lines|, which |
| 125 | // contains the lines in the status file. Returns true if the process state |
| 126 | // can be determined. |
| 127 | bool GetStateFromStatus(const std::vector<std::string> &status_lines, |
| 128 | std::string *state); |
| 129 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 130 | void LogCollectionError(const std::string &error_message); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 131 | void EnqueueCollectionErrorLog(pid_t pid, ErrorType error_type, |
| 132 | const std::string &exec_name); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 133 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 134 | bool CopyOffProcFiles(pid_t pid, const base::FilePath &container_dir); |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 135 | |
| 136 | // Validates the proc files at |container_dir| and returns true if they |
| 137 | // are usable for the core-to-minidump conversion later. For instance, if |
| 138 | // a process is reaped by the kernel before the copying of its proc files |
| 139 | // takes place, some proc files like /proc/<pid>/maps may contain nothing |
| 140 | // and thus become unusable. |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 141 | bool ValidateProcFiles(const base::FilePath &container_dir) const; |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 142 | |
| 143 | // Validates the core file at |core_path| and returns kErrorNone if |
| 144 | // the file contains the ELF magic bytes and an ELF class that matches the |
| 145 | // platform (i.e. 32-bit ELF on a 32-bit platform or 64-bit ELF on a 64-bit |
| 146 | // platform), which is due to the limitation in core2md. It returns an error |
| 147 | // type otherwise. |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 148 | ErrorType ValidateCoreFile(const base::FilePath &core_path) const; |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 149 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 150 | // Determines the crash directory for given pid based on pid's owner, |
| 151 | // and creates the directory if necessary with appropriate permissions. |
| 152 | // Returns true whether or not directory needed to be created, false on |
| 153 | // any failure. |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 154 | bool GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid, |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 155 | base::FilePath *crash_file_path, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 156 | bool *out_of_capacity); |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 157 | bool CopyStdinToCoreFile(const base::FilePath &core_path); |
| 158 | bool RunCoreToMinidump(const base::FilePath &core_path, |
| 159 | const base::FilePath &procfs_directory, |
| 160 | const base::FilePath &minidump_path, |
| 161 | const base::FilePath &temp_directory); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 162 | ErrorType ConvertCoreToMinidump(pid_t pid, |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 163 | const base::FilePath &container_dir, |
| 164 | const base::FilePath &core_path, |
| 165 | const base::FilePath &minidump_path); |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 166 | ErrorType ConvertAndEnqueueCrash(pid_t pid, const std::string &exec_name, |
| 167 | uid_t supplied_ruid, bool *out_of_capacity); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 168 | bool ParseCrashAttributes(const std::string &crash_attributes, |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 169 | pid_t *pid, int *signal, uid_t *uid, |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 170 | std::string *kernel_supplied_name); |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame] | 171 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 172 | bool ShouldDump(bool has_owner_consent, |
| 173 | bool is_developer, |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 174 | std::string *reason); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 175 | |
| 176 | bool generate_diagnostics_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 177 | std::string our_path_; |
| 178 | bool initialized_; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 179 | |
Steve Fung | d6169a2 | 2014-08-11 15:52:23 -0700 | [diff] [blame] | 180 | bool core2md_failure_; |
| 181 | bool directory_failure_; |
| 182 | std::string filter_in_; |
| 183 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 184 | static const char *kUserId; |
| 185 | static const char *kGroupId; |
Ben Chan | 895fa5d | 2014-09-02 20:58:20 -0700 | [diff] [blame] | 186 | |
| 187 | DISALLOW_COPY_AND_ASSIGN(UserCollector); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 188 | }; |
| 189 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 190 | #endif // CRASH_REPORTER_USER_COLLECTOR_H_ |