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