init.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 
22 
23 #include <limits.h>
24 #include <stdint.h>
25 
26 #include "absl/base/thread_annotations.h"
27 
28 #include <grpc/fork.h>
29 #include <grpc/grpc.h>
30 #include <grpc/grpc_security.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/sync.h>
34 #include <grpc/support/time.h>
35 
44 #include "src/core/lib/gprpp/thd.h"
58 
59 /* (generated) built in registry of plugins */
60 extern void grpc_register_built_in_plugins(void);
61 
62 #define MAX_PLUGINS 128
63 
66 static int g_initializations ABSL_GUARDED_BY(g_init_mu) = []() {
69  return 0;
70 }();
72 static bool g_shutting_down ABSL_GUARDED_BY(g_init_mu) = false;
73 
76  if (builder->channel_args().Contains(GRPC_ARG_SECURITY_CONNECTOR)) {
78  }
79  return true;
80 }
81 
84  if (builder->channel_args().Contains(GRPC_SERVER_CREDENTIALS_ARG)) {
85  builder->PrependFilter(&grpc_server_auth_filter);
86  }
87  return true;
88 }
89 
92  if (builder->channel_args().GetPointer<grpc_authorization_policy_provider>(
95  }
96  return true;
97 }
98 
99 namespace grpc_core {
101  // Register the auth client with a priority < INT_MAX to allow the authority
102  // filter -on which the auth filter depends- to be higher on the channel
103  // stack.
104  builder->channel_init()->RegisterStage(GRPC_CLIENT_SUBCHANNEL, INT_MAX - 1,
106  builder->channel_init()->RegisterStage(GRPC_CLIENT_DIRECT_CHANNEL,
107  INT_MAX - 1,
109  builder->channel_init()->RegisterStage(GRPC_SERVER_CHANNEL, INT_MAX - 1,
111  // Register the GrpcServerAuthzFilter with a priority less than
112  // server_auth_filter to allow server_auth_filter on which the grpc filter
113  // depends on to be higher on the channel stack.
114  builder->channel_init()->RegisterStage(
116 }
117 } // namespace grpc_core
118 
119 static void do_basic_init(void) {
121  g_init_mu = new grpc_core::Mutex();
126  gpr_time_init();
127 }
128 
129 typedef struct grpc_plugin {
130  void (*init)();
131  void (*destroy)();
132 } grpc_plugin;
133 
135 static int g_number_of_plugins = 0;
136 
137 void grpc_register_plugin(void (*init)(void), void (*destroy)(void)) {
138  GRPC_API_TRACE("grpc_register_plugin(init=%p, destroy=%p)", 2,
139  ((void*)(intptr_t)init, (void*)(intptr_t)destroy));
144 }
145 
146 void grpc_init(void) {
148 
150  if (++g_initializations == 1) {
151  if (g_shutting_down) {
152  g_shutting_down = false;
154  }
158  grpc_stats_init();
160  grpc_iomgr_init();
162  for (int i = 0; i < g_number_of_plugins; i++) {
163  if (g_all_of_the_plugins[i].init != nullptr) {
165  }
166  }
169  }
170 
171  GRPC_API_TRACE("grpc_init(void)", 0, ());
172 }
173 
176  int i;
177  {
180  {
181  grpc_timer_manager_set_threading(false); // shutdown timer_manager thread
182  for (i = g_number_of_plugins; i >= 0; i--) {
183  if (g_all_of_the_plugins[i].destroy != nullptr) {
185  }
186  }
187  }
192  }
194  g_shutting_down = false;
196 }
197 
198 void grpc_shutdown_internal(void* /*ignored*/) {
199  GRPC_API_TRACE("grpc_shutdown_internal", 0, ());
201  // We have released lock from the shutdown thread and it is possible that
202  // another grpc_init has been called, and do nothing if that is the case.
203  if (--g_initializations != 0) {
204  return;
205  }
207 }
208 
209 void grpc_shutdown(void) {
210  GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
212 
213  if (--g_initializations == 0) {
217  (acec == nullptr ||
219  0)) {
220  // just run clean-up when this is called on non-executor thread.
221  gpr_log(GPR_DEBUG, "grpc_shutdown starts clean-up now");
222  g_shutting_down = true;
224  } else {
225  // spawn a detached thread to do the actual clean up in case we are
226  // currently in an executor thread.
227  gpr_log(GPR_DEBUG, "grpc_shutdown spawns clean-up thread");
228  g_initializations++;
229  g_shutting_down = true;
230  grpc_core::Thread cleanup_thread(
231  "grpc_shutdown", grpc_shutdown_internal, nullptr, nullptr,
232  grpc_core::Thread::Options().set_joinable(false).set_tracked(false));
233  cleanup_thread.Start();
234  }
235  }
236 }
237 
239  GRPC_API_TRACE("grpc_shutdown_blocking(void)", 0, ());
241  if (--g_initializations == 0) {
242  g_shutting_down = true;
244  }
245 }
246 
248  int r;
251  r = g_initializations > 0;
252  return r;
253 }
254 
258  while (g_shutting_down) {
260  }
261 }
grpc_event_engine::experimental::RegisterForkHandlers
void RegisterForkHandlers()
Definition: forkable.cc:90
trace.h
auth_filters.h
fork.h
iomgr.h
grpc_is_initialized
int grpc_is_initialized(void)
Definition: init.cc:247
grpc_core::CondVar
Definition: src/core/lib/gprpp/sync.h:126
init
const char * init
Definition: upb/upb/bindings/lua/main.c:49
log.h
core_configuration.h
gpr_log_verbosity_init
GPRAPI void gpr_log_verbosity_init(void)
Definition: log.cc:115
timers.h
gpr_once
pthread_once_t gpr_once
Definition: impl/codegen/sync_posix.h:50
grpc_core::ApplicationCallbackExecCtx::Get
static ApplicationCallbackExecCtx * Get()
Definition: exec_ctx.h:315
grpc_iomgr_is_any_background_poller_thread
bool grpc_iomgr_is_any_background_poller_thread()
Definition: iomgr.cc:175
g_init_mu
static grpc_core::Mutex * g_init_mu
Definition: init.cc:65
grpc_timer_manager_set_threading
void grpc_timer_manager_set_threading(bool enabled)
Definition: iomgr/timer_manager.cc:346
gpr_timers_global_destroy
void gpr_timers_global_destroy(void)
Definition: basic_timers.cc:290
grpc_core::CoreConfiguration::SetDefaultBuilder
static void SetDefaultBuilder(void(*builder)(CoreConfiguration::Builder *))
Definition: core_configuration.h:161
timer_manager.h
grpc_core
Definition: call_metric_recorder.h:31
ABSL_GUARDED_BY
static int g_initializations ABSL_GUARDED_BY(g_init_mu)
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
GRPC_CLIENT_DIRECT_CHANNEL
@ GRPC_CLIENT_DIRECT_CHANNEL
Definition: channel_stack_type.h:34
grpc_core::GrpcServerAuthzFilter::kFilterVtable
static const grpc_channel_filter kFilterVtable
Definition: grpc_server_authz_filter.h:39
grpc_iomgr_shutdown_background_closure
void grpc_iomgr_shutdown_background_closure()
Definition: iomgr.cc:171
completion_queue.h
grpc_authorization_policy_provider
Definition: src/core/lib/security/authorization/authorization_policy_provider.h:30
grpc_core::ApplicationCallbackExecCtx
Definition: exec_ctx.h:283
GPR_ONCE_INIT
#define GPR_ONCE_INIT
Definition: impl/codegen/sync_posix.h:52
time.h
GRPC_CLIENT_SUBCHANNEL
@ GRPC_CLIENT_SUBCHANNEL
Definition: channel_stack_type.h:29
grpc_security.h
grpc_shutdown_internal
void grpc_shutdown_internal(void *)
Definition: init.cc:198
credentials.h
grpc_tracer_init
void grpc_tracer_init(const char *env_var_name)
Definition: debug/trace.cc:141
grpc_iomgr_start
void grpc_iomgr_start()
Definition: iomgr.cc:77
stats.h
grpc_core::ChannelStackBuilder
Definition: channel_stack_builder.h:41
gpr_once_init
GPRAPI void gpr_once_init(gpr_once *once, void(*init_function)(void))
grpc_types.h
GRPC_SERVER_CHANNEL
@ GRPC_SERVER_CHANNEL
Definition: channel_stack_type.h:36
g_shutting_down_cv
static grpc_core::CondVar * g_shutting_down_cv
Definition: init.cc:71
MAX_PLUGINS
#define MAX_PLUGINS
Definition: init.cc:62
grpc_core::CondVar::SignalAll
void SignalAll()
Definition: src/core/lib/gprpp/sync.h:135
grpc_shutdown
void grpc_shutdown(void)
Definition: init.cc:209
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel_init.h
g_basic_init
static gpr_once g_basic_init
Definition: init.cc:64
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_register_built_in_plugins
void grpc_register_built_in_plugins(void)
Definition: grpc_cronet_plugin_registry.cc:32
grpc_core::ApplicationCallbackExecCtx::Flags
uintptr_t Flags()
Definition: exec_ctx.h:313
grpc_stats_init
void grpc_stats_init(void)
Definition: stats.cc:40
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_cq_global_init
void grpc_cq_global_init()
Definition: completion_queue.cc:450
grpc_init
void grpc_init(void)
Definition: init.cc:146
ABSL_EXCLUSIVE_LOCKS_REQUIRED
#define ABSL_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: abseil-cpp/absl/base/thread_annotations.h:145
init.h
grpc_shutdown_internal_locked
void grpc_shutdown_internal_locked(void) ABSL_EXCLUSIVE_LOCKS_REQUIRED(g_init_mu)
Definition: init.cc:174
grpc.h
grpc_server_auth_filter
const grpc_channel_filter grpc_server_auth_filter
Definition: server_auth_filter.cc:347
security_connector.h
grpc_core::RegisterSecurityFilters
void RegisterSecurityFilters(CoreConfiguration::Builder *builder)
Definition: init.cc:100
grpc_core::ApplicationCallbackExecCtx::GlobalInit
static void GlobalInit(void)
Definition: exec_ctx.h:342
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_iomgr_shutdown
void grpc_iomgr_shutdown()
Definition: iomgr.cc:97
grpc_plugin
Definition: init.cc:129
g_number_of_plugins
static int g_number_of_plugins
Definition: init.cc:135
grpc_core::Thread::Options
Definition: thd.h:45
grpc_register_plugin
void grpc_register_plugin(void(*init)(void), void(*destroy)(void))
Definition: init.cc:137
channel_stack_type.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
maybe_prepend_grpc_server_authz_filter
static bool maybe_prepend_grpc_server_authz_filter(grpc_core::ChannelStackBuilder *builder)
Definition: init.cc:90
stdint.h
executor.h
GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER
#define GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER
Definition: grpc_types.h:450
grpc_iomgr_init
void grpc_iomgr_init()
Definition: iomgr.cc:61
GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
#define GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
Definition: exec_ctx.h:55
grpc_core::ClientAuthFilter::kFilter
static const grpc_channel_filter kFilter
Definition: auth_filters.h:46
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_server_authz_filter.h
maybe_prepend_server_auth_filter
static bool maybe_prepend_server_auth_filter(grpc_core::ChannelStackBuilder *builder)
Definition: init.cc:82
grpc_plugin::init
void(* init)()
Definition: init.cc:130
maybe_prepend_client_auth_filter
static bool maybe_prepend_client_auth_filter(grpc_core::ChannelStackBuilder *builder)
Definition: init.cc:74
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_shutdown_blocking
void grpc_shutdown_blocking(void)
Definition: init.cc:238
grpc_core::Fork::GlobalShutdown
static void GlobalShutdown()
Definition: fork.cc:177
grpc_core::BuildCoreConfiguration
void BuildCoreConfiguration(CoreConfiguration::Builder *builder)
Definition: grpc_plugin_registry.cc:109
google::protobuf.internal::Mutex
WrappedMutex Mutex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/mutex.h:113
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc_core::CondVar::Wait
void Wait(Mutex *mu)
Definition: src/core/lib/gprpp/sync.h:137
thd.h
grpc_core::ApplicationCallbackExecCtx::GlobalShutdown
static void GlobalShutdown(void)
Definition: exec_ctx.h:345
exec_ctx.h
grpc_plugin::destroy
void(* destroy)()
Definition: init.cc:131
grpc_core::Thread
Definition: thd.h:43
GRPC_ARG_SECURITY_CONNECTOR
#define GRPC_ARG_SECURITY_CONNECTOR
Definition: security_connector.h:59
do_basic_init
static void do_basic_init(void)
Definition: init.cc:119
grpc_maybe_wait_for_async_shutdown
void grpc_maybe_wait_for_async_shutdown(void)
Definition: init.cc:255
grpc_fork_handlers_auto_register
void grpc_fork_handlers_auto_register(void)
Definition: fork_windows.cc:39
fork.h
channel_args.h
api_trace.h
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
GRPC_SERVER_CREDENTIALS_ARG
#define GRPC_SERVER_CREDENTIALS_ARG
Definition: src/core/lib/security/credentials/credentials.h:287
grpc_tracer_shutdown
void grpc_tracer_shutdown(void)
Definition: debug/trace.cc:151
channel_stack_builder.h
sync.h
gpr_timers_global_init
void gpr_timers_global_init(void)
Definition: basic_timers.cc:288
g_all_of_the_plugins
static grpc_plugin g_all_of_the_plugins[MAX_PLUGINS]
Definition: init.cc:134
gpr_time_init
GPRAPI void gpr_time_init(void)
grpc_plugin
struct grpc_plugin grpc_plugin
grpc_core::Fork::GlobalInit
static void GlobalInit()
Definition: fork.cc:166
sync.h
grpc_core::grpc_executor_global_init
void grpc_executor_global_init()
Definition: executor.cc:459
destroy
static std::function< void(void *, Slot *)> destroy
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:42
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_API_TRACE
#define GRPC_API_TRACE(fmt, nargs, args)
Definition: api_trace.h:48
forkable.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:15