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