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 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame^] | 10 | #include "crash-reporter/crash_collector.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; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame^] | 14 | class SystemLogging; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 15 | |
| 16 | // User crash collector. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame^] | 17 | class UserCollector : public CrashCollector { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 18 | public: |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 19 | UserCollector(); |
| 20 | |
| 21 | // Initialize the user crash collector for detection of crashes, |
| 22 | // given a crash counting function, the path to this executable, |
| 23 | // metrics collection enabled oracle, and system logger facility. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 24 | // Crash detection/reporting is not enabled until Enable is called. |
| 25 | // |generate_diagnostics| is used to indicate whether or not to try |
| 26 | // to generate a minidump from crashes. |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 27 | void Initialize(CountCrashFunction count_crash, |
| 28 | const std::string &our_path, |
| 29 | IsFeedbackAllowedFunction is_metrics_allowed, |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 30 | SystemLogging *logger, |
| 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. |
| 42 | bool HandleCrash(int signal, int pid, const char *force_exec); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 43 | |
| 44 | // Set (override the default) core file pattern. |
| 45 | void set_core_pattern_file(const std::string &pattern) { |
| 46 | core_pattern_file_ = pattern; |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | friend class UserCollectorTest; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 51 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath); |
| 52 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid); |
| 53 | FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 54 | FRIEND_TEST(UserCollectorTest, GetIdFromStatus); |
| 55 | FRIEND_TEST(UserCollectorTest, GetProcessPath); |
| 56 | FRIEND_TEST(UserCollectorTest, GetSymlinkTarget); |
| 57 | FRIEND_TEST(UserCollectorTest, GetUserInfoFromName); |
| 58 | |
| 59 | // Enumeration to pass to GetIdFromStatus. Must match the order |
| 60 | // that the kernel lists IDs in the status file. |
| 61 | enum IdKind { |
| 62 | kIdReal = 0, // uid and gid |
| 63 | kIdEffective = 1, // euid and egid |
| 64 | kIdSet = 2, // suid and sgid |
| 65 | kIdFileSystem = 3, // fsuid and fsgid |
| 66 | kIdMax |
| 67 | }; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 68 | |
| 69 | std::string GetPattern(bool enabled) const; |
| 70 | bool SetUpInternal(bool enabled); |
| 71 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 72 | FilePath GetProcessPath(pid_t pid); |
| 73 | bool GetSymlinkTarget(const FilePath &symlink, |
| 74 | FilePath *target); |
| 75 | bool GetExecutableBaseNameFromPid(uid_t pid, |
| 76 | std::string *base_name); |
| 77 | bool GetIdFromStatus(const char *prefix, |
| 78 | IdKind kind, |
| 79 | const std::string &status_contents, |
| 80 | int *id); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 81 | bool CopyOffProcFiles(pid_t pid, const FilePath &process_map); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 82 | // Determines the crash directory for given pid based on pid's owner, |
| 83 | // and creates the directory if necessary with appropriate permissions. |
| 84 | // Returns true whether or not directory needed to be created, false on |
| 85 | // any failure. |
| 86 | bool GetCreatedCrashDirectory(pid_t pid, |
| 87 | FilePath *crash_file_path); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 88 | bool CopyStdinToCoreFile(const FilePath &core_path); |
| 89 | bool ConvertCoreToMinidump(const FilePath &core_path, |
| 90 | const FilePath &procfs_directory, |
| 91 | const FilePath &minidump_path, |
| 92 | const FilePath &temp_directory); |
| 93 | bool GenerateDiagnostics(pid_t pid, const std::string &exec_name); |
| 94 | |
| 95 | bool generate_diagnostics_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 96 | std::string core_pattern_file_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 97 | std::string our_path_; |
| 98 | bool initialized_; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 99 | |
| 100 | static const char *kUserId; |
| 101 | static const char *kGroupId; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 104 | #endif // _CRASH_REPORTER_USER_COLLECTOR_H_ |