3 * Copyright 2017 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>
21 #include "src/core/lib/iomgr/port.h"
23 #ifdef GRPC_POSIX_FORK
27 #include <grpc/fork.h>
28 #include <grpc/grpc.h>
29 #include <grpc/support/log.h>
31 #include "src/core/lib/gprpp/fork.h"
32 #include "src/core/lib/gprpp/thd.h"
33 #include "src/core/lib/iomgr/ev_posix.h"
34 #include "src/core/lib/iomgr/executor.h"
35 #include "src/core/lib/iomgr/timer_manager.h"
36 #include "src/core/lib/iomgr/wakeup_fd_posix.h"
39 * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
40 * AROUND VERY SPECIFIC USE CASES.
44 bool skipped_handler = true;
45 bool registered_handlers = false;
49 skipped_handler = true;
50 // This may be called after core shuts down, so verify initialized before
51 // instantiating an ExecCtx.
52 if (!grpc_is_initialized()) {
55 grpc_core::ExecCtx exec_ctx;
56 if (!grpc_core::Fork::Enabled()) {
58 "Fork support not enabled; try running with the "
59 "environment variable GRPC_ENABLE_FORK_SUPPORT=1");
62 const char* poll_strategy_name = grpc_get_poll_strategy_name();
63 if (poll_strategy_name == nullptr ||
64 (strcmp(poll_strategy_name, "epoll1") != 0 &&
65 strcmp(poll_strategy_name, "poll") != 0)) {
67 "Fork support is only compatible with the epoll1 and poll polling "
70 if (!grpc_core::Fork::BlockExecCtx()) {
72 "Other threads are currently calling into gRPC, skipping fork() "
76 grpc_timer_manager_set_threading(false);
77 grpc_core::Executor::SetThreadingAll(false);
78 grpc_core::ExecCtx::Get()->Flush();
79 grpc_core::Fork::AwaitThreads();
80 skipped_handler = false;
83 void grpc_postfork_parent() {
84 if (!skipped_handler) {
85 grpc_core::Fork::AllowExecCtx();
86 grpc_core::ExecCtx exec_ctx;
87 grpc_timer_manager_set_threading(true);
88 grpc_core::Executor::SetThreadingAll(true);
92 void grpc_postfork_child() {
93 if (!skipped_handler) {
94 grpc_core::Fork::AllowExecCtx();
95 grpc_core::ExecCtx exec_ctx;
96 grpc_core::Fork::child_postfork_func reset_polling_engine =
97 grpc_core::Fork::GetResetChildPollingEngineFunc();
98 if (reset_polling_engine != nullptr) {
99 reset_polling_engine();
101 grpc_timer_manager_set_threading(true);
102 grpc_core::Executor::SetThreadingAll(true);
106 void grpc_fork_handlers_auto_register() {
107 if (grpc_core::Fork::Enabled() & !registered_handlers) {
108 #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
109 pthread_atfork(grpc_prefork, grpc_postfork_parent, grpc_postfork_child);
110 registered_handlers = true;
111 #endif // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
115 #endif // GRPC_POSIX_FORK