1 //
2 // Copyright 2023 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 <utility>
20 #include <vector>
21 
22 #include <fruit/fruit.h>
23 
24 #include "common/libs/fs/shared_fd.h"
25 #include "common/libs/utils/result.h"
26 #include "host/commands/run_cvd/launch/log_tee_creator.h"
27 #include "host/libs/config/command_source.h"
28 #include "host/libs/config/cuttlefish_config.h"
29 #include "host/libs/config/known_paths.h"
30 
31 namespace cuttlefish {
32 
Casimir(const CuttlefishConfig & config,const CuttlefishConfig::EnvironmentSpecific & environment,const CuttlefishConfig::InstanceSpecific & instance,LogTeeCreator & log_tee)33 Result<std::vector<MonitorCommand>> Casimir(
34     const CuttlefishConfig& config,
35     const CuttlefishConfig::EnvironmentSpecific& environment,
36     const CuttlefishConfig::InstanceSpecific& instance,
37     LogTeeCreator& log_tee) {
38   if (!(config.enable_host_nfc() && instance.start_casimir())) {
39     return {};
40   }
41 
42   SharedFD nci_server = SharedFD::SocketLocalServer(
43       environment.casimir_nci_socket_path(), false, SOCK_STREAM, 0600);
44   CF_EXPECTF(nci_server->IsOpen(), "{}", nci_server->StrError());
45 
46   SharedFD rf_server = SharedFD::SocketLocalServer(
47       environment.casimir_rf_socket_path(), false, SOCK_STREAM, 0600);
48   CF_EXPECTF(rf_server->IsOpen(), "{}", rf_server->StrError());
49 
50   Command casimir = Command(ProcessRestarterBinary())
51                         .AddParameter("-when_killed")
52                         .AddParameter("-when_dumped")
53                         .AddParameter("-when_exited_with_failure")
54                         .AddParameter("--")
55                         .AddParameter(CasimirBinary())
56                         .AddParameter("--nci-unix-fd")
57                         .AddParameter(nci_server)
58                         .AddParameter("--rf-unix-fd")
59                         .AddParameter(rf_server);
60 
61   for (auto const& arg : config.casimir_args()) {
62     casimir.AddParameter(arg);
63   }
64 
65   std::vector<MonitorCommand> commands;
66   commands.emplace_back(CF_EXPECT(log_tee.CreateLogTee(casimir, "casimir")));
67   commands.emplace_back(std::move(casimir));
68   return commands;
69 }
70 
71 }  // namespace cuttlefish
72