Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 1 | // Copyright 2018 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 | |
| 5 | #ifndef CRASH_REPORTER_UTIL_H_ |
| 6 | #define CRASH_REPORTER_UTIL_H_ |
| 7 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/files/file_path.h> |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 12 | #include <base/time/clock.h> |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 13 | #include <base/time/time.h> |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 14 | #include <brillo/process/process.h> |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 15 | #include <brillo/streams/stream.h> |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 16 | #include <metrics/metrics_library.h> |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 17 | #include <session_manager/dbus-proxies.h> |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 18 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 19 | namespace util { |
| 20 | |
Ian Barkley-Yeung | 7aebd75 | 2019-08-13 20:22:56 -0700 | [diff] [blame] | 21 | // From //net/crash/collector/collector.h |
| 22 | extern const int kDefaultMaxUploadBytes; |
| 23 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 24 | // Returns true if integration tests are currently running. |
| 25 | bool IsCrashTestInProgress(); |
| 26 | |
Satoru Takabayashi | 2d72804 | 2018-12-10 09:19:00 +0900 | [diff] [blame] | 27 | // Returns true if uploading of device coredumps is allowed. |
| 28 | bool IsDeviceCoredumpUploadAllowed(); |
| 29 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 30 | // Returns true if running on a developer image. |
| 31 | bool IsDeveloperImage(); |
| 32 | |
Satoru Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 33 | // Returns true if running on a test image. |
| 34 | bool IsTestImage(); |
| 35 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 36 | // Returns true if running on an official image. |
| 37 | bool IsOfficialImage(); |
| 38 | |
Ian Barkley-Yeung | b12d6cc | 2020-02-12 14:00:54 -0800 | [diff] [blame] | 39 | // Returns true if we are mocking metrics consent as granted. |
| 40 | bool HasMockConsent(); |
| 41 | |
Miriam Zimmerman | a23f18f | 2020-11-12 11:09:27 -0800 | [diff] [blame] | 42 | // Determines whether feedback is allowed, based on: |
| 43 | // * The presence/absence of mock consent |
| 44 | // * Whether this is a developer image |
| 45 | // * Whether the metrics library indicates consent |
| 46 | // Does not take ownership of |metrics_lib| |
| 47 | bool IsFeedbackAllowed(MetricsLibraryInterface* metrics_lib); |
| 48 | |
Miriam Zimmerman | 00c1c35 | 2020-05-15 09:44:47 -0700 | [diff] [blame] | 49 | // Returns true if we should skip crash collection (based on the filter-in |
Miriam Zimmerman | 9322118 | 2021-04-02 13:47:58 -0700 | [diff] [blame] | 50 | // and filter-out files). |
| 51 | // Specifically, if filter-in exists, crash_reporter will exit early unless its |
Miriam Zimmerman | 00c1c35 | 2020-05-15 09:44:47 -0700 | [diff] [blame] | 52 | // contents are a substring of the command-line parameters. |
Miriam Zimmerman | 9322118 | 2021-04-02 13:47:58 -0700 | [diff] [blame] | 53 | // Alternatively, if filter-in contains the string "none", then crash_reporter |
Miriam Zimmerman | 00c1c35 | 2020-05-15 09:44:47 -0700 | [diff] [blame] | 54 | // will always exit early. |
Miriam Zimmerman | 9322118 | 2021-04-02 13:47:58 -0700 | [diff] [blame] | 55 | // If filter-out exists, crash_reporter will exit early *if* its contents |
| 56 | // are a substring of the command-line parameters. |
| 57 | bool SkipCrashCollection(int argc, const char* const argv[]); |
Miriam Zimmerman | 00c1c35 | 2020-05-15 09:44:47 -0700 | [diff] [blame] | 58 | |
Miriam Zimmerman | a23b1a0 | 2019-12-27 17:21:39 -0800 | [diff] [blame] | 59 | // Change group ownership of "file" to "group", and grant g+rw (optionally x). |
| 60 | bool SetGroupAndPermissions(const base::FilePath& file, |
| 61 | const char* group, |
| 62 | bool execute); |
| 63 | |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 64 | // Returns the timestamp for the OS version we are currently running. Returns |
| 65 | // a null (zero-valued) base::Time if it is unable to calculate it for some |
| 66 | // reason. |
| 67 | base::Time GetOsTimestamp(); |
| 68 | |
| 69 | // Returns true if this version is old enough that we do not want to upload the |
| 70 | // crash reports anymore. This just checks if |timestamp| is more than 180 |
| 71 | // days old. If |timestamp| is null (zero-valued) then this will return false. |
Ian Barkley-Yeung | c377b09 | 2019-10-09 19:23:53 -0700 | [diff] [blame] | 72 | bool IsOsTimestampTooOldForUploads(base::Time timestamp, base::Clock* clock); |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 73 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 74 | // Gets a string describing the hardware class of the device. Returns |
| 75 | // "undefined" if this cannot be determined. |
| 76 | std::string GetHardwareClass(); |
| 77 | |
| 78 | // Returns the boot mode which will either be "dev", "missing-crossystem" (if it |
| 79 | // cannot be determined) or the empty string. |
| 80 | std::string GetBootModeString(); |
| 81 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 82 | // Tries to find |key| in a key-value file named |base_name| in |directories| in |
| 83 | // the specified order, and writes the value to |value|. This function returns |
| 84 | // as soon as the key is found (i.e. if the key is found in the first directory, |
| 85 | // the remaining directories won't be checked). Returns true on success. |
| 86 | bool GetCachedKeyValue(const base::FilePath& base_name, |
| 87 | const std::string& key, |
| 88 | const std::vector<base::FilePath>& directories, |
| 89 | std::string* value); |
| 90 | |
| 91 | // Similar to GetCachedKeyValue(), but this version checks the predefined |
| 92 | // default directories. |
| 93 | bool GetCachedKeyValueDefault(const base::FilePath& base_name, |
| 94 | const std::string& key, |
| 95 | std::string* value); |
| 96 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 97 | // Gets the user crash directories via D-Bus using |session_manager_proxy|. |
Fergus Dall | a55e564 | 2020-03-10 20:17:00 +1100 | [diff] [blame] | 98 | // Returns true on success. |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 99 | bool GetUserCrashDirectories( |
| 100 | org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy, |
| 101 | std::vector<base::FilePath>* directories); |
| 102 | |
Fergus Dall | a55e564 | 2020-03-10 20:17:00 +1100 | [diff] [blame] | 103 | bool GetDaemonStoreCrashDirectories( |
| 104 | org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy, |
| 105 | std::vector<base::FilePath>* directories); |
| 106 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 107 | // Gzip's the |data| passed in and returns the compressed data. Returns an empty |
Ian Barkley-Yeung | e87b4f4 | 2019-05-22 15:05:13 -0700 | [diff] [blame] | 108 | // vector on failure. |
| 109 | std::vector<unsigned char> GzipStream(brillo::StreamPtr data); |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 110 | |
Jeffrey Kardatzke | 18d5098 | 2019-03-21 14:13:34 -0700 | [diff] [blame] | 111 | // Runs |process| and redirects |fd| to |output|. Returns the exit code, or -1 |
| 112 | // if the process failed to start. |
| 113 | int RunAndCaptureOutput(brillo::ProcessImpl* process, |
| 114 | int fd, |
| 115 | std::string* output); |
| 116 | |
| 117 | // Breaks up |error| using std::getline and then does a LOG(ERROR) of each |
| 118 | // individual line. |
| 119 | void LogMultilineError(const std::string& error); |
| 120 | |
Tim Zheng | 11a665e | 2019-06-26 17:44:01 -0700 | [diff] [blame] | 121 | // Read the memfd file contents. Return false on failure. |
| 122 | bool ReadMemfdToString(int mem_fd, std::string* contents); |
| 123 | |
Miriam Zimmerman | a06220e | 2019-10-23 18:36:31 -0700 | [diff] [blame] | 124 | // Return the weight for SELinux failures. We'll only collect |
| 125 | // 1.0/GetSelinuxWeight() of the failures. |
| 126 | int GetSelinuxWeight(); |
| 127 | |
| 128 | // Return the weight for service failures. We'll only collect |
| 129 | // 1.0/GetServiceFailureWeight() of the failures. |
| 130 | int GetServiceFailureWeight(); |
| 131 | |
Miriam Zimmerman | b260e48 | 2021-04-13 13:06:44 -0700 | [diff] [blame] | 132 | // Return the weight for suspend failures. We'll only collect |
| 133 | // 1.0/GetSuspendFailureWeight() of the failures. |
| 134 | int GetSuspendFailureWeight(); |
| 135 | |
Miriam Zimmerman | a48a68d | 2021-04-13 16:58:21 -0700 | [diff] [blame] | 136 | // Return the weight for kernel warnings with the specified command-line flag. |
| 137 | // We'll only collect 1.0/GetKernelWarningWeight(flag) of the failures. |
| 138 | int GetKernelWarningWeight(const std::string& flag); |
| 139 | |
Miriam Zimmerman | 79b5988 | 2021-04-15 16:22:21 -0700 | [diff] [blame] | 140 | // Return the weight for stateful umount failures. |
| 141 | int GetUmountStatefulFailureWeight(); |
| 142 | |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 143 | // Read the content binding to fd to stream. |
| 144 | bool ReadFdToStream(unsigned int fd, std::stringstream* stream); |
| 145 | |
Sarthak Kukreti | d5d9b27 | 2020-04-12 22:27:12 -0700 | [diff] [blame] | 146 | #if USE_DIRENCRYPTION |
| 147 | // Joins the session key if the kernel supports ext4 directory encryption. |
| 148 | void JoinSessionKeyring(); |
| 149 | #endif // USE_DIRENCRYPTION |
| 150 | |
Kimiyuki Onaka | b767cb1 | 2021-01-22 15:00:22 +0900 | [diff] [blame] | 151 | // Hash a string to a number. We define our own hash function to not |
| 152 | // be dependent on a C++ library that might change. |
| 153 | unsigned HashString(base::StringPiece input); |
| 154 | |
Chloe Pelling | 83dda4c | 2021-02-17 17:01:21 +1100 | [diff] [blame] | 155 | // Get the absolute path to this binary given its command-line arguments, |
| 156 | // and allowing override with the LD_ARGV0 environment variable. |
| 157 | base::FilePath GetPathToThisBinary(const char* const argv[]); |
| 158 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 159 | } // namespace util |
| 160 | |
| 161 | #endif // CRASH_REPORTER_UTIL_H_ |