blob: 1f9312315a3de7cdd7eb68e7b8bfe918c95b8a24 [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>
Jeffrey Kardatzke18d50982019-03-21 14:13:34 -070014#include <brillo/process.h>
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070015#include <brillo/streams/stream.h>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090016#include <session_manager/dbus-proxies.h>
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090017
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090018namespace util {
19
Ian Barkley-Yeung7aebd752019-08-13 20:22:56 -070020// From //net/crash/collector/collector.h
21extern const int kDefaultMaxUploadBytes;
22
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090023// Returns true if integration tests are currently running.
24bool IsCrashTestInProgress();
25
Satoru Takabayashi2d728042018-12-10 09:19:00 +090026// Returns true if uploading of device coredumps is allowed.
27bool IsDeviceCoredumpUploadAllowed();
28
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090029// Returns true if running on a developer image.
30bool IsDeveloperImage();
31
Satoru Takabayashif6a36802018-08-14 16:23:05 +090032// Returns true if running on a test image.
33bool IsTestImage();
34
Satoru Takabayashi9a587522018-10-29 09:36:27 +090035// Returns true if running on an official image.
36bool IsOfficialImage();
37
Miriam Zimmermana23b1a02019-12-27 17:21:39 -080038// Change group ownership of "file" to "group", and grant g+rw (optionally x).
39bool SetGroupAndPermissions(const base::FilePath& file,
40 const char* group,
41 bool execute);
42
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070043// Returns the timestamp for the OS version we are currently running. Returns
44// a null (zero-valued) base::Time if it is unable to calculate it for some
45// reason.
46base::Time GetOsTimestamp();
47
48// Returns true if this version is old enough that we do not want to upload the
49// crash reports anymore. This just checks if |timestamp| is more than 180
50// days old. If |timestamp| is null (zero-valued) then this will return false.
Ian Barkley-Yeungc377b092019-10-09 19:23:53 -070051bool IsOsTimestampTooOldForUploads(base::Time timestamp, base::Clock* clock);
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070052
Jeffrey Kardatzkeea333932019-04-12 10:17:51 -070053// Gets a string describing the hardware class of the device. Returns
54// "undefined" if this cannot be determined.
55std::string GetHardwareClass();
56
57// Returns the boot mode which will either be "dev", "missing-crossystem" (if it
58// cannot be determined) or the empty string.
59std::string GetBootModeString();
60
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090061// Tries to find |key| in a key-value file named |base_name| in |directories| in
62// the specified order, and writes the value to |value|. This function returns
63// as soon as the key is found (i.e. if the key is found in the first directory,
64// the remaining directories won't be checked). Returns true on success.
65bool GetCachedKeyValue(const base::FilePath& base_name,
66 const std::string& key,
67 const std::vector<base::FilePath>& directories,
68 std::string* value);
69
70// Similar to GetCachedKeyValue(), but this version checks the predefined
71// default directories.
72bool GetCachedKeyValueDefault(const base::FilePath& base_name,
73 const std::string& key,
74 std::string* value);
75
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090076// Gets the user crash directories via D-Bus using |session_manager_proxy|.
77// Returns true on success. The original contents of |directories| will be lost.
78bool GetUserCrashDirectories(
79 org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy,
80 std::vector<base::FilePath>* directories);
81
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070082// Gzip's the |data| passed in and returns the compressed data. Returns an empty
Ian Barkley-Yeunge87b4f42019-05-22 15:05:13 -070083// vector on failure.
84std::vector<unsigned char> GzipStream(brillo::StreamPtr data);
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070085
Jeffrey Kardatzke18d50982019-03-21 14:13:34 -070086// Runs |process| and redirects |fd| to |output|. Returns the exit code, or -1
87// if the process failed to start.
88int RunAndCaptureOutput(brillo::ProcessImpl* process,
89 int fd,
90 std::string* output);
91
92// Breaks up |error| using std::getline and then does a LOG(ERROR) of each
93// individual line.
94void LogMultilineError(const std::string& error);
95
Tim Zheng11a665e2019-06-26 17:44:01 -070096// Read the memfd file contents. Return false on failure.
97bool ReadMemfdToString(int mem_fd, std::string* contents);
98
Miriam Zimmermana06220e2019-10-23 18:36:31 -070099// Return the weight for SELinux failures. We'll only collect
100// 1.0/GetSelinuxWeight() of the failures.
101int GetSelinuxWeight();
102
103// Return the weight for service failures. We'll only collect
104// 1.0/GetServiceFailureWeight() of the failures.
105int GetServiceFailureWeight();
106
Kansho Nishida630cc7a2019-10-23 17:37:41 +0900107// Read the content binding to fd to stream.
108bool ReadFdToStream(unsigned int fd, std::stringstream* stream);
109
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +0900110} // namespace util
111
112#endif // CRASH_REPORTER_UTIL_H_