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