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 |
| 50 | // file). |
| 51 | // Specifically, if the file exists, crash_reporter will exit early unless its |
| 52 | // contents are a substring of the command-line parameters. |
| 53 | // Alternatively, if the file contains the string "none", then crash_reporter |
| 54 | // will always exit early. |
| 55 | bool SkipCrashCollection(int argc, char* argv[]); |
| 56 | |
Miriam Zimmerman | a23b1a0 | 2019-12-27 17:21:39 -0800 | [diff] [blame] | 57 | // Change group ownership of "file" to "group", and grant g+rw (optionally x). |
| 58 | bool SetGroupAndPermissions(const base::FilePath& file, |
| 59 | const char* group, |
| 60 | bool execute); |
| 61 | |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 62 | // Returns the timestamp for the OS version we are currently running. Returns |
| 63 | // a null (zero-valued) base::Time if it is unable to calculate it for some |
| 64 | // reason. |
| 65 | base::Time GetOsTimestamp(); |
| 66 | |
| 67 | // Returns true if this version is old enough that we do not want to upload the |
| 68 | // crash reports anymore. This just checks if |timestamp| is more than 180 |
| 69 | // 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] | 70 | bool IsOsTimestampTooOldForUploads(base::Time timestamp, base::Clock* clock); |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 71 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 72 | // Gets a string describing the hardware class of the device. Returns |
| 73 | // "undefined" if this cannot be determined. |
| 74 | std::string GetHardwareClass(); |
| 75 | |
| 76 | // Returns the boot mode which will either be "dev", "missing-crossystem" (if it |
| 77 | // cannot be determined) or the empty string. |
| 78 | std::string GetBootModeString(); |
| 79 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 80 | // Tries to find |key| in a key-value file named |base_name| in |directories| in |
| 81 | // the specified order, and writes the value to |value|. This function returns |
| 82 | // as soon as the key is found (i.e. if the key is found in the first directory, |
| 83 | // the remaining directories won't be checked). Returns true on success. |
| 84 | bool GetCachedKeyValue(const base::FilePath& base_name, |
| 85 | const std::string& key, |
| 86 | const std::vector<base::FilePath>& directories, |
| 87 | std::string* value); |
| 88 | |
| 89 | // Similar to GetCachedKeyValue(), but this version checks the predefined |
| 90 | // default directories. |
| 91 | bool GetCachedKeyValueDefault(const base::FilePath& base_name, |
| 92 | const std::string& key, |
| 93 | std::string* value); |
| 94 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 95 | // Gets the user crash directories via D-Bus using |session_manager_proxy|. |
Fergus Dall | a55e564 | 2020-03-10 20:17:00 +1100 | [diff] [blame] | 96 | // Returns true on success. |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 97 | bool GetUserCrashDirectories( |
| 98 | org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy, |
| 99 | std::vector<base::FilePath>* directories); |
| 100 | |
Fergus Dall | a55e564 | 2020-03-10 20:17:00 +1100 | [diff] [blame] | 101 | bool GetDaemonStoreCrashDirectories( |
| 102 | org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy, |
| 103 | std::vector<base::FilePath>* directories); |
| 104 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 105 | // 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] | 106 | // vector on failure. |
| 107 | std::vector<unsigned char> GzipStream(brillo::StreamPtr data); |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 108 | |
Jeffrey Kardatzke | 18d5098 | 2019-03-21 14:13:34 -0700 | [diff] [blame] | 109 | // Runs |process| and redirects |fd| to |output|. Returns the exit code, or -1 |
| 110 | // if the process failed to start. |
| 111 | int RunAndCaptureOutput(brillo::ProcessImpl* process, |
| 112 | int fd, |
| 113 | std::string* output); |
| 114 | |
| 115 | // Breaks up |error| using std::getline and then does a LOG(ERROR) of each |
| 116 | // individual line. |
| 117 | void LogMultilineError(const std::string& error); |
| 118 | |
Tim Zheng | 11a665e | 2019-06-26 17:44:01 -0700 | [diff] [blame] | 119 | // Read the memfd file contents. Return false on failure. |
| 120 | bool ReadMemfdToString(int mem_fd, std::string* contents); |
| 121 | |
Miriam Zimmerman | a06220e | 2019-10-23 18:36:31 -0700 | [diff] [blame] | 122 | // Return the weight for SELinux failures. We'll only collect |
| 123 | // 1.0/GetSelinuxWeight() of the failures. |
| 124 | int GetSelinuxWeight(); |
| 125 | |
| 126 | // Return the weight for service failures. We'll only collect |
| 127 | // 1.0/GetServiceFailureWeight() of the failures. |
| 128 | int GetServiceFailureWeight(); |
| 129 | |
Kansho Nishida | 630cc7a | 2019-10-23 17:37:41 +0900 | [diff] [blame] | 130 | // Read the content binding to fd to stream. |
| 131 | bool ReadFdToStream(unsigned int fd, std::stringstream* stream); |
| 132 | |
Sarthak Kukreti | d5d9b27 | 2020-04-12 22:27:12 -0700 | [diff] [blame] | 133 | #if USE_DIRENCRYPTION |
| 134 | // Joins the session key if the kernel supports ext4 directory encryption. |
| 135 | void JoinSessionKeyring(); |
| 136 | #endif // USE_DIRENCRYPTION |
| 137 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 138 | } // namespace util |
| 139 | |
| 140 | #endif // CRASH_REPORTER_UTIL_H_ |