/Users/deen/code/yugabyte-db/src/yb/util/user.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/user.h" |
34 | | |
35 | | #include <pwd.h> |
36 | | |
37 | | #include <boost/algorithm/string/trim.hpp> |
38 | | |
39 | | #include "yb/util/errno.h" |
40 | | #include "yb/util/malloc.h" |
41 | | #include "yb/util/result.h" |
42 | | #include "yb/util/subprocess.h" |
43 | | |
44 | | using std::string; |
45 | | |
46 | | namespace yb { |
47 | | |
48 | | namespace { |
49 | | |
50 | 0 | Result<std::string> DoGetLoggedInUser() { |
51 | 0 | std::string result; |
52 | 0 | RETURN_NOT_OK(Subprocess::Call({"whoami"}, &result)); |
53 | 0 | boost::trim(result); |
54 | 0 | return result; |
55 | 0 | } |
56 | | |
57 | 0 | Result<std::string> GetLoggedInUserFallback() { |
58 | 0 | static auto result = DoGetLoggedInUser(); |
59 | 0 | return result; |
60 | 0 | } |
61 | | |
62 | | } // namespace |
63 | | |
64 | 11 | Result<std::string> GetLoggedInUser() { |
65 | 11 | auto bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); |
66 | 11 | if (bufsize == -1) { // Value was indeterminate. |
67 | 0 | bufsize = 16384; // Should be more than enough, per the man page. |
68 | 0 | } |
69 | | |
70 | 11 | std::unique_ptr<char[], FreeDeleter> buf(static_cast<char*>(malloc(bufsize))); |
71 | 11 | if (buf.get() == nullptr) { |
72 | 0 | return STATUS(RuntimeError, "Malloc failed", Errno(errno)); |
73 | 0 | } |
74 | | |
75 | 11 | struct passwd pwd; |
76 | 11 | struct passwd *result; |
77 | | |
78 | 11 | auto uid = getuid(); |
79 | 11 | int ret = getpwuid_r(uid, &pwd, buf.get(), bufsize, &result); |
80 | 11 | if (result == nullptr) { |
81 | 0 | if (ret == 0) { |
82 | 0 | return GetLoggedInUserFallback(); |
83 | 0 | } else { |
84 | | // Errno in ret |
85 | 0 | return STATUS(RuntimeError, "Error calling getpwuid_r()", Errno(ret)); |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | 11 | return pwd.pw_name; |
90 | 11 | } |
91 | | |
92 | | } // namespace yb |