• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/surface/init.h"
22 
23 #include "absl/base/thread_annotations.h"
24 
25 #include <grpc/fork.h>
26 #include <grpc/grpc.h>
27 #include <grpc/impl/channel_arg_names.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/sync.h>
30 #include <grpc/support/time.h>
31 
32 #include "src/core/client_channel/backup_poller.h"
33 #include "src/core/lib/config/core_configuration.h"
34 #include "src/core/lib/debug/trace.h"
35 #include "src/core/lib/event_engine/posix_engine/timer_manager.h"
36 #include "src/core/lib/experiments/config.h"
37 #include "src/core/lib/gprpp/fork.h"
38 #include "src/core/lib/gprpp/sync.h"
39 #include "src/core/lib/gprpp/thd.h"
40 #include "src/core/lib/iomgr/exec_ctx.h"
41 #include "src/core/lib/iomgr/iomgr.h"
42 #include "src/core/lib/iomgr/timer_manager.h"
43 #include "src/core/lib/security/authorization/grpc_server_authz_filter.h"
44 #include "src/core/lib/security/credentials/credentials.h"
45 #include "src/core/lib/security/security_connector/security_connector.h"
46 #include "src/core/lib/security/transport/auth_filters.h"
47 #include "src/core/lib/surface/api_trace.h"
48 #include "src/core/lib/surface/channel_stack_type.h"
49 #include "src/core/lib/surface/init_internally.h"
50 
51 // Remnants of the old plugin system
52 void grpc_resolver_dns_ares_init(void);
53 void grpc_resolver_dns_ares_shutdown(void);
54 
55 #define MAX_PLUGINS 128
56 
57 static gpr_once g_basic_init = GPR_ONCE_INIT;
58 static grpc_core::Mutex* g_init_mu;
__anonc88085ee0102() 59 static int g_initializations ABSL_GUARDED_BY(g_init_mu) = []() {
60   grpc_core::CoreConfiguration::SetDefaultBuilder(
61       grpc_core::BuildCoreConfiguration);
62   return 0;
63 }();
64 static grpc_core::CondVar* g_shutting_down_cv;
65 static bool g_shutting_down ABSL_GUARDED_BY(g_init_mu) = false;
66 
67 namespace grpc_core {
RegisterSecurityFilters(CoreConfiguration::Builder * builder)68 void RegisterSecurityFilters(CoreConfiguration::Builder* builder) {
69   builder->channel_init()
70       ->RegisterFilter<ClientAuthFilter>(GRPC_CLIENT_SUBCHANNEL)
71       .IfHasChannelArg(GRPC_ARG_SECURITY_CONNECTOR);
72   builder->channel_init()
73       ->RegisterFilter<ClientAuthFilter>(GRPC_CLIENT_DIRECT_CHANNEL)
74       .IfHasChannelArg(GRPC_ARG_SECURITY_CONNECTOR);
75   builder->channel_init()
76       ->RegisterFilter<ServerAuthFilter>(GRPC_SERVER_CHANNEL)
77       .IfHasChannelArg(GRPC_SERVER_CREDENTIALS_ARG);
78   builder->channel_init()
79       ->RegisterFilter<GrpcServerAuthzFilter>(GRPC_SERVER_CHANNEL)
80       .IfHasChannelArg(GRPC_ARG_AUTHORIZATION_POLICY_PROVIDER)
81       .After<ServerAuthFilter>();
82 }
83 }  // namespace grpc_core
84 
do_basic_init(void)85 static void do_basic_init(void) {
86   grpc_core::InitInternally = grpc_init;
87   grpc_core::ShutdownInternally = grpc_shutdown;
88   grpc_core::IsInitializedInternally = []() {
89     return grpc_is_initialized() != 0;
90   };
91   gpr_log_verbosity_init();
92   g_init_mu = new grpc_core::Mutex();
93   g_shutting_down_cv = new grpc_core::CondVar();
94   gpr_time_init();
95   grpc_core::PrintExperimentsList();
96   grpc_core::Fork::GlobalInit();
97   grpc_fork_handlers_auto_register();
98   grpc_tracer_init();
99   grpc_client_channel_global_init_backup_polling();
100 }
101 
grpc_init(void)102 void grpc_init(void) {
103   gpr_once_init(&g_basic_init, do_basic_init);
104 
105   grpc_core::MutexLock lock(g_init_mu);
106   if (++g_initializations == 1) {
107     if (g_shutting_down) {
108       g_shutting_down = false;
109       g_shutting_down_cv->SignalAll();
110     }
111     grpc_iomgr_init();
112     grpc_resolver_dns_ares_init();
113     grpc_iomgr_start();
114   }
115 
116   GRPC_API_TRACE("grpc_init(void)", 0, ());
117 }
118 
grpc_shutdown_internal_locked(void)119 void grpc_shutdown_internal_locked(void)
120     ABSL_EXCLUSIVE_LOCKS_REQUIRED(g_init_mu) {
121   {
122     grpc_core::ExecCtx exec_ctx(0);
123     grpc_iomgr_shutdown_background_closure();
124     grpc_timer_manager_set_threading(false);  // shutdown timer_manager thread
125     grpc_resolver_dns_ares_shutdown();
126     grpc_iomgr_shutdown();
127   }
128   g_shutting_down = false;
129   g_shutting_down_cv->SignalAll();
130 }
131 
grpc_shutdown_from_cleanup_thread(void *)132 void grpc_shutdown_from_cleanup_thread(void* /*ignored*/) {
133   GRPC_API_TRACE("grpc_shutdown_from_cleanup_thread", 0, ());
134   grpc_core::MutexLock lock(g_init_mu);
135   // We have released lock from the shutdown thread and it is possible that
136   // another grpc_init has been called, and do nothing if that is the case.
137   if (--g_initializations != 0) {
138     return;
139   }
140   grpc_shutdown_internal_locked();
141   gpr_log(GPR_DEBUG, "grpc_shutdown from cleanup thread done");
142 }
143 
grpc_shutdown(void)144 void grpc_shutdown(void) {
145   GRPC_API_TRACE("grpc_shutdown(void)", 0, ());
146   grpc_core::MutexLock lock(g_init_mu);
147 
148   if (--g_initializations == 0) {
149     grpc_core::ApplicationCallbackExecCtx* acec =
150         grpc_core::ApplicationCallbackExecCtx::Get();
151     if (!grpc_iomgr_is_any_background_poller_thread() &&
152         !grpc_event_engine::experimental::TimerManager::
153             IsTimerManagerThread() &&
154         (acec == nullptr ||
155          (acec->Flags() & GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD) ==
156              0) &&
157         grpc_core::ExecCtx::Get() == nullptr) {
158       // just run clean-up when this is called on non-executor thread.
159       gpr_log(GPR_DEBUG, "grpc_shutdown starts clean-up now");
160       g_shutting_down = true;
161       grpc_shutdown_internal_locked();
162       gpr_log(GPR_DEBUG, "grpc_shutdown done");
163     } else {
164       // spawn a detached thread to do the actual clean up in case we are
165       // currently in an executor thread.
166       gpr_log(GPR_DEBUG, "grpc_shutdown spawns clean-up thread");
167       g_initializations++;
168       g_shutting_down = true;
169       grpc_core::Thread cleanup_thread(
170           "grpc_shutdown", grpc_shutdown_from_cleanup_thread, nullptr, nullptr,
171           grpc_core::Thread::Options().set_joinable(false).set_tracked(false));
172       cleanup_thread.Start();
173     }
174   }
175 }
176 
grpc_shutdown_blocking(void)177 void grpc_shutdown_blocking(void) {
178   GRPC_API_TRACE("grpc_shutdown_blocking(void)", 0, ());
179   grpc_core::MutexLock lock(g_init_mu);
180   if (--g_initializations == 0) {
181     g_shutting_down = true;
182     grpc_shutdown_internal_locked();
183   }
184 }
185 
grpc_is_initialized(void)186 int grpc_is_initialized(void) {
187   int r;
188   gpr_once_init(&g_basic_init, do_basic_init);
189   grpc_core::MutexLock lock(g_init_mu);
190   r = g_initializations > 0;
191   return r;
192 }
193 
grpc_maybe_wait_for_async_shutdown(void)194 void grpc_maybe_wait_for_async_shutdown(void) {
195   gpr_once_init(&g_basic_init, do_basic_init);
196   grpc_core::MutexLock lock(g_init_mu);
197   while (g_shutting_down) {
198     g_shutting_down_cv->Wait(g_init_mu);
199   }
200 }
201