You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2020/01/07 13:47:37 UTC

[mesos] branch master updated: Added 'issocket()' helper to stout.

This is an automated email from the ASF dual-hosted git repository.

bennoe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git


The following commit(s) were added to refs/heads/master by this push:
     new af676fd  Added 'issocket()' helper to stout.
af676fd is described below

commit af676fd6f967a4d7ce18f4cccaf26711e3bd608c
Author: Benno Evers <be...@apache.org>
AuthorDate: Wed Nov 27 12:37:37 2019 +0100

    Added 'issocket()' helper to stout.
    
    Added a new helper 'issocket()' that tells whether a given
    file is a socket or not.
    
    Review: https://reviews.apache.org/r/71832
---
 3rdparty/stout/include/stout/os/posix/stat.hpp |  9 ++++++
 3rdparty/stout/tests/os/socket_tests.cpp       | 43 ++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

diff --git a/3rdparty/stout/include/stout/os/posix/stat.hpp b/3rdparty/stout/include/stout/os/posix/stat.hpp
index 74e0c93..1cfb5df 100644
--- a/3rdparty/stout/include/stout/os/posix/stat.hpp
+++ b/3rdparty/stout/include/stout/os/posix/stat.hpp
@@ -114,6 +114,15 @@ inline bool isfile(
 }
 
 
+inline bool issocket(
+    const std::string& path,
+    const FollowSymlink follow = FollowSymlink::FOLLOW_SYMLINK)
+{
+  Try<struct ::stat> s = internal::stat(path, follow);
+  return s.isSome() && S_ISSOCK(s->st_mode);
+}
+
+
 // Returns the size in Bytes of a given file system entry. When
 // applied to a symbolic link with `follow` set to
 // `DO_NOT_FOLLOW_SYMLINK`, this will return the length of the entry
diff --git a/3rdparty/stout/tests/os/socket_tests.cpp b/3rdparty/stout/tests/os/socket_tests.cpp
index 9ca236c..3216f46 100644
--- a/3rdparty/stout/tests/os/socket_tests.cpp
+++ b/3rdparty/stout/tests/os/socket_tests.cpp
@@ -11,7 +11,12 @@
 // limitations under the License
 
 #include <stout/os/int_fd.hpp>
+#include <stout/os/mktemp.hpp>
+#include <stout/os/mkdtemp.hpp>
+#include <stout/os/rm.hpp>
+#include <stout/os/rmdir.hpp>
 #include <stout/os/socket.hpp>
+#include <stout/os/stat.hpp>
 
 #include <stout/tests/utils.hpp>
 
@@ -45,3 +50,41 @@ TEST_F(SocketTests, IntFD)
   EXPECT_GT(0, fd);
 }
 #endif // __WINDOWS__
+
+#ifndef __WINDOWS__
+
+#include <sys/un.h>
+
+TEST_F(SocketTests, IsSocket)
+{
+  Try<std::string> nonsocketPath = os::mktemp();
+  Try<std::string> nonsocketDir = os::mkdtemp();
+
+  ASSERT_SOME(nonsocketPath);
+  ASSERT_SOME(nonsocketDir);
+
+  // Avoiding `ASSERT()` after this point to ensure we will arrive
+  // at the `rmdir()` cleanup.
+
+  int socket = ::socket(AF_UNIX, SOCK_STREAM, 0);
+  EXPECT_GE(socket, 0);
+
+  const char* socketFilename = "/agent.sock";
+
+  struct sockaddr_un addr;
+  addr.sun_family = AF_UNIX;
+  ::strncpy(&addr.sun_path[0], &nonsocketDir.get()[0], nonsocketDir->size()+1);
+  ::strncat(&addr.sun_path[0], socketFilename, ::strlen(socketFilename)+1);
+
+  EXPECT_NE(
+    ::bind(socket, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)),
+    -1);
+
+  EXPECT_TRUE(os::stat::issocket(addr.sun_path));
+  EXPECT_FALSE(os::stat::issocket(nonsocketPath.get()));
+  EXPECT_FALSE(os::stat::issocket(nonsocketDir.get()));
+
+  os::rm(nonsocketPath.get());
+  os::rmdir(nonsocketDir.get());
+}
+#endif // __WINDOWS__