blob: 4741fa6b1be6b2928ce31f747f5552574a9fc81d [file] [log] [blame]
Josh Gaoe1aa0ca2017-03-01 17:23:22 -08001/*
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>
Christopher Ferrisac225782017-04-25 11:23:10 -070025#include <async_safe/log.h>
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080026#include <cutils/sockets.h>
27
28#include "debuggerd/protocol.h"
29#include "debuggerd/util.h"
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080030
31using android::base::unique_fd;
32
33bool 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) {
Christopher Ferrisac225782017-04-25 11:23:10 -070037 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to connect to tombstoned: %s",
38 strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080039 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)) {
Christopher Ferrisac225782017-04-25 11:23:10 -070046 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to write DumpRequest packet: %s",
47 strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080048 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) {
Christopher Ferrisac225782017-04-25 11:23:10 -070054 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
55 "failed to read response to DumpRequest packet: %s", strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080056 return false;
57 } else if (rc != sizeof(packet)) {
Christopher Ferrisac225782017-04-25 11:23:10 -070058 async_safe_format_log(
59 ANDROID_LOG_ERROR, "libc",
60 "received DumpRequest response packet of incorrect length (expected %zu, got %zd)",
61 sizeof(packet), rc);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080062 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) {
Christopher Ferrisac225782017-04-25 11:23:10 -070070 async_safe_format_log(ANDROID_LOG_WARN, "libc", "failed to set output fd flags: %s",
71 strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080072 }
73
74 *tombstoned_socket = std::move(sockfd);
75 *output_fd = std::move(tmp_output_fd);
76 return true;
77}
78
79bool 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}