Skip to content

Commit b8d9225

Browse files
Centralize FIFO reuse and draining logic, migrate IPC connectors
1 parent 43e8074 commit b8d9225

12 files changed

Lines changed: 62 additions & 36 deletions

File tree

base/cvd/cuttlefish/common/libs/fs/shared_fd.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -518,32 +518,16 @@ int SharedFD::Fchdir(SharedFD shared_fd) {
518518

519519
Result<SharedFD> SharedFD::Fifo(const std::string& path, mode_t mode) {
520520
struct stat st {};
521-
bool existed = false;
522-
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) != 0) {
523-
CF_EXPECTF(TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode)) == 0,
524-
"Failed to mkfifo('{}', {:o}): {}", path, mode,
521+
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) == 0) {
522+
CF_EXPECTF(TEMP_FAILURE_RETRY(remove(path.c_str())) == 0,
523+
"Failed to delete old file at '{}': '{}'", path,
525524
::cuttlefish::StrError(errno));
526-
} else {
527-
CF_EXPECTF(S_ISFIFO(st.st_mode),
528-
"File at '{}' exists but is not a FIFO", path);
529-
existed = true;
530525
}
531526

527+
CF_EXPECTF(TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode)) == 0,
528+
"Failed to mkfifo('{}', {:o})", path, mode);
532529
auto ret = Open(path, O_RDWR);
533530
CF_EXPECTF(ret->IsOpen(), "Failed to open '{}': '{}'", path, ret->StrError());
534-
535-
if (existed) {
536-
int flags = ret->Fcntl(F_GETFL, 0);
537-
if (flags >= 0) {
538-
ret->Fcntl(F_SETFL, flags | O_NONBLOCK);
539-
char buf[4096];
540-
while (ret->Read(buf, sizeof(buf)) > 0) {
541-
// Reading while there is data to read
542-
}
543-
ret->Fcntl(F_SETFL, flags);
544-
}
545-
}
546-
547531
return ret;
548532
}
549533

base/cvd/cuttlefish/common/libs/utils/files.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,4 +650,35 @@ Result<std::string> Search(const std::vector<std::string>& path,
650650
return CF_ERR("Not found: ") << name << ", path " << absl::StrJoin(path, ":");
651651
}
652652

653+
Result<SharedFD> CreateOrReuseAndDrainFifo(const std::string& path, mode_t mode) {
654+
struct stat st {};
655+
bool existed = false;
656+
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) != 0) {
657+
CF_EXPECTF(TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode)) == 0,
658+
"Failed to mkfifo('{}', {:o}): {}", path, mode,
659+
::cuttlefish::StrError(errno));
660+
} else {
661+
CF_EXPECTF(S_ISFIFO(st.st_mode),
662+
"File at '{}' exists but is not a FIFO", path);
663+
existed = true;
664+
}
665+
666+
auto ret = SharedFD::Open(path, O_RDWR);
667+
CF_EXPECTF(ret->IsOpen(), "Failed to open '{}': '{}'", path, ret->StrError());
668+
669+
if (existed) {
670+
int flags = ret->Fcntl(F_GETFL, 0);
671+
if (flags >= 0) {
672+
ret->Fcntl(F_SETFL, flags | O_NONBLOCK);
673+
char buf[4096];
674+
while (ret->Read(buf, sizeof(buf)) > 0) {
675+
// Reading while there is data to read
676+
}
677+
ret->Fcntl(F_SETFL, flags);
678+
}
679+
}
680+
681+
return ret;
682+
}
683+
653684
} // namespace cuttlefish

base/cvd/cuttlefish/common/libs/utils/files.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <vector>
2626

2727
#include "cuttlefish/result/result.h"
28+
#include "cuttlefish/common/libs/fs/shared_fd.h"
2829

2930
namespace cuttlefish {
3031
bool FileExists(const std::string& path, bool follow_symlinks = true);
@@ -110,4 +111,6 @@ std::vector<std::string> Path(const std::string& env_name = "PATH");
110111
Result<std::string> Search(const std::vector<std::string>& path,
111112
std::string_view name);
112113

114+
Result<SharedFD> CreateOrReuseAndDrainFifo(const std::string& path, mode_t mode);
115+
113116
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/run_cvd/launch/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ cf_cc_library(
3434
hdrs = ["bluetooth_connector.h"],
3535
deps = [
3636
"//cuttlefish/common/libs/fs",
37+
"//cuttlefish/common/libs/utils:files",
3738
"//cuttlefish/host/libs/config:cuttlefish_config",
3839
"//cuttlefish/host/libs/config:known_paths",
3940
"//cuttlefish/host/libs/feature",
@@ -150,6 +151,7 @@ cf_cc_library(
150151
hdrs = ["gnss_grpc_proxy.h"],
151152
deps = [
152153
"//cuttlefish/common/libs/fs",
154+
"//cuttlefish/common/libs/utils:files",
153155
"//cuttlefish/common/libs/utils:in_sandbox",
154156
"//cuttlefish/host/commands/run_cvd/launch:grpc_socket_creator",
155157
"//cuttlefish/host/libs/config:cuttlefish_config",
@@ -291,6 +293,7 @@ cf_cc_library(
291293
hdrs = ["nfc_connector.h"],
292294
deps = [
293295
"//cuttlefish/common/libs/fs",
296+
"//cuttlefish/common/libs/utils:files",
294297
"//cuttlefish/common/libs/utils:subprocess",
295298
"//cuttlefish/host/libs/config:cuttlefish_config",
296299
"//cuttlefish/host/libs/config:known_paths",
@@ -386,6 +389,8 @@ cf_cc_library(
386389
srcs = ["secure_env.cpp"],
387390
hdrs = ["secure_env.h"],
388391
deps = [
392+
"//cuttlefish/common/libs/fs",
393+
"//cuttlefish/common/libs/utils:files",
389394
"//cuttlefish/common/libs/utils:subprocess",
390395
"//cuttlefish/host/commands/run_cvd/launch:snapshot_control_files",
391396
"//cuttlefish/host/libs/config:cuttlefish_config",
@@ -402,6 +407,7 @@ cf_cc_library(
402407
hdrs = ["sensors_simulator.h"],
403408
deps = [
404409
"//cuttlefish/common/libs/fs",
410+
"//cuttlefish/common/libs/utils:files",
405411
"//cuttlefish/common/libs/utils:subprocess",
406412
"//cuttlefish/host/commands/run_cvd/launch:sensors_socket_pair",
407413
"//cuttlefish/host/libs/config:cuttlefish_config",
@@ -441,6 +447,7 @@ cf_cc_library(
441447
hdrs = ["streamer.h"],
442448
deps = [
443449
"//cuttlefish/common/libs/fs",
450+
"//cuttlefish/common/libs/utils:files",
444451
"//cuttlefish/common/libs/utils:subprocess",
445452
"//cuttlefish/host/commands/run_cvd:reporting",
446453
"//cuttlefish/host/commands/run_cvd/launch:enable_multitouch",
@@ -509,6 +516,7 @@ cf_cc_library(
509516
hdrs = ["uwb_connector.h"],
510517
deps = [
511518
"//cuttlefish/common/libs/fs",
519+
"//cuttlefish/common/libs/utils:files",
512520
"//cuttlefish/common/libs/utils:subprocess",
513521
"//cuttlefish/host/libs/config:config_utils",
514522
"//cuttlefish/host/libs/config:cuttlefish_config",

base/cvd/cuttlefish/host/commands/run_cvd/launch/bluetooth_connector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include "cuttlefish/common/libs/fs/shared_fd.h"
25+
#include "cuttlefish/common/libs/utils/files.h"
2526
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
2627
#include "cuttlefish/host/libs/config/known_paths.h"
2728
#include "cuttlefish/host/libs/feature/command_source.h"
@@ -49,7 +50,7 @@ Result<std::optional<MonitorCommand>> BluetoothConnector(
4950
};
5051
std::vector<SharedFD> fifos;
5152
for (const auto& path : fifo_paths) {
52-
fifos.emplace_back(CF_EXPECT(SharedFD::Fifo(path, 0660)));
53+
fifos.emplace_back(CF_EXPECT(CreateOrReuseAndDrainFifo(path, 0660)));
5354
}
5455
return Command(TcpConnectorBinary())
5556
.AddParameter("-fifo_out=", fifos[0])

base/cvd/cuttlefish/host/commands/run_cvd/launch/gnss_grpc_proxy.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <vector>
2121

2222
#include "cuttlefish/common/libs/fs/shared_fd.h"
23+
#include "cuttlefish/common/libs/utils/files.h"
2324
#include "cuttlefish/common/libs/utils/in_sandbox.h"
2425
#include "cuttlefish/host/commands/run_cvd/launch/grpc_socket_creator.h"
2526
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
@@ -43,7 +44,7 @@ Result<std::optional<MonitorCommand>> GnssGrpcProxyServer(
4344
instance.PerInstanceInternalPath("locationhvc_fifo_vm.out"),
4445
};
4546
for (const auto& path : fifo_paths) {
46-
fifos.emplace_back(CF_EXPECT(SharedFD::Fifo(path, 0660)));
47+
fifos.emplace_back(CF_EXPECT(CreateOrReuseAndDrainFifo(path, 0660)));
4748
}
4849

4950
auto gnss_grpc_proxy_cmd =

base/cvd/cuttlefish/host/commands/run_cvd/launch/netsim_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class NetsimServer : public CommandSource {
209209
Result<SharedFD> MakeFifo(const CuttlefishConfig::InstanceSpecific& instance,
210210
const char* relative_path) {
211211
auto path = instance.PerInstanceInternalPath(relative_path);
212-
return CF_EXPECT(SharedFD::Fifo(path, 0660));
212+
return CF_EXPECT(CreateOrReuseAndDrainFifo(path, 0660));
213213
}
214214

215215
private:

base/cvd/cuttlefish/host/commands/run_cvd/launch/nfc_connector.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <vector>
2222

2323
#include "cuttlefish/common/libs/fs/shared_fd.h"
24+
#include "cuttlefish/common/libs/utils/files.h"
2425
#include "cuttlefish/common/libs/utils/subprocess.h"
2526
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
2627
#include "cuttlefish/host/libs/config/known_paths.h"
@@ -40,7 +41,7 @@ Result<MonitorCommand> NfcConnector(
4041
};
4142
std::vector<SharedFD> fifos;
4243
for (const auto& path : fifo_paths) {
43-
fifos.emplace_back(CF_EXPECT(SharedFD::Fifo(path, 0660)));
44+
fifos.emplace_back(CF_EXPECT(CreateOrReuseAndDrainFifo(path, 0660)));
4445
}
4546
return Command(TcpConnectorBinary())
4647
.AddParameter("-fifo_out=", fifos[0])

base/cvd/cuttlefish/host/commands/run_cvd/launch/secure_env.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919
#include <vector>
2020

21+
#include "cuttlefish/common/libs/utils/files.h"
2122
#include "cuttlefish/common/libs/utils/subprocess.h"
2223
#include "cuttlefish/host/commands/run_cvd/launch/snapshot_control_files.h"
2324
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
@@ -54,7 +55,7 @@ Result<MonitorCommand> SecureEnv(
5455
};
5556
std::vector<SharedFD> fifos;
5657
for (const auto& path : fifo_paths) {
57-
fifos.emplace_back(CF_EXPECT(SharedFD::Fifo(path, 0660)));
58+
fifos.emplace_back(CF_EXPECT(CreateOrReuseAndDrainFifo(path, 0660)));
5859
}
5960
command.AddParameter("-keymaster_fd_out=", fifos[0]);
6061
command.AddParameter("-keymaster_fd_in=", fifos[1]);

base/cvd/cuttlefish/host/commands/run_cvd/launch/sensors_simulator.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515

1616
#include "cuttlefish/host/commands/run_cvd/launch/sensors_simulator.h"
1717

18-
#include <unistd.h>
19-
2018
#include <string>
2119

2220
#include "cuttlefish/common/libs/fs/shared_fd.h"
21+
#include "cuttlefish/common/libs/utils/files.h"
2322
#include "cuttlefish/common/libs/utils/subprocess.h"
2423
#include "cuttlefish/host/commands/run_cvd/launch/sensors_socket_pair.h"
2524
#include "cuttlefish/host/libs/config/cuttlefish_config.h"
@@ -32,8 +31,7 @@ namespace cuttlefish {
3231
namespace {
3332

3433
Result<SharedFD> CreateFifo(const std::string& path) {
35-
unlink(path.c_str());
36-
return SharedFD::Fifo(path, 0660);
34+
return CreateOrReuseAndDrainFifo(path, 0660);
3735
}
3836

3937
} // namespace

0 commit comments

Comments
 (0)