• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2017 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/iomgr/port.h"
22 
23 #ifdef GRPC_POSIX_FORK
24 
25 #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
26 #include <pthread.h>
27 #endif
28 
29 #include <grpc/fork.h>
30 #include <grpc/grpc.h>
31 #include <string.h>
32 
33 #include "absl/log/log.h"
34 #include "src/core/lib/iomgr/ev_posix.h"
35 #include "src/core/lib/iomgr/executor.h"
36 #include "src/core/lib/iomgr/timer_manager.h"
37 #include "src/core/lib/iomgr/wakeup_fd_posix.h"
38 #include "src/core/lib/surface/init_internally.h"
39 #include "src/core/util/crash.h"
40 #include "src/core/util/fork.h"
41 #include "src/core/util/thd.h"
42 
43 //
44 // NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
45 //       AROUND VERY SPECIFIC USE CASES.
46 //
47 
48 namespace {
49 bool skipped_handler = true;
50 bool registered_handlers = false;
51 }  // namespace
52 
grpc_prefork()53 void grpc_prefork() {
54   skipped_handler = true;
55   // This  may be called after core shuts down, so verify initialized before
56   // instantiating an ExecCtx.
57   if (!grpc_core::IsInitializedInternally()) {
58     return;
59   }
60   grpc_core::ExecCtx exec_ctx;
61   if (!grpc_core::Fork::Enabled()) {
62     LOG(ERROR) << "Fork support not enabled; try running with the "
63                   "environment variable GRPC_ENABLE_FORK_SUPPORT=1";
64     return;
65   }
66   const char* poll_strategy_name = grpc_get_poll_strategy_name();
67   if (poll_strategy_name == nullptr ||
68       (strcmp(poll_strategy_name, "epoll1") != 0 &&
69        strcmp(poll_strategy_name, "poll") != 0)) {
70     LOG(INFO) << "Fork support is only compatible with the epoll1 and poll "
71                  "polling strategies";
72     return;
73   }
74   if (!grpc_core::Fork::BlockExecCtx()) {
75     LOG(INFO) << "Other threads are currently calling into gRPC, skipping "
76                  "fork() handlers";
77     return;
78   }
79   grpc_timer_manager_set_threading(false);
80   grpc_core::Executor::SetThreadingAll(false);
81   grpc_core::ExecCtx::Get()->Flush();
82   grpc_core::Fork::AwaitThreads();
83   skipped_handler = false;
84 }
85 
grpc_postfork_parent()86 void grpc_postfork_parent() {
87   if (!skipped_handler) {
88     grpc_core::Fork::AllowExecCtx();
89     grpc_core::ExecCtx exec_ctx;
90     grpc_timer_manager_set_threading(true);
91     grpc_core::Executor::SetThreadingAll(true);
92   }
93 }
94 
grpc_postfork_child()95 void grpc_postfork_child() {
96   if (!skipped_handler) {
97     grpc_core::Fork::AllowExecCtx();
98     grpc_core::ExecCtx exec_ctx;
99     for (auto* reset_polling_engine :
100          grpc_core::Fork::GetResetChildPollingEngineFunc()) {
101       if (reset_polling_engine != nullptr) {
102         reset_polling_engine();
103       }
104     }
105     grpc_timer_manager_set_threading(true);
106     grpc_core::Executor::SetThreadingAll(true);
107   }
108 }
109 
grpc_fork_handlers_auto_register()110 void grpc_fork_handlers_auto_register() {
111   if (grpc_core::Fork::Enabled() & !registered_handlers) {
112 #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
113     pthread_atfork(grpc_prefork, grpc_postfork_parent, grpc_postfork_child);
114     registered_handlers = true;
115 #endif  // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
116   }
117 }
118 
119 #endif  // GRPC_POSIX_FORK
120