blob: 5ddaf11b669cd14a10cf13739f9ad0e6cea71813 [file] [log] [blame]
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +09001// 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 Takabayashib2ca40d2018-08-09 14:02:04 +09008#include <string>
9#include <vector>
10
11#include <base/files/file_path.h>
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070012#include <base/time/clock.h>
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070013#include <base/time/time.h>
Simon Glass2b1da092020-05-21 12:24:16 -060014#include <brillo/process/process.h>
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070015#include <brillo/streams/stream.h>
Miriam Zimmermana23f18f2020-11-12 11:09:27 -080016#include <metrics/metrics_library.h>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090017#include <session_manager/dbus-proxies.h>
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090018
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090019namespace util {
20
Ian Barkley-Yeung7aebd752019-08-13 20:22:56 -070021// From //net/crash/collector/collector.h
22extern const int kDefaultMaxUploadBytes;
23
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090024// Returns true if integration tests are currently running.
25bool IsCrashTestInProgress();
26
Satoru Takabayashi2d728042018-12-10 09:19:00 +090027// Returns true if uploading of device coredumps is allowed.
28bool IsDeviceCoredumpUploadAllowed();
29
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090030// Returns true if running on a developer image.
31bool IsDeveloperImage();
32
Satoru Takabayashif6a36802018-08-14 16:23:05 +090033// Returns true if running on a test image.
34bool IsTestImage();
35
Satoru Takabayashi9a587522018-10-29 09:36:27 +090036// Returns true if running on an official image.
37bool IsOfficialImage();
38
Ian Barkley-Yeungb12d6cc2020-02-12 14:00:54 -080039// Returns true if we are mocking metrics consent as granted.
40bool HasMockConsent();
41
Miriam Zimmermana23f18f2020-11-12 11:09:27 -080042// 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|
47bool IsFeedbackAllowed(MetricsLibraryInterface* metrics_lib);
48
Miriam Zimmerman00c1c352020-05-15 09:44:47 -070049// Returns true if we should skip crash collection (based on the filter-in
Miriam Zimmerman93221182021-04-02 13:47:58 -070050// and filter-out files).
51// Specifically, if filter-in exists, crash_reporter will exit early unless its
Miriam Zimmerman00c1c352020-05-15 09:44:47 -070052// contents are a substring of the command-line parameters.
Miriam Zimmerman93221182021-04-02 13:47:58 -070053// Alternatively, if filter-in contains the string "none", then crash_reporter
Miriam Zimmerman00c1c352020-05-15 09:44:47 -070054// will always exit early.
Miriam Zimmerman93221182021-04-02 13:47:58 -070055// If filter-out exists, crash_reporter will exit early *if* its contents
56// are a substring of the command-line parameters.
57bool SkipCrashCollection(int argc, const char* const argv[]);
Miriam Zimmerman00c1c352020-05-15 09:44:47 -070058
Miriam Zimmermana23b1a02019-12-27 17:21:39 -080059// Change group ownership of "file" to "group", and grant g+rw (optionally x).
60bool SetGroupAndPermissions(const base::FilePath& file,
61 const char* group,
62 bool execute);
63
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070064// 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.
67base::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-Yeungc377b092019-10-09 19:23:53 -070072bool IsOsTimestampTooOldForUploads(base::Time timestamp, base::Clock* clock);
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070073
Jeffrey Kardatzkeea333932019-04-12 10:17:51 -070074// Gets a string describing the hardware class of the device. Returns
75// "undefined" if this cannot be determined.
76std::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.
80std::string GetBootModeString();
81
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090082// 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.
86bool 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.
93bool GetCachedKeyValueDefault(const base::FilePath& base_name,
94 const std::string& key,
95 std::string* value);
96
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090097// Gets the user crash directories via D-Bus using |session_manager_proxy|.
Fergus Dalla55e5642020-03-10 20:17:00 +110098// Returns true on success.
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090099bool GetUserCrashDirectories(
100 org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy,
101 std::vector<base::FilePath>* directories);
102
Fergus Dalla55e5642020-03-10 20:17:00 +1100103bool GetDaemonStoreCrashDirectories(
104 org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy,
105 std::vector<base::FilePath>* directories);
106
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -0700107// Gzip's the |data| passed in and returns the compressed data. Returns an empty
Ian Barkley-Yeunge87b4f42019-05-22 15:05:13 -0700108// vector on failure.
109std::vector<unsigned char> GzipStream(brillo::StreamPtr data);
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -0700110
Jeffrey Kardatzke18d50982019-03-21 14:13:34 -0700111// Runs |process| and redirects |fd| to |output|. Returns the exit code, or -1
112// if the process failed to start.
113int 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.
119void LogMultilineError(const std::string& error);
120
Tim Zheng11a665e2019-06-26 17:44:01 -0700121// Read the memfd file contents. Return false on failure.
122bool ReadMemfdToString(int mem_fd, std::string* contents);
123
Miriam Zimmermana06220e2019-10-23 18:36:31 -0700124// Return the weight for SELinux failures. We'll only collect
125// 1.0/GetSelinuxWeight() of the failures.
126int GetSelinuxWeight();
127
128// Return the weight for service failures. We'll only collect
129// 1.0/GetServiceFailureWeight() of the failures.
130int GetServiceFailureWeight();
131
Miriam Zimmermanb260e482021-04-13 13:06:44 -0700132// Return the weight for suspend failures. We'll only collect
133// 1.0/GetSuspendFailureWeight() of the failures.
134int GetSuspendFailureWeight();
135
Miriam Zimmermana48a68d2021-04-13 16:58:21 -0700136// Return the weight for kernel warnings with the specified command-line flag.
137// We'll only collect 1.0/GetKernelWarningWeight(flag) of the failures.
138int GetKernelWarningWeight(const std::string& flag);
139
Miriam Zimmerman79b59882021-04-15 16:22:21 -0700140// Return the weight for stateful umount failures.
141int GetUmountStatefulFailureWeight();
142
Kansho Nishida630cc7a2019-10-23 17:37:41 +0900143// Read the content binding to fd to stream.
144bool ReadFdToStream(unsigned int fd, std::stringstream* stream);
145
Sarthak Kukretid5d9b272020-04-12 22:27:12 -0700146#if USE_DIRENCRYPTION
147// Joins the session key if the kernel supports ext4 directory encryption.
148void JoinSessionKeyring();
149#endif // USE_DIRENCRYPTION
150
Kimiyuki Onakab767cb12021-01-22 15:00:22 +0900151// Hash a string to a number. We define our own hash function to not
152// be dependent on a C++ library that might change.
153unsigned HashString(base::StringPiece input);
154
Chloe Pelling83dda4c2021-02-17 17:01:21 +1100155// Get the absolute path to this binary given its command-line arguments,
156// and allowing override with the LD_ARGV0 environment variable.
157base::FilePath GetPathToThisBinary(const char* const argv[]);
158
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +0900159} // namespace util
160
161#endif // CRASH_REPORTER_UTIL_H_