• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 inhereted "
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     CF_EXPECT(mkfifo(log_name.c_str(), 0600) == 0,
92               "Unable to create named pipe at " << log_name << ": "
93                                                 << strerror(errno));
94 
95     // Open the pipe here (from the launcher) to ensure the pipe is not deleted
96     // due to the usage counters in the kernel reaching zero. If this is not
97     // done and the kernel_log_monitor crashes for some reason the VMM may get
98     // SIGPIPE.
99     fifo_ = SharedFD::Open(log_name, O_RDWR);
100     CF_EXPECT(fifo_->IsOpen(),
101               "Unable to open \"" << log_name << "\": " << fifo_->StrError());
102 
103     for (unsigned int i = 0; i < number_of_event_pipes_; ++i) {
104       SharedFD event_pipe_write_end, event_pipe_read_end;
105       CF_EXPECT(SharedFD::Pipe(&event_pipe_read_end, &event_pipe_write_end),
106                 "Failed creating kernel log pipe: " << strerror(errno));
107       event_pipe_write_ends_.push_back(event_pipe_write_end);
108       event_pipe_read_ends_.push_back(event_pipe_read_end);
109     }
110     return {};
111   }
112 
113   int number_of_event_pipes_ = 0;
114   const CuttlefishConfig::InstanceSpecific& instance_;
115   SharedFD fifo_;
116   std::vector<SharedFD> event_pipe_write_ends_;
117   std::vector<SharedFD> event_pipe_read_ends_;
118 };
119 
120 }  // namespace
121 
122 fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>,
123                  KernelLogPipeProvider>
KernelLogMonitorComponent()124 KernelLogMonitorComponent() {
125   return fruit::createComponent()
126       .bind<KernelLogPipeProvider, KernelLogMonitor>()
127       .addMultibinding<CommandSource, KernelLogMonitor>()
128       .addMultibinding<SetupFeature, KernelLogMonitor>()
129       .addMultibinding<DiagnosticInformation, KernelLogMonitor>()
130       .addMultibinding<LateInjected, KernelLogMonitor>();
131 }
132 
133 }  // namespace cuttlefish
134