• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/support/alloc.h>
24 #include <grpc/support/sync.h>
25 #include <string.h>
26 
27 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
28 
29 #define GRPC_ALTS_EXPECT_NAME_GOOGLE "Google"
30 #define GRPC_ALTS_EXPECT_NAME_GCE "Google Compute Engine"
31 #define GRPC_ALTS_PRODUCT_NAME_FILE "/sys/class/dmi/id/product_name"
32 
33 static bool g_compute_engine_detection_done = false;
34 static bool g_is_on_compute_engine = false;
35 static gpr_mu g_mu;
36 static gpr_once g_once = GPR_ONCE_INIT;
37 
38 namespace grpc_core {
39 namespace internal {
40 
check_bios_data(const char * bios_data_file)41 bool check_bios_data(const char* bios_data_file) {
42   char* bios_data = read_bios_file(bios_data_file);
43   bool result =
44       bios_data && ((!strcmp(bios_data, GRPC_ALTS_EXPECT_NAME_GOOGLE)) ||
45                     (!strcmp(bios_data, GRPC_ALTS_EXPECT_NAME_GCE)));
46   gpr_free(bios_data);
47   return result;
48 }
49 
50 }  // namespace internal
51 }  // namespace grpc_core
52 
init_mu(void)53 static void init_mu(void) { gpr_mu_init(&g_mu); }
54 
grpc_alts_is_running_on_gcp()55 bool grpc_alts_is_running_on_gcp() {
56   gpr_once_init(&g_once, init_mu);
57   gpr_mu_lock(&g_mu);
58   if (!g_compute_engine_detection_done) {
59     g_is_on_compute_engine =
60         grpc_core::internal::check_bios_data(GRPC_ALTS_PRODUCT_NAME_FILE);
61     g_compute_engine_detection_done = true;
62   }
63   gpr_mu_unlock(&g_mu);
64   return g_is_on_compute_engine;
65 }
66 
67 #endif  // GPR_LINUX
68