From ad86d62cd6098c0ff89bc0a64bda5391adf4fef8 Mon Sep 17 00:00:00 2001 From: Elliott Clark Date: Thu, 21 Apr 2016 21:17:11 -0700 Subject: [PATCH] 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 --- hbase-native-client/core/BUCK | 1 - 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 +++++++++++++++++++ 5 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 hbase-native-client/utils/BUCK create mode 100644 hbase-native-client/utils/user-util-test.cc create mode 100644 hbase-native-client/utils/user-util.cc create mode 100644 hbase-native-client/utils/user-util.h diff --git a/hbase-native-client/core/BUCK b/hbase-native-client/core/BUCK index 195fc5c..3a96aaa 100644 --- a/hbase-native-client/core/BUCK +++ b/hbase-native-client/core/BUCK @@ -44,7 +44,6 @@ cxx_library(name="core", visibility=[ 'PUBLIC', ], ) - cxx_test(name="location-cache-test", srcs=[ "test-env.cc", diff --git a/hbase-native-client/utils/BUCK b/hbase-native-client/utils/BUCK new file mode 100644 index 0000000..74c5c0c --- /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", + ],) 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 +#include +#include + +#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/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 +#include +#include +#include + +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 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/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 +#include +#include + +namespace hbase { +class UserUtil { +public: + std::string user_name(); + +private: + void compute_user_name(); + std::atomic init_{false}; + std::string user_name_; + std::mutex m_; +}; +} // namespace hbase -- 2.8.0-rc2