diff options
author | Mario Emmenlauer <memmenlauer@biodataanalysis.de> | 2020-11-19 10:24:25 +0100 |
---|---|---|
committer | Zezeng Wang <51382517@qq.com> | 2020-12-19 14:22:13 +0800 |
commit | 3b9259d88b6ceb13bb6b8c6afe676fed707dcd4e (patch) | |
tree | a16382bf613e70340fb7b82d45e82f5d9be95af7 | |
parent | 37c2ceb737cb40377346c63a05f407da1c119ba0 (diff) | |
download | thrift-3b9259d88b6ceb13bb6b8c6afe676fed707dcd4e.tar.gz |
TServerSocket.cpp::isOpen(): Check that a domain socket file already exists
-rw-r--r-- | lib/cpp/src/thrift/transport/TServerSocket.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/cpp/src/thrift/transport/TServerSocket.cpp b/lib/cpp/src/thrift/transport/TServerSocket.cpp index 0a47b3fe0..0c32b106e 100644 --- a/lib/cpp/src/thrift/transport/TServerSocket.cpp +++ b/lib/cpp/src/thrift/transport/TServerSocket.cpp @@ -43,6 +43,9 @@ #ifdef HAVE_UNISTD_H #include <unistd.h> #endif +#ifdef HAVE_SYS_STAT_H +#include <sys/stat.h> +#endif #include <thrift/transport/PlatformSocket.h> #include <thrift/transport/TServerSocket.h> @@ -62,6 +65,16 @@ #endif // _WIN32 #endif +#ifdef _WIN32 +// Including Windows.h can conflict with Winsock2 usage, and also +// adds problematic macros like min() and max(). Try to work around: +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include <Windows.h> +#undef NOMINMAX +#undef WIN32_LEAN_AND_MEAN +#endif + template <class T> inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { return reinterpret_cast<const SOCKOPT_CAST_T*>(v); @@ -174,6 +187,25 @@ bool TServerSocket::isOpen() const { if (!listening_) return false; + if (!path_.empty() && (path_[0] != '\0')) { + // On some platforms the domain socket file may not be instantly + // available yet, i.e. the Windows file system can be slow. Therefore + // we should check that the domain socket file actually exists. +#ifdef _MSC_VER + // Currently there is a bug in ClangCl on Windows so the stat() call + // does not work. Workaround is a Windows-specific call if file exists: + DWORD const f_attrib = GetFileAttributesA(path_.c_str()); + if (f_attrib == INVALID_FILE_ATTRIBUTES) { +#else + struct THRIFT_STAT path_info; + if (::THRIFT_STAT(path_.c_str(), &path_info) < 0) { +#endif + const std::string vError = "TServerSocket::isOpen(): The domain socket path '" + path_ + "' does not exist (yet)."; + GlobalOutput.perror(vError.c_str(), THRIFT_GET_SOCKET_ERROR); + return false; + } + } + return true; } |