1 //
2 // Copyright (C) 2019 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "host/commands/run_cvd/launch/launch.h"
17
18 #include <string>
19 #include <unordered_set>
20 #include <utility>
21 #include <vector>
22
23 #include <fruit/fruit.h>
24
25 #include "common/libs/utils/result.h"
26 #include "host/commands/run_cvd/reporting.h"
27 #include "host/libs/config/command_source.h"
28 #include "host/libs/config/inject.h"
29 #include "host/libs/config/known_paths.h"
30
31 namespace cuttlefish {
32 namespace {
33
34 class KernelLogMonitor : public CommandSource,
35 public KernelLogPipeProvider,
36 public DiagnosticInformation,
37 public LateInjected {
38 public:
INJECT(KernelLogMonitor (const CuttlefishConfig::InstanceSpecific & instance))39 INJECT(KernelLogMonitor(const CuttlefishConfig::InstanceSpecific& instance))
40 : instance_(instance) {}
41
42 // DiagnosticInformation
Diagnostics() const43 std::vector<std::string> Diagnostics() const override {
44 return {"Kernel log: " + instance_.PerInstancePath("kernel.log")};
45 }
46
LateInject(fruit::Injector<> & injector)47 Result<void> LateInject(fruit::Injector<>& injector) override {
48 number_of_event_pipes_ =
49 injector.getMultibindings<KernelLogPipeConsumer>().size();
50 return {};
51 }
52
53 // CommandSource
Commands()54 Result<std::vector<MonitorCommand>> Commands() override {
55 Command command(KernelLogMonitorBinary());
56 command.AddParameter("-log_pipe_fd=", fifo_);
57
58 if (!event_pipe_write_ends_.empty()) {
59 command.AddParameter("-subscriber_fds=");
60 for (size_t i = 0; i < event_pipe_write_ends_.size(); i++) {
61 if (i > 0) {
62 command.AppendToLastParameter(",");
63 }
64 command.AppendToLastParameter(event_pipe_write_ends_[i]);
65 }
66 }
67 std::vector<MonitorCommand> commands;
68 commands.emplace_back(std::move(command)).can_sandbox = true;
69 return commands;
70 }
71
72 // KernelLogPipeProvider
KernelLogPipe()73 SharedFD KernelLogPipe() override {
74 CHECK(!event_pipe_read_ends_.empty()) << "No more kernel pipes left. Make sure you inherited "
75 "KernelLogPipeProvider and provided multibinding "
76 "from KernelLogPipeConsumer to your type.";
77 SharedFD ret = event_pipe_read_ends_.back();
78 event_pipe_read_ends_.pop_back();
79 return ret;
80 }
81
82 private:
83 // SetupFeature
Enabled() const84 bool Enabled() const override { return true; }
Name() const85 std::string Name() const override { return "KernelLogMonitor"; }
86
87 private:
Dependencies() const88 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()89 Result<void> ResultSetup() override {
90 auto log_name = instance_.kernel_log_pipe_name();
91 // Open the pipe here (from the launcher) to ensure the pipe is not deleted
92 // due to the usage counters in the kernel reaching zero. If this is not
93 // done and the kernel_log_monitor crashes for some reason the VMM may get
94 // SIGPIPE.
95 fifo_ = CF_EXPECT(SharedFD::Fifo(log_name, 0600));
96
97 for (unsigned int i = 0; i < number_of_event_pipes_; ++i) {
98 SharedFD event_pipe_write_end, event_pipe_read_end;
99 CF_EXPECT(SharedFD::Pipe(&event_pipe_read_end, &event_pipe_write_end),
100 "Failed creating kernel log pipe: " << strerror(errno));
101 event_pipe_write_ends_.push_back(event_pipe_write_end);
102 event_pipe_read_ends_.push_back(event_pipe_read_end);
103 }
104 return {};
105 }
106
107 int number_of_event_pipes_ = 0;
108 const CuttlefishConfig::InstanceSpecific& instance_;
109 SharedFD fifo_;
110 std::vector<SharedFD> event_pipe_write_ends_;
111 std::vector<SharedFD> event_pipe_read_ends_;
112 };
113
114 } // namespace
115
116 fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>,
117 KernelLogPipeProvider>
KernelLogMonitorComponent()118 KernelLogMonitorComponent() {
119 return fruit::createComponent()
120 .bind<KernelLogPipeProvider, KernelLogMonitor>()
121 .addMultibinding<CommandSource, KernelLogMonitor>()
122 .addMultibinding<SetupFeature, KernelLogMonitor>()
123 .addMultibinding<DiagnosticInformation, KernelLogMonitor>()
124 .addMultibinding<LateInjected, KernelLogMonitor>();
125 }
126
127 } // namespace cuttlefish
128