From b5c569cd30f8a657b543356e0b563f86b23ab832 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 20 Aug 2018 16:05:03 +0200 Subject: system/FileDescriptor: add IsPipe(), IsSocket() --- src/system/FileDescriptor.cxx | 14 ++++++++++++++ src/system/FileDescriptor.hxx | 12 ++++++++++++ 2 files changed, 26 insertions(+) (limited to 'src/system') diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index 403aee068..e8760f370 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -65,6 +65,20 @@ FileDescriptor::IsValid() const noexcept return IsDefined() && fcntl(fd, F_GETFL) >= 0; } +bool +FileDescriptor::IsPipe() const noexcept +{ + struct stat st; + return IsDefined() && fstat(fd, &st) == 0 && S_ISFIFO(st.st_mode); +} + +bool +FileDescriptor::IsSocket() const noexcept +{ + struct stat st; + return IsDefined() && fstat(fd, &st) == 0 && S_ISSOCK(st.st_mode); +} + #endif bool diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx index 1cc780050..a6e32b963 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -75,6 +75,18 @@ public: */ gcc_pure bool IsValid() const noexcept; + + /** + * Ask the kernel whether this is a pipe. + */ + gcc_pure + bool IsPipe() const noexcept; + + /** + * Ask the kernel whether this is a socket descriptor. + */ + gcc_pure + bool IsSocket() const noexcept; #endif /** -- cgit v1.2.3