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 check_bios_data(const char * bios_data_file)42bool 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 init_mu(void)54static void init_mu(void) { gpr_mu_init(&g_mu); } 55 grpc_alts_is_running_on_gcp()56bool 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 69