Shikha Panwar | abde59e | 2023-02-03 15:05:18 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2023, 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 "tombstoned/tombstoned.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | #include <android-base/properties.h> |
| 23 | #include <android-base/unique_fd.h> |
| 24 | #include <cutils/sockets.h> |
| 25 | #include <linux/vm_sockets.h> |
| 26 | #include "util.h" |
| 27 | |
| 28 | using android::base::unique_fd; |
| 29 | |
| 30 | /* |
| 31 | Port number that VirtualMachineService listens on connections from the guest VMs. |
| 32 | Kep in sync with IVirtualMachineService.aidl |
| 33 | */ |
| 34 | const unsigned int VM_TOMBSTONES_SERVICE_PORT = 2000; |
| 35 | |
| 36 | static bool is_microdroid() { |
| 37 | return android::base::GetProperty("ro.hardware", "") == "microdroid"; |
| 38 | } |
| 39 | |
| 40 | static bool connect_tombstone_server_microdroid(unique_fd* text_output_fd, |
| 41 | unique_fd* proto_output_fd, |
| 42 | DebuggerdDumpType dump_type) { |
| 43 | // We do not wait for the property to be set, the default behaviour is not export tombstones. |
| 44 | if (!android::base::GetBoolProperty("microdroid_manager.export_tombstones.enabled", false)) { |
| 45 | LOG(WARNING) << "exporting tombstones is not enabled"; |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | // Microdroid supports handling requests originating from crash_dump which |
| 50 | // supports limited dump types. Java traces and incept management are not supported. |
| 51 | switch (dump_type) { |
| 52 | case kDebuggerdNativeBacktrace: |
| 53 | case kDebuggerdTombstone: |
| 54 | case kDebuggerdTombstoneProto: |
| 55 | break; |
| 56 | |
| 57 | default: |
| 58 | LOG(WARNING) << "Requested dump type: " << dump_type << " " |
| 59 | << "not supported"; |
| 60 | } |
| 61 | |
| 62 | int fd1 = TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)); |
| 63 | int fd2 = TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM, 0)); |
| 64 | if (fd1 < 0 || fd2 < 0) { |
| 65 | LOG(WARNING) << "Unable to create virtual socket for writing tombstones"; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | unique_fd vsock_output_fd(fd1), vsock_proto_fd(fd2); |
| 70 | |
| 71 | struct sockaddr_vm sa = (struct sockaddr_vm){ |
| 72 | .svm_family = AF_VSOCK, |
| 73 | .svm_port = VM_TOMBSTONES_SERVICE_PORT, |
| 74 | .svm_cid = VMADDR_CID_HOST, |
| 75 | }; |
| 76 | |
| 77 | if (TEMP_FAILURE_RETRY(connect(vsock_output_fd, (struct sockaddr*)&sa, sizeof(sa))) < 0) { |
| 78 | PLOG(WARNING) << "Unable to connect to tombstone service in host"; |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (dump_type == kDebuggerdTombstoneProto) { |
| 83 | if (TEMP_FAILURE_RETRY(connect(vsock_proto_fd, (struct sockaddr*)&sa, sizeof(sa))) < 0) { |
| 84 | PLOG(WARNING) << "Unable to connect to tombstone service in host"; |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | *text_output_fd = std::move(vsock_output_fd); |
| 90 | if (proto_output_fd) { |
| 91 | *proto_output_fd = std::move(vsock_proto_fd); |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | static bool notify_completion_microdroid(int vsock_out, int vsock_proto) { |
| 97 | if (shutdown(vsock_out, SHUT_WR) || shutdown(vsock_proto, SHUT_WR)) return false; |
| 98 | return true; |
| 99 | } |
| 100 | bool connect_tombstone_server(pid_t pid, unique_fd* tombstoned_socket, unique_fd* text_output_fd, |
| 101 | unique_fd* proto_output_fd, DebuggerdDumpType dump_type) { |
| 102 | if (is_microdroid()) { |
| 103 | return connect_tombstone_server_microdroid(text_output_fd, proto_output_fd, dump_type); |
| 104 | } |
| 105 | return tombstoned_connect(pid, tombstoned_socket, text_output_fd, proto_output_fd, dump_type); |
| 106 | } |
| 107 | |
| 108 | bool notify_completion(int tombstoned_socket, int vsock_out, int vsock_proto) { |
| 109 | if (is_microdroid()) { |
| 110 | return notify_completion_microdroid(vsock_out, vsock_proto); |
| 111 | } |
| 112 | return tombstoned_notify_completion(tombstoned_socket); |
| 113 | } |