blob: 2c17f5270f44c586a1524c2b2d6dabe9f097c64b [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
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080017#include "debugd/src/anonymizer_tool.h"
Fletcher Woodruff07c28532019-01-24 11:08:53 -070018#include "debugd/src/sandboxed_process.h"
Ahmed Fakhry21140cf2016-03-04 17:15:19 -080019
Elly Jones03cd6d72012-06-11 13:04:28 -040020namespace debugd {
21
22class LogTool {
23 public:
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070024 // 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.
35 kBinary
36 };
37
Fletcher Woodruff07c28532019-01-24 11:08:53 -070038 class Log {
39 public:
40 enum LogType { kCommand, kFile };
41
42 static constexpr int64_t kDefaultMaxBytes = 512 * 1024;
43
44 Log(LogType type,
45 std::string name,
46 std::string data,
47 std::string user = SandboxedProcess::kDefaultUser,
48 std::string group = SandboxedProcess::kDefaultGroup,
49 int64_t max_bytes = kDefaultMaxBytes,
50 LogTool::Encoding encoding = LogTool::Encoding::kAutodetect);
51
52 std::string GetName() const;
53 std::string GetLogData() const;
54
55 std::string GetCommandLogData() const;
56 std::string GetFileLogData() const;
57
58 void DisableMinijailForTest();
59
60 private:
61 static uid_t UidForUser(const std::string& name);
62 static gid_t GidForGroup(const std::string& group);
63
64 LogType type_;
65 std::string name_;
66 // For kCommand logs, this is the command to run.
67 // For kFile logs, this is the file path to read.
68 std::string data_;
69 std::string user_;
70 std::string group_;
71 int64_t max_bytes_; // passed as arg to 'tail -c'
72 LogTool::Encoding encoding_;
73
74 bool minijail_disabled_for_test_ = false;
75 };
76
Eric Carusof9091f82017-04-28 14:18:59 -070077 explicit LogTool(scoped_refptr<dbus::Bus> bus) : bus_(bus) {}
Ben Chan78f89532014-08-29 09:35:09 -070078 ~LogTool() = default;
Elly Jones03cd6d72012-06-11 13:04:28 -040079
Ben Chan81905fb2017-02-08 22:03:11 -080080 using LogMap = std::map<std::string, std::string>;
Elly Jones533c7c42012-08-10 15:07:05 -040081
Eric Carusoc93a15c2017-04-24 16:15:12 -070082 std::string GetLog(const std::string& name);
Eric Carusof9091f82017-04-28 14:18:59 -070083 LogMap GetAllLogs();
Brian Norrisca4fc042018-04-03 00:24:26 -070084 LogMap GetAllDebugLogs();
Eric Carusof9091f82017-04-28 14:18:59 -070085 LogMap GetFeedbackLogs();
Eric Caruso0b241882018-04-04 13:43:46 -070086 void GetBigFeedbackLogs(const base::ScopedFD& fd);
Eric Carusoc93a15c2017-04-24 16:15:12 -070087 LogMap GetUserLogFiles();
Darin Petkovce9b3a12013-01-10 16:38:54 +010088
Luis Hector Chavezfc2566f2018-09-13 15:00:36 -070089 // Returns a representation of |value| that is valid UTF-8 encoded. The value
90 // of |source_encoding| determines whether it will use Unicode U+FFFD
91 // REPLACEMENT CHARACTER or base64-encode the whole string in case there are
92 // invalid characters.
93 static std::string EnsureUTF8String(const std::string& value,
94 Encoding source_encoding);
95
Elly Jones03cd6d72012-06-11 13:04:28 -040096 private:
Darin Petkovce9b3a12013-01-10 16:38:54 +010097 friend class LogToolTest;
98
99 void AnonymizeLogMap(LogMap* log_map);
Fletcher Woodruff70f27232019-01-24 11:41:34 -0700100 void CreateConnectivityReport(bool wait_for_results);
Eric Carusof9091f82017-04-28 14:18:59 -0700101
102 scoped_refptr<dbus::Bus> bus_;
Darin Petkovce9b3a12013-01-10 16:38:54 +0100103
Ahmed Fakhry21140cf2016-03-04 17:15:19 -0800104 AnonymizerTool anonymizer_;
105
Elly Jones03cd6d72012-06-11 13:04:28 -0400106 DISALLOW_COPY_AND_ASSIGN(LogTool);
107};
108
Ben Chana0011d82014-05-13 00:19:29 -0700109} // namespace debugd
Elly Jones03cd6d72012-06-11 13:04:28 -0400110
Alex Vakulenko262be3f2014-07-30 15:25:50 -0700111#endif // DEBUGD_SRC_LOG_TOOL_H_