| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef _CRASH_REPORTER_CRASH_COLLECTOR_H_ | 5 #ifndef _CRASH_REPORTER_CRASH_COLLECTOR_H_ |
| 6 #define _CRASH_REPORTER_CRASH_COLLECTOR_H_ | 6 #define _CRASH_REPORTER_CRASH_COLLECTOR_H_ |
| 7 | 7 |
| 8 #include <sys/stat.h> | 8 #include <sys/stat.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/file_path.h" |
| 13 #include "gtest/gtest_prod.h" // for FRIEND_TEST | 14 #include "gtest/gtest_prod.h" // for FRIEND_TEST |
| 14 | 15 |
| 15 class FilePath; | |
| 16 class SystemLogging; | 16 class SystemLogging; |
| 17 | 17 |
| 18 // User crash collector. | 18 // User crash collector. |
| 19 class CrashCollector { | 19 class CrashCollector { |
| 20 public: | 20 public: |
| 21 typedef void (*CountCrashFunction)(); | 21 typedef void (*CountCrashFunction)(); |
| 22 typedef bool (*IsFeedbackAllowedFunction)(); | 22 typedef bool (*IsFeedbackAllowedFunction)(); |
| 23 | 23 |
| 24 CrashCollector(); | 24 CrashCollector(); |
| 25 | 25 |
| 26 virtual ~CrashCollector(); | 26 virtual ~CrashCollector(); |
| 27 | 27 |
| 28 // Initialize the crash collector for detection of crashes, given a | 28 // Initialize the crash collector for detection of crashes, given a |
| 29 // crash counting function, metrics collection enabled oracle, and | 29 // crash counting function, metrics collection enabled oracle, and |
| 30 // system logger facility. | 30 // system logger facility. |
| 31 void Initialize(CountCrashFunction count_crash, | 31 void Initialize(CountCrashFunction count_crash, |
| 32 IsFeedbackAllowedFunction is_metrics_allowed, | 32 IsFeedbackAllowedFunction is_metrics_allowed, |
| 33 SystemLogging *logger); | 33 SystemLogging *logger); |
| 34 | 34 |
| 35 protected: | 35 protected: |
| 36 friend class CrashCollectorTest; | 36 friend class CrashCollectorTest; |
| 37 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityCorrectBasename); | 37 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityCorrectBasename); |
| 38 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityStrangeNames); | 38 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityStrangeNames); |
| 39 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityUsual); | 39 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityUsual); |
| 40 FRIEND_TEST(CrashCollectorTest, GetCrashDirectoryInfo); | 40 FRIEND_TEST(CrashCollectorTest, GetCrashDirectoryInfo); |
| 41 FRIEND_TEST(CrashCollectorTest, GetCrashPath); |
| 41 FRIEND_TEST(CrashCollectorTest, FormatDumpBasename); | 42 FRIEND_TEST(CrashCollectorTest, FormatDumpBasename); |
| 42 FRIEND_TEST(CrashCollectorTest, Initialize); | 43 FRIEND_TEST(CrashCollectorTest, Initialize); |
| 43 FRIEND_TEST(CrashCollectorTest, MetaData); | 44 FRIEND_TEST(CrashCollectorTest, MetaData); |
| 44 FRIEND_TEST(CrashCollectorTest, ReadKeyValueFile); | 45 FRIEND_TEST(CrashCollectorTest, ReadKeyValueFile); |
| 45 FRIEND_TEST(CrashCollectorTest, Sanitize); | 46 FRIEND_TEST(CrashCollectorTest, Sanitize); |
| 46 | 47 |
| 47 // Set maximum enqueued crashes in a crash directory. | 48 // Set maximum enqueued crashes in a crash directory. |
| 48 static const int kMaxCrashDirectorySize; | 49 static const int kMaxCrashDirectorySize; |
| 49 | 50 |
| 50 // Return a filename that has only [a-z0-1_] characters by mapping | 51 // Return a filename that has only [a-z0-1_] characters by mapping |
| 51 // all others into '_'. | 52 // all others into '_'. |
| 52 std::string Sanitize(const std::string &name); | 53 std::string Sanitize(const std::string &name); |
| 53 | 54 |
| 54 // For testing, set the directory always returned by | 55 // For testing, set the directory always returned by |
| 55 // GetCreatedCrashDirectoryByEuid. | 56 // GetCreatedCrashDirectoryByEuid. |
| 56 void ForceCrashDirectory(const char *forced_directory) { | 57 void ForceCrashDirectory(const char *forced_directory) { |
| 57 forced_crash_directory_ = forced_directory; | 58 forced_crash_directory_ = forced_directory; |
| 58 } | 59 } |
| 59 | 60 |
| 60 FilePath GetCrashDirectoryInfo(uid_t process_euid, | 61 FilePath GetCrashDirectoryInfo(uid_t process_euid, |
| 61 uid_t default_user_id, | 62 uid_t default_user_id, |
| 62 gid_t default_user_group, | 63 gid_t default_user_group, |
| 63 mode_t *mode, | 64 mode_t *mode, |
| 64 uid_t *directory_owner, | 65 uid_t *directory_owner, |
| 65 gid_t *directory_group); | 66 gid_t *directory_group); |
| 66 bool GetUserInfoFromName(const std::string &name, | 67 bool GetUserInfoFromName(const std::string &name, |
| 67 uid_t *uid, | 68 uid_t *uid, |
| 68 gid_t *gid); | 69 gid_t *gid); |
| 69 // Determines the crash directory for given eud, and creates the | 70 // Determines the crash directory for given eud, and creates the |
| 70 // directory if necessary with appropriate permissions. Returns | 71 // directory if necessary with appropriate permissions. If |
| 72 // |out_of_capacity| is not NULL, it is set to indicate if the call |
| 73 // failed due to not having capacity in the crash directory. Returns |
| 71 // true whether or not directory needed to be created, false on any | 74 // true whether or not directory needed to be created, false on any |
| 72 // failure. If the crash directory is already full, returns false. | 75 // failure. If the crash directory is at capacity, returns false. |
| 73 bool GetCreatedCrashDirectoryByEuid(uid_t euid, | 76 bool GetCreatedCrashDirectoryByEuid(uid_t euid, |
| 74 FilePath *crash_file_path); | 77 FilePath *crash_file_path, |
| 78 bool *out_of_capacity); |
| 75 | 79 |
| 76 // Format crash name based on components. | 80 // Format crash name based on components. |
| 77 std::string FormatDumpBasename(const std::string &exec_name, | 81 std::string FormatDumpBasename(const std::string &exec_name, |
| 78 time_t timestamp, | 82 time_t timestamp, |
| 79 pid_t pid); | 83 pid_t pid); |
| 80 | 84 |
| 85 // Create a file path to a file in |crash_directory| with the given |
| 86 // |basename| and |extension|. |
| 87 FilePath GetCrashPath(const FilePath &crash_directory, |
| 88 const std::string &basename, |
| 89 const std::string &extension); |
| 90 |
| 81 // Check given crash directory still has remaining capacity for another | 91 // Check given crash directory still has remaining capacity for another |
| 82 // crash. | 92 // crash. |
| 83 bool CheckHasCapacity(const FilePath &crash_directory); | 93 bool CheckHasCapacity(const FilePath &crash_directory); |
| 84 | 94 |
| 85 // Read the given file of form [<key><separator><value>\n...] and return | 95 // Read the given file of form [<key><separator><value>\n...] and return |
| 86 // a map of its contents. | 96 // a map of its contents. |
| 87 bool ReadKeyValueFile(const FilePath &file, | 97 bool ReadKeyValueFile(const FilePath &file, |
| 88 char separator, | 98 char separator, |
| 89 std::map<std::string, std::string> *dictionary); | 99 std::map<std::string, std::string> *dictionary); |
| 90 | 100 |
| 91 // Add non-standard meta data to the crash metadata file. Call | 101 // Add non-standard meta data to the crash metadata file. Call |
| 92 // before calling WriteCrashMetaData. Key must not contain "=" or | 102 // before calling WriteCrashMetaData. Key must not contain "=" or |
| 93 // "\n" characters. Value must not contain "\n" characters. | 103 // "\n" characters. Value must not contain "\n" characters. |
| 94 void AddCrashMetaData(const std::string &key, const std::string &value); | 104 void AddCrashMetaData(const std::string &key, const std::string &value); |
| 95 | 105 |
| 96 // Write a file of metadata about crash. | 106 // Write a file of metadata about crash. |
| 97 void WriteCrashMetaData(const FilePath &meta_path, | 107 void WriteCrashMetaData(const FilePath &meta_path, |
| 98 const std::string &exec_name, | 108 const std::string &exec_name, |
| 99 const std::string &payload_path); | 109 const std::string &payload_path); |
| 100 | 110 |
| 101 CountCrashFunction count_crash_function_; | 111 CountCrashFunction count_crash_function_; |
| 102 IsFeedbackAllowedFunction is_feedback_allowed_function_; | 112 IsFeedbackAllowedFunction is_feedback_allowed_function_; |
| 103 SystemLogging *logger_; | 113 SystemLogging *logger_; |
| 104 std::string extra_metadata_; | 114 std::string extra_metadata_; |
| 105 const char *forced_crash_directory_; | 115 const char *forced_crash_directory_; |
| 106 const char *lsb_release_; | 116 const char *lsb_release_; |
| 107 }; | 117 }; |
| 108 | 118 |
| 109 #endif // _CRASH_REPORTER_CRASH_COLLECTOR_H_ | 119 #endif // _CRASH_REPORTER_CRASH_COLLECTOR_H_ |
| OLD | NEW |