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