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> |
Ben Chan | 5facf4a | 2014-07-23 16:36:54 -0700 | [diff] [blame] | 10 | #include <string> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 11 | |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 12 | #include <base/files/scoped_file.h> |
Ben Chan | 657bed8 | 2014-09-02 20:40:51 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
Eric Caruso | cc7106c | 2017-04-27 14:22:42 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
| 15 | #include <dbus/bus.h> |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 16 | |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 17 | #include "debugd/src/anonymizer_tool.h" |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 18 | #include "debugd/src/sandboxed_process.h" |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 19 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 20 | namespace debugd { |
| 21 | |
| 22 | class LogTool { |
| 23 | public: |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 24 | // The encoding for a particular log. |
| 25 | enum class Encoding { |
| 26 | // Tries to see if the log output is valid UTF-8. Outputs it as-is if it is, |
| 27 | // or base64-encodes it otherwise. |
| 28 | kAutodetect, |
| 29 | |
| 30 | // Replaces any characters that are not valid UTF-8 encoded with the |
| 31 | // replacement character. |
| 32 | kUtf8, |
| 33 | |
| 34 | // base64-encodes the output. |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 35 | kBase64, |
| 36 | |
| 37 | // Doesn't apply an encoding. Copies the data as is. |
| 38 | kBinary, |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 41 | class Log { |
| 42 | public: |
| 43 | enum LogType { kCommand, kFile }; |
| 44 | |
| 45 | static constexpr int64_t kDefaultMaxBytes = 512 * 1024; |
| 46 | |
| 47 | Log(LogType type, |
| 48 | std::string name, |
| 49 | std::string data, |
| 50 | std::string user = SandboxedProcess::kDefaultUser, |
| 51 | std::string group = SandboxedProcess::kDefaultGroup, |
| 52 | int64_t max_bytes = kDefaultMaxBytes, |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 53 | LogTool::Encoding encoding = LogTool::Encoding::kAutodetect, |
| 54 | bool access_root_mount_ns = false); |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 55 | |
| 56 | std::string GetName() const; |
| 57 | std::string GetLogData() const; |
| 58 | |
| 59 | std::string GetCommandLogData() const; |
| 60 | std::string GetFileLogData() const; |
| 61 | |
| 62 | void DisableMinijailForTest(); |
| 63 | |
| 64 | private: |
| 65 | static uid_t UidForUser(const std::string& name); |
| 66 | static gid_t GidForGroup(const std::string& group); |
| 67 | |
| 68 | LogType type_; |
| 69 | std::string name_; |
| 70 | // For kCommand logs, this is the command to run. |
| 71 | // For kFile logs, this is the file path to read. |
| 72 | std::string data_; |
| 73 | std::string user_; |
| 74 | std::string group_; |
| 75 | int64_t max_bytes_; // passed as arg to 'tail -c' |
| 76 | LogTool::Encoding encoding_; |
Brian Norris | afc9f63 | 2019-05-09 14:08:28 -0700 | [diff] [blame] | 77 | bool access_root_mount_ns_; |
Fletcher Woodruff | 07c2853 | 2019-01-24 11:08:53 -0700 | [diff] [blame] | 78 | |
| 79 | bool minijail_disabled_for_test_ = false; |
| 80 | }; |
| 81 | |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 82 | explicit LogTool(scoped_refptr<dbus::Bus> bus) : bus_(bus) {} |
Ben Chan | 78f8953 | 2014-08-29 09:35:09 -0700 | [diff] [blame] | 83 | ~LogTool() = default; |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 84 | |
Ben Chan | 81905fb | 2017-02-08 22:03:11 -0800 | [diff] [blame] | 85 | using LogMap = std::map<std::string, std::string>; |
Elly Jones | 533c7c4 | 2012-08-10 15:07:05 -0400 | [diff] [blame] | 86 | |
Eric Caruso | c93a15c | 2017-04-24 16:15:12 -0700 | [diff] [blame] | 87 | std::string GetLog(const std::string& name); |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 88 | LogMap GetAllLogs(); |
Brian Norris | ca4fc04 | 2018-04-03 00:24:26 -0700 | [diff] [blame] | 89 | LogMap GetAllDebugLogs(); |
Eric Caruso | 0b24188 | 2018-04-04 13:43:46 -0700 | [diff] [blame] | 90 | void GetBigFeedbackLogs(const base::ScopedFD& fd); |
Chris Morin | 790fd26 | 2019-04-03 20:29:36 -0700 | [diff] [blame] | 91 | void GetJournalLog(bool scrub, const base::ScopedFD& fd); |
| 92 | |
| 93 | // Returns a representation of |value| with the specified encoding. |
| 94 | static std::string EncodeString(std::string value, |
| 95 | Encoding source_encoding); |
Luis Hector Chavez | fc2566f | 2018-09-13 15:00:36 -0700 | [diff] [blame] | 96 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 97 | private: |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 98 | friend class LogToolTest; |
| 99 | |
Fletcher Woodruff | 70f2723 | 2019-01-24 11:41:34 -0700 | [diff] [blame] | 100 | void CreateConnectivityReport(bool wait_for_results); |
Eric Caruso | f9091f8 | 2017-04-28 14:18:59 -0700 | [diff] [blame] | 101 | |
| 102 | scoped_refptr<dbus::Bus> bus_; |
Darin Petkov | ce9b3a1 | 2013-01-10 16:38:54 +0100 | [diff] [blame] | 103 | |
Ahmed Fakhry | 21140cf | 2016-03-04 17:15:19 -0800 | [diff] [blame] | 104 | AnonymizerTool anonymizer_; |
| 105 | |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 106 | DISALLOW_COPY_AND_ASSIGN(LogTool); |
| 107 | }; |
| 108 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 109 | } // namespace debugd |
Elly Jones | 03cd6d7 | 2012-06-11 13:04:28 -0400 | [diff] [blame] | 110 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 111 | #endif // DEBUGD_SRC_LOG_TOOL_H_ |