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> |
| 9 | |
Chris Sosa | 522c061 | 2010-06-18 11:08:11 -0700 | [diff] [blame] | 10 | #include "crash-reporter/system_logging.h" |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 11 | #include "gtest/gtest_prod.h" // for FRIEND_TEST |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 12 | |
| 13 | class FilePath; |
| 14 | |
| 15 | // User crash collector. |
| 16 | class UserCollector { |
| 17 | public: |
| 18 | typedef void (*CountCrashFunction)(); |
| 19 | typedef bool (*IsFeedbackAllowedFunction)(); |
| 20 | |
| 21 | UserCollector(); |
| 22 | |
| 23 | // Initialize the user crash collector for detection of crashes, |
| 24 | // given a crash counting function, the path to this executable, |
| 25 | // metrics collection enabled oracle, and system logger facility. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 26 | // Crash detection/reporting is not enabled until Enable is called. |
| 27 | // |generate_diagnostics| is used to indicate whether or not to try |
| 28 | // to generate a minidump from crashes. |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 29 | void Initialize(CountCrashFunction count_crash, |
| 30 | const std::string &our_path, |
| 31 | IsFeedbackAllowedFunction is_metrics_allowed, |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 32 | SystemLogging *logger, |
| 33 | bool generate_diagnostics); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 34 | |
| 35 | virtual ~UserCollector(); |
| 36 | |
| 37 | // Enable collection. |
| 38 | bool Enable() { return SetUpInternal(true); } |
| 39 | |
| 40 | // Disable collection. |
| 41 | bool Disable() { return SetUpInternal(false); } |
| 42 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 43 | // Handle a specific user crash. Returns true on success. |
| 44 | bool HandleCrash(int signal, int pid, 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 | |
| 51 | private: |
| 52 | friend class UserCollectorTest; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 53 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath); |
| 54 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid); |
| 55 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK); |
| 56 | FRIEND_TEST(UserCollectorTest, FormatDumpBasename); |
| 57 | FRIEND_TEST(UserCollectorTest, GetCrashDirectoryInfo); |
| 58 | FRIEND_TEST(UserCollectorTest, GetIdFromStatus); |
| 59 | FRIEND_TEST(UserCollectorTest, GetProcessPath); |
| 60 | FRIEND_TEST(UserCollectorTest, GetSymlinkTarget); |
| 61 | FRIEND_TEST(UserCollectorTest, GetUserInfoFromName); |
| 62 | |
| 63 | // Enumeration to pass to GetIdFromStatus. Must match the order |
| 64 | // that the kernel lists IDs in the status file. |
| 65 | enum IdKind { |
| 66 | kIdReal = 0, // uid and gid |
| 67 | kIdEffective = 1, // euid and egid |
| 68 | kIdSet = 2, // suid and sgid |
| 69 | kIdFileSystem = 3, // fsuid and fsgid |
| 70 | kIdMax |
| 71 | }; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 72 | |
| 73 | std::string GetPattern(bool enabled) const; |
| 74 | bool SetUpInternal(bool enabled); |
| 75 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 76 | FilePath GetProcessPath(pid_t pid); |
| 77 | bool GetSymlinkTarget(const FilePath &symlink, |
| 78 | FilePath *target); |
| 79 | bool GetExecutableBaseNameFromPid(uid_t pid, |
| 80 | std::string *base_name); |
| 81 | bool GetIdFromStatus(const char *prefix, |
| 82 | IdKind kind, |
| 83 | const std::string &status_contents, |
| 84 | int *id); |
| 85 | bool GetUserInfoFromName(const std::string &name, |
| 86 | uid_t *uid, |
| 87 | gid_t *gid); |
| 88 | bool CopyOffProcFiles(pid_t pid, const FilePath &process_map); |
| 89 | FilePath GetCrashDirectoryInfo(uid_t process_euid, |
| 90 | uid_t default_user_id, |
| 91 | gid_t default_user_group, |
| 92 | mode_t *mode, |
| 93 | uid_t *directory_owner, |
| 94 | gid_t *directory_group); |
| 95 | // Determines the crash directory for given pid based on pid's owner, |
| 96 | // and creates the directory if necessary with appropriate permissions. |
| 97 | // Returns true whether or not directory needed to be created, false on |
| 98 | // any failure. |
| 99 | bool GetCreatedCrashDirectory(pid_t pid, |
| 100 | FilePath *crash_file_path); |
| 101 | std::string FormatDumpBasename(const std::string &exec_name, |
| 102 | time_t timestamp, |
| 103 | pid_t pid); |
| 104 | bool CopyStdinToCoreFile(const FilePath &core_path); |
| 105 | bool ConvertCoreToMinidump(const FilePath &core_path, |
| 106 | const FilePath &procfs_directory, |
| 107 | const FilePath &minidump_path, |
| 108 | const FilePath &temp_directory); |
| 109 | bool GenerateDiagnostics(pid_t pid, const std::string &exec_name); |
| 110 | |
| 111 | bool generate_diagnostics_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 112 | std::string core_pattern_file_; |
| 113 | CountCrashFunction count_crash_function_; |
| 114 | std::string our_path_; |
| 115 | bool initialized_; |
| 116 | IsFeedbackAllowedFunction is_feedback_allowed_function_; |
| 117 | SystemLogging *logger_; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 118 | |
| 119 | static const char *kUserId; |
| 120 | static const char *kGroupId; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 123 | #endif // _CRASH_REPORTER_USER_COLLECTOR_H_ |