Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #ifndef DEBUGD_SRC_LOG_TOOL_H_ |
| 6 | #define DEBUGD_SRC_LOG_TOOL_H_ |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 7 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 8 | #include <sys/types.h> |
Gaurav Shah | f6c8f2a | 2012-10-11 17:22:43 -0700 | [diff] [blame] | 9 | #include <map> |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <set> |
Ben Chan | 5facf4a | 2014-07-23 16:36:54 -0700 | [diff] [blame] | 12 | #include <string> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 13 | |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 14 | #include <base/files/scoped_file.h> |
Ben Chan | 657bed8 | 2014-09-02 20:40:51 -0700 | [diff] [blame] | 15 | #include <base/macros.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 16 | #include <base/memory/ref_counted.h> |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 17 | #include <cryptohome/proto_bindings/rpc.pb.h> |
| 18 | #include <cryptohome-client/cryptohome/dbus-proxies.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 19 | #include <dbus/bus.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 20 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 21 | #include "debugd/src/sandboxed_process.h" |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 22 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 23 | namespace debugd { |
| 24 | |
| 25 | class LogTool { |
| 26 | public: |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 27 | // The encoding for a particular log. |
| 28 | enum class Encoding { |
| 29 | // Tries to see if the log output is valid UTF-8. Outputs it as-is if it is, |
| 30 | // or base64-encodes it otherwise. |
| 31 | kAutodetect, |
| 32 | |
| 33 | // Replaces any characters that are not valid UTF-8 encoded with the |
| 34 | // replacement character. |
| 35 | kUtf8, |
| 36 | |
| 37 | // base64-encodes the output. |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 38 | kBase64, |
| 39 | |
| 40 | // Doesn't apply an encoding. Copies the data as is. |
| 41 | kBinary, |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 44 | class Log { |
| 45 | public: |
Mike Frysinger | 020c240 | 2020-12-16 05:40:53 -0500 | [diff] [blame^] | 46 | enum LogType { kCommand, kFile, kGlob }; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 47 | |
| 48 | static constexpr int64_t kDefaultMaxBytes = 512 * 1024; |
| 49 | |
| 50 | Log(LogType type, |
| 51 | std::string name, |
| 52 | std::string data, |
| 53 | std::string user = SandboxedProcess::kDefaultUser, |
| 54 | std::string group = SandboxedProcess::kDefaultGroup, |
| 55 | int64_t max_bytes = kDefaultMaxBytes, |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 56 | LogTool::Encoding encoding = LogTool::Encoding::kAutodetect, |
| 57 | bool access_root_mount_ns = false); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 58 | |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 59 | virtual ~Log() = default; |
| 60 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 61 | std::string GetName() const; |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 62 | virtual std::string GetLogData() const; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 63 | |
| 64 | std::string GetCommandLogData() const; |
| 65 | std::string GetFileLogData() const; |
Mike Frysinger | 020c240 | 2020-12-16 05:40:53 -0500 | [diff] [blame^] | 66 | std::string GetGlobLogData() const; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 67 | |
| 68 | void DisableMinijailForTest(); |
| 69 | |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 70 | protected: |
| 71 | Log() = default; // For testing only. |
| 72 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 73 | private: |
| 74 | static uid_t UidForUser(const std::string& name); |
| 75 | static gid_t GidForGroup(const std::string& group); |
Mike Frysinger | 020c240 | 2020-12-16 05:40:53 -0500 | [diff] [blame^] | 76 | static std::string GetFileData(const base::FilePath& path, |
| 77 | int64_t max_bytes, |
| 78 | const std::string& user, |
| 79 | const std::string& group); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 80 | |
| 81 | LogType type_; |
| 82 | std::string name_; |
| 83 | // For kCommand logs, this is the command to run. |
| 84 | // For kFile logs, this is the file path to read. |
| 85 | std::string data_; |
| 86 | std::string user_; |
| 87 | std::string group_; |
| 88 | int64_t max_bytes_; // passed as arg to 'tail -c' |
| 89 | LogTool::Encoding encoding_; |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 90 | bool access_root_mount_ns_; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 91 | |
| 92 | bool minijail_disabled_for_test_ = false; |
| 93 | }; |
| 94 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 95 | explicit LogTool(scoped_refptr<dbus::Bus> bus); |
| 96 | |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 97 | ~LogTool() = default; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 98 | |
Ben Chan | 81905fb | 2017-02-08 22:03:11 -0800 | [diff] [blame] | 99 | using LogMap = std::map<std::string, std::string>; |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 100 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 101 | std::string GetLog(const std::string& name); |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 102 | LogMap GetAllLogs(); |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 103 | LogMap GetAllDebugLogs(); |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 104 | void GetBigFeedbackLogs(const base::ScopedFD& fd, |
| 105 | const std::string& username); |
mhasank | 7186aae | 2020-09-16 20:06:00 -0700 | [diff] [blame] | 106 | void BackupArcBugReport(const std::string& username); |
| 107 | void DeleteArcBugReportBackup(const std::string& username); |
Jeffrey Kardatzke | e3ec6fd | 2019-08-05 12:25:17 -0700 | [diff] [blame] | 108 | void GetJournalLog(const base::ScopedFD& fd); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 109 | |
| 110 | // Returns a representation of |value| with the specified encoding. |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 111 | static std::string EncodeString(std::string value, Encoding source_encoding); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 112 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 113 | private: |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 114 | friend class LogToolTest; |
| 115 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 116 | // For testing only. |
| 117 | LogTool(scoped_refptr<dbus::Bus> bus, |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 118 | std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface> |
| 119 | cryptohome_proxy, |
| 120 | const std::unique_ptr<LogTool::Log> arc_bug_report_log, |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 121 | const base::FilePath& daemon_store_base_dir); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 122 | LogTool(const LogTool&) = delete; |
| 123 | LogTool& operator=(const LogTool&) = delete; |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 124 | |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 125 | void CreateConnectivityReport(bool wait_for_results); |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 126 | |
| 127 | // Returns the output of arc-bugreport program in ARC. |
| 128 | // Returns cached output if it is available for this user. |
mhasank | d2b8488 | 2020-05-04 17:02:19 -0700 | [diff] [blame] | 129 | std::string GetArcBugReport(const std::string& username, bool* is_backup); |
mhasank | 7186aae | 2020-09-16 20:06:00 -0700 | [diff] [blame] | 130 | bool IsUserHashValid(const std::string& userhash); |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 131 | base::FilePath GetArcBugReportBackupFilePath(const std::string& userhash); |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 132 | |
| 133 | scoped_refptr<dbus::Bus> bus_; |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 134 | std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface> |
| 135 | cryptohome_proxy_; |
mhasank | af5251d | 2020-04-29 18:53:03 -0700 | [diff] [blame] | 136 | |
| 137 | std::unique_ptr<LogTool::Log> arc_bug_report_log_; |
| 138 | |
mhasank | 80cbe4d | 2020-04-02 22:46:08 -0700 | [diff] [blame] | 139 | base::FilePath daemon_store_base_dir_; |
mhasank | 4f599d3 | 2020-04-09 22:07:35 -0700 | [diff] [blame] | 140 | // Set containing userhash of all users for which |
| 141 | // ARC bug report has been backed up. |
| 142 | std::set<std::string> arc_bug_report_backups_; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 143 | }; |
| 144 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 145 | } // namespace debugd |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 146 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 147 | #endif // DEBUGD_SRC_LOG_TOOL_H_ |