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/files.h"
26 #include "common/libs/utils/result.h"
27 #include "host/libs/config/command_source.h"
28 #include "host/libs/config/known_paths.h"
29
30 namespace cuttlefish {
31
32 class TombstoneReceiver : public CommandSource {
33 public:
INJECT(TombstoneReceiver (const CuttlefishConfig::InstanceSpecific & instance))34 INJECT(TombstoneReceiver(const CuttlefishConfig::InstanceSpecific& instance))
35 : instance_(instance) {}
36
37 // CommandSource
Commands()38 Result<std::vector<MonitorCommand>> Commands() override {
39 Command command(TombstoneReceiverBinary());
40 command.AddParameter("-server_fd=", socket_);
41 command.AddParameter("-tombstone_dir=", tombstone_dir_);
42 std::vector<MonitorCommand> commands;
43 commands.emplace_back(std::move(command));
44 return commands;
45 }
46
47 // SetupFeature
Name() const48 std::string Name() const override { return "TombstoneReceiver"; }
Enabled() const49 bool Enabled() const override { return true; }
50
51 private:
Dependencies() const52 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()53 Result<void> ResultSetup() override {
54 tombstone_dir_ = instance_.PerInstancePath("tombstones");
55 if (!DirectoryExists(tombstone_dir_)) {
56 LOG(DEBUG) << "Setting up " << tombstone_dir_;
57 CF_EXPECT(mkdir(tombstone_dir_.c_str(),
58 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0,
59 "Failed to create tombstone directory: "
60 << tombstone_dir_ << ". Error: " << strerror(errno));
61 }
62
63 auto port = instance_.tombstone_receiver_port();
64 socket_ = SharedFD::VsockServer(port, SOCK_STREAM);
65 CF_EXPECT(socket_->IsOpen(), "Unable to create tombstone server socket: "
66 << socket_->StrError());
67 return {};
68 }
69
70 const CuttlefishConfig::InstanceSpecific& instance_;
71 SharedFD socket_;
72 std::string tombstone_dir_;
73 };
74
75 fruit::Component<fruit::Required<const CuttlefishConfig::InstanceSpecific>>
TombstoneReceiverComponent()76 TombstoneReceiverComponent() {
77 return fruit::createComponent()
78 .addMultibinding<CommandSource, TombstoneReceiver>()
79 .addMultibinding<SetupFeature, TombstoneReceiver>();
80 }
81
82 } // namespace cuttlefish
83