blob: 3fc83992ca73d25822885c2606d7d141ad3f47bd [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>
Ben Chan5facf4a2014-07-23 16:36:54 -070010#include <string>
Elly Jones03cd6d72012-06-11 13:04:28 -040011
Eric Caruso0b241882018-04-04 13:43:46 -070012#include <base/files/scoped_file.h>
Ben Chan657bed82014-09-02 20:40:51 -070013#include <base/macros.h>
Eric Carusocc7106c2017-04-27 14:22:42 -070014#include <base/memory/ref_counted.h>
15#include <dbus/bus.h>
Elly Jones03cd6d72012-06-11 13:04:28 -040016
Fletcher Woodruff07c28532019-01-24 11:08:53 -070017#include "debugd/src/sandboxed_process.h"
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080018
Elly Jones03cd6d72012-06-11 13:04:28 -040019namespace debugd {
20
21class LogTool {
22 public:
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070023 // The encoding for a particular log.
24 enum class Encoding {
25 // Tries to see if the log output is valid UTF-8. Outputs it as-is if it is,
26 // or base64-encodes it otherwise.
27 kAutodetect,
28
29 // Replaces any characters that are not valid UTF-8 encoded with the
30 // replacement character.
31 kUtf8,
32
33 // base64-encodes the output.
Chris Morin790fd262019-04-03 20:29:36 -070034 kBase64,
35
36 // Doesn't apply an encoding. Copies the data as is.
37 kBinary,
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070038 };
39
Fletcher Woodruff07c28532019-01-24 11:08:53 -070040 class Log {
41 public:
42 enum LogType { kCommand, kFile };
43
44 static constexpr int64_t kDefaultMaxBytes = 512 * 1024;
45
46 Log(LogType type,
47 std::string name,
48 std::string data,
49 std::string user = SandboxedProcess::kDefaultUser,
50 std::string group = SandboxedProcess::kDefaultGroup,
51 int64_t max_bytes = kDefaultMaxBytes,
Brian Norrisafc9f632019-05-09 14:08:28 -070052 LogTool::Encoding encoding = LogTool::Encoding::kAutodetect,
53 bool access_root_mount_ns = false);
Fletcher Woodruff07c28532019-01-24 11:08:53 -070054
55 std::string GetName() const;
56 std::string GetLogData() const;
57
58 std::string GetCommandLogData() const;
59 std::string GetFileLogData() const;
60
61 void DisableMinijailForTest();
62
63 private:
64 static uid_t UidForUser(const std::string& name);
65 static gid_t GidForGroup(const std::string& group);
66
67 LogType type_;
68 std::string name_;
69 // For kCommand logs, this is the command to run.
70 // For kFile logs, this is the file path to read.
71 std::string data_;
72 std::string user_;
73 std::string group_;
74 int64_t max_bytes_; // passed as arg to 'tail -c'
75 LogTool::Encoding encoding_;
Brian Norrisafc9f632019-05-09 14:08:28 -070076 bool access_root_mount_ns_;
Fletcher Woodruff07c28532019-01-24 11:08:53 -070077
78 bool minijail_disabled_for_test_ = false;
79 };
80
mhasank80cbe4d2020-04-02 22:46:08 -070081 explicit LogTool(scoped_refptr<dbus::Bus> bus);
82
Ben Chan78f89532014-08-29 09:35:09 -070083 ~LogTool() = default;
Elly Jones03cd6d72012-06-11 13:04:28 -040084
Ben Chan81905fb2017-02-08 22:03:11 -080085 using LogMap = std::map<std::string, std::string>;
Elly Jones533c7c42012-08-10 15:07:05 -040086
Eric Carusoc93a15c2017-04-24 16:15:12 -070087 std::string GetLog(const std::string& name);
Eric Carusof9091f82017-04-28 14:18:59 -070088 LogMap GetAllLogs();
Brian Norrisca4fc042018-04-03 00:24:26 -070089 LogMap GetAllDebugLogs();
Eric Caruso0b241882018-04-04 13:43:46 -070090 void GetBigFeedbackLogs(const base::ScopedFD& fd);
mhasank80cbe4d2020-04-02 22:46:08 -070091 void BackupArcBugReport(const std::string& userhash);
92 void DeleteArcBugReportBackup(const std::string& userhash);
Jeffrey Kardatzkee3ec6fd2019-08-05 12:25:17 -070093 void GetJournalLog(const base::ScopedFD& fd);
Chris Morin790fd262019-04-03 20:29:36 -070094
95 // Returns a representation of |value| with the specified encoding.
96 static std::string EncodeString(std::string value,
97 Encoding source_encoding);
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070098
Elly Jones03cd6d72012-06-11 13:04:28 -040099 private:
Darin Petkovce9b3a12013-01-10 16:38:54 +0100100 friend class LogToolTest;
101
mhasank80cbe4d2020-04-02 22:46:08 -0700102 // For testing only.
103 LogTool(scoped_refptr<dbus::Bus> bus,
104 const base::FilePath& daemon_store_base_dir);
105
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700106 void CreateConnectivityReport(bool wait_for_results);
mhasank80cbe4d2020-04-02 22:46:08 -0700107 base::FilePath GetArcBugReportBackupFilePath(const std::string& userhash);
Eric Carusof9091f82017-04-28 14:18:59 -0700108
109 scoped_refptr<dbus::Bus> bus_;
mhasank80cbe4d2020-04-02 22:46:08 -0700110 base::FilePath daemon_store_base_dir_;
Darin Petkovce9b3a12013-01-10 16:38:54 +0100111
Elly Jones03cd6d72012-06-11 13:04:28 -0400112 DISALLOW_COPY_AND_ASSIGN(LogTool);
113};
114
Ben Chana0011d82014-05-13 00:19:29 -0700115} // namespace debugd
Elly Jones03cd6d72012-06-11 13:04:28 -0400116
Alex Vakulenko262be3f2014-07-30 15:25:50 -0700117#endif // DEBUGD_SRC_LOG_TOOL_H_