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