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 +----------------------------------------------------------------------+
17 #include "hphp/util/build-info.h"
19 #include "hphp/util/embedded-data.h"
21 #include <folly/Conv.h>
22 #include <folly/Range.h>
28 #include <sys/types.h>
32 ////////////////////////////////////////////////////////////////////////////////
36 std::atomic
<bool> inited
;
38 std::string repoSchema
;
41 std::string hhjsbabeltransform
;
44 ////////////////////////////////////////////////////////////////////////////////
47 * Initializes the repo schema id and the compiler id from their special
48 * sections in the hhvm binary.
50 void readBuildInfo() {
51 if (inited
.load(std::memory_order_acquire
)) return;
52 std::unique_lock
<std::mutex
> lock(mtx
);
53 if (inited
.load(std::memory_order_acquire
)) return;
55 auto const get
= [&] (const char* section
) -> std::string
{
56 auto constexpr bad
= "(UNKNOWN)";
59 if (!get_embedded_data(section
, &desc
)) {
63 auto const data
= read_embedded_data(desc
);
71 if (auto const env_schema
= getenv("HHVM_RUNTIME_REPO_SCHEMA")) {
72 repoSchema
= env_schema
;
74 repoSchema
= get("repo_schema_id");
77 compiler
= get("compiler_id");
78 buildid
= get("build_id");
79 hhjsbabeltransform
= get("hhjs_babel_transform");
82 timestamp
= std::stoll(get("compiler_ts"));
83 } catch(std::exception
& e
) {
87 inited
.store(true, std::memory_order_release
);
90 template<typename ValueFn
>
91 void replacePlaceholder(std::string
& target
,
92 const char* placeholder
,
94 auto const pos
= target
.find(placeholder
);
95 if (pos
== std::string::npos
) return;
96 target
.replace(pos
, strlen(placeholder
), value());
101 ////////////////////////////////////////////////////////////////////////////////
103 folly::StringPiece
repoSchemaId() {
108 folly::StringPiece
compilerId() {
113 int64_t compilerTimestamp() {
118 folly::StringPiece
buildId() {
123 folly::StringPiece
hhjsBabelTransform() {
125 return hhjsbabeltransform
;
128 void replacePlaceholders(std::string
& s
) {
129 replacePlaceholder(s
, "%{schema}", [] { return repoSchemaId().begin(); });
130 replacePlaceholder(s
, "%{euid}", [] {
131 return folly::to
<std::string
>(geteuid());
133 replacePlaceholder(s
, "%{user}", [] {
134 auto user
= getenv("SUDO_USER");
135 if (user
== nullptr) user
= getenv("USER");
136 return user
!= nullptr ? user
: "%{user}";
138 replacePlaceholder(s
, "%{pid}", [] {
139 return folly::to
<std::string
>(getpid());
143 ////////////////////////////////////////////////////////////////////////////////