You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by qi...@apache.org on 2018/11/08 08:19:15 UTC
[mesos] 01/09: Added `lsof()` into stout.
This is an automated email from the ASF dual-hosted git repository.
qianzhang pushed a commit to branch 1.7.x
in repository https://gitbox.apache.org/repos/asf/mesos.git
commit 7c803db018dcf6d1c16d6d88e9042a351c9f9f4c
Author: Qian Zhang <zh...@gmail.com>
AuthorDate: Wed Aug 29 10:16:43 2018 +0800
Added `lsof()` into stout.
Review: https://reviews.apache.org/r/68642
---
3rdparty/stout/include/Makefile.am | 3 +
3rdparty/stout/include/stout/os.hpp | 1 +
3rdparty/stout/include/stout/os/lsof.hpp | 26 ++++++++
3rdparty/stout/include/stout/os/posix/lsof.hpp | 82 ++++++++++++++++++++++++
3rdparty/stout/include/stout/os/windows/lsof.hpp | 28 ++++++++
5 files changed, 140 insertions(+)
diff --git a/3rdparty/stout/include/Makefile.am b/3rdparty/stout/include/Makefile.am
index 0a4ea7b..e55df8a 100644
--- a/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/stout/include/Makefile.am
@@ -89,6 +89,7 @@ nobase_include_HEADERS = \
stout/os/linux.hpp \
stout/os/ls.hpp \
stout/os/lseek.hpp \
+ stout/os/lsof.hpp \
stout/os/mkdir.hpp \
stout/os/mkdtemp.hpp \
stout/os/mktemp.hpp \
@@ -139,6 +140,7 @@ nobase_include_HEADERS = \
stout/os/posix/killtree.hpp \
stout/os/posix/ls.hpp \
stout/os/posix/lseek.hpp \
+ stout/os/posix/lsof.hpp \
stout/os/posix/mkdir.hpp \
stout/os/posix/mkdtemp.hpp \
stout/os/posix/mktemp.hpp \
@@ -182,6 +184,7 @@ nobase_include_HEADERS = \
stout/os/windows/killtree.hpp \
stout/os/windows/ls.hpp \
stout/os/windows/lseek.hpp \
+ stout/os/windows/lsof.hpp \
stout/os/windows/mkdir.hpp \
stout/os/windows/mktemp.hpp \
stout/os/windows/mkdtemp.hpp \
diff --git a/3rdparty/stout/include/stout/os.hpp b/3rdparty/stout/include/stout/os.hpp
index aee0418..15cbb50 100644
--- a/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/stout/include/stout/os.hpp
@@ -54,6 +54,7 @@
#include <stout/os/kill.hpp>
#include <stout/os/ls.hpp>
#include <stout/os/lseek.hpp>
+#include <stout/os/lsof.hpp>
#include <stout/os/mkdir.hpp>
#include <stout/os/mkdtemp.hpp>
#include <stout/os/mktemp.hpp>
diff --git a/3rdparty/stout/include/stout/os/lsof.hpp b/3rdparty/stout/include/stout/os/lsof.hpp
new file mode 100644
index 0000000..668c664
--- /dev/null
+++ b/3rdparty/stout/include/stout/os/lsof.hpp
@@ -0,0 +1,26 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_OS_LSOF_HPP__
+#define __STOUT_OS_LSOF_HPP__
+
+
+// For readability, we minimize the number of #ifdef blocks in the code by
+// splitting platform specific system calls into separate directories.
+#ifdef __WINDOWS__
+#include <stout/os/windows/lsof.hpp>
+#else
+#include <stout/os/posix/lsof.hpp>
+#endif // __WINDOWS__
+
+
+#endif // __STOUT_OS_LSOF_HPP__
diff --git a/3rdparty/stout/include/stout/os/posix/lsof.hpp b/3rdparty/stout/include/stout/os/posix/lsof.hpp
new file mode 100644
index 0000000..12c522a
--- /dev/null
+++ b/3rdparty/stout/include/stout/os/posix/lsof.hpp
@@ -0,0 +1,82 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_OS_POSIX_LSOF_HPP__
+#define __STOUT_OS_POSIX_LSOF_HPP__
+
+#include <string>
+#include <vector>
+
+#include <stout/numify.hpp>
+#include <stout/try.hpp>
+
+#include <stout/os/int_fd.hpp>
+#include <stout/os/ls.hpp>
+
+namespace os {
+
+// Get all the open file descriptors of the current process.
+inline Try<std::vector<int_fd>> lsof()
+{
+ int fdDir = ::open("/dev/fd", O_RDONLY | O_CLOEXEC);
+ if (fdDir == -1) {
+ return ErrnoError("Failed to open '/dev/fd'");
+ }
+
+ DIR* dir = ::fdopendir(fdDir);
+ if (dir == nullptr) {
+ Error error = ErrnoError("Failed to fdopendir '/dev/fd'");
+ ::close(fdDir);
+ return error;
+ }
+
+ struct dirent* entry;
+ std::vector<int_fd> result;
+
+ // Zero `errno` before starting to call `readdir`. This is necessary
+ // to allow us to determine when `readdir` returns an error.
+ errno = 0;
+
+ while ((entry = ::readdir(dir)) != nullptr) {
+ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
+ continue;
+ }
+
+ Try<int_fd> fd = numify<int_fd>(entry->d_name);
+ if (fd.isError()) {
+ return Error(
+ "Could not interpret file descriptor '" +
+ std::string(entry->d_name) + "': " + fd.error());
+ }
+
+ if (fd.get() != fdDir) {
+ result.push_back(fd.get());
+ }
+ }
+
+ if (errno != 0) {
+ // Preserve `readdir` error.
+ Error error = ErrnoError("Failed to read directory");
+ ::closedir(dir);
+ return error;
+ }
+
+ if (::closedir(dir) == -1) {
+ return ErrnoError("Failed to close directory");
+ }
+
+ return result;
+}
+
+} // namespace os {
+
+#endif /* __STOUT_OS_POSIX_LSOF_HPP__ */
diff --git a/3rdparty/stout/include/stout/os/windows/lsof.hpp b/3rdparty/stout/include/stout/os/windows/lsof.hpp
new file mode 100644
index 0000000..d5a6397
--- /dev/null
+++ b/3rdparty/stout/include/stout/os/windows/lsof.hpp
@@ -0,0 +1,28 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_OS_WINDOWS_LSOF_HPP__
+#define __STOUT_OS_WINDOWS_LSOF_HPP__
+
+#include <vector>
+
+#include <stout/try.hpp>
+
+#include <stout/os/int_fd.hpp>
+
+namespace os {
+
+inline Try<std::vector<int_fd>> lsof() = delete;
+
+} // namespace os {
+
+#endif /* __STOUT_OS_WINDOWS_LSOF_HPP__ */