Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / security / credentials / alts / check_gcp_environment_linux.cc
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #ifdef GPR_LINUX
22
23 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/sync.h>
27
28 #include <string.h>
29
30 #define GRPC_ALTS_EXPECT_NAME_GOOGLE "Google"
31 #define GRPC_ALTS_EXPECT_NAME_GCE "Google Compute Engine"
32 #define GRPC_ALTS_PRODUCT_NAME_FILE "/sys/class/dmi/id/product_name"
33
34 static bool g_compute_engine_detection_done = false;
35 static bool g_is_on_compute_engine = false;
36 static gpr_mu g_mu;
37 static gpr_once g_once = GPR_ONCE_INIT;
38
39 namespace grpc_core {
40 namespace internal {
41
42 bool check_bios_data(const char* bios_data_file) {
43   char* bios_data = read_bios_file(bios_data_file);
44   bool result =
45       bios_data && ((!strcmp(bios_data, GRPC_ALTS_EXPECT_NAME_GOOGLE)) ||
46                     (!strcmp(bios_data, GRPC_ALTS_EXPECT_NAME_GCE)));
47   gpr_free(bios_data);
48   return result;
49 }
50
51 }  // namespace internal
52 }  // namespace grpc_core
53
54 static void init_mu(void) { gpr_mu_init(&g_mu); }
55
56 bool grpc_alts_is_running_on_gcp() {
57   gpr_once_init(&g_once, init_mu);
58   gpr_mu_lock(&g_mu);
59   if (!g_compute_engine_detection_done) {
60     g_is_on_compute_engine =
61         grpc_core::internal::check_bios_data(GRPC_ALTS_PRODUCT_NAME_FILE);
62     g_compute_engine_detection_done = true;
63   }
64   gpr_mu_unlock(&g_mu);
65   return g_is_on_compute_engine;
66 }
67
68 #endif  // GPR_LINUX