You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2013/06/20 05:30:19 UTC
[3/6] git commit: Added utilities for getting process group and
session members.
Added utilities for getting process group and session members.
Review: https://reviews.apache.org/r/11847
Project: http://git-wip-us.apache.org/repos/asf/incubator-mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mesos/commit/5faf7001
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mesos/tree/5faf7001
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mesos/diff/5faf7001
Branch: refs/heads/master
Commit: 5faf7001a7ef0b00dfd4d7611f6c7c0929e12e7a
Parents: f1dd735
Author: Benjamin Mahler <bm...@twitter.com>
Authored: Wed Jun 12 12:05:11 2013 -0700
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Wed Jun 19 20:29:34 2013 -0700
----------------------------------------------------------------------
.../3rdparty/stout/include/stout/os.hpp | 46 ++++++++++++++++++++
.../3rdparty/stout/tests/os_tests.cpp | 27 +++++++++---
2 files changed, 66 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/5faf7001/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index f2c59ee..47e877c 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -47,6 +47,7 @@
#include <stout/foreach.hpp>
#include <stout/none.hpp>
#include <stout/nothing.hpp>
+#include <stout/option.hpp>
#include <stout/path.hpp>
#include <stout/result.hpp>
#include <stout/strings.hpp>
@@ -1063,6 +1064,51 @@ inline Try<std::set<pid_t> > children(pid_t pid, bool recursive = true)
}
+// Overload of os::pids for filtering by groups and sessions.
+// A group / session id of 0 will fitler on the group / session ID
+// of the calling process.
+inline Try<std::set<pid_t> > pids(Option<pid_t> group, Option<pid_t> session)
+{
+ if (group.isNone() && session.isNone()) {
+ return os::pids();
+ } else if (group.isSome() && group.get() < 0) {
+ return Error("Invalid group");
+ } else if (session.isSome() && session.get() < 0) {
+ return Error("Invalid session");
+ }
+
+ const Try<std::list<Process> >& processes = os::processes();
+
+ if (processes.isError()) {
+ return Error(processes.error());
+ }
+
+ // Obtain the calling process group / session ID when 0 is provided.
+ if (group.isSome() && group.get() == 0) {
+ group = getpgid(0);
+ }
+ if (session.isSome() && session.get() == 0) {
+ session = getsid(0);
+ }
+
+ std::set<pid_t> result;
+ foreach (const Process& process, processes.get()) {
+ // Group AND Session (intersection).
+ if (group.isSome() && session.isSome()) {
+ if (group.get() == process.group && session.get() == process.session) {
+ result.insert(process.pid);
+ }
+ } else if (group.isSome() && group.get() == process.group) {
+ result.insert(process.pid);
+ } else if (session.isSome() && session.get() == process.session) {
+ result.insert(process.pid);
+ }
+ }
+
+ return result;
+}
+
+
inline Try<bool> alive(pid_t pid)
{
CHECK(pid > 0);
http://git-wip-us.apache.org/repos/asf/incubator-mesos/blob/5faf7001/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
index 041b4d1..99f69a2 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
@@ -40,7 +40,7 @@ class OsTest : public ::testing::Test
protected:
virtual void SetUp()
{
- Try<string> mkdtemp = os::mkdtemp();
+ const Try<string>& mkdtemp = os::mkdtemp();
ASSERT_SOME(mkdtemp);
tmpdir = mkdtemp.get();
}
@@ -171,7 +171,7 @@ TEST_F(OsTest, find)
TEST_F(OsTest, uname)
{
- Try<os::UTSInfo> info = os::uname();
+ const Try<os::UTSInfo>& info = os::uname();
ASSERT_SOME(info);
#ifdef __linux__
@@ -185,7 +185,7 @@ TEST_F(OsTest, uname)
TEST_F(OsTest, sysname)
{
- Try<string> name = os::sysname();
+ const Try<string>& name = os::sysname();
ASSERT_SOME(name);
#ifdef __linux__
@@ -199,7 +199,7 @@ TEST_F(OsTest, sysname)
TEST_F(OsTest, release)
{
- Try<os::Release> info = os::release();
+ const Try<os::Release>& info = os::release();
ASSERT_SOME(info);
}
@@ -257,11 +257,24 @@ TEST_F(OsTest, sysctl)
TEST_F(OsTest, pids)
{
Try<set<pid_t> > pids = os::pids();
-
ASSERT_SOME(pids);
EXPECT_NE(0u, pids.get().size());
EXPECT_EQ(1u, pids.get().count(getpid()));
EXPECT_EQ(1u, pids.get().count(1));
+
+ pids = os::pids(getpgid(0), None());
+ EXPECT_SOME(pids);
+ EXPECT_GE(pids.get().size(), 1u);
+ EXPECT_EQ(1u, pids.get().count(getpid()));
+
+ EXPECT_ERROR(os::pids(-1, None()));
+
+ pids = os::pids(None(), getsid(0));
+ EXPECT_SOME(pids);
+ EXPECT_GE(pids.get().size(), 1u);
+ EXPECT_EQ(1u, pids.get().count(getpid()));
+
+ EXPECT_ERROR(os::pids(None(), -1));
}
@@ -356,7 +369,7 @@ TEST_F(OsTest, children)
TEST_F(OsTest, process)
{
- Try<os::Process> status = os::process(getpid());
+ const Try<os::Process>& status = os::process(getpid());
ASSERT_SOME(status);
EXPECT_EQ(getpid(), status.get().pid);
@@ -375,7 +388,7 @@ TEST_F(OsTest, process)
TEST_F(OsTest, processes)
{
- Try<list<os::Process> > processes = os::processes();
+ const Try<list<os::Process> >& processes = os::processes();
ASSERT_SOME(processes);
ASSERT_GT(processes.get().size(), 2);