blob: 63e3dbd0760f9dc919cf184eecb5a38ebd1a7807 [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
37#include "debuggerd/protocol.h"
38#include "debuggerd/util.h"
39
40#include "intercept_manager.h"
41
42using android::base::StringPrintf;
43using android::base::unique_fd;
44
45static InterceptManager* intercept_manager;
46
47enum CrashStatus {
48 kCrashStatusRunning,
49 kCrashStatusQueued,
50};
51
52// Ownership of Crash is a bit messy.
53// It's either owned by an active event that must have a timeout, or owned by
54// queued_requests, in the case that multiple crashes come in at the same time.
55struct Crash {
56 ~Crash() {
57 event_free(crash_event);
58 }
59
60 unique_fd crash_fd;
61 pid_t crash_pid;
62 event* crash_event = nullptr;
63};
64
65static constexpr char kTombstoneDirectory[] = "/data/tombstones/";
66static constexpr size_t kTombstoneCount = 10;
67static int tombstone_directory_fd = -1;
68static int next_tombstone = 0;
69
70static constexpr size_t kMaxConcurrentDumps = 1;
71static size_t num_concurrent_dumps = 0;
72
73static std::deque<Crash*> queued_requests;
74
75// Forward declare the callbacks so they can be placed in a sensible order.
76static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, void*);
77static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg);
78static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg);
79
80static void find_oldest_tombstone() {
81 size_t oldest_tombstone = 0;
82 time_t oldest_time = std::numeric_limits<time_t>::max();
83
84 for (size_t i = 0; i < kTombstoneCount; ++i) {
85 std::string path = android::base::StringPrintf("%stombstone_%02zu", kTombstoneDirectory, i);
86 struct stat st;
87 if (stat(path.c_str(), &st) != 0) {
88 PLOG(ERROR) << "failed to stat " << path;
89 }
90
91 if (st.st_mtime < oldest_time) {
92 oldest_tombstone = i;
93 oldest_time = st.st_mtime;
94 }
95 }
96
97 next_tombstone = oldest_tombstone;
98}
99
100static unique_fd get_tombstone_fd() {
101 // If kMaxConcurrentDumps is greater than 1, then theoretically the same
102 // filename could be handed out to multiple processes. Unlink and create the
103 // file, instead of using O_TRUNC, to avoid two processes interleaving their
104 // output.
105 unique_fd result;
106 char buf[PATH_MAX];
107 snprintf(buf, sizeof(buf), "tombstone_%02d", next_tombstone);
108 if (unlinkat(tombstone_directory_fd, buf, 0) != 0 && errno != ENOENT) {
109 PLOG(FATAL) << "failed to unlink tombstone at " << kTombstoneDirectory << buf;
110 }
111
112 result.reset(
George Burgess IV7008c842017-01-19 13:33:52 -0800113 openat(tombstone_directory_fd, buf, O_CREAT | O_EXCL | O_WRONLY | O_APPEND | O_CLOEXEC, 0700));
Josh Gaocbe70cb2016-10-18 18:17:52 -0700114 if (result == -1) {
115 PLOG(FATAL) << "failed to create tombstone at " << kTombstoneDirectory << buf;
116 }
117
118 next_tombstone = (next_tombstone + 1) % kTombstoneCount;
119 return result;
120}
121
122static void dequeue_request(Crash* crash) {
123 ++num_concurrent_dumps;
124
125 unique_fd output_fd;
126 if (!intercept_manager->GetIntercept(crash->crash_pid, &output_fd)) {
127 output_fd = get_tombstone_fd();
128 }
129
130 TombstonedCrashPacket response = {
131 .packet_type = CrashPacketType::kPerformDump
132 };
133 ssize_t rc = send_fd(crash->crash_fd, &response, sizeof(response), std::move(output_fd));
134 if (rc == -1) {
135 PLOG(WARNING) << "failed to send response to CrashRequest";
136 goto fail;
137 } else if (rc != sizeof(response)) {
138 PLOG(WARNING) << "crash socket write returned short";
139 goto fail;
140 } else {
141 // TODO: Make this configurable by the interceptor?
142 struct timeval timeout = { 10, 0 };
143
144 event_base* base = event_get_base(crash->crash_event);
145 event_assign(crash->crash_event, base, crash->crash_fd, EV_TIMEOUT | EV_READ,
146 crash_completed_cb, crash);
147 event_add(crash->crash_event, &timeout);
148 }
149 return;
150
151fail:
152 delete crash;
153}
154
155static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int,
156 void*) {
157 event_base* base = evconnlistener_get_base(listener);
158 Crash* crash = new Crash();
159
160 struct timeval timeout = { 1, 0 };
161 event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash);
162 crash->crash_fd.reset(sockfd);
163 crash->crash_event = crash_event;
164 event_add(crash_event, &timeout);
165}
166
167static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg) {
168 ssize_t rc;
169 Crash* crash = static_cast<Crash*>(arg);
170 TombstonedCrashPacket request = {};
171
172 if ((ev & EV_TIMEOUT) != 0) {
173 LOG(WARNING) << "crash request timed out";
174 goto fail;
175 } else if ((ev & EV_READ) == 0) {
176 LOG(WARNING) << "tombstoned received unexpected event from crash socket";
177 goto fail;
178 }
179
180 rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
181 if (rc == -1) {
182 PLOG(WARNING) << "failed to read from crash socket";
183 goto fail;
184 } else if (rc != sizeof(request)) {
185 LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
186 << sizeof(request) << ")";
187 goto fail;
188 }
189
190 if (request.packet_type != CrashPacketType::kDumpRequest) {
191 LOG(WARNING) << "unexpected crash packet type, expected kDumpRequest, received "
192 << StringPrintf("%#2hhX", request.packet_type);
193 goto fail;
194 }
195
196 crash->crash_pid = request.packet.dump_request.pid;
197 LOG(INFO) << "received crash request for pid " << crash->crash_pid;
198
199 if (num_concurrent_dumps == kMaxConcurrentDumps) {
200 LOG(INFO) << "enqueueing crash request for pid " << crash->crash_pid;
201 queued_requests.push_back(crash);
202 } else {
203 dequeue_request(crash);
204 }
205
206 return;
207
208fail:
209 delete crash;
210}
211
212static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) {
213 ssize_t rc;
214 Crash* crash = static_cast<Crash*>(arg);
215 TombstonedCrashPacket request = {};
216
217 --num_concurrent_dumps;
218
219 if ((ev & EV_READ) == 0) {
220 goto fail;
221 }
222
223 rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request)));
224 if (rc == -1) {
225 PLOG(WARNING) << "failed to read from crash socket";
226 goto fail;
227 } else if (rc != sizeof(request)) {
228 LOG(WARNING) << "crash socket received short read of length " << rc << " (expected "
229 << sizeof(request) << ")";
230 goto fail;
231 }
232
233 if (request.packet_type != CrashPacketType::kCompletedDump) {
234 LOG(WARNING) << "unexpected crash packet type, expected kCompletedDump, received "
235 << uint32_t(request.packet_type);
236 goto fail;
237 }
238
239fail:
240 delete crash;
241
242 // If there's something queued up, let them proceed.
243 if (!queued_requests.empty()) {
244 Crash* next_crash = queued_requests.front();
245 queued_requests.pop_front();
246 dequeue_request(next_crash);
247 }
248}
249
250int main(int, char* []) {
251 tombstone_directory_fd = open(kTombstoneDirectory, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
252 if (tombstone_directory_fd == -1) {
253 PLOG(FATAL) << "failed to open tombstone directory";
254 }
255
256 find_oldest_tombstone();
257
258 int intercept_socket = android_get_control_socket(kTombstonedInterceptSocketName);
259 int crash_socket = android_get_control_socket(kTombstonedCrashSocketName);
260
261 if (intercept_socket == -1 || crash_socket == -1) {
262 PLOG(FATAL) << "failed to get socket from init";
263 }
264
265 evutil_make_socket_nonblocking(intercept_socket);
266 evutil_make_socket_nonblocking(crash_socket);
267
268 event_base* base = event_base_new();
269 if (!base) {
270 LOG(FATAL) << "failed to create event_base";
271 }
272
273 intercept_manager = new InterceptManager(base, intercept_socket);
274
275 evconnlistener* listener =
276 evconnlistener_new(base, crash_accept_cb, nullptr, -1, LEV_OPT_CLOSE_ON_FREE, crash_socket);
277 if (!listener) {
278 LOG(FATAL) << "failed to create evconnlistener";
279 }
280
281 LOG(INFO) << "tombstoned successfully initialized";
282 event_base_dispatch(base);
283}