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