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> |
Jeffrey Kardatzke | 18d5098 | 2019-03-21 14:13:34 -0700 | [diff] [blame] | 14 | #include <brillo/process.h> |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 15 | #include <brillo/streams/stream.h> |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 16 | #include <session_manager/dbus-proxies.h> |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 17 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 18 | namespace util { |
| 19 | |
Ian Barkley-Yeung | 7aebd75 | 2019-08-13 20:22:56 -0700 | [diff] [blame] | 20 | // From //net/crash/collector/collector.h |
| 21 | extern const int kDefaultMaxUploadBytes; |
| 22 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 23 | // Returns true if integration tests are currently running. |
| 24 | bool IsCrashTestInProgress(); |
| 25 | |
Satoru Takabayashi | 2d72804 | 2018-12-10 09:19:00 +0900 | [diff] [blame] | 26 | // Returns true if uploading of device coredumps is allowed. |
| 27 | bool IsDeviceCoredumpUploadAllowed(); |
| 28 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 29 | // Returns true if running on a developer image. |
| 30 | bool IsDeveloperImage(); |
| 31 | |
Satoru Takabayashi | f6a3680 | 2018-08-14 16:23:05 +0900 | [diff] [blame] | 32 | // Returns true if running on a test image. |
| 33 | bool IsTestImage(); |
| 34 | |
Satoru Takabayashi | 9a58752 | 2018-10-29 09:36:27 +0900 | [diff] [blame] | 35 | // Returns true if running on an official image. |
| 36 | bool IsOfficialImage(); |
| 37 | |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 38 | // Returns the timestamp for the OS version we are currently running. Returns |
| 39 | // a null (zero-valued) base::Time if it is unable to calculate it for some |
| 40 | // reason. |
| 41 | base::Time GetOsTimestamp(); |
| 42 | |
| 43 | // Returns true if this version is old enough that we do not want to upload the |
| 44 | // crash reports anymore. This just checks if |timestamp| is more than 180 |
| 45 | // 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] | 46 | bool IsOsTimestampTooOldForUploads(base::Time timestamp, base::Clock* clock); |
Jeffrey Kardatzke | e3fb8fb | 2019-05-13 13:59:12 -0700 | [diff] [blame] | 47 | |
Jeffrey Kardatzke | ea33393 | 2019-04-12 10:17:51 -0700 | [diff] [blame] | 48 | // Gets a string describing the hardware class of the device. Returns |
| 49 | // "undefined" if this cannot be determined. |
| 50 | std::string GetHardwareClass(); |
| 51 | |
| 52 | // Returns the boot mode which will either be "dev", "missing-crossystem" (if it |
| 53 | // cannot be determined) or the empty string. |
| 54 | std::string GetBootModeString(); |
| 55 | |
Satoru Takabayashi | b2ca40d | 2018-08-09 14:02:04 +0900 | [diff] [blame] | 56 | // Tries to find |key| in a key-value file named |base_name| in |directories| in |
| 57 | // the specified order, and writes the value to |value|. This function returns |
| 58 | // as soon as the key is found (i.e. if the key is found in the first directory, |
| 59 | // the remaining directories won't be checked). Returns true on success. |
| 60 | bool GetCachedKeyValue(const base::FilePath& base_name, |
| 61 | const std::string& key, |
| 62 | const std::vector<base::FilePath>& directories, |
| 63 | std::string* value); |
| 64 | |
| 65 | // Similar to GetCachedKeyValue(), but this version checks the predefined |
| 66 | // default directories. |
| 67 | bool GetCachedKeyValueDefault(const base::FilePath& base_name, |
| 68 | const std::string& key, |
| 69 | std::string* value); |
| 70 | |
Satoru Takabayashi | 8ce6db8 | 2018-08-17 15:18:41 +0900 | [diff] [blame] | 71 | // Gets the user crash directories via D-Bus using |session_manager_proxy|. |
| 72 | // Returns true on success. The original contents of |directories| will be lost. |
| 73 | bool GetUserCrashDirectories( |
| 74 | org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy, |
| 75 | std::vector<base::FilePath>* directories); |
| 76 | |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 77 | // 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] | 78 | // vector on failure. |
| 79 | std::vector<unsigned char> GzipStream(brillo::StreamPtr data); |
Jeffrey Kardatzke | 437fa92 | 2019-05-09 11:34:32 -0700 | [diff] [blame] | 80 | |
Jeffrey Kardatzke | 18d5098 | 2019-03-21 14:13:34 -0700 | [diff] [blame] | 81 | // Runs |process| and redirects |fd| to |output|. Returns the exit code, or -1 |
| 82 | // if the process failed to start. |
| 83 | int RunAndCaptureOutput(brillo::ProcessImpl* process, |
| 84 | int fd, |
| 85 | std::string* output); |
| 86 | |
| 87 | // Breaks up |error| using std::getline and then does a LOG(ERROR) of each |
| 88 | // individual line. |
| 89 | void LogMultilineError(const std::string& error); |
| 90 | |
Tim Zheng | 11a665e | 2019-06-26 17:44:01 -0700 | [diff] [blame] | 91 | // Read the memfd file contents. Return false on failure. |
| 92 | bool ReadMemfdToString(int mem_fd, std::string* contents); |
| 93 | |
Miriam Zimmerman | a06220e | 2019-10-23 18:36:31 -0700 | [diff] [blame^] | 94 | // Return the weight for SELinux failures. We'll only collect |
| 95 | // 1.0/GetSelinuxWeight() of the failures. |
| 96 | int GetSelinuxWeight(); |
| 97 | |
| 98 | // Return the weight for service failures. We'll only collect |
| 99 | // 1.0/GetServiceFailureWeight() of the failures. |
| 100 | int GetServiceFailureWeight(); |
| 101 | |
Satoru Takabayashi | e7f6d2a | 2018-08-08 17:06:29 +0900 | [diff] [blame] | 102 | } // namespace util |
| 103 | |
| 104 | #endif // CRASH_REPORTER_UTIL_H_ |