blob: e878b6a4955c5d204c569f9d1310b7f2f8f2bb16 [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
Narayan Kamath922f6b22017-05-15 15:59:30 +010033bool tombstoned_connect(pid_t pid, unique_fd* tombstoned_socket, unique_fd* output_fd,
34 bool is_native_crash) {
35 unique_fd sockfd(socket_local_client(
36 (is_native_crash ? kTombstonedCrashSocketName : kTombstonedJavaTraceSocketName),
37 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_SEQPACKET));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080038 if (sockfd == -1) {
Christopher Ferrisac225782017-04-25 11:23:10 -070039 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to connect to tombstoned: %s",
40 strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080041 return false;
42 }
43
44 TombstonedCrashPacket packet = {};
45 packet.packet_type = CrashPacketType::kDumpRequest;
46 packet.packet.dump_request.pid = pid;
47 if (TEMP_FAILURE_RETRY(write(sockfd, &packet, sizeof(packet))) != sizeof(packet)) {
Christopher Ferrisac225782017-04-25 11:23:10 -070048 async_safe_format_log(ANDROID_LOG_ERROR, "libc", "failed to write DumpRequest packet: %s",
49 strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080050 return false;
51 }
52
53 unique_fd tmp_output_fd;
54 ssize_t rc = recv_fd(sockfd, &packet, sizeof(packet), &tmp_output_fd);
55 if (rc == -1) {
Christopher Ferrisac225782017-04-25 11:23:10 -070056 async_safe_format_log(ANDROID_LOG_ERROR, "libc",
57 "failed to read response to DumpRequest packet: %s", strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080058 return false;
59 } else if (rc != sizeof(packet)) {
Christopher Ferrisac225782017-04-25 11:23:10 -070060 async_safe_format_log(
61 ANDROID_LOG_ERROR, "libc",
62 "received DumpRequest response packet of incorrect length (expected %zu, got %zd)",
63 sizeof(packet), rc);
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080064 return false;
65 }
66
67 // Make the fd O_APPEND so that our output is guaranteed to be at the end of a file.
68 // (This also makes selinux rules consistent, because selinux distinguishes between writing to
69 // a regular fd, and writing to an fd with O_APPEND).
70 int flags = fcntl(tmp_output_fd.get(), F_GETFL);
71 if (fcntl(tmp_output_fd.get(), F_SETFL, flags | O_APPEND) != 0) {
Christopher Ferrisac225782017-04-25 11:23:10 -070072 async_safe_format_log(ANDROID_LOG_WARN, "libc", "failed to set output fd flags: %s",
73 strerror(errno));
Josh Gaoe1aa0ca2017-03-01 17:23:22 -080074 }
75
76 *tombstoned_socket = std::move(sockfd);
77 *output_fd = std::move(tmp_output_fd);
78 return true;
79}
80
81bool tombstoned_notify_completion(int tombstoned_socket) {
82 TombstonedCrashPacket packet = {};
83 packet.packet_type = CrashPacketType::kCompletedDump;
84 if (TEMP_FAILURE_RETRY(write(tombstoned_socket, &packet, sizeof(packet))) != sizeof(packet)) {
85 return false;
86 }
87 return true;
88}