check_gcp_environment_linux.cc
Go to the documentation of this file.
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 
20 
21 #ifdef GPR_LINUX
22 
23 #include <string.h>
24 
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/sync.h>
27 
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;
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 
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  }
65  return g_is_on_compute_engine;
66 }
67 
68 #endif // GPR_LINUX
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
grpc_core::internal::check_bios_data
bool check_bios_data(const char *bios_data)
grpc_core
Definition: call_metric_recorder.h:31
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
g_once
static gpr_once g_once
Definition: backup_poller.cc:57
grpc_alts_is_running_on_gcp
bool grpc_alts_is_running_on_gcp()
Definition: check_gcp_environment_no_op.cc:27
check_gcp_environment.h
GPR_ONCE_INIT
#define GPR_ONCE_INIT
Definition: impl/codegen/sync_posix.h:52
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
g_mu
static gpr_mu g_mu
Definition: iomgr.cc:55
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
grpc_core::internal::read_bios_file
char * read_bios_file(const char *bios_file)
Definition: check_gcp_environment.cc:57
alloc.h
internal
Definition: benchmark/test/output_test_helper.cc:20
sync.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:44