blob: ecd117115b19c85b8bfc3253b3daac0da8fbe181 [file] [log] [blame]
Elly Jones03cd6d72012-06-11 13:04:28 -04001// 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 Vakulenko262be3f2014-07-30 15:25:50 -07005#ifndef DEBUGD_SRC_LOG_TOOL_H_
6#define DEBUGD_SRC_LOG_TOOL_H_
Elly Jones03cd6d72012-06-11 13:04:28 -04007
Fletcher Woodruff07c28532019-01-24 11:08:53 -07008#include <sys/types.h>
Gaurav Shahf6c8f2a2012-10-11 17:22:43 -07009#include <map>
mhasank4f599d32020-04-09 22:07:35 -070010#include <memory>
11#include <set>
Ben Chan5facf4a2014-07-23 16:36:54 -070012#include <string>
Elly Jones03cd6d72012-06-11 13:04:28 -040013
Eric Caruso0b241882018-04-04 13:43:46 -070014#include <base/files/scoped_file.h>
Ben Chan657bed82014-09-02 20:40:51 -070015#include <base/macros.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070016#include <base/memory/ref_counted.h>
mhasank4f599d32020-04-09 22:07:35 -070017#include <cryptohome/proto_bindings/rpc.pb.h>
18#include <cryptohome-client/cryptohome/dbus-proxies.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070019#include <dbus/bus.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040020
Fletcher Woodruff07c28532019-01-24 11:08:53 -070021#include "debugd/src/sandboxed_process.h"
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080022
Elly Jones03cd6d72012-06-11 13:04:28 -040023namespace debugd {
24
25class LogTool {
26 public:
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070027 // 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 Morin790fd262019-04-03 20:29:36 -070038 kBase64,
39
40 // Doesn't apply an encoding. Copies the data as is.
41 kBinary,
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070042 };
43
Fletcher Woodruff07c28532019-01-24 11:08:53 -070044 class Log {
45 public:
46 enum LogType { kCommand, kFile };
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 Norrisafc9f632019-05-09 14:08:28 -070056 LogTool::Encoding encoding = LogTool::Encoding::kAutodetect,
57 bool access_root_mount_ns = false);
Fletcher Woodruff07c28532019-01-24 11:08:53 -070058
mhasankaf5251d2020-04-29 18:53:03 -070059 virtual ~Log() = default;
60
Fletcher Woodruff07c28532019-01-24 11:08:53 -070061 std::string GetName() const;
mhasankaf5251d2020-04-29 18:53:03 -070062 virtual std::string GetLogData() const;
Fletcher Woodruff07c28532019-01-24 11:08:53 -070063
64 std::string GetCommandLogData() const;
65 std::string GetFileLogData() const;
66
67 void DisableMinijailForTest();
68
mhasankaf5251d2020-04-29 18:53:03 -070069 protected:
70 Log() = default; // For testing only.
71
Fletcher Woodruff07c28532019-01-24 11:08:53 -070072 private:
73 static uid_t UidForUser(const std::string& name);
74 static gid_t GidForGroup(const std::string& group);
75
76 LogType type_;
77 std::string name_;
78 // For kCommand logs, this is the command to run.
79 // For kFile logs, this is the file path to read.
80 std::string data_;
81 std::string user_;
82 std::string group_;
83 int64_t max_bytes_; // passed as arg to 'tail -c'
84 LogTool::Encoding encoding_;
Brian Norrisafc9f632019-05-09 14:08:28 -070085 bool access_root_mount_ns_;
Fletcher Woodruff07c28532019-01-24 11:08:53 -070086
87 bool minijail_disabled_for_test_ = false;
88 };
89
mhasank80cbe4d2020-04-02 22:46:08 -070090 explicit LogTool(scoped_refptr<dbus::Bus> bus);
91
Ben Chan78f89532014-08-29 09:35:09 -070092 ~LogTool() = default;
Elly Jones03cd6d72012-06-11 13:04:28 -040093
Ben Chan81905fb2017-02-08 22:03:11 -080094 using LogMap = std::map<std::string, std::string>;
Elly Jones533c7c42012-08-10 15:07:05 -040095
Eric Carusoc93a15c2017-04-24 16:15:12 -070096 std::string GetLog(const std::string& name);
Eric Carusof9091f82017-04-28 14:18:59 -070097 LogMap GetAllLogs();
Brian Norrisca4fc042018-04-03 00:24:26 -070098 LogMap GetAllDebugLogs();
mhasank4f599d32020-04-09 22:07:35 -070099 void GetBigFeedbackLogs(const base::ScopedFD& fd,
100 const std::string& username);
mhasank7186aae2020-09-16 20:06:00 -0700101 void BackupArcBugReport(const std::string& username);
102 void DeleteArcBugReportBackup(const std::string& username);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -0700103 void GetJournalLog(const base::ScopedFD& fd);
Chris Morin790fd262019-04-03 20:29:36 -0700104
105 // Returns a representation of |value| with the specified encoding.
Tom Hughesd6c2d392020-08-24 18:12:11 -0700106 static std::string EncodeString(std::string value, Encoding source_encoding);
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -0700107
Elly Jones03cd6d72012-06-11 13:04:28 -0400108 private:
Darin Petkovce9b3a12013-01-10 16:38:54 +0100109 friend class LogToolTest;
110
mhasank80cbe4d2020-04-02 22:46:08 -0700111 // For testing only.
112 LogTool(scoped_refptr<dbus::Bus> bus,
mhasankaf5251d2020-04-29 18:53:03 -0700113 std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface>
114 cryptohome_proxy,
115 const std::unique_ptr<LogTool::Log> arc_bug_report_log,
mhasank80cbe4d2020-04-02 22:46:08 -0700116 const base::FilePath& daemon_store_base_dir);
Qijiang Fan6bc59e12020-11-11 02:51:06 +0900117 LogTool(const LogTool&) = delete;
118 LogTool& operator=(const LogTool&) = delete;
mhasank80cbe4d2020-04-02 22:46:08 -0700119
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700120 void CreateConnectivityReport(bool wait_for_results);
mhasank4f599d32020-04-09 22:07:35 -0700121
122 // Returns the output of arc-bugreport program in ARC.
123 // Returns cached output if it is available for this user.
mhasankd2b84882020-05-04 17:02:19 -0700124 std::string GetArcBugReport(const std::string& username, bool* is_backup);
mhasank7186aae2020-09-16 20:06:00 -0700125 bool IsUserHashValid(const std::string& userhash);
mhasank80cbe4d2020-04-02 22:46:08 -0700126 base::FilePath GetArcBugReportBackupFilePath(const std::string& userhash);
Eric Carusof9091f82017-04-28 14:18:59 -0700127
128 scoped_refptr<dbus::Bus> bus_;
mhasank4f599d32020-04-09 22:07:35 -0700129 std::unique_ptr<org::chromium::CryptohomeInterfaceProxyInterface>
130 cryptohome_proxy_;
mhasankaf5251d2020-04-29 18:53:03 -0700131
132 std::unique_ptr<LogTool::Log> arc_bug_report_log_;
133
mhasank80cbe4d2020-04-02 22:46:08 -0700134 base::FilePath daemon_store_base_dir_;
mhasank4f599d32020-04-09 22:07:35 -0700135 // Set containing userhash of all users for which
136 // ARC bug report has been backed up.
137 std::set<std::string> arc_bug_report_backups_;
Elly Jones03cd6d72012-06-11 13:04:28 -0400138};
139
Ben Chana0011d82014-05-13 00:19:29 -0700140} // namespace debugd
Elly Jones03cd6d72012-06-11 13:04:28 -0400141
Alex Vakulenko262be3f2014-07-30 15:25:50 -0700142#endif // DEBUGD_SRC_LOG_TOOL_H_