Skip to content

Commit 43e8074

Browse files
Reading while there is data to read in FIFO
1 parent 6a48b31 commit 43e8074

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,32 @@ 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;
521522
if (TEMP_FAILURE_RETRY(stat(path.c_str(), &st)) != 0) {
522523
CF_EXPECTF(TEMP_FAILURE_RETRY(mkfifo(path.c_str(), mode)) == 0,
523524
"Failed to mkfifo('{}', {:o}): {}", path, mode,
524525
::cuttlefish::StrError(errno));
525526
} else {
526527
CF_EXPECTF(S_ISFIFO(st.st_mode),
527528
"File at '{}' exists but is not a FIFO", path);
529+
existed = true;
528530
}
529531

530532
auto ret = Open(path, O_RDWR);
531533
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+
532547
return ret;
533548
}
534549

0 commit comments

Comments
 (0)