blob: 05df9f2b6c01e80c861a59c07b98da05d16ef7d9 [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>
26#include <unordered_map>
27
28#include <event2/event.h>
29#include <event2/listener.h>
30#include <event2/thread.h>
31
32#include <android-base/logging.h>
33#include <android-base/stringprintf.h>
34#include <android-base/unique_fd.h>
35#include <cutils/sockets.h>
36
Josh Gao55f79a52017-03-06 12:24:07 -080037#include "debuggerd/handler.h"
Narayan Kamath2d377cd2017-05-10 10:58:59 +010038#include "protocol.h"
39#include "util.h"
Josh Gaocbe70cb2016-10-18 18:17:52 -070040
41#include "intercept_manager.h"
42
43using android::base::StringPrintf;
44using android::base::unique_fd;
45
46static InterceptManager* intercept_manager;
47
48enum CrashStatus {
49 kCrashStatusRunning,
50 kCrashStatusQueued,
51};
52
Narayan Kamath922f6b22017-05-15 15:59:30 +010053struct Crash;
54
55class CrashType {
56 public:
57 CrashType(const std::string& dir_path, const std::string& file_name_prefix, size_t max_artifacts,
58 size_t max_concurrent_dumps)
59 : file_name_prefix_(file_name_prefix),
60 dir_path_(dir_path),
61 dir_fd_(open(dir_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)),
62 max_artifacts_(max_artifacts),
63 next_artifact_(0),
64 max_concurrent_dumps_(max_concurrent_dumps),
65 num_concurrent_dumps_(0) {
66 if (dir_fd_ == -1) {
67 PLOG(FATAL) << "failed to open directory: " << dir_path;
68 }
69
70 // NOTE: If max_artifacts_ <= max_concurrent_dumps_, then theoretically the
71 // same filename could be handed out to multiple processes.
72 CHECK(max_artifacts_ > max_concurrent_dumps_);
73
74 find_oldest_artifact();
75 }
76
77 unique_fd get_output_fd() {
78 unique_fd result;
79 char buf[PATH_MAX];
80 snprintf(buf, sizeof(buf), "%s%02d", file_name_prefix_.c_str(), next_artifact_);
81 // Unlink and create the file, instead of using O_TRUNC, to avoid two processes
82 // interleaving their output in case we ever get into that situation.
83 if (unlinkat(dir_fd_, buf, 0) != 0 && errno != ENOENT) {
84 PLOG(FATAL) << "failed to unlink tombstone at " << dir_path_ << buf;
85 }
86
87 result.reset(openat(dir_fd_, buf, O_CREAT | O_EXCL | O_WRONLY | O_APPEND | O_CLOEXEC, 0640));
88 if (result == -1) {
89 PLOG(FATAL) << "failed to create tombstone at " << dir_path_ << buf;
90 }
91
92 next_artifact_ = (next_artifact_ + 1) % max_artifacts_;
93 return result;
94 }
95
96 bool maybe_enqueue_crash(Crash* crash) {
97 if (num_concurrent_dumps_ == max_concurrent_dumps_) {
98 queued_requests_.push_back(crash);
99 return true;
100 }
101
102 return false;
103 }
104
105 void maybe_dequeue_crashes(void (*handler)(Crash* crash)) {
106 while (!queued_requests_.empty() && num_concurrent_dumps_ < max_concurrent_dumps_) {
107 Crash* next_crash = queued_requests_.front();
108 queued_requests_.pop_front();
109 handler(next_crash);
110 }
111 }
112
113 void on_crash_started() { ++num_concurrent_dumps_; }
114
115 void on_crash_completed() { --num_concurrent_dumps_; }
116
117 static CrashType* const tombstone;
118 static CrashType* const java_trace;
119
120 private:
121 void find_oldest_artifact() {
122 size_t oldest_tombstone = 0;
123 time_t oldest_time = std::numeric_limits<time_t>::max();
124
125 for (size_t i = 0; i < max_artifacts_; ++i) {
126 std::string path = android::base::StringPrintf("%s/%s%02zu", dir_path_.c_str(),
127 file_name_prefix_.c_str(), i);
128 struct stat st;
129 if (stat(path.c_str(), &st) != 0) {
130 if (errno == ENOENT) {
131 oldest_tombstone = i;
132 break;
133 } else {
134 PLOG(ERROR) << "failed to stat " << path;
135 continue;
136 }
137 }
138
139 if (st.st_mtime < oldest_time) {
140 oldest_tombstone = i;
141 oldest_time = st.st_mtime;
142 }
143 }
144
145 next_artifact_ = oldest_tombstone;
146 }
147
148 const std::string file_name_prefix_;
149
150 const std::string dir_path_;
151 const int dir_fd_;
152
153 const size_t max_artifacts_;
154 int next_artifact_;
155
156 const size_t max_concurrent_dumps_;
157 size_t num_concurrent_dumps_;
158
159 std::deque<Crash*> queued_requests_;
160
161 DISALLOW_COPY_AND_ASSIGN(CrashType);
162};
163
164// Whether java trace dumps are produced via tombstoned.
165static constexpr bool kJavaTraceDumpsEnabled = false;
166
167/* static */ CrashType* const CrashType::tombstone =
168 new CrashType("/data/tombstones", "tombstone_" /* file_name_prefix */, 10 /* max_artifacts */,
169 1 /* max_concurrent_dumps */);
170
171/* static */ CrashType* const CrashType::java_trace =
172 (kJavaTraceDumpsEnabled ? new CrashType("/data/anr", "anr_" /* file_name_prefix */,
173 64 /* max_artifacts */, 4 /* max_concurrent_dumps */)
174 : nullptr);
175
Josh Gaocbe70cb2016-10-18 18:17:52 -0700176// Ownership of Crash is a bit messy.
177// It's either owned by an active event that must have a timeout, or owned by
178// queued_requests, in the case that multiple crashes come in at the same time.
179struct Crash {
Narayan Kamath922f6b22017-05-15 15:59:30 +0100180 ~Crash() { event_free(crash_event); }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700181
182 unique_fd crash_fd;
183 pid_t crash_pid;
184 event* crash_event = nullptr;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100185
186 // Not owned by |Crash|.
187 CrashType* crash_type = nullptr;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700188};
189
Josh Gaocbe70cb2016-10-18 18:17:52 -0700190// Forward declare the callbacks so they can be placed in a sensible order.
191static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, void*);
192static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg);
193static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg);
194
Josh Gao807a4582017-03-30 14:51:55 -0700195static void perform_request(Crash* crash) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700196 unique_fd output_fd;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100197 // Note that java traces are not interceptible.
198 if ((crash->crash_type == CrashType::java_trace) ||
199 !intercept_manager->GetIntercept(crash->crash_pid, &output_fd)) {
200 output_fd = crash->crash_type->get_output_fd();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700201 }
202
203 TombstonedCrashPacket response = {
204 .packet_type = CrashPacketType::kPerformDump
205 };
206 ssize_t rc = send_fd(crash->crash_fd, &response, sizeof(response), std::move(output_fd));
207 if (rc == -1) {
208 PLOG(WARNING) << "failed to send response to CrashRequest";
209 goto fail;
210 } else if (rc != sizeof(response)) {
211 PLOG(WARNING) << "crash socket write returned short";
212 goto fail;
213 } else {
214 // TODO: Make this configurable by the interceptor?
215 struct timeval timeout = { 10, 0 };
216
217 event_base* base = event_get_base(crash->crash_event);
218 event_assign(crash->crash_event, base, crash->crash_fd, EV_TIMEOUT | EV_READ,
219 crash_completed_cb, crash);
220 event_add(crash->crash_event, &timeout);
221 }
Josh Gao13078242017-03-30 14:42:46 -0700222
Narayan Kamath922f6b22017-05-15 15:59:30 +0100223 crash->crash_type->on_crash_started();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700224 return;
225
226fail:
227 delete crash;
228}
229
230static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int,
Narayan Kamath922f6b22017-05-15 15:59:30 +0100231 void* crash_type) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700232 event_base* base = evconnlistener_get_base(listener);
233 Crash* crash = new Crash();
234
235 struct timeval timeout = { 1, 0 };
236 event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash);
237 crash->crash_fd.reset(sockfd);
238 crash->crash_event = crash_event;
Narayan Kamath922f6b22017-05-15 15:59:30 +0100239 crash->crash_type = static_cast<CrashType*>(crash_type);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700240 event_add(crash_event, &timeout);
241}
242
243static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg) {
244 ssize_t rc;
245 Crash* crash = static_cast<Crash*>(arg);
Narayan Kamath922f6b22017-05-15 15:59:30 +0100246 CrashType* type = crash->crash_type;
247
Josh Gaocbe70cb2016-10-18 18:17:52 -0700248 TombstonedCrashPacket request = {};
249
250 if ((ev & EV_TIMEOUT) != 0) {
251 LOG(WARNING) << "crash request timed out";
252 goto fail;
253 } else if ((ev & EV_READ) == 0) {
254 LOG(WARNING) << "tombstoned received unexpected event from crash socket";
255 goto fail;
256 }
257
258 rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
259 if (rc == -1) {
260 PLOG(WARNING) << "failed to read from crash socket";
261 goto fail;
262 } else if (rc != sizeof(request)) {
263 LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
264 << sizeof(request) << ")";
265 goto fail;
266 }
267
268 if (request.packet_type != CrashPacketType::kDumpRequest) {
269 LOG(WARNING) << "unexpected crash packet type, expected kDumpRequest, received "
270 << StringPrintf("%#2hhX", request.packet_type);
271 goto fail;
272 }
273
Narayan Kamath922f6b22017-05-15 15:59:30 +0100274 if (type == CrashType::tombstone) {
275 crash->crash_pid = request.packet.dump_request.pid;
276 } else {
277 // Requests for java traces are sent from untrusted processes, so we
278 // must not trust the PID sent down with the request. Instead, we ask the
279 // kernel.
280 ucred cr = {};
281 socklen_t len = sizeof(cr);
282 int ret = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
283 if (ret != 0) {
284 PLOG(ERROR) << "Failed to getsockopt(..SO_PEERCRED)";
285 goto fail;
286 }
287
288 crash->crash_pid = cr.pid;
289 }
290
Josh Gaocbe70cb2016-10-18 18:17:52 -0700291 LOG(INFO) << "received crash request for pid " << crash->crash_pid;
292
Narayan Kamath922f6b22017-05-15 15:59:30 +0100293 if (type->maybe_enqueue_crash(crash)) {
Josh Gaocbe70cb2016-10-18 18:17:52 -0700294 LOG(INFO) << "enqueueing crash request for pid " << crash->crash_pid;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700295 } else {
Josh Gao807a4582017-03-30 14:51:55 -0700296 perform_request(crash);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700297 }
298
299 return;
300
301fail:
302 delete crash;
303}
304
305static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) {
306 ssize_t rc;
307 Crash* crash = static_cast<Crash*>(arg);
308 TombstonedCrashPacket request = {};
309
Narayan Kamath922f6b22017-05-15 15:59:30 +0100310 crash->crash_type->on_crash_completed();
Josh Gaocbe70cb2016-10-18 18:17:52 -0700311
312 if ((ev & EV_READ) == 0) {
313 goto fail;
314 }
315
316 rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
317 if (rc == -1) {
318 PLOG(WARNING) << "failed to read from crash socket";
319 goto fail;
320 } else if (rc != sizeof(request)) {
321 LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
322 << sizeof(request) << ")";
323 goto fail;
324 }
325
326 if (request.packet_type != CrashPacketType::kCompletedDump) {
327 LOG(WARNING) << "unexpected crash packet type, expected kCompletedDump, received "
328 << uint32_t(request.packet_type);
329 goto fail;
330 }
331
332fail:
Narayan Kamath922f6b22017-05-15 15:59:30 +0100333 CrashType* type = crash->crash_type;
Josh Gaocbe70cb2016-10-18 18:17:52 -0700334 delete crash;
335
336 // If there's something queued up, let them proceed.
Narayan Kamath922f6b22017-05-15 15:59:30 +0100337 type->maybe_dequeue_crashes(perform_request);
Josh Gaocbe70cb2016-10-18 18:17:52 -0700338}
339
340int main(int, char* []) {
Josh Gao8830c952017-03-06 12:23:55 -0800341 umask(0137);
342
Josh Gao55f79a52017-03-06 12:24:07 -0800343 // Don't try to connect to ourselves if we crash.
344 struct sigaction action = {};
345 action.sa_handler = [](int signal) {
346 LOG(ERROR) << "received fatal signal " << signal;
347 _exit(1);
348 };
349 debuggerd_register_handlers(&action);
350
Josh Gaocbe70cb2016-10-18 18:17:52 -0700351 int intercept_socket = android_get_control_socket(kTombstonedInterceptSocketName);
352 int crash_socket = android_get_control_socket(kTombstonedCrashSocketName);
353
354 if (intercept_socket == -1 || crash_socket == -1) {
355 PLOG(FATAL) << "failed to get socket from init";
356 }
357
358 evutil_make_socket_nonblocking(intercept_socket);
359 evutil_make_socket_nonblocking(crash_socket);
360
361 event_base* base = event_base_new();
362 if (!base) {
363 LOG(FATAL) << "failed to create event_base";
364 }
365
366 intercept_manager = new InterceptManager(base, intercept_socket);
367
Narayan Kamath922f6b22017-05-15 15:59:30 +0100368 evconnlistener* tombstone_listener = evconnlistener_new(
369 base, crash_accept_cb, CrashType::tombstone, -1, LEV_OPT_CLOSE_ON_FREE, crash_socket);
370 if (!tombstone_listener) {
371 LOG(FATAL) << "failed to create evconnlistener for tombstones.";
372 }
373
374 if (kJavaTraceDumpsEnabled) {
375 const int java_trace_socket = android_get_control_socket(kTombstonedJavaTraceSocketName);
376 if (java_trace_socket == -1) {
377 PLOG(FATAL) << "failed to get socket from init";
378 }
379
380 evutil_make_socket_nonblocking(java_trace_socket);
381 evconnlistener* java_trace_listener = evconnlistener_new(
382 base, crash_accept_cb, CrashType::java_trace, -1, LEV_OPT_CLOSE_ON_FREE, java_trace_socket);
383 if (!java_trace_listener) {
384 LOG(FATAL) << "failed to create evconnlistener for java traces.";
385 }
Josh Gaocbe70cb2016-10-18 18:17:52 -0700386 }
387
388 LOG(INFO) << "tombstoned successfully initialized";
389 event_base_dispatch(base);
390}