Deshim VirtualExecutor in folly
[hiphop-php.git] / hphp / util / user-info.h
blobf4a02988257dde86628bcd333615ea41d3371303
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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 +----------------------------------------------------------------------+
17 #pragma once
19 #include <grp.h>
20 #include <pwd.h>
21 #include <unistd.h>
22 #include <vector>
24 #include <folly/String.h>
26 #include "hphp/util/exception.h"
28 namespace HPHP {
30 template <typename Entry>
31 struct PwdGrpBuffer {
32 explicit PwdGrpBuffer(int name) : size{sysconf(name)} {
33 if (size < 1) size = 1024;
34 data = std::make_unique<char[]>(size);
37 void resize() {
38 data.reset();
39 size *= 2;
40 data = std::make_unique<char[]>(size);
43 Entry ent;
44 std::unique_ptr<char[]> data;
45 long size;
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());
62 if (pw == nullptr) {
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());
72 if (pw == nullptr) {
73 throw Exception("getpwuid_r: no such uid: %u", uid);
77 PasswdBuffer buf;
78 passwd* pw;
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());
87 if (gr == nullptr) {
88 throw Exception("getgrnam_r: no such group: %s", name);
92 explicit GroupInfo(gid_t gid) {
93 if (getgrgid_r(gid, &buf.ent, buf.data.get(), buf.size, &gr)) {
94 throw Exception("getgrgid_r: %s", folly::errnoStr(errno).c_str());
97 if (gr == nullptr) {
98 throw Exception("getgrgid_r: no such group with gid: %u", gid);
102 GroupBuffer buf;
103 group* gr;
106 } // namespace HPHP