3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
23 #include "src/core/lib/security/credentials/alts/check_gcp_environment.h"
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/sync.h>
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"
34 static bool g_compute_engine_detection_done = false;
35 static bool g_is_on_compute_engine = false;
37 static gpr_once g_once = GPR_ONCE_INIT;
42 bool check_bios_data(const char* bios_data_file) {
43 char* bios_data = read_bios_file(bios_data_file);
45 bios_data && ((!strcmp(bios_data, GRPC_ALTS_EXPECT_NAME_GOOGLE)) ||
46 (!strcmp(bios_data, GRPC_ALTS_EXPECT_NAME_GCE)));
51 } // namespace internal
52 } // namespace grpc_core
54 static void init_mu(void) { gpr_mu_init(&g_mu); }
56 bool grpc_alts_is_running_on_gcp() {
57 gpr_once_init(&g_once, init_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;
65 return g_is_on_compute_engine;