• 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 #include <string.h>
26 
27 #include <grpc/fork.h>
28 #include <grpc/grpc.h>
29 #include <grpc/support/log.h>
30 
31 #include "src/core/lib/gpr/env.h"
32 #include "src/core/lib/gprpp/fork.h"
33 #include "src/core/lib/gprpp/thd.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 
39 /*
40  * NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
41  *       AROUND VERY SPECIFIC USE CASES.
42  */
43 
44 namespace {
45 bool skipped_handler = true;
46 bool registered_handlers = false;
47 }  // namespace
48 
grpc_prefork()49 void grpc_prefork() {
50   grpc_core::ExecCtx exec_ctx;
51   skipped_handler = true;
52   if (!grpc_is_initialized()) {
53     return;
54   }
55   if (!grpc_core::Fork::Enabled()) {
56     gpr_log(GPR_ERROR,
57             "Fork support not enabled; try running with the "
58             "environment variable GRPC_ENABLE_FORK_SUPPORT=1");
59     return;
60   }
61   if (strcmp(grpc_get_poll_strategy_name(), "epoll1") != 0 &&
62       strcmp(grpc_get_poll_strategy_name(), "poll") != 0) {
63     gpr_log(GPR_ERROR,
64             "Fork support is only compatible with the epoll1 and poll polling "
65             "strategies");
66   }
67   if (!grpc_core::Fork::BlockExecCtx()) {
68     gpr_log(GPR_INFO,
69             "Other threads are currently calling into gRPC, skipping fork() "
70             "handlers");
71     return;
72   }
73   grpc_timer_manager_set_threading(false);
74   grpc_executor_set_threading(false);
75   grpc_core::ExecCtx::Get()->Flush();
76   grpc_core::Fork::AwaitThreads();
77   skipped_handler = false;
78 }
79 
grpc_postfork_parent()80 void grpc_postfork_parent() {
81   if (!skipped_handler) {
82     grpc_core::Fork::AllowExecCtx();
83     grpc_core::ExecCtx exec_ctx;
84     grpc_timer_manager_set_threading(true);
85     grpc_executor_set_threading(true);
86   }
87 }
88 
grpc_postfork_child()89 void grpc_postfork_child() {
90   if (!skipped_handler) {
91     grpc_core::Fork::AllowExecCtx();
92     grpc_core::ExecCtx exec_ctx;
93     grpc_core::Fork::child_postfork_func reset_polling_engine =
94         grpc_core::Fork::GetResetChildPollingEngineFunc();
95     if (reset_polling_engine != nullptr) {
96       reset_polling_engine();
97     }
98     grpc_timer_manager_set_threading(true);
99     grpc_executor_set_threading(true);
100   }
101 }
102 
grpc_fork_handlers_auto_register()103 void grpc_fork_handlers_auto_register() {
104   if (grpc_core::Fork::Enabled() & !registered_handlers) {
105 #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
106     pthread_atfork(grpc_prefork, grpc_postfork_parent, grpc_postfork_child);
107     registered_handlers = true;
108 #endif  // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
109   }
110 }
111 
112 #endif  // GRPC_POSIX_FORK
113