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 
20 #include <fruit/fruit.h>
21 
22 #include "common/libs/utils/files.h"
23 #include "common/libs/utils/result.h"
24 #include "host/libs/config/command_source.h"
25 #include "host/libs/config/known_paths.h"
26 
27 namespace cuttlefish {
28 
TombstoneReceiver(const CuttlefishConfig::InstanceSpecific & instance)29 Result<MonitorCommand> TombstoneReceiver(
30     const CuttlefishConfig::InstanceSpecific& instance) {
31   auto tombstone_dir = instance.PerInstancePath("tombstones");
32   if (!DirectoryExists(tombstone_dir)) {
33     LOG(DEBUG) << "Setting up " << tombstone_dir;
34     CF_EXPECTF(mkdir(tombstone_dir.c_str(),
35                      S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0,
36                "Failed to create tombstone directory: '{}'. error: '{}'",
37                tombstone_dir, strerror(errno));
38   }
39 
40   auto port = instance.tombstone_receiver_port();
41   auto socket =
42       SharedFD::VsockServer(port, SOCK_STREAM,
43                             instance.vhost_user_vsock()
44                                 ? std::make_optional(instance.vsock_guest_cid())
45                                 : std::nullopt);
46   CF_EXPECTF(socket->IsOpen(), "Can't tombstone server socket: '{}'",
47              socket->StrError());
48 
49   return Command(TombstoneReceiverBinary())
50       .AddParameter("-server_fd=", socket)
51       .AddParameter("-tombstone_dir=", tombstone_dir);
52 }
53 
54 }  // namespace cuttlefish
55