1 /* 2 * Copyright 2017, 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 <fcntl.h> 20 #include <unistd.h> 21 22 #include <utility> 23 24 #include <android-base/unique_fd.h> 25 #include <async_safe/log.h> 26 #include <cutils/sockets.h> 27 28 #include "protocol.h" 29 #include "util.h" 30 31 using android::base::unique_fd; 32 33 bool tombstoned_connect(pid_t pid, unique_fd* tombstoned_socket, unique_fd* output_fd, 34 DebuggerdDumpType dump_type) { 35 unique_fd sockfd( 36 socket_local_client((dump_type != kDebuggerdJavaBacktrace ? kTombstonedCrashSocketName 37 : kTombstonedJavaTraceSocketName), 38 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET)); 39 if (sockfd == -1) { 40 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to connect to tombstoned: %s", 41 strerror(errno)); 42 return false; 43 } 44 45 TombstonedCrashPacket packet = {}; 46 packet.packet_type = CrashPacketType::kDumpRequest; 47 packet.packet.dump_request.pid = pid; 48 packet.packet.dump_request.dump_type = dump_type; 49 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) { 50 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to write DumpRequest packet: %s", 51 strerror(errno)); 52 return false; 53 } 54 55 unique_fd tmp_output_fd; 56 ssize_t rc = recv_fd(sockfd, &packet, sizeof(packet), &tmp_output_fd); 57 if (rc == -1) { 58 async_safe_format_log(ANDROID_LOG_ERROR, "libc", 59 "failed to read response to DumpRequest packet: %s", strerror(errno)); 60 return false; 61 } else if (rc != sizeof(packet)) { 62 async_safe_format_log( 63 ANDROID_LOG_ERROR, "libc", 64 "received DumpRequest response packet of incorrect length (expected %zu, got %zd)", 65 sizeof(packet), rc); 66 return false; 67 } 68 69 // Make the fd O_APPEND so that our output is guaranteed to be at the end of a file. 70 // (This also makes selinux rules consistent, because selinux distinguishes between writing to 71 // a regular fd, and writing to an fd with O_APPEND). 72 int flags = fcntl(tmp_output_fd.get(), F_GETFL); 73 if (fcntl(tmp_output_fd.get(), F_SETFL, flags | O_APPEND) != 0) { 74 async_safe_format_log(ANDROID_LOG_WARN, "libc", "failed to set output fd flags: %s", 75 strerror(errno)); 76 } 77 78 *tombstoned_socket = std::move(sockfd); 79 *output_fd = std::move(tmp_output_fd); 80 return true; 81 } 82 83 bool tombstoned_notify_completion(int tombstoned_socket) { 84 TombstonedCrashPacket packet = {}; 85 packet.packet_type = CrashPacketType::kCompletedDump; 86 if (TEMP_FAILURE_RETRY(write(tombstoned_socket, &packet, sizeof(packet))) != sizeof(packet)) { 87 return false; 88 } 89 return true; 90 } 91