You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by jo...@apache.org on 2016/12/16 02:27:27 UTC

[3/4] mesos git commit: Windows: Implemented `os::user()'.

Windows: Implemented `os::user()'.

This adds a basic implementation for `os::user` that returns a string.
For now, this string should only be used to populate the `user` field
in protobufs, such as the `FrameworkInfo` protobuf.  No other usage
of `os::user` is supported.

This replaces review: https://reviews.apache.org/r/53706/

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


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

Branch: refs/heads/master
Commit: a22c733ae0b3c4e457c64b7eaf8770dee4461f42
Parents: a0f5caa
Author: Daniel Pravat <dp...@outlook.com>
Authored: Thu Dec 15 17:05:50 2016 -0800
Committer: Joseph Wu <jo...@apache.org>
Committed: Thu Dec 15 17:15:09 2016 -0800

----------------------------------------------------------------------
 3rdparty/stout/cmake/StoutConfigure.cmake      |  1 +
 3rdparty/stout/include/stout/os/windows/su.hpp | 42 ++++++++++++++++++++-
 3rdparty/stout/tests/os_tests.cpp              | 13 +++++++
 3 files changed, 55 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a22c733a/3rdparty/stout/cmake/StoutConfigure.cmake
----------------------------------------------------------------------
diff --git a/3rdparty/stout/cmake/StoutConfigure.cmake b/3rdparty/stout/cmake/StoutConfigure.cmake
index 7e483aa..d8da0f0 100644
--- a/3rdparty/stout/cmake/StoutConfigure.cmake
+++ b/3rdparty/stout/cmake/StoutConfigure.cmake
@@ -111,6 +111,7 @@ if (WIN32)
     ${ZLIB_LFLAG}
     ws2_32
     Mswsock
+    Secur32
     )
 else (WIN32)
   set(STOUT_LIBS

http://git-wip-us.apache.org/repos/asf/mesos/blob/a22c733a/3rdparty/stout/include/stout/os/windows/su.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/windows/su.hpp b/3rdparty/stout/include/stout/os/windows/su.hpp
index 1bb7096..573fdef 100644
--- a/3rdparty/stout/include/stout/os/windows/su.hpp
+++ b/3rdparty/stout/include/stout/os/windows/su.hpp
@@ -18,6 +18,7 @@
 #define __STOUT_OS_WINDOWS_SU_HPP__
 
 #include <string>
+#include <vector>
 
 #include <stout/error.hpp>
 #include <stout/nothing.hpp>
@@ -26,6 +27,22 @@
 
 #include <stout/windows.hpp>
 
+// Include for `GetUserNameEx`. `SECURITY_WIN32` or `SECURITY_KERNEL` must be
+// defined to include `SecExt.h`, which defines `GetUserNameEx` (the choice
+// depends on whether you want the functions defined to be usermode or kernel
+// operations). We include `security.h` instead of `SecExt.h` because comments
+// in this header indicate that it should only be included from `security.h`.
+// Finally, we `#undef` to avoid accidentally interfering with Windows headers
+// that might be sensitive to `SECURITY_WIN32`.
+#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL)
+#include <security.h>
+#else
+#define SECURITY_WIN32
+#include <security.h>
+#undef SECURITY_WIN32
+#endif // SECURITY_WIN32 || SECURITY_KERNEL
+
+
 namespace os {
 
 // NOTE: We delete these functions because they are not meaningful on Windows.
@@ -50,9 +67,32 @@ inline Result<uid_t> getuid(const Option<std::string>& user = None()) = delete;
 inline Result<gid_t> getgid(const Option<std::string>& user = None()) = delete;
 
 
+// Returns the SAM account name for the current user. This username is
+// unprocessed, meaning it contains punctuation, possibly including '\'.
+// NOTE: The `uid` parameter is unsupported on Windows, and will result in an
+// error.
 inline Result<std::string> user(Option<uid_t> uid = None())
 {
-  return WindowsError(ERROR_NOT_SUPPORTED);
+  if (uid.isSome()) {
+    return Error(
+        "os::user: Retrieving user information via uid "
+        "is not supported on Windows");
+  }
+
+  EXTENDED_NAME_FORMAT username_format = NameSamCompatible;
+  ULONG buffer_size = 0;
+  if (::GetUserNameEx(username_format, nullptr, &buffer_size) == FALSE) {
+    if (::GetLastError() != ERROR_MORE_DATA) {
+      return WindowsError("os::user: Failed to get buffer size for username");
+    }
+  }
+
+  std::vector<TCHAR> user_name(buffer_size);
+  if (::GetUserNameEx(username_format, &user_name[0], &buffer_size) == FALSE) {
+    return WindowsError("os::user: Failed to get username from OS");
+  }
+
+  return std::string(&user_name[0]);
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/a22c733a/3rdparty/stout/tests/os_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/os_tests.cpp b/3rdparty/stout/tests/os_tests.cpp
index bed1449..8d2005b 100644
--- a/3rdparty/stout/tests/os_tests.cpp
+++ b/3rdparty/stout/tests/os_tests.cpp
@@ -719,6 +719,19 @@ TEST_F(OsTest, User)
 #endif // __WINDOWS__
 
 
+TEST_F(OsTest, TrivialUser)
+{
+  const Result<string> user1 = os::user();
+  ASSERT_SOME(user1);
+  ASSERT_NE("", user1.get());
+
+#ifdef __WINDOWS__
+  const Result<string> user2 = os::user(INT_MAX);
+  ASSERT_ERROR(user2);
+#endif // __WINDOWS__
+}
+
+
 // TODO(hausdorff): Look into enabling this on Windows. Right now,
 // `LD_LIBRARY_PATH` doesn't exist on Windows, so `setPaths` doesn't work. See
 // MESOS-5940.