2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
24 #include <folly/String.h>
26 #include "hphp/util/exception.h"
30 template <typename Entry
>
32 explicit PwdGrpBuffer(int name
) : size
{sysconf(name
)} {
33 if (size
< 1) size
= 1024;
34 data
= std::make_unique
<char[]>(size
);
40 data
= std::make_unique
<char[]>(size
);
44 std::unique_ptr
<char[]> data
;
48 struct PasswdBuffer
: PwdGrpBuffer
<passwd
> {
49 explicit PasswdBuffer() : PwdGrpBuffer(_SC_GETPW_R_SIZE_MAX
) {}
52 struct GroupBuffer
: PwdGrpBuffer
<group
> {
53 explicit GroupBuffer() : PwdGrpBuffer(_SC_GETGR_R_SIZE_MAX
) {}
56 struct UserInfo final
{
57 explicit UserInfo(const char* name
) {
58 if (getpwnam_r(name
, &buf
.ent
, buf
.data
.get(), buf
.size
, &pw
)) {
59 throw Exception("getpwnam_r: %s", folly::errnoStr(errno
).c_str());
63 throw Exception("getpwnam_r: no such user: %s", name
);
67 explicit UserInfo(uid_t uid
) {
68 if (getpwuid_r(uid
, &buf
.ent
, buf
.data
.get(), buf
.size
, &pw
)) {
69 throw Exception("getpwuid_r: %s", folly::errnoStr(errno
).c_str());
73 throw Exception("getpwuid_r: no such uid: %u", uid
);
81 struct GroupInfo final
{
82 explicit GroupInfo(const char* name
) {
83 if (getgrnam_r(name
, &buf
.ent
, buf
.data
.get(), buf
.size
, &gr
)) {
84 throw Exception("getgrnam_r: %s", folly::errnoStr(errno
).c_str());
88 throw Exception("getgrnam_r: no such group: %s", name
);