blob: c78711376593ea59b8ce1e46d57baef71552f241 [file] [log] [blame]
Felipe Leme75876a22016-10-27 16:31:27 -07001/**
2 * Copyright (c) 2016, 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#define LOG_TAG "dumpstate"
18
19#include "DumpstateService.h"
20
Nandana Dutt2bbba942019-01-30 13:13:52 +000021#include <memory>
Felipe Leme75876a22016-10-27 16:31:27 -070022
Nandana Dutt2bbba942019-01-30 13:13:52 +000023#include <android-base/stringprintf.h>
Felipe Leme75876a22016-10-27 16:31:27 -070024#include "android/os/BnDumpstate.h"
25
Felipe Lemef0292972016-11-22 13:57:05 -080026#include "DumpstateInternal.h"
27
Nandana Dutt197661d2018-11-16 16:40:21 +000028using android::base::StringPrintf;
29
Felipe Leme75876a22016-10-27 16:31:27 -070030namespace android {
31namespace os {
32
Felipe Leme009ecbb2016-11-07 10:18:44 -080033namespace {
Nandana Dutt197661d2018-11-16 16:40:21 +000034
Nandana Duttd2f5f082019-01-18 17:13:52 +000035struct DumpstateInfo {
36 public:
37 Dumpstate* ds = nullptr;
38 int32_t calling_uid = -1;
39 std::string calling_package;
40};
41
Hunter Knepshielde30d8212020-12-21 18:08:17 -080042static binder::Status exception(uint32_t code, const std::string& msg,
43 const std::string& extra_msg = "") {
44 if (extra_msg.empty()) {
45 MYLOGE("%s (%d) ", msg.c_str(), code);
46 } else {
47 MYLOGE("%s %s (%d) ", msg.c_str(), extra_msg.c_str(), code);
48 }
Nandana Dutt197661d2018-11-16 16:40:21 +000049 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
50}
51
Nandana Dutt16d1aee2019-02-15 16:13:53 +000052// Creates a bugreport and exits, thus preserving the oneshot nature of the service.
53// Note: takes ownership of data.
Kean Mariotti306633e2022-09-05 16:30:47 +000054[[noreturn]] static void* dumpstate_thread_bugreport(void* data) {
Nandana Dutt2bbba942019-01-30 13:13:52 +000055 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
56 ds_info->ds->Run(ds_info->calling_uid, ds_info->calling_package);
Nandana Dutt16d1aee2019-02-15 16:13:53 +000057 MYLOGD("Finished taking a bugreport. Exiting.\n");
58 exit(0);
59}
60
Gavin Corkerya44686c2022-11-23 18:16:51 +000061[[noreturn]] static void* dumpstate_thread_retrieve(void* data) {
62 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
63 ds_info->ds->Retrieve(ds_info->calling_uid, ds_info->calling_package);
64 MYLOGD("Finished retrieving a bugreport. Exiting.\n");
65 exit(0);
66}
67
Nandana Dutt16d1aee2019-02-15 16:13:53 +000068[[noreturn]] static void signalErrorAndExit(sp<IDumpstateListener> listener, int error_code) {
69 listener->onError(error_code);
70 exit(0);
Nandana Dutt197661d2018-11-16 16:40:21 +000071}
72
Nandana Duttd2f5f082019-01-18 17:13:52 +000073} // namespace
Felipe Leme009ecbb2016-11-07 10:18:44 -080074
Hunter Knepshielde30d8212020-12-21 18:08:17 -080075DumpstateService::DumpstateService() : ds_(nullptr), calling_uid_(-1), calling_package_() {
Felipe Leme75876a22016-10-27 16:31:27 -070076}
77
78char const* DumpstateService::getServiceName() {
79 return "dumpstate";
80}
81
82status_t DumpstateService::Start() {
83 IPCThreadState::self()->disableBackgroundScheduling(true);
84 status_t ret = BinderService<DumpstateService>::publish();
85 if (ret != android::OK) {
86 return ret;
87 }
88 sp<ProcessState> ps(ProcessState::self());
89 ps->startThreadPool();
90 ps->giveThreadPoolName();
91 return android::OK;
92}
93
Kean Mariotti306633e2022-09-05 16:30:47 +000094binder::Status DumpstateService::preDumpUiData(const std::string&) {
95 std::lock_guard<std::mutex> lock(lock_);
96 MYLOGI("preDumpUiData()");
97
98 if (ds_ != nullptr) {
99 MYLOGE("Error! DumpstateService is currently already being used. Returning.");
100 return exception(binder::Status::EX_SERVICE_SPECIFIC,
101 "DumpstateService is already being used");
102 }
103
104 ds_ = &(Dumpstate::GetInstance());
105 ds_->PreDumpUiData();
106
107 return binder::Status::ok();
108}
109
Nandana Duttd2f5f082019-01-18 17:13:52 +0000110binder::Status DumpstateService::startBugreport(int32_t calling_uid,
111 const std::string& calling_package,
Jiyong Park3b25ae12019-11-25 11:06:16 +0900112 android::base::unique_fd bugreport_fd,
113 android::base::unique_fd screenshot_fd,
Nandana Dutt05290ad2019-01-04 15:48:09 +0000114 int bugreport_mode,
Kean Mariotti306633e2022-09-05 16:30:47 +0000115 int bugreport_flags,
Paul Changf59c2b72020-03-10 02:08:55 +0800116 const sp<IDumpstateListener>& listener,
117 bool is_screenshot_requested) {
Nandana Dutt197661d2018-11-16 16:40:21 +0000118 MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
119
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100120 // Ensure there is only one bugreport in progress at a time.
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000121 std::lock_guard<std::mutex> lock(lock_);
122 if (ds_ != nullptr) {
Kean Mariotti306633e2022-09-05 16:30:47 +0000123 MYLOGE("Error! DumpstateService is currently already being used. Returning.");
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000124 if (listener != nullptr) {
Nandana Dutt0eb86bf2019-02-21 16:10:10 +0000125 listener->onError(IDumpstateListener::BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000126 }
127 return exception(binder::Status::EX_SERVICE_SPECIFIC,
Kean Mariotti306633e2022-09-05 16:30:47 +0000128 "DumpstateService is already being used");
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000129 }
130
131 // From here on, all conditions that indicate we are done with this incoming request should
132 // result in exiting the service to free it up for next invocation.
133 if (listener == nullptr) {
134 MYLOGE("Invalid input: no listener");
135 exit(0);
136 }
137
Nandana Dutt197661d2018-11-16 16:40:21 +0000138 if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL &&
139 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE &&
140 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE &&
141 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR &&
142 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000143 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI &&
Elis Elliott8e401ad2023-08-08 11:18:59 +0000144 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_ONBOARDING &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000145 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000146 MYLOGE("Invalid input: bad bugreport mode: %d", bugreport_mode);
147 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
Nandana Dutt197661d2018-11-16 16:40:21 +0000148 }
149
Abhijeet Kaur1f3d8862019-03-28 11:04:43 +0000150 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
Kean Mariotti306633e2022-09-05 16:30:47 +0000151 options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_flags,
152 bugreport_fd, screenshot_fd, is_screenshot_requested);
Abhijeet Kaur1f3d8862019-03-28 11:04:43 +0000153
Paul Chang0d2aad72020-02-13 20:04:03 +0800154 if (bugreport_fd.get() == -1 || (options->do_screenshot && screenshot_fd.get() == -1)) {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000155 MYLOGE("Invalid filedescriptor");
156 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
Nandana Dutt54dbd672019-01-11 12:58:05 +0000157 }
158
Nandana Dutt54dbd672019-01-11 12:58:05 +0000159
Nandana Duttd9737662019-01-29 15:59:38 +0000160 ds_ = &(Dumpstate::GetInstance());
161 ds_->SetOptions(std::move(options));
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000162 ds_->listener_ = listener;
Nandana Dutt197661d2018-11-16 16:40:21 +0000163
Hunter Knepshielde30d8212020-12-21 18:08:17 -0800164 // Track caller info for cancellation purposes.
165 calling_uid_ = calling_uid;
166 calling_package_ = calling_package;
167
Nandana Dutt2bbba942019-01-30 13:13:52 +0000168 DumpstateInfo* ds_info = new DumpstateInfo();
169 ds_info->ds = ds_;
170 ds_info->calling_uid = calling_uid;
171 ds_info->calling_package = calling_package;
Nandana Duttd2f5f082019-01-18 17:13:52 +0000172
Nandana Dutt197661d2018-11-16 16:40:21 +0000173 pthread_t thread;
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000174 // Initialize dumpstate
175 ds_->Initialize();
Kean Mariotti306633e2022-09-05 16:30:47 +0000176 status_t err = pthread_create(&thread, nullptr, dumpstate_thread_bugreport, ds_info);
Nandana Dutt197661d2018-11-16 16:40:21 +0000177 if (err != 0) {
Nandana Dutt2bbba942019-01-30 13:13:52 +0000178 delete ds_info;
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000179 MYLOGE("Could not create a thread");
180 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
Nandana Dutt197661d2018-11-16 16:40:21 +0000181 }
Nandana Duttcccbb5f2018-09-27 09:23:36 +0100182 return binder::Status::ok();
183}
184
Hunter Knepshielde30d8212020-12-21 18:08:17 -0800185binder::Status DumpstateService::cancelBugreport(int32_t calling_uid,
186 const std::string& calling_package) {
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000187 std::lock_guard<std::mutex> lock(lock_);
Hunter Knepshielde30d8212020-12-21 18:08:17 -0800188 if (calling_uid != calling_uid_ || calling_package != calling_package_) {
189 // Note: we use a SecurityException to prevent BugreportManagerServiceImpl from killing the
190 // report in progress (from another caller).
191 return exception(
192 binder::Status::EX_SECURITY,
193 StringPrintf("Cancellation requested by %d/%s does not match report in "
194 "progress",
195 calling_uid, calling_package.c_str()),
196 // Sharing the owner of the BR is a (minor) leak, so leave it out of the app's exception
197 StringPrintf("started by %d/%s", calling_uid_, calling_package_.c_str()));
198 }
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000199 ds_->Cancel();
Nandana Duttcc4ead82019-01-23 08:29:23 +0000200 return binder::Status::ok();
201}
202
Gavin Corkerya44686c2022-11-23 18:16:51 +0000203binder::Status DumpstateService::retrieveBugreport(
204 int32_t calling_uid, const std::string& calling_package,
205 android::base::unique_fd bugreport_fd,
206 const std::string& bugreport_file,
207 const sp<IDumpstateListener>& listener) {
208
209 ds_ = &(Dumpstate::GetInstance());
210 DumpstateInfo* ds_info = new DumpstateInfo();
211 ds_info->ds = ds_;
212 ds_info->calling_uid = calling_uid;
213 ds_info->calling_package = calling_package;
214 ds_->listener_ = listener;
215 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
216 // Use a /dev/null FD when initializing options since none is provided.
217 android::base::unique_fd devnull_fd(
218 TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC)));
219
220 options->Initialize(Dumpstate::BugreportMode::BUGREPORT_DEFAULT,
221 0, bugreport_fd, devnull_fd, false);
222
223 if (bugreport_fd.get() == -1) {
224 MYLOGE("Invalid filedescriptor");
225 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
226 }
227 ds_->SetOptions(std::move(options));
228 ds_->path_ = bugreport_file;
229 pthread_t thread;
230 status_t err = pthread_create(&thread, nullptr, dumpstate_thread_retrieve, ds_info);
231 if (err != 0) {
232 MYLOGE("Could not create a thread");
233 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
234 }
235 return binder::Status::ok();
236}
237
Felipe Leme75876a22016-10-27 16:31:27 -0700238status_t DumpstateService::dump(int fd, const Vector<String16>&) {
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000239 std::lock_guard<std::mutex> lock(lock_);
Nandana Duttd9737662019-01-29 15:59:38 +0000240 if (ds_ == nullptr) {
241 dprintf(fd, "Bugreport not in progress yet");
242 return NO_ERROR;
243 }
244 std::string destination = ds_->options_->bugreport_fd.get() != -1
245 ? StringPrintf("[fd:%d]", ds_->options_->bugreport_fd.get())
246 : ds_->bugreport_internal_dir_.c_str();
247 dprintf(fd, "id: %d\n", ds_->id_);
248 dprintf(fd, "pid: %d\n", ds_->pid_);
249 dprintf(fd, "update_progress: %s\n", ds_->options_->do_progress_updates ? "true" : "false");
Nandana Dutt402a8392019-06-14 14:25:13 +0100250 dprintf(fd, "last_percent_progress: %d\n", ds_->last_reported_percent_progress_);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700251 dprintf(fd, "progress:\n");
Nandana Duttd9737662019-01-29 15:59:38 +0000252 ds_->progress_->Dump(fd, " ");
253 dprintf(fd, "args: %s\n", ds_->options_->args.c_str());
Kedar Chitnis9fd8c052021-11-16 09:09:22 +0000254 dprintf(fd, "bugreport_mode: %s\n", ds_->options_->bugreport_mode_string.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000255 dprintf(fd, "version: %s\n", ds_->version_.c_str());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000256 dprintf(fd, "bugreport_dir: %s\n", destination.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000257 dprintf(fd, "screenshot_path: %s\n", ds_->screenshot_path_.c_str());
258 dprintf(fd, "log_path: %s\n", ds_->log_path_.c_str());
259 dprintf(fd, "tmp_path: %s\n", ds_->tmp_path_.c_str());
260 dprintf(fd, "path: %s\n", ds_->path_.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000261 dprintf(fd, "base_name: %s\n", ds_->base_name_.c_str());
262 dprintf(fd, "name: %s\n", ds_->name_.c_str());
263 dprintf(fd, "now: %ld\n", ds_->now_);
Nandana Duttd9737662019-01-29 15:59:38 +0000264 dprintf(fd, "notification title: %s\n", ds_->options_->notification_title.c_str());
265 dprintf(fd, "notification description: %s\n", ds_->options_->notification_description.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700266
267 return NO_ERROR;
268}
269} // namespace os
270} // namespace android