blob: 57e6b6c56182ba945cf883429dfbf940ccd788aa [file] [log] [blame]
Josh Gaocbe70cb2016-10-18 18:17:52 -07001/*
2 * Copyright 2016, 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 <fcntl.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include <array>
25#include <deque>
Josh Gaocb68a032017-06-02 13:02:10 -070026#include <string>
Josh Gaocbe70cb2016-10-18 18:17:52 -070027#include <unordered_map>
Josh Gaocb68a032017-06-02 13:02:10 -070028#include <utility>
Josh Gaocbe70cb2016-10-18 18:17:52 -070029
30#include <event2/event.h>
31#include <event2/listener.h>
32#include <event2/thread.h>
33
Josh Gao5f87bbd2019-01-09 17:01:49 -080034#include <android-base/cmsg.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070035#include <android-base/logging.h>
Elliott Hughes35bb6d22017-06-26 13:54:05 -070036#include <android-base/properties.h>
Josh Gaocbe70cb2016-10-18 18:17:52 -070037#include <android-base/stringprintf.h>
38#include <android-base/unique_fd.h>
39#include <cutils/sockets.h>
40
Josh Gao55f79a52017-03-06 12:24:07 -080041#include "debuggerd/handler.h"
Narayan Kamatha73df602017-05-24 15:07:25 +010042#include "dump_type.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010043#include "protocol.h"
44#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070045
46#include "intercept_manager.h"
47
Elliott Hughes35bb6d22017-06-26 13:54:05 -070048using android::base::GetIntProperty;
Josh Gao5f87bbd2019-01-09 17:01:49 -080049using android::base::SendFileDescriptors;
Josh Gaocbe70cb2016-10-18 18:17:52 -070050using android::base::StringPrintf;
Josh Gao9a61f682020-12-01 21:04:09 -080051
52using android::base::borrowed_fd;
Josh Gaocbe70cb2016-10-18 18:17:52 -070053using android::base::unique_fd;
54
55static InterceptManager* intercept_manager;
56
57enum CrashStatus {
58 kCrashStatusRunning,
59 kCrashStatusQueued,
60};
61
Elliott Hughes35bb6d22017-06-26 13:54:05 -070062// Ownership of Crash is a bit messy.
63// It's either owned by an active event that must have a timeout, or owned by
64// queued_requests, in the case that multiple crashes come in at the same time.
65struct Crash {
66 ~Crash() { event_free(crash_event); }
67
Josh Gaof5974ae2018-05-03 16:05:32 -070068 std::string crash_tombstone_path;
Josh Gao48383c82018-04-18 18:11:01 -070069 unique_fd crash_tombstone_fd;
70 unique_fd crash_socket_fd;
Elliott Hughes35bb6d22017-06-26 13:54:05 -070071 pid_t crash_pid;
72 event* crash_event = nullptr;
Elliott Hughes35bb6d22017-06-26 13:54:05 -070073
74 DebuggerdDumpType crash_type;
75};
Narayan Kamath922f6b22017-05-15 15:59:30 +010076
Narayan Kamatha73df602017-05-24 15:07:25 +010077class CrashQueue {
Narayan Kamath922f6b22017-05-15 15:59:30 +010078 public:
Narayan Kamatha73df602017-05-24 15:07:25 +010079 CrashQueue(const std::string& dir_path, const std::string& file_name_prefix, size_t max_artifacts,
80 size_t max_concurrent_dumps)
Narayan Kamath922f6b22017-05-15 15:59:30 +010081 : file_name_prefix_(file_name_prefix),
82 dir_path_(dir_path),
83 dir_fd_(open(dir_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)),
84 max_artifacts_(max_artifacts),
85 next_artifact_(0),
86 max_concurrent_dumps_(max_concurrent_dumps),
87 num_concurrent_dumps_(0) {
88 if (dir_fd_ == -1) {
89 PLOG(FATAL) << "failed to open directory: " << dir_path;
90 }
91
92 // NOTE: If max_artifacts_ <= max_concurrent_dumps_, then theoretically the
93 // same filename could be handed out to multiple processes.
94 CHECK(max_artifacts_ > max_concurrent_dumps_);
95
96 find_oldest_artifact();
97 }
98
Elliott Hughes35bb6d22017-06-26 13:54:05 -070099 static CrashQueue* for_crash(const Crash* crash) {
100 return (crash->crash_type == kDebuggerdJavaBacktrace) ? for_anrs() : for_tombstones();
101 }
102
Josh Gao9a61f682020-12-01 21:04:09 -0800103 static CrashQueue* for_crash(const std::unique_ptr<Crash>& crash) {
104 return for_crash(crash.get());
105 }
106
Elliott Hughes35bb6d22017-06-26 13:54:05 -0700107 static CrashQueue* for_tombstones() {
108 static CrashQueue queue("/data/tombstones", "tombstone_" /* file_name_prefix */,
Elliott Hughesec220cd2019-09-26 14:35:24 -0700109 GetIntProperty("tombstoned.max_tombstone_count", 32),
Elliott Hughes35bb6d22017-06-26 13:54:05 -0700110 1 /* max_concurrent_dumps */);
111 return &queue;
112 }
113
114 static CrashQueue* for_anrs() {
115 static CrashQueue queue("/data/anr", "trace_" /* file_name_prefix */,
116 GetIntProperty("tombstoned.max_anr_count", 64),
117 4 /* max_concurrent_dumps */);
118 return &queue;
119 }
120
Josh Gaof5974ae2018-05-03 16:05:32 -0700121 std::pair<std::string, unique_fd> get_output() {
122 std::string path;
Josh Gao48383c82018-04-18 18:11:01 -0700123 unique_fd result(openat(dir_fd_, ".", O_WRONLY | O_APPEND | O_TMPFILE | O_CLOEXEC, 0640));
Narayan Kamath922f6b22017-05-15 15:59:30 +0100124 if (result == -1) {
Josh Gaof5974ae2018-05-03 16:05:32 -0700125 // We might not have O_TMPFILE. Try creating with an arbitrary filename instead.
126 static size_t counter = 0;
127 std::string tmp_filename = StringPrintf(".temporary%zu", counter++);
128 result.reset(openat(dir_fd_, tmp_filename.c_str(),
129 O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_CLOEXEC, 0640));
Josh Gao48383c82018-04-18 18:11:01 -0700130 if (result == -1) {
131 PLOG(FATAL) << "failed to create temporary tombstone in " << dir_path_;
132 }
Josh Gaof5974ae2018-05-03 16:05:32 -0700133
134 path = StringPrintf("%s/%s", dir_path_.c_str(), tmp_filename.c_str());
Narayan Kamath922f6b22017-05-15 15:59:30 +0100135 }
Josh Gaof5974ae2018-05-03 16:05:32 -0700136 return std::make_pair(std::move(path), std::move(result));
Josh Gao48383c82018-04-18 18:11:01 -0700137 }
Narayan Kamath922f6b22017-05-15 15:59:30 +0100138
Josh Gao48383c82018-04-18 18:11:01 -0700139 std::string get_next_artifact_path() {
140 std::string file_name =
141 StringPrintf("%s/%s%02d", dir_path_.c_str(), file_name_prefix_.c_str(), next_artifact_);
Narayan Kamath922f6b22017-05-15 15:59:30 +0100142 next_artifact_ = (next_artifact_ + 1) % max_artifacts_;
Josh Gao48383c82018-04-18 18:11:01 -0700143 return file_name;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100144 }
145
Josh Gao9a61f682020-12-01 21:04:09 -0800146 // Consumes crash if it returns true, otherwise leaves it untouched.
147 bool maybe_enqueue_crash(std::unique_ptr<Crash>&& crash) {
Narayan Kamath922f6b22017-05-15 15:59:30 +0100148 if (num_concurrent_dumps_ == max_concurrent_dumps_) {
Josh Gao9a61f682020-12-01 21:04:09 -0800149 queued_requests_.emplace_back(std::move(crash));
Narayan Kamath922f6b22017-05-15 15:59:30 +0100150 return true;
151 }
152
153 return false;
154 }
155
Josh Gao9a61f682020-12-01 21:04:09 -0800156 void maybe_dequeue_crashes(void (*handler)(std::unique_ptr<Crash> crash)) {
Narayan Kamath922f6b22017-05-15 15:59:30 +0100157 while (!queued_requests_.empty() && num_concurrent_dumps_ < max_concurrent_dumps_) {
Josh Gao9a61f682020-12-01 21:04:09 -0800158 std::unique_ptr<Crash> next_crash = std::move(queued_requests_.front());
Narayan Kamath922f6b22017-05-15 15:59:30 +0100159 queued_requests_.pop_front();
Josh Gao9a61f682020-12-01 21:04:09 -0800160 handler(std::move(next_crash));
Narayan Kamath922f6b22017-05-15 15:59:30 +0100161 }
162 }
163
164 void on_crash_started() { ++num_concurrent_dumps_; }
165
166 void on_crash_completed() { --num_concurrent_dumps_; }
167
Narayan Kamath922f6b22017-05-15 15:59:30 +0100168 private:
169 void find_oldest_artifact() {
170 size_t oldest_tombstone = 0;
171 time_t oldest_time = std::numeric_limits<time_t>::max();
172
173 for (size_t i = 0; i < max_artifacts_; ++i) {
Josh Gaocb68a032017-06-02 13:02:10 -0700174 std::string path = StringPrintf("%s/%s%02zu", dir_path_.c_str(), file_name_prefix_.c_str(), i);
Narayan Kamath922f6b22017-05-15 15:59:30 +0100175 struct stat st;
176 if (stat(path.c_str(), &st) != 0) {
177 if (errno == ENOENT) {
178 oldest_tombstone = i;
179 break;
180 } else {
181 PLOG(ERROR) << "failed to stat " << path;
182 continue;
183 }
184 }
185
186 if (st.st_mtime < oldest_time) {
187 oldest_tombstone = i;
188 oldest_time = st.st_mtime;
189 }
190 }
191
192 next_artifact_ = oldest_tombstone;
193 }
194
195 const std::string file_name_prefix_;
196
197 const std::string dir_path_;
198 const int dir_fd_;
199
200 const size_t max_artifacts_;
201 int next_artifact_;
202
203 const size_t max_concurrent_dumps_;
204 size_t num_concurrent_dumps_;
205
Josh Gao9a61f682020-12-01 21:04:09 -0800206 std::deque<std::unique_ptr<Crash>> queued_requests_;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100207
Narayan Kamatha73df602017-05-24 15:07:25 +0100208 DISALLOW_COPY_AND_ASSIGN(CrashQueue);
Narayan Kamath922f6b22017-05-15 15:59:30 +0100209};
210
211// Whether java trace dumps are produced via tombstoned.
Narayan Kamathca5e9082017-06-02 15:42:06 +0100212static constexpr bool kJavaTraceDumpsEnabled = true;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100213
Josh Gaocbe70cb2016-10-18 18:17:52 -0700214// Forward declare the callbacks so they can be placed in a sensible order.
215static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, void*);
216static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg);
217static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg);
218
Josh Gao9a61f682020-12-01 21:04:09 -0800219static void perform_request(std::unique_ptr<Crash> crash) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700220 unique_fd output_fd;
Josh Gao48383c82018-04-18 18:11:01 -0700221 bool intercepted =
222 intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd);
223 if (!intercepted) {
Josh Gao2b22ae12018-09-12 14:51:03 -0700224 if (crash->crash_type == kDebuggerdNativeBacktrace) {
225 // Don't generate tombstones for native backtrace requests.
226 output_fd.reset(open("/dev/null", O_WRONLY | O_CLOEXEC));
227 } else {
228 std::tie(crash->crash_tombstone_path, output_fd) = CrashQueue::for_crash(crash)->get_output();
229 crash->crash_tombstone_fd.reset(dup(output_fd.get()));
230 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700231 }
232
233 TombstonedCrashPacket response = {
234 .packet_type = CrashPacketType::kPerformDump
235 };
Josh Gao5f87bbd2019-01-09 17:01:49 -0800236 ssize_t rc =
237 SendFileDescriptors(crash->crash_socket_fd, &response, sizeof(response), output_fd.get());
238 output_fd.reset();
239
Josh Gaocbe70cb2016-10-18 18:17:52 -0700240 if (rc == -1) {
241 PLOG(WARNING) << "failed to send response to CrashRequest";
Josh Gao9a61f682020-12-01 21:04:09 -0800242 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700243 } else if (rc != sizeof(response)) {
244 PLOG(WARNING) << "crash socket write returned short";
Josh Gao9a61f682020-12-01 21:04:09 -0800245 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700246 }
Josh Gao13078242017-03-30 14:42:46 -0700247
Josh Gao9a61f682020-12-01 21:04:09 -0800248 // TODO: Make this configurable by the interceptor?
249 struct timeval timeout = {10, 0};
Josh Gaocbe70cb2016-10-18 18:17:52 -0700250
Josh Gao9a61f682020-12-01 21:04:09 -0800251 event_base* base = event_get_base(crash->crash_event);
252
253 event_assign(crash->crash_event, base, crash->crash_socket_fd, EV_TIMEOUT | EV_READ,
254 crash_completed_cb, crash.get());
255 event_add(crash->crash_event, &timeout);
256 CrashQueue::for_crash(crash)->on_crash_started();
257
258 // The crash is now owned by the event loop.
259 crash.release();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700260}
261
262static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int,
Narayan Kamatha73df602017-05-24 15:07:25 +0100263 void*) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700264 event_base* base = evconnlistener_get_base(listener);
265 Crash* crash = new Crash();
266
Narayan Kamatha73df602017-05-24 15:07:25 +0100267 // TODO: Make sure that only java crashes come in on the java socket
268 // and only native crashes on the native socket.
Josh Gaocbe70cb2016-10-18 18:17:52 -0700269 struct timeval timeout = { 1, 0 };
270 event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash);
Josh Gao48383c82018-04-18 18:11:01 -0700271 crash->crash_socket_fd.reset(sockfd);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700272 crash->crash_event = crash_event;
273 event_add(crash_event, &timeout);
274}
275
276static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg) {
Josh Gao9a61f682020-12-01 21:04:09 -0800277 std::unique_ptr<Crash> crash(static_cast<Crash*>(arg));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700278 TombstonedCrashPacket request = {};
279
280 if ((ev & EV_TIMEOUT) != 0) {
281 LOG(WARNING) << "crash request timed out";
Josh Gao9a61f682020-12-01 21:04:09 -0800282 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700283 } else if ((ev & EV_READ) == 0) {
284 LOG(WARNING) << "tombstoned received unexpected event from crash socket";
Josh Gao9a61f682020-12-01 21:04:09 -0800285 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700286 }
287
Josh Gao9a61f682020-12-01 21:04:09 -0800288 ssize_t rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700289 if (rc == -1) {
290 PLOG(WARNING) << "failed to read from crash socket";
Josh Gao9a61f682020-12-01 21:04:09 -0800291 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700292 } else if (rc != sizeof(request)) {
293 LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
294 << sizeof(request) << ")";
Josh Gao9a61f682020-12-01 21:04:09 -0800295 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700296 }
297
298 if (request.packet_type != CrashPacketType::kDumpRequest) {
299 LOG(WARNING) << "unexpected crash packet type, expected kDumpRequest, received "
300 << StringPrintf("%#2hhX", request.packet_type);
Josh Gao9a61f682020-12-01 21:04:09 -0800301 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700302 }
303
Narayan Kamatha73df602017-05-24 15:07:25 +0100304 crash->crash_type = request.packet.dump_request.dump_type;
305 if (crash->crash_type < 0 || crash->crash_type > kDebuggerdAnyIntercept) {
306 LOG(WARNING) << "unexpected crash dump type: " << crash->crash_type;
Josh Gao9a61f682020-12-01 21:04:09 -0800307 return;
Narayan Kamatha73df602017-05-24 15:07:25 +0100308 }
309
310 if (crash->crash_type != kDebuggerdJavaBacktrace) {
Narayan Kamath922f6b22017-05-15 15:59:30 +0100311 crash->crash_pid = request.packet.dump_request.pid;
312 } else {
313 // Requests for java traces are sent from untrusted processes, so we
314 // must not trust the PID sent down with the request. Instead, we ask the
315 // kernel.
316 ucred cr = {};
317 socklen_t len = sizeof(cr);
318 int ret = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
319 if (ret != 0) {
320 PLOG(ERROR) << "Failed to getsockopt(..SO_PEERCRED)";
Josh Gao9a61f682020-12-01 21:04:09 -0800321 return;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100322 }
323
324 crash->crash_pid = cr.pid;
325 }
326
Josh Gao9a61f682020-12-01 21:04:09 -0800327 pid_t crash_pid = crash->crash_pid;
328 LOG(INFO) << "received crash request for pid " << crash_pid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700329
Josh Gao9a61f682020-12-01 21:04:09 -0800330 if (CrashQueue::for_crash(crash)->maybe_enqueue_crash(std::move(crash))) {
331 LOG(INFO) << "enqueueing crash request for pid " << crash_pid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700332 } else {
Josh Gao9a61f682020-12-01 21:04:09 -0800333 perform_request(std::move(crash));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700334 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700335}
336
Josh Gao9a61f682020-12-01 21:04:09 -0800337static void crash_completed(borrowed_fd sockfd, std::unique_ptr<Crash> crash) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700338 TombstonedCrashPacket request = {};
339
Josh Gao9a61f682020-12-01 21:04:09 -0800340 ssize_t rc = TEMP_FAILURE_RETRY(read(sockfd.get(), &request, sizeof(request)));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700341 if (rc == -1) {
342 PLOG(WARNING) << "failed to read from crash socket";
Josh Gao9a61f682020-12-01 21:04:09 -0800343 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700344 } else if (rc != sizeof(request)) {
345 LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
346 << sizeof(request) << ")";
Josh Gao9a61f682020-12-01 21:04:09 -0800347 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700348 }
349
350 if (request.packet_type != CrashPacketType::kCompletedDump) {
351 LOG(WARNING) << "unexpected crash packet type, expected kCompletedDump, received "
352 << uint32_t(request.packet_type);
Josh Gao9a61f682020-12-01 21:04:09 -0800353 return;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700354 }
355
Josh Gao48383c82018-04-18 18:11:01 -0700356 if (crash->crash_tombstone_fd != -1) {
357 std::string fd_path = StringPrintf("/proc/self/fd/%d", crash->crash_tombstone_fd.get());
358 std::string tombstone_path = CrashQueue::for_crash(crash)->get_next_artifact_path();
Josh Gaof5974ae2018-05-03 16:05:32 -0700359
360 // linkat doesn't let us replace a file, so we need to unlink first.
Josh Gao48383c82018-04-18 18:11:01 -0700361 int rc = unlink(tombstone_path.c_str());
Josh Gao28f8cf02018-05-03 14:31:08 -0700362 if (rc != 0 && errno != ENOENT) {
Josh Gao48383c82018-04-18 18:11:01 -0700363 PLOG(ERROR) << "failed to unlink tombstone at " << tombstone_path;
Josh Gao9a61f682020-12-01 21:04:09 -0800364 return;
Josh Gao48383c82018-04-18 18:11:01 -0700365 }
366
367 rc = linkat(AT_FDCWD, fd_path.c_str(), AT_FDCWD, tombstone_path.c_str(), AT_SYMLINK_FOLLOW);
368 if (rc != 0) {
369 PLOG(ERROR) << "failed to link tombstone";
Narayan Kamath79dd1432017-06-21 19:42:00 +0100370 } else {
Josh Gao48383c82018-04-18 18:11:01 -0700371 if (crash->crash_type == kDebuggerdJavaBacktrace) {
372 LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << tombstone_path;
373 } else {
374 // NOTE: Several tools parse this log message to figure out where the
375 // tombstone associated with a given native crash was written. Any changes
376 // to this message must be carefully considered.
377 LOG(ERROR) << "Tombstone written to: " << tombstone_path;
378 }
Narayan Kamath79dd1432017-06-21 19:42:00 +0100379 }
Josh Gaof5974ae2018-05-03 16:05:32 -0700380
381 // If we don't have O_TMPFILE, we need to clean up after ourselves.
382 if (!crash->crash_tombstone_path.empty()) {
383 rc = unlink(crash->crash_tombstone_path.c_str());
384 if (rc != 0) {
385 PLOG(ERROR) << "failed to unlink temporary tombstone at " << crash->crash_tombstone_path;
386 }
387 }
Josh Gaocb68a032017-06-02 13:02:10 -0700388 }
Josh Gao9a61f682020-12-01 21:04:09 -0800389}
Josh Gaocb68a032017-06-02 13:02:10 -0700390
Josh Gao9a61f682020-12-01 21:04:09 -0800391static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) {
392 std::unique_ptr<Crash> crash(static_cast<Crash*>(arg));
Elliott Hughes35bb6d22017-06-26 13:54:05 -0700393 CrashQueue* queue = CrashQueue::for_crash(crash);
Josh Gao9a61f682020-12-01 21:04:09 -0800394
395 CrashQueue::for_crash(crash)->on_crash_completed();
396
397 if ((ev & EV_READ) == EV_READ) {
398 crash_completed(sockfd, std::move(crash));
399 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700400
401 // If there's something queued up, let them proceed.
Narayan Kamatha73df602017-05-24 15:07:25 +0100402 queue->maybe_dequeue_crashes(perform_request);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700403}
404
405int main(int, char* []) {
Josh Gao8830c952017-03-06 12:23:55 -0800406 umask(0137);
407
Josh Gao55f79a52017-03-06 12:24:07 -0800408 // Don't try to connect to ourselves if we crash.
409 struct sigaction action = {};
410 action.sa_handler = [](int signal) {
411 LOG(ERROR) << "received fatal signal " << signal;
412 _exit(1);
413 };
414 debuggerd_register_handlers(&action);
415
Josh Gaocbe70cb2016-10-18 18:17:52 -0700416 int intercept_socket = android_get_control_socket(kTombstonedInterceptSocketName);
417 int crash_socket = android_get_control_socket(kTombstonedCrashSocketName);
418
419 if (intercept_socket == -1 || crash_socket == -1) {
420 PLOG(FATAL) << "failed to get socket from init";
421 }
422
423 evutil_make_socket_nonblocking(intercept_socket);
424 evutil_make_socket_nonblocking(crash_socket);
425
426 event_base* base = event_base_new();
427 if (!base) {
428 LOG(FATAL) << "failed to create event_base";
429 }
430
431 intercept_manager = new InterceptManager(base, intercept_socket);
432
Narayan Kamathc2e98f62017-09-13 13:12:34 +0100433 evconnlistener* tombstone_listener =
434 evconnlistener_new(base, crash_accept_cb, CrashQueue::for_tombstones(), LEV_OPT_CLOSE_ON_FREE,
435 -1 /* backlog */, crash_socket);
Narayan Kamath922f6b22017-05-15 15:59:30 +0100436 if (!tombstone_listener) {
437 LOG(FATAL) << "failed to create evconnlistener for tombstones.";
438 }
439
440 if (kJavaTraceDumpsEnabled) {
441 const int java_trace_socket = android_get_control_socket(kTombstonedJavaTraceSocketName);
442 if (java_trace_socket == -1) {
443 PLOG(FATAL) << "failed to get socket from init";
444 }
445
446 evutil_make_socket_nonblocking(java_trace_socket);
Narayan Kamathc2e98f62017-09-13 13:12:34 +0100447 evconnlistener* java_trace_listener =
448 evconnlistener_new(base, crash_accept_cb, CrashQueue::for_anrs(), LEV_OPT_CLOSE_ON_FREE,
449 -1 /* backlog */, java_trace_socket);
Narayan Kamath922f6b22017-05-15 15:59:30 +0100450 if (!java_trace_listener) {
451 LOG(FATAL) << "failed to create evconnlistener for java traces.";
452 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700453 }
454
455 LOG(INFO) << "tombstoned successfully initialized";
456 event_base_dispatch(base);
457}