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));
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
Name() const84 std::string Name() const override { return "KernelLogMonitor"; }
85
86 private:
Dependencies() const87 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()88 Result<void> ResultSetup() override {
89 auto log_name = instance_.kernel_log_pipe_name();
90 // Open the pipe here (from the launcher) to ensure the pipe is not deleted
91 // due to the usage counters in the kernel reaching zero. If this is not
92 // done and the kernel_log_monitor crashes for some reason the VMM may get
93 // SIGPIPE.
94 fifo_ = CF_EXPECT(SharedFD::Fifo(log_name, 0600));
95
96 for (unsigned int i = 0; i < number_of_event_pipes_; ++i) {
97 SharedFD event_pipe_write_end, event_pipe_read_end;
98 CF_EXPECT(SharedFD::Pipe(&event_pipe_read_end, &event_pipe_write_end),
99 "Failed creating kernel log pipe: " << strerror(errno));
100 event_pipe_write_ends_.push_back(event_pipe_write_end);
101 event_pipe_read_ends_.push_back(event_pipe_read_end);
102 }
103 return {};
104 }
105
106 int number_of_event_pipes_ = 0;
107 const CuttlefishConfig::InstanceSpecific& instance_;
108 SharedFD fifo_;
109 std::vector<SharedFD> event_pipe_write_ends_;
110 std::vector<SharedFD> event_pipe_read_ends_;
111 };
112
113 } // namespace
114
115 fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>,
116 KernelLogPipeProvider>
KernelLogMonitorComponent()117 KernelLogMonitorComponent() {
118 return fruit::createComponent()
119 .bind<KernelLogPipeProvider, KernelLogMonitor>()
120 .addMultibinding<CommandSource, KernelLogMonitor>()
121 .addMultibinding<SetupFeature, KernelLogMonitor>()
122 .addMultibinding<DiagnosticInformation, KernelLogMonitor>()
123 .addMultibinding<LateInjected, KernelLogMonitor>();
124 }
125
126 } // namespace cuttlefish
127