1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 #include <csignal>
21 #include <cstring>
22 #include <memory>
23 #include <optional>
24 #include <string>
25 #include <thread>
26
27 #include "stack_manager.h"
28
29 // clang-format off
30 #include <client/linux/handler/exception_handler.h>
31 #include <unwindstack/AndroidUnwinder.h>
32 // clang-format on
33
34 #include "common/init_flags.h"
35 #include "facade/grpc_root_server.h"
36 #include "hal/hci_hal_host.h"
37 #include "hal/snoop_logger.h"
38 #include "os/log.h"
39 #include "os/parameter_provider.h"
40 #include "os/system_properties.h"
41
42 using ::bluetooth::ModuleList;
43 using ::bluetooth::StackManager;
44 using ::bluetooth::hal::HciHalHostRootcanalConfig;
45 using ::bluetooth::os::Thread;
46
__asan_default_options()47 extern "C" const char* __asan_default_options() {
48 return "detect_container_overflow=0";
49 }
50
51 namespace {
52 ::bluetooth::facade::GrpcRootServer grpc_root_server;
53
54 std::promise<void> interrupt_promise;
55 std::future<void> interrupt_future;
56 bool interrupted = false;
57 struct sigaction old_act = {};
interrupt_handler(int signal_number)58 void interrupt_handler(int signal_number) {
59 if (!interrupted) {
60 interrupted = true;
61 LOG_INFO("Stopping gRPC root server due to signal: %s[%d]", strsignal(signal_number), signal_number);
62 interrupt_promise.set_value();
63 } else {
64 LOG_WARN("Already interrupted by signal: %s[%d]", strsignal(signal_number), signal_number);
65 }
66 if (old_act.sa_handler != nullptr && old_act.sa_handler != SIG_IGN && old_act.sa_handler != SIG_DFL) {
67 LOG_INFO("Calling saved signal handler");
68 old_act.sa_handler(signal_number);
69 }
70 }
71 struct sigaction new_act = {.sa_handler = interrupt_handler};
72
crash_callback(const void * crash_context,size_t crash_context_size,void * context)73 bool crash_callback(const void* crash_context, size_t crash_context_size, void* context) {
74 std::optional<pid_t> tid;
75 if (crash_context_size >= sizeof(google_breakpad::ExceptionHandler::CrashContext)) {
76 auto* ctx = static_cast<const google_breakpad::ExceptionHandler::CrashContext*>(crash_context);
77 tid = ctx->tid;
78 int signal_number = ctx->siginfo.si_signo;
79 LOG_ERROR("Process crashed, signal: %s[%d], tid: %d", strsignal(signal_number), signal_number, ctx->tid);
80 } else {
81 LOG_ERROR("Process crashed, signal: unknown, tid: unknown");
82 }
83 unwindstack::AndroidLocalUnwinder unwinder;
84 unwindstack::AndroidUnwinderData data;
85 if (!unwinder.Unwind(tid, data)) {
86 LOG_ERROR("Unwind failed");
87 return false;
88 }
89 LOG_ERROR("Backtrace:");
90 for (const auto& frame : data.frames) {
91 LOG_ERROR("%s", unwinder.FormatFrame(frame).c_str());
92 }
93 return true;
94 }
95
96 // Need to stop server on a thread that is not part of a signal handler due to an issue with gRPC
97 // See: https://github.com/grpc/grpc/issues/24884
thread_check_shutdown()98 void thread_check_shutdown() {
99 LOG_INFO("shutdown thread waiting for interruption");
100 interrupt_future.wait();
101 LOG_INFO("interrupted, stopping server");
102 grpc_root_server.StopServer();
103 }
104
105 } // namespace
106
107 // The entry point for the binary with libbluetooth + facades
main(int argc,const char ** argv)108 int main(int argc, const char** argv) {
109 google_breakpad::MinidumpDescriptor descriptor(google_breakpad::MinidumpDescriptor::kMicrodumpOnConsole);
110 google_breakpad::ExceptionHandler eh(descriptor, nullptr, nullptr, nullptr, true, -1);
111 eh.set_crash_handler(crash_callback);
112
113 int root_server_port = 8897;
114 int grpc_port = 8899;
115
116 bluetooth::common::InitFlags::SetAllForTesting();
117
118 const std::string arg_grpc_root_server_port = "--root-server-port=";
119 const std::string arg_grpc_server_port = "--grpc-port=";
120 const std::string arg_rootcanal_port = "--rootcanal-port=";
121 const std::string arg_btsnoop_path = "--btsnoop=";
122 const std::string arg_btsnooz_path = "--btsnooz=";
123 const std::string arg_btconfig_path = "--btconfig=";
124 for (int i = 1; i < argc; i++) {
125 std::string arg = argv[i];
126 if (arg.find(arg_grpc_root_server_port) == 0) {
127 auto port_number = arg.substr(arg_grpc_root_server_port.size());
128 root_server_port = std::stoi(port_number);
129 }
130 if (arg.find(arg_grpc_server_port) == 0) {
131 auto port_number = arg.substr(arg_grpc_server_port.size());
132 grpc_port = std::stoi(port_number);
133 }
134 if (arg.find(arg_rootcanal_port) == 0) {
135 auto port_number = arg.substr(arg_rootcanal_port.size());
136 HciHalHostRootcanalConfig::Get()->SetPort(std::stoi(port_number));
137 }
138 if (arg.find(arg_btsnoop_path) == 0) {
139 auto btsnoop_path = arg.substr(arg_btsnoop_path.size());
140 ::bluetooth::os::ParameterProvider::OverrideSnoopLogFilePath(btsnoop_path);
141 CHECK(::bluetooth::os::SetSystemProperty(
142 ::bluetooth::hal::SnoopLogger::kBtSnoopLogModeProperty, ::bluetooth::hal::SnoopLogger::kBtSnoopLogModeFull));
143 }
144 if (arg.find(arg_btsnooz_path) == 0) {
145 auto btsnooz_path = arg.substr(arg_btsnooz_path.size());
146 ::bluetooth::os::ParameterProvider::OverrideSnoozLogFilePath(btsnooz_path);
147 }
148 if (arg.find(arg_btconfig_path) == 0) {
149 auto btconfig_path = arg.substr(arg_btconfig_path.size());
150 ::bluetooth::os::ParameterProvider::OverrideConfigFilePath(btconfig_path);
151 }
152 }
153
154 int ret = sigaction(SIGINT, &new_act, &old_act);
155 if (ret < 0) {
156 LOG_ERROR("sigaction error: %s", strerror(errno));
157 }
158
159 LOG_INFO("Starting Server");
160 grpc_root_server.StartServer("0.0.0.0", root_server_port, grpc_port);
161 LOG_INFO("Server started");
162 auto wait_thread = std::thread([] { grpc_root_server.RunGrpcLoop(); });
163 interrupt_future = interrupt_promise.get_future();
164 auto shutdown_thread = std::thread{thread_check_shutdown};
165 wait_thread.join();
166 LOG_INFO("Server terminated");
167 shutdown_thread.join();
168 LOG_INFO("Shutdown thread terminated");
169
170 return 0;
171 }
172