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 Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/8b2d65f1 Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/8b2d65f1 Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/8b2d65f1 Branch: refs/heads/HBASE-14850 Commit: 8b2d65f1aa7c7dddcde4be03133f8c35e1fbcba2 Parents: b23e863 Author: Elliott Clark <ecl...@apache.org> Authored: Thu Apr 21 21:17:11 2016 -0700 Committer: Elliott Clark <ecl...@apache.org> Committed: Mon Jul 11 16:47:26 2016 -0700 ---------------------------------------------------------------------- hbase-native-client/utils/BUCK | 40 +++++++++++++++++ hbase-native-client/utils/user-util-test.cc | 34 +++++++++++++++ hbase-native-client/utils/user-util.cc | 55 ++++++++++++++++++++++++ hbase-native-client/utils/user-util.h | 37 ++++++++++++++++ 4 files changed, 166 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/8b2d65f1/hbase-native-client/utils/BUCK ---------------------------------------------------------------------- diff --git a/hbase-native-client/utils/BUCK b/hbase-native-client/utils/BUCK new file mode 100644 index 0000000..2b65b12 --- /dev/null +++ b/hbase-native-client/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", + ],) http://git-wip-us.apache.org/repos/asf/hbase/blob/8b2d65f1/hbase-native-client/utils/user-util-test.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/utils/user-util-test.cc b/hbase-native-client/utils/user-util-test.cc new file mode 100644 index 0000000..2a7434f --- /dev/null +++ b/hbase-native-client/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); +} http://git-wip-us.apache.org/repos/asf/hbase/blob/8b2d65f1/hbase-native-client/utils/user-util.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/utils/user-util.cc b/hbase-native-client/utils/user-util.cc new file mode 100644 index 0000000..c4427e3 --- /dev/null +++ b/hbase-native-client/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; + } +} http://git-wip-us.apache.org/repos/asf/hbase/blob/8b2d65f1/hbase-native-client/utils/user-util.h ---------------------------------------------------------------------- diff --git a/hbase-native-client/utils/user-util.h b/hbase-native-client/utils/user-util.h new file mode 100644 index 0000000..4f74948 --- /dev/null +++ b/hbase-native-client/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