You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by zg...@apache.org on 2019/04/10 13:47:46 UTC

[hbase] 14/137: HBASE-15690 Add utility to get current username

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

zghao pushed a commit to branch hbase-native-client
in repository https://gitbox.apache.org/repos/asf/hbase.git

commit a7b33dd70bff6738b748d3e64e83768e4f9db1e9
Author: Elliott Clark <ec...@apache.org>
AuthorDate: Thu Apr 21 21:17:11 2016 -0700

    HBASE-15690 Add utility to get current username
    
    Summary:
    Add a class to fine the username of the current process.
    It will only call out once and is multithread safe
    
    Test Plan: Unit Test
    
    Differential Revision: https://reviews.facebook.net/D57081
---
 utils/BUCK              | 40 +++++++++++++++++++++++++++++++++++
 utils/user-util-test.cc | 34 ++++++++++++++++++++++++++++++
 utils/user-util.cc      | 55 +++++++++++++++++++++++++++++++++++++++++++++++++
 utils/user-util.h       | 37 +++++++++++++++++++++++++++++++++
 4 files changed, 166 insertions(+)

diff --git a/utils/BUCK b/utils/BUCK
new file mode 100644
index 0000000..2b65b12
--- /dev/null
+++ b/utils/BUCK
@@ -0,0 +1,40 @@
+##
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+cxx_library(name="utils",
+            exported_headers=[
+                "user-util.h",
+            ],
+            srcs=[
+                "user-util.cc",
+            ],
+            deps=[
+              '//third-party:folly',
+            ],
+            visibility=[
+                'PUBLIC',
+            ],
+            tests=[
+              ":user-util-test"
+            ],)
+cxx_test(name="user-util-test",
+         srcs=[
+             "user-util-test.cc",
+         ],
+         deps=[
+             ":utils",
+         ],)
diff --git a/utils/user-util-test.cc b/utils/user-util-test.cc
new file mode 100644
index 0000000..2a7434f
--- /dev/null
+++ b/utils/user-util-test.cc
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ *
+ */
+
+#include <gtest/gtest.h>
+#include <string>
+#include <folly/Logging.h>
+
+#include "utils/user-util.h"
+
+using namespace std;
+using namespace hbase;
+
+TEST(TestUserUtil, TestGetSomething) {
+  UserUtil u_util;
+  string name = u_util.user_name();
+
+  ASSERT_GT(name.length(), 0);
+}
diff --git a/utils/user-util.cc b/utils/user-util.cc
new file mode 100644
index 0000000..c4427e3
--- /dev/null
+++ b/utils/user-util.cc
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ *
+ */
+
+#include "utils/user-util.h"
+
+#include <pwd.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <folly/Logging.h>
+
+using namespace hbase;
+using namespace std;
+
+string UserUtil::user_name() {
+  if (!init_) {
+    compute_user_name();
+  }
+  return user_name_;
+}
+
+void UserUtil::compute_user_name() {
+  lock_guard<mutex> lock(m_);
+
+  if (init_) {
+    return;
+  }
+
+  // According to the man page of getpwuid
+  // this should never be free'd
+  //
+  // So yeah a raw pointer with no ownership....
+  struct passwd *passwd = getpwuid(getuid());
+
+  // make sure that we got something.
+  if (passwd && passwd->pw_name) {
+    user_name_ = string{passwd->pw_name};
+    init_ = true;
+  }
+}
diff --git a/utils/user-util.h b/utils/user-util.h
new file mode 100644
index 0000000..4f74948
--- /dev/null
+++ b/utils/user-util.h
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ *
+ */
+
+#pragma once
+
+#include <memory>
+#include <atomic>
+#include <mutex>
+
+namespace hbase {
+class UserUtil {
+public:
+  std::string user_name();
+
+private:
+  void compute_user_name();
+  std::atomic<bool> init_{false};
+  std::string user_name_;
+  std::mutex m_;
+};
+} // namespace hbase