| 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> | 
| Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 26 | #include <string> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 27 | #include <unordered_map> | 
| Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 28 | #include <utility> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | #include <event2/event.h> | 
|  | 31 | #include <event2/listener.h> | 
|  | 32 | #include <event2/thread.h> | 
|  | 33 |  | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 34 | #include <android-base/cmsg.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> | 
|  | 38 | #include <android-base/unique_fd.h> | 
|  | 39 | #include <cutils/sockets.h> | 
|  | 40 |  | 
| Josh Gao | 55f79a5 | 2017-03-06 12:24:07 -0800 | [diff] [blame] | 41 | #include "debuggerd/handler.h" | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 42 | #include "dump_type.h" | 
| Narayan Kamath | 2d377cd | 2017-05-10 10:58:59 +0100 | [diff] [blame] | 43 | #include "protocol.h" | 
|  | 44 | #include "util.h" | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | #include "intercept_manager.h" | 
|  | 47 |  | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 48 | using android::base::GetIntProperty; | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 49 | using android::base::SendFileDescriptors; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 50 | using android::base::StringPrintf; | 
|  | 51 | using android::base::unique_fd; | 
|  | 52 |  | 
|  | 53 | static InterceptManager* intercept_manager; | 
|  | 54 |  | 
|  | 55 | enum CrashStatus { | 
|  | 56 | kCrashStatusRunning, | 
|  | 57 | kCrashStatusQueued, | 
|  | 58 | }; | 
|  | 59 |  | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 60 | // Ownership of Crash is a bit messy. | 
|  | 61 | // It's either owned by an active event that must have a timeout, or owned by | 
|  | 62 | // queued_requests, in the case that multiple crashes come in at the same time. | 
|  | 63 | struct Crash { | 
|  | 64 | ~Crash() { event_free(crash_event); } | 
|  | 65 |  | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 66 | std::string crash_tombstone_path; | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 67 | unique_fd crash_tombstone_fd; | 
|  | 68 | unique_fd crash_socket_fd; | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 69 | pid_t crash_pid; | 
|  | 70 | event* crash_event = nullptr; | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 71 |  | 
|  | 72 | DebuggerdDumpType crash_type; | 
|  | 73 | }; | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 74 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 75 | class CrashQueue { | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 76 | public: | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 77 | CrashQueue(const std::string& dir_path, const std::string& file_name_prefix, size_t max_artifacts, | 
|  | 78 | size_t max_concurrent_dumps) | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 79 | : file_name_prefix_(file_name_prefix), | 
|  | 80 | dir_path_(dir_path), | 
|  | 81 | dir_fd_(open(dir_path.c_str(), O_DIRECTORY | O_RDONLY | O_CLOEXEC)), | 
|  | 82 | max_artifacts_(max_artifacts), | 
|  | 83 | next_artifact_(0), | 
|  | 84 | max_concurrent_dumps_(max_concurrent_dumps), | 
|  | 85 | num_concurrent_dumps_(0) { | 
|  | 86 | if (dir_fd_ == -1) { | 
|  | 87 | PLOG(FATAL) << "failed to open directory: " << dir_path; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | // NOTE: If max_artifacts_ <= max_concurrent_dumps_, then theoretically the | 
|  | 91 | // same filename could be handed out to multiple processes. | 
|  | 92 | CHECK(max_artifacts_ > max_concurrent_dumps_); | 
|  | 93 |  | 
|  | 94 | find_oldest_artifact(); | 
|  | 95 | } | 
|  | 96 |  | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 97 | static CrashQueue* for_crash(const Crash* crash) { | 
|  | 98 | return (crash->crash_type == kDebuggerdJavaBacktrace) ? for_anrs() : for_tombstones(); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static CrashQueue* for_tombstones() { | 
|  | 102 | static CrashQueue queue("/data/tombstones", "tombstone_" /* file_name_prefix */, | 
| Elliott Hughes | ec220cd | 2019-09-26 14:35:24 -0700 | [diff] [blame] | 103 | GetIntProperty("tombstoned.max_tombstone_count", 32), | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 104 | 1 /* max_concurrent_dumps */); | 
|  | 105 | return &queue; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | static CrashQueue* for_anrs() { | 
|  | 109 | static CrashQueue queue("/data/anr", "trace_" /* file_name_prefix */, | 
|  | 110 | GetIntProperty("tombstoned.max_anr_count", 64), | 
|  | 111 | 4 /* max_concurrent_dumps */); | 
|  | 112 | return &queue; | 
|  | 113 | } | 
|  | 114 |  | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 115 | std::pair<std::string, unique_fd> get_output() { | 
|  | 116 | std::string path; | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 117 | unique_fd result(openat(dir_fd_, ".", O_WRONLY | O_APPEND | O_TMPFILE | O_CLOEXEC, 0640)); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 118 | if (result == -1) { | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 119 | // We might not have O_TMPFILE. Try creating with an arbitrary filename instead. | 
|  | 120 | static size_t counter = 0; | 
|  | 121 | std::string tmp_filename = StringPrintf(".temporary%zu", counter++); | 
|  | 122 | result.reset(openat(dir_fd_, tmp_filename.c_str(), | 
|  | 123 | O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_CLOEXEC, 0640)); | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 124 | if (result == -1) { | 
|  | 125 | PLOG(FATAL) << "failed to create temporary tombstone in " << dir_path_; | 
|  | 126 | } | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 127 |  | 
|  | 128 | path = StringPrintf("%s/%s", dir_path_.c_str(), tmp_filename.c_str()); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 129 | } | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 130 | return std::make_pair(std::move(path), std::move(result)); | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 131 | } | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 132 |  | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 133 | std::string get_next_artifact_path() { | 
|  | 134 | std::string file_name = | 
|  | 135 | StringPrintf("%s/%s%02d", dir_path_.c_str(), file_name_prefix_.c_str(), next_artifact_); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 136 | next_artifact_ = (next_artifact_ + 1) % max_artifacts_; | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 137 | return file_name; | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 138 | } | 
|  | 139 |  | 
|  | 140 | bool maybe_enqueue_crash(Crash* crash) { | 
|  | 141 | if (num_concurrent_dumps_ == max_concurrent_dumps_) { | 
|  | 142 | queued_requests_.push_back(crash); | 
|  | 143 | return true; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | return false; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | void maybe_dequeue_crashes(void (*handler)(Crash* crash)) { | 
|  | 150 | while (!queued_requests_.empty() && num_concurrent_dumps_ < max_concurrent_dumps_) { | 
|  | 151 | Crash* next_crash = queued_requests_.front(); | 
|  | 152 | queued_requests_.pop_front(); | 
|  | 153 | handler(next_crash); | 
|  | 154 | } | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | void on_crash_started() { ++num_concurrent_dumps_; } | 
|  | 158 |  | 
|  | 159 | void on_crash_completed() { --num_concurrent_dumps_; } | 
|  | 160 |  | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 161 | private: | 
|  | 162 | void find_oldest_artifact() { | 
|  | 163 | size_t oldest_tombstone = 0; | 
|  | 164 | time_t oldest_time = std::numeric_limits<time_t>::max(); | 
|  | 165 |  | 
|  | 166 | for (size_t i = 0; i < max_artifacts_; ++i) { | 
| Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 167 | std::string path = StringPrintf("%s/%s%02zu", dir_path_.c_str(), file_name_prefix_.c_str(), i); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 168 | struct stat st; | 
|  | 169 | if (stat(path.c_str(), &st) != 0) { | 
|  | 170 | if (errno == ENOENT) { | 
|  | 171 | oldest_tombstone = i; | 
|  | 172 | break; | 
|  | 173 | } else { | 
|  | 174 | PLOG(ERROR) << "failed to stat " << path; | 
|  | 175 | continue; | 
|  | 176 | } | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | if (st.st_mtime < oldest_time) { | 
|  | 180 | oldest_tombstone = i; | 
|  | 181 | oldest_time = st.st_mtime; | 
|  | 182 | } | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | next_artifact_ = oldest_tombstone; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | const std::string file_name_prefix_; | 
|  | 189 |  | 
|  | 190 | const std::string dir_path_; | 
|  | 191 | const int dir_fd_; | 
|  | 192 |  | 
|  | 193 | const size_t max_artifacts_; | 
|  | 194 | int next_artifact_; | 
|  | 195 |  | 
|  | 196 | const size_t max_concurrent_dumps_; | 
|  | 197 | size_t num_concurrent_dumps_; | 
|  | 198 |  | 
|  | 199 | std::deque<Crash*> queued_requests_; | 
|  | 200 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(CrashQueue); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 202 | }; | 
|  | 203 |  | 
|  | 204 | // Whether java trace dumps are produced via tombstoned. | 
| Narayan Kamath | ca5e908 | 2017-06-02 15:42:06 +0100 | [diff] [blame] | 205 | static constexpr bool kJavaTraceDumpsEnabled = true; | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 206 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 207 | // Forward declare the callbacks so they can be placed in a sensible order. | 
|  | 208 | static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, void*); | 
|  | 209 | static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg); | 
|  | 210 | static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg); | 
|  | 211 |  | 
| Josh Gao | 807a458 | 2017-03-30 14:51:55 -0700 | [diff] [blame] | 212 | static void perform_request(Crash* crash) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 213 | unique_fd output_fd; | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 214 | bool intercepted = | 
|  | 215 | intercept_manager->GetIntercept(crash->crash_pid, crash->crash_type, &output_fd); | 
|  | 216 | if (!intercepted) { | 
| Josh Gao | 2b22ae1 | 2018-09-12 14:51:03 -0700 | [diff] [blame] | 217 | if (crash->crash_type == kDebuggerdNativeBacktrace) { | 
|  | 218 | // Don't generate tombstones for native backtrace requests. | 
|  | 219 | output_fd.reset(open("/dev/null", O_WRONLY | O_CLOEXEC)); | 
|  | 220 | } else { | 
|  | 221 | std::tie(crash->crash_tombstone_path, output_fd) = CrashQueue::for_crash(crash)->get_output(); | 
|  | 222 | crash->crash_tombstone_fd.reset(dup(output_fd.get())); | 
|  | 223 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 224 | } | 
|  | 225 |  | 
|  | 226 | TombstonedCrashPacket response = { | 
|  | 227 | .packet_type = CrashPacketType::kPerformDump | 
|  | 228 | }; | 
| Josh Gao | 5f87bbd | 2019-01-09 17:01:49 -0800 | [diff] [blame] | 229 | ssize_t rc = | 
|  | 230 | SendFileDescriptors(crash->crash_socket_fd, &response, sizeof(response), output_fd.get()); | 
|  | 231 | output_fd.reset(); | 
|  | 232 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 233 | if (rc == -1) { | 
|  | 234 | PLOG(WARNING) << "failed to send response to CrashRequest"; | 
|  | 235 | goto fail; | 
|  | 236 | } else if (rc != sizeof(response)) { | 
|  | 237 | PLOG(WARNING) << "crash socket write returned short"; | 
|  | 238 | goto fail; | 
|  | 239 | } else { | 
|  | 240 | // TODO: Make this configurable by the interceptor? | 
|  | 241 | struct timeval timeout = { 10, 0 }; | 
|  | 242 |  | 
|  | 243 | event_base* base = event_get_base(crash->crash_event); | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 244 | event_assign(crash->crash_event, base, crash->crash_socket_fd, EV_TIMEOUT | EV_READ, | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 245 | crash_completed_cb, crash); | 
|  | 246 | event_add(crash->crash_event, &timeout); | 
|  | 247 | } | 
| Josh Gao | 1307824 | 2017-03-30 14:42:46 -0700 | [diff] [blame] | 248 |  | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 249 | CrashQueue::for_crash(crash)->on_crash_started(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 250 | return; | 
|  | 251 |  | 
|  | 252 | fail: | 
|  | 253 | delete crash; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | static void crash_accept_cb(evconnlistener* listener, evutil_socket_t sockfd, sockaddr*, int, | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 257 | void*) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 258 | event_base* base = evconnlistener_get_base(listener); | 
|  | 259 | Crash* crash = new Crash(); | 
|  | 260 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 261 | // TODO: Make sure that only java crashes come in on the java socket | 
|  | 262 | // and only native crashes on the native socket. | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 263 | struct timeval timeout = { 1, 0 }; | 
|  | 264 | event* crash_event = event_new(base, sockfd, EV_TIMEOUT | EV_READ, crash_request_cb, crash); | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 265 | crash->crash_socket_fd.reset(sockfd); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 266 | crash->crash_event = crash_event; | 
|  | 267 | event_add(crash_event, &timeout); | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | static void crash_request_cb(evutil_socket_t sockfd, short ev, void* arg) { | 
|  | 271 | ssize_t rc; | 
|  | 272 | Crash* crash = static_cast<Crash*>(arg); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 273 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 274 | TombstonedCrashPacket request = {}; | 
|  | 275 |  | 
|  | 276 | if ((ev & EV_TIMEOUT) != 0) { | 
|  | 277 | LOG(WARNING) << "crash request timed out"; | 
|  | 278 | goto fail; | 
|  | 279 | } else if ((ev & EV_READ) == 0) { | 
|  | 280 | LOG(WARNING) << "tombstoned received unexpected event from crash socket"; | 
|  | 281 | goto fail; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request))); | 
|  | 285 | if (rc == -1) { | 
|  | 286 | PLOG(WARNING) << "failed to read from crash socket"; | 
|  | 287 | goto fail; | 
|  | 288 | } else if (rc != sizeof(request)) { | 
|  | 289 | LOG(WARNING) << "crash socket received short read of length " << rc << " (expected " | 
|  | 290 | << sizeof(request) << ")"; | 
|  | 291 | goto fail; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | if (request.packet_type != CrashPacketType::kDumpRequest) { | 
|  | 295 | LOG(WARNING) << "unexpected crash packet type, expected kDumpRequest, received  " | 
|  | 296 | << StringPrintf("%#2hhX", request.packet_type); | 
|  | 297 | goto fail; | 
|  | 298 | } | 
|  | 299 |  | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 300 | crash->crash_type = request.packet.dump_request.dump_type; | 
|  | 301 | if (crash->crash_type < 0 || crash->crash_type > kDebuggerdAnyIntercept) { | 
|  | 302 | LOG(WARNING) << "unexpected crash dump type: " << crash->crash_type; | 
|  | 303 | goto fail; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | if (crash->crash_type != kDebuggerdJavaBacktrace) { | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 307 | crash->crash_pid = request.packet.dump_request.pid; | 
|  | 308 | } else { | 
|  | 309 | // Requests for java traces are sent from untrusted processes, so we | 
|  | 310 | // must not trust the PID sent down with the request. Instead, we ask the | 
|  | 311 | // kernel. | 
|  | 312 | ucred cr = {}; | 
|  | 313 | socklen_t len = sizeof(cr); | 
|  | 314 | int ret = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &len); | 
|  | 315 | if (ret != 0) { | 
|  | 316 | PLOG(ERROR) << "Failed to getsockopt(..SO_PEERCRED)"; | 
|  | 317 | goto fail; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | crash->crash_pid = cr.pid; | 
|  | 321 | } | 
|  | 322 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 323 | LOG(INFO) << "received crash request for pid " << crash->crash_pid; | 
|  | 324 |  | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 325 | if (CrashQueue::for_crash(crash)->maybe_enqueue_crash(crash)) { | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 326 | LOG(INFO) << "enqueueing crash request for pid " << crash->crash_pid; | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 327 | } else { | 
| Josh Gao | 807a458 | 2017-03-30 14:51:55 -0700 | [diff] [blame] | 328 | perform_request(crash); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 329 | } | 
|  | 330 |  | 
|  | 331 | return; | 
|  | 332 |  | 
|  | 333 | fail: | 
|  | 334 | delete crash; | 
|  | 335 | } | 
|  | 336 |  | 
|  | 337 | static void crash_completed_cb(evutil_socket_t sockfd, short ev, void* arg) { | 
|  | 338 | ssize_t rc; | 
|  | 339 | Crash* crash = static_cast<Crash*>(arg); | 
|  | 340 | TombstonedCrashPacket request = {}; | 
|  | 341 |  | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 342 | CrashQueue::for_crash(crash)->on_crash_completed(); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 343 |  | 
|  | 344 | if ((ev & EV_READ) == 0) { | 
|  | 345 | goto fail; | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | rc = TEMP_FAILURE_RETRY(read(sockfd, &request, sizeof(request))); | 
|  | 349 | if (rc == -1) { | 
|  | 350 | PLOG(WARNING) << "failed to read from crash socket"; | 
|  | 351 | goto fail; | 
|  | 352 | } else if (rc != sizeof(request)) { | 
|  | 353 | LOG(WARNING) << "crash socket received short read of length " << rc << " (expected " | 
|  | 354 | << sizeof(request) << ")"; | 
|  | 355 | goto fail; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | if (request.packet_type != CrashPacketType::kCompletedDump) { | 
|  | 359 | LOG(WARNING) << "unexpected crash packet type, expected kCompletedDump, received " | 
|  | 360 | << uint32_t(request.packet_type); | 
|  | 361 | goto fail; | 
|  | 362 | } | 
|  | 363 |  | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 364 | if (crash->crash_tombstone_fd != -1) { | 
|  | 365 | std::string fd_path = StringPrintf("/proc/self/fd/%d", crash->crash_tombstone_fd.get()); | 
|  | 366 | std::string tombstone_path = CrashQueue::for_crash(crash)->get_next_artifact_path(); | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 367 |  | 
|  | 368 | // linkat doesn't let us replace a file, so we need to unlink first. | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 369 | int rc = unlink(tombstone_path.c_str()); | 
| Josh Gao | 28f8cf0 | 2018-05-03 14:31:08 -0700 | [diff] [blame] | 370 | if (rc != 0 && errno != ENOENT) { | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 371 | PLOG(ERROR) << "failed to unlink tombstone at " << tombstone_path; | 
|  | 372 | goto fail; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | rc = linkat(AT_FDCWD, fd_path.c_str(), AT_FDCWD, tombstone_path.c_str(), AT_SYMLINK_FOLLOW); | 
|  | 376 | if (rc != 0) { | 
|  | 377 | PLOG(ERROR) << "failed to link tombstone"; | 
| Narayan Kamath | 79dd143 | 2017-06-21 19:42:00 +0100 | [diff] [blame] | 378 | } else { | 
| Josh Gao | 48383c8 | 2018-04-18 18:11:01 -0700 | [diff] [blame] | 379 | if (crash->crash_type == kDebuggerdJavaBacktrace) { | 
|  | 380 | LOG(ERROR) << "Traces for pid " << crash->crash_pid << " written to: " << tombstone_path; | 
|  | 381 | } else { | 
|  | 382 | // NOTE: Several tools parse this log message to figure out where the | 
|  | 383 | // tombstone associated with a given native crash was written. Any changes | 
|  | 384 | // to this message must be carefully considered. | 
|  | 385 | LOG(ERROR) << "Tombstone written to: " << tombstone_path; | 
|  | 386 | } | 
| Narayan Kamath | 79dd143 | 2017-06-21 19:42:00 +0100 | [diff] [blame] | 387 | } | 
| Josh Gao | f5974ae | 2018-05-03 16:05:32 -0700 | [diff] [blame] | 388 |  | 
|  | 389 | // If we don't have O_TMPFILE, we need to clean up after ourselves. | 
|  | 390 | if (!crash->crash_tombstone_path.empty()) { | 
|  | 391 | rc = unlink(crash->crash_tombstone_path.c_str()); | 
|  | 392 | if (rc != 0) { | 
|  | 393 | PLOG(ERROR) << "failed to unlink temporary tombstone at " << crash->crash_tombstone_path; | 
|  | 394 | } | 
|  | 395 | } | 
| Josh Gao | cb68a03 | 2017-06-02 13:02:10 -0700 | [diff] [blame] | 396 | } | 
|  | 397 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 398 | fail: | 
| Elliott Hughes | 35bb6d2 | 2017-06-26 13:54:05 -0700 | [diff] [blame] | 399 | CrashQueue* queue = CrashQueue::for_crash(crash); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 400 | delete crash; | 
|  | 401 |  | 
|  | 402 | // If there's something queued up, let them proceed. | 
| Narayan Kamath | a73df60 | 2017-05-24 15:07:25 +0100 | [diff] [blame] | 403 | queue->maybe_dequeue_crashes(perform_request); | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
|  | 406 | int main(int, char* []) { | 
| Josh Gao | 8830c95 | 2017-03-06 12:23:55 -0800 | [diff] [blame] | 407 | umask(0137); | 
|  | 408 |  | 
| Josh Gao | 55f79a5 | 2017-03-06 12:24:07 -0800 | [diff] [blame] | 409 | // Don't try to connect to ourselves if we crash. | 
|  | 410 | struct sigaction action = {}; | 
|  | 411 | action.sa_handler = [](int signal) { | 
|  | 412 | LOG(ERROR) << "received fatal signal " << signal; | 
|  | 413 | _exit(1); | 
|  | 414 | }; | 
|  | 415 | debuggerd_register_handlers(&action); | 
|  | 416 |  | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 417 | int intercept_socket = android_get_control_socket(kTombstonedInterceptSocketName); | 
|  | 418 | int crash_socket = android_get_control_socket(kTombstonedCrashSocketName); | 
|  | 419 |  | 
|  | 420 | if (intercept_socket == -1 || crash_socket == -1) { | 
|  | 421 | PLOG(FATAL) << "failed to get socket from init"; | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | evutil_make_socket_nonblocking(intercept_socket); | 
|  | 425 | evutil_make_socket_nonblocking(crash_socket); | 
|  | 426 |  | 
|  | 427 | event_base* base = event_base_new(); | 
|  | 428 | if (!base) { | 
|  | 429 | LOG(FATAL) << "failed to create event_base"; | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | intercept_manager = new InterceptManager(base, intercept_socket); | 
|  | 433 |  | 
| Narayan Kamath | c2e98f6 | 2017-09-13 13:12:34 +0100 | [diff] [blame] | 434 | evconnlistener* tombstone_listener = | 
|  | 435 | evconnlistener_new(base, crash_accept_cb, CrashQueue::for_tombstones(), LEV_OPT_CLOSE_ON_FREE, | 
|  | 436 | -1 /* backlog */, crash_socket); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 437 | if (!tombstone_listener) { | 
|  | 438 | LOG(FATAL) << "failed to create evconnlistener for tombstones."; | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | if (kJavaTraceDumpsEnabled) { | 
|  | 442 | const int java_trace_socket = android_get_control_socket(kTombstonedJavaTraceSocketName); | 
|  | 443 | if (java_trace_socket == -1) { | 
|  | 444 | PLOG(FATAL) << "failed to get socket from init"; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | evutil_make_socket_nonblocking(java_trace_socket); | 
| Narayan Kamath | c2e98f6 | 2017-09-13 13:12:34 +0100 | [diff] [blame] | 448 | evconnlistener* java_trace_listener = | 
|  | 449 | evconnlistener_new(base, crash_accept_cb, CrashQueue::for_anrs(), LEV_OPT_CLOSE_ON_FREE, | 
|  | 450 | -1 /* backlog */, java_trace_socket); | 
| Narayan Kamath | 922f6b2 | 2017-05-15 15:59:30 +0100 | [diff] [blame] | 451 | if (!java_trace_listener) { | 
|  | 452 | LOG(FATAL) << "failed to create evconnlistener for java traces."; | 
|  | 453 | } | 
| Josh Gao | cbe70cb | 2016-10-18 18:17:52 -0700 | [diff] [blame] | 454 | } | 
|  | 455 |  | 
|  | 456 | LOG(INFO) << "tombstoned successfully initialized"; | 
|  | 457 | event_base_dispatch(base); | 
|  | 458 | } |