Windows: Abstracted out `os::name_job` in stout.

Almost all uses identify a job object by the PID, but the job object
handle must be obtained by name. So this overloads `os::open_job` to
call `os::name_job` when given a PID.

Review: https://reviews.apache.org/r/63275


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/17564c7f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/17564c7f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/17564c7f

Branch: refs/heads/master
Commit: 17564c7f8d05adede26f49f8842f1a9592055d8d
Parents: b81784d
Author: Andrew Schwartzmeyer <[email protected]>
Authored: Wed Oct 18 22:53:18 2017 -0700
Committer: Andrew Schwartzmeyer <[email protected]>
Committed: Thu Nov 30 15:54:53 2017 -0800

----------------------------------------------------------------------
 3rdparty/stout/include/stout/windows/os.hpp | 45 +++++++++++-------------
 1 file changed, 20 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/17564c7f/3rdparty/stout/include/stout/windows/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/windows/os.hpp 
b/3rdparty/stout/include/stout/windows/os.hpp
index 3713eb6..586c35f 100644
--- a/3rdparty/stout/include/stout/windows/os.hpp
+++ b/3rdparty/stout/include/stout/windows/os.hpp
@@ -659,7 +659,7 @@ inline Try<std::wstring> name_job(pid_t pid) {
 // do not inherit this handle.
 inline Try<SharedHandle> open_job(
     const DWORD desired_access,
-    BOOL inherit_handles,
+    const BOOL inherit_handles,
     const std::wstring& name)
 {
   SharedHandle job_handle(
@@ -679,6 +679,20 @@ inline Try<SharedHandle> open_job(
 }
 
 
+
+inline Try<SharedHandle> open_job(
+  const DWORD desired_access,
+  const BOOL inherit_handles,
+  const pid_t pid)
+{
+  Try<std::wstring> name = os::name_job(pid);
+  if (name.isError()) {
+    return Error(name.error());
+  }
+
+  return open_job(desired_access, inherit_handles, name.get());
+}
+
 // `create_job` function creates a named job object using `name`.
 // This returns the safe job handle, which closes the job handle
 // when destructed. Because the job is destroyed when its last
@@ -732,15 +746,10 @@ inline Try<SharedHandle> create_job(const std::wstring& 
name)
 // 
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684925(v=vs.85).aspx 
// NOLINT(whitespace/line_length)
 inline Try<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION> get_job_info(pid_t pid)
 {
-  Try<std::wstring> name = os::name_job(pid);
-  if (name.isError()) {
-    return Error(name.error());
-  }
-
   Try<SharedHandle> job_handle = os::open_job(
       JOB_OBJECT_QUERY,
       false,
-      name.get());
+      pid);
   if (job_handle.isError()) {
     return Error(job_handle.error());
   }
@@ -805,15 +814,11 @@ Result<std::set<Process>> _get_job_processes(const 
SharedHandle& job_handle) {
 
 inline Try<std::set<Process>> get_job_processes(pid_t pid)
 {
-  Try<std::wstring> name = os::name_job(pid);
-  if (name.isError()) {
-    return Error(name.error());
-  }
-
+  // TODO(andschwa): Overload open_job to use pid.
   Try<SharedHandle> job_handle = os::open_job(
     JOB_OBJECT_QUERY,
     false,
-    name.get());
+    pid);
   if (job_handle.isError()) {
     return Error(job_handle.error());
   }
@@ -895,15 +900,10 @@ inline Try<Nothing> set_job_cpu_limit(pid_t pid, double 
cpus)
     control_info.CpuRate = 1;
   }
 
-  Try<std::wstring> name = os::name_job(pid);
-  if (name.isError()) {
-    return Error(name.error());
-  }
-
   Try<SharedHandle> job_handle = os::open_job(
       JOB_OBJECT_SET_ATTRIBUTES,
       false,
-      name.get());
+      pid);
   if (job_handle.isError()) {
     return Error(job_handle.error());
   }
@@ -933,15 +933,10 @@ inline Try<Nothing> set_job_mem_limit(pid_t pid, Bytes 
limit)
   info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_JOB_MEMORY;
   info.JobMemoryLimit = limit.bytes();
 
-  Try<std::wstring> name = os::name_job(pid);
-  if (name.isError()) {
-    return Error(name.error());
-  }
-
   Try<SharedHandle> job_handle = os::open_job(
       JOB_OBJECT_SET_ATTRIBUTES,
       false,
-      name.get());
+      pid);
   if (job_handle.isError()) {
     return Error(job_handle.error());
   }

Reply via email to