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 | SystemLogging *logger, |
| 32 | bool generate_diagnostics); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 33 | |
| 34 | virtual ~UserCollector(); |
| 35 | |
| 36 | // Enable collection. |
| 37 | bool Enable() { return SetUpInternal(true); } |
| 38 | |
| 39 | // Disable collection. |
| 40 | bool Disable() { return SetUpInternal(false); } |
| 41 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 42 | // Handle a specific user crash. Returns true on success. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame^] | 43 | bool HandleCrash(const std::string &crash_attributes, |
| 44 | const char *force_exec); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 45 | |
| 46 | // Set (override the default) core file pattern. |
| 47 | void set_core_pattern_file(const std::string &pattern) { |
| 48 | core_pattern_file_ = pattern; |
| 49 | } |
| 50 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 51 | // Set (override the default) core pipe limit file. |
| 52 | void set_core_pipe_limit_file(const std::string &path) { |
| 53 | core_pipe_limit_file_ = path; |
| 54 | } |
| 55 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 56 | private: |
| 57 | friend class UserCollectorTest; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 58 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath); |
| 59 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid); |
| 60 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 61 | FRIEND_TEST(UserCollectorTest, GetIdFromStatus); |
| 62 | FRIEND_TEST(UserCollectorTest, GetProcessPath); |
| 63 | FRIEND_TEST(UserCollectorTest, GetSymlinkTarget); |
| 64 | FRIEND_TEST(UserCollectorTest, GetUserInfoFromName); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame^] | 65 | FRIEND_TEST(UserCollectorTest, ParseCrashAttributes); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 66 | |
| 67 | // Enumeration to pass to GetIdFromStatus. Must match the order |
| 68 | // that the kernel lists IDs in the status file. |
| 69 | enum IdKind { |
| 70 | kIdReal = 0, // uid and gid |
| 71 | kIdEffective = 1, // euid and egid |
| 72 | kIdSet = 2, // suid and sgid |
| 73 | kIdFileSystem = 3, // fsuid and fsgid |
| 74 | kIdMax |
| 75 | }; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 76 | |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 77 | static const int kForkProblem = 255; |
| 78 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 79 | std::string GetPattern(bool enabled) const; |
| 80 | bool SetUpInternal(bool enabled); |
| 81 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 82 | FilePath GetProcessPath(pid_t pid); |
| 83 | bool GetSymlinkTarget(const FilePath &symlink, |
| 84 | FilePath *target); |
| 85 | bool GetExecutableBaseNameFromPid(uid_t pid, |
| 86 | std::string *base_name); |
| 87 | bool GetIdFromStatus(const char *prefix, |
| 88 | IdKind kind, |
| 89 | const std::string &status_contents, |
| 90 | int *id); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 91 | |
| 92 | void LogCollectionError(const std::string &error_message); |
| 93 | void EnqueueCollectionErrorLog(pid_t pid, const std::string &exec_name); |
| 94 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 95 | bool CopyOffProcFiles(pid_t pid, const FilePath &process_map); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 96 | // Determines the crash directory for given pid based on pid's owner, |
| 97 | // and creates the directory if necessary with appropriate permissions. |
| 98 | // Returns true whether or not directory needed to be created, false on |
| 99 | // any failure. |
| 100 | bool GetCreatedCrashDirectory(pid_t pid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 101 | FilePath *crash_file_path, |
| 102 | bool *out_of_capacity); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 103 | bool CopyStdinToCoreFile(const FilePath &core_path); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 104 | bool RunCoreToMinidump(const FilePath &core_path, |
| 105 | const FilePath &procfs_directory, |
| 106 | const FilePath &minidump_path, |
| 107 | const FilePath &temp_directory); |
| 108 | bool ConvertCoreToMinidump(pid_t pid, |
| 109 | const FilePath &container_dir, |
| 110 | const FilePath &core_path, |
| 111 | const FilePath &minidump_path); |
| 112 | bool ConvertAndEnqueueCrash(int pid, const std::string &exec_name, |
| 113 | bool *out_of_capacity); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame^] | 114 | bool ParseCrashAttributes(const std::string &crash_attributes, |
| 115 | pid_t *pid, int *signal, |
| 116 | std::string *kernel_supplied_name); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 117 | |
| 118 | bool generate_diagnostics_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 119 | std::string core_pattern_file_; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 120 | std::string core_pipe_limit_file_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 121 | std::string our_path_; |
| 122 | bool initialized_; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 123 | |
| 124 | static const char *kUserId; |
| 125 | static const char *kGroupId; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 128 | #endif // _CRASH_REPORTER_USER_COLLECTOR_H_ |