blob: a8d12a1a91259b674e82cdf56d20811692cf8fa5 [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;
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +000040 int32_t user_id = -1;
41 bool keep_bugreport_on_retrieval = false;
Kholoud Mohameddef4e432024-05-15 12:47:46 +000042 bool skip_user_consent = false;
Nandana Duttd2f5f082019-01-18 17:13:52 +000043};
44
Hunter Knepshielde30d8212020-12-21 18:08:17 -080045static binder::Status exception(uint32_t code, const std::string& msg,
46 const std::string& extra_msg = "") {
47 if (extra_msg.empty()) {
48 MYLOGE("%s (%d) ", msg.c_str(), code);
49 } else {
50 MYLOGE("%s %s (%d) ", msg.c_str(), extra_msg.c_str(), code);
51 }
Nandana Dutt197661d2018-11-16 16:40:21 +000052 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
53}
54
Nandana Dutt16d1aee2019-02-15 16:13:53 +000055// Creates a bugreport and exits, thus preserving the oneshot nature of the service.
56// Note: takes ownership of data.
Kean Mariotti306633e2022-09-05 16:30:47 +000057[[noreturn]] static void* dumpstate_thread_bugreport(void* data) {
Nandana Dutt2bbba942019-01-30 13:13:52 +000058 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
59 ds_info->ds->Run(ds_info->calling_uid, ds_info->calling_package);
Nandana Dutt16d1aee2019-02-15 16:13:53 +000060 MYLOGD("Finished taking a bugreport. Exiting.\n");
61 exit(0);
62}
63
Gavin Corkerya44686c2022-11-23 18:16:51 +000064[[noreturn]] static void* dumpstate_thread_retrieve(void* data) {
65 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
Kholoud Mohameddef4e432024-05-15 12:47:46 +000066 ds_info->ds->Retrieve(ds_info->calling_uid, ds_info->calling_package,
67 ds_info->keep_bugreport_on_retrieval, ds_info->skip_user_consent);
Gavin Corkerya44686c2022-11-23 18:16:51 +000068 MYLOGD("Finished retrieving a bugreport. Exiting.\n");
69 exit(0);
70}
71
Nandana Dutt16d1aee2019-02-15 16:13:53 +000072[[noreturn]] static void signalErrorAndExit(sp<IDumpstateListener> listener, int error_code) {
73 listener->onError(error_code);
74 exit(0);
Nandana Dutt197661d2018-11-16 16:40:21 +000075}
76
Nandana Duttd2f5f082019-01-18 17:13:52 +000077} // namespace
Felipe Leme009ecbb2016-11-07 10:18:44 -080078
Hunter Knepshielde30d8212020-12-21 18:08:17 -080079DumpstateService::DumpstateService() : ds_(nullptr), calling_uid_(-1), calling_package_() {
Felipe Leme75876a22016-10-27 16:31:27 -070080}
81
82char const* DumpstateService::getServiceName() {
83 return "dumpstate";
84}
85
86status_t DumpstateService::Start() {
87 IPCThreadState::self()->disableBackgroundScheduling(true);
88 status_t ret = BinderService<DumpstateService>::publish();
89 if (ret != android::OK) {
90 return ret;
91 }
92 sp<ProcessState> ps(ProcessState::self());
93 ps->startThreadPool();
94 ps->giveThreadPoolName();
95 return android::OK;
96}
97
Kean Mariotti306633e2022-09-05 16:30:47 +000098binder::Status DumpstateService::preDumpUiData(const std::string&) {
99 std::lock_guard<std::mutex> lock(lock_);
100 MYLOGI("preDumpUiData()");
101
102 if (ds_ != nullptr) {
103 MYLOGE("Error! DumpstateService is currently already being used. Returning.");
104 return exception(binder::Status::EX_SERVICE_SPECIFIC,
105 "DumpstateService is already being used");
106 }
107
108 ds_ = &(Dumpstate::GetInstance());
109 ds_->PreDumpUiData();
110
111 return binder::Status::ok();
112}
113
Nandana Duttd2f5f082019-01-18 17:13:52 +0000114binder::Status DumpstateService::startBugreport(int32_t calling_uid,
115 const std::string& calling_package,
Jiyong Park3b25ae12019-11-25 11:06:16 +0900116 android::base::unique_fd bugreport_fd,
117 android::base::unique_fd screenshot_fd,
Nandana Dutt05290ad2019-01-04 15:48:09 +0000118 int bugreport_mode,
Kean Mariotti306633e2022-09-05 16:30:47 +0000119 int bugreport_flags,
Paul Changf59c2b72020-03-10 02:08:55 +0800120 const sp<IDumpstateListener>& listener,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000121 bool is_screenshot_requested,
122 bool skip_user_consent) {
Nandana Dutt197661d2018-11-16 16:40:21 +0000123 MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
124
Abhijeet Kaured5d6a62019-10-07 15:02:05 +0100125 // Ensure there is only one bugreport in progress at a time.
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000126 std::lock_guard<std::mutex> lock(lock_);
127 if (ds_ != nullptr) {
Kean Mariotti306633e2022-09-05 16:30:47 +0000128 MYLOGE("Error! DumpstateService is currently already being used. Returning.");
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000129 if (listener != nullptr) {
Nandana Dutt0eb86bf2019-02-21 16:10:10 +0000130 listener->onError(IDumpstateListener::BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000131 }
132 return exception(binder::Status::EX_SERVICE_SPECIFIC,
Kean Mariotti306633e2022-09-05 16:30:47 +0000133 "DumpstateService is already being used");
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000134 }
135
136 // From here on, all conditions that indicate we are done with this incoming request should
137 // result in exiting the service to free it up for next invocation.
138 if (listener == nullptr) {
139 MYLOGE("Invalid input: no listener");
140 exit(0);
141 }
142
Nandana Dutt197661d2018-11-16 16:40:21 +0000143 if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL &&
144 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE &&
145 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE &&
146 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR &&
147 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000148 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI &&
Elis Elliott8e401ad2023-08-08 11:18:59 +0000149 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_ONBOARDING &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000150 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000151 MYLOGE("Invalid input: bad bugreport mode: %d", bugreport_mode);
152 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
Nandana Dutt197661d2018-11-16 16:40:21 +0000153 }
154
Abhijeet Kaur1f3d8862019-03-28 11:04:43 +0000155 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
Kean Mariotti306633e2022-09-05 16:30:47 +0000156 options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_flags,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000157 bugreport_fd, screenshot_fd, is_screenshot_requested, skip_user_consent);
Abhijeet Kaur1f3d8862019-03-28 11:04:43 +0000158
Paul Chang0d2aad72020-02-13 20:04:03 +0800159 if (bugreport_fd.get() == -1 || (options->do_screenshot && screenshot_fd.get() == -1)) {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000160 MYLOGE("Invalid filedescriptor");
161 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
Nandana Dutt54dbd672019-01-11 12:58:05 +0000162 }
163
Nandana Dutt54dbd672019-01-11 12:58:05 +0000164
Nandana Duttd9737662019-01-29 15:59:38 +0000165 ds_ = &(Dumpstate::GetInstance());
166 ds_->SetOptions(std::move(options));
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000167 ds_->listener_ = listener;
Nandana Dutt197661d2018-11-16 16:40:21 +0000168
Hunter Knepshielde30d8212020-12-21 18:08:17 -0800169 // Track caller info for cancellation purposes.
170 calling_uid_ = calling_uid;
171 calling_package_ = calling_package;
172
Nandana Dutt2bbba942019-01-30 13:13:52 +0000173 DumpstateInfo* ds_info = new DumpstateInfo();
174 ds_info->ds = ds_;
175 ds_info->calling_uid = calling_uid;
176 ds_info->calling_package = calling_package;
Nandana Duttd2f5f082019-01-18 17:13:52 +0000177
Nandana Dutt197661d2018-11-16 16:40:21 +0000178 pthread_t thread;
Abhijeet Kaura407fb82020-03-27 12:51:12 +0000179 // Initialize dumpstate
180 ds_->Initialize();
Kean Mariotti306633e2022-09-05 16:30:47 +0000181 status_t err = pthread_create(&thread, nullptr, dumpstate_thread_bugreport, ds_info);
Nandana Dutt197661d2018-11-16 16:40:21 +0000182 if (err != 0) {
Nandana Dutt2bbba942019-01-30 13:13:52 +0000183 delete ds_info;
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000184 MYLOGE("Could not create a thread");
185 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
Nandana Dutt197661d2018-11-16 16:40:21 +0000186 }
Nandana Duttcccbb5f2018-09-27 09:23:36 +0100187 return binder::Status::ok();
188}
189
Hunter Knepshielde30d8212020-12-21 18:08:17 -0800190binder::Status DumpstateService::cancelBugreport(int32_t calling_uid,
191 const std::string& calling_package) {
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000192 std::lock_guard<std::mutex> lock(lock_);
Hunter Knepshielde30d8212020-12-21 18:08:17 -0800193 if (calling_uid != calling_uid_ || calling_package != calling_package_) {
194 // Note: we use a SecurityException to prevent BugreportManagerServiceImpl from killing the
195 // report in progress (from another caller).
196 return exception(
197 binder::Status::EX_SECURITY,
198 StringPrintf("Cancellation requested by %d/%s does not match report in "
199 "progress",
200 calling_uid, calling_package.c_str()),
201 // Sharing the owner of the BR is a (minor) leak, so leave it out of the app's exception
202 StringPrintf("started by %d/%s", calling_uid_, calling_package_.c_str()));
203 }
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000204 ds_->Cancel();
Nandana Duttcc4ead82019-01-23 08:29:23 +0000205 return binder::Status::ok();
206}
207
Gavin Corkerya44686c2022-11-23 18:16:51 +0000208binder::Status DumpstateService::retrieveBugreport(
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +0000209 int32_t calling_uid, const std::string& calling_package, int32_t user_id,
Gavin Corkerya44686c2022-11-23 18:16:51 +0000210 android::base::unique_fd bugreport_fd,
211 const std::string& bugreport_file,
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +0000212 const bool keep_bugreport_on_retrieval,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000213 const bool skip_user_consent,
Gavin Corkerya44686c2022-11-23 18:16:51 +0000214 const sp<IDumpstateListener>& listener) {
215
216 ds_ = &(Dumpstate::GetInstance());
217 DumpstateInfo* ds_info = new DumpstateInfo();
218 ds_info->ds = ds_;
219 ds_info->calling_uid = calling_uid;
220 ds_info->calling_package = calling_package;
Kholoud Mohamed7c3fb7c2023-10-18 12:56:22 +0000221 ds_info->user_id = user_id;
222 ds_info->keep_bugreport_on_retrieval = keep_bugreport_on_retrieval;
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000223 ds_info->skip_user_consent = skip_user_consent;
Gavin Corkerya44686c2022-11-23 18:16:51 +0000224 ds_->listener_ = listener;
225 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
226 // Use a /dev/null FD when initializing options since none is provided.
227 android::base::unique_fd devnull_fd(
228 TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC)));
229
230 options->Initialize(Dumpstate::BugreportMode::BUGREPORT_DEFAULT,
Kholoud Mohameddef4e432024-05-15 12:47:46 +0000231 0, bugreport_fd, devnull_fd, false, skip_user_consent);
Gavin Corkerya44686c2022-11-23 18:16:51 +0000232
233 if (bugreport_fd.get() == -1) {
234 MYLOGE("Invalid filedescriptor");
235 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
236 }
237 ds_->SetOptions(std::move(options));
238 ds_->path_ = bugreport_file;
239 pthread_t thread;
240 status_t err = pthread_create(&thread, nullptr, dumpstate_thread_retrieve, ds_info);
241 if (err != 0) {
242 MYLOGE("Could not create a thread");
243 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
244 }
245 return binder::Status::ok();
246}
247
Felipe Leme75876a22016-10-27 16:31:27 -0700248status_t DumpstateService::dump(int fd, const Vector<String16>&) {
Nandana Dutt8ae16e62020-03-27 10:20:22 +0000249 std::lock_guard<std::mutex> lock(lock_);
Nandana Duttd9737662019-01-29 15:59:38 +0000250 if (ds_ == nullptr) {
251 dprintf(fd, "Bugreport not in progress yet");
252 return NO_ERROR;
253 }
254 std::string destination = ds_->options_->bugreport_fd.get() != -1
255 ? StringPrintf("[fd:%d]", ds_->options_->bugreport_fd.get())
256 : ds_->bugreport_internal_dir_.c_str();
257 dprintf(fd, "id: %d\n", ds_->id_);
258 dprintf(fd, "pid: %d\n", ds_->pid_);
259 dprintf(fd, "update_progress: %s\n", ds_->options_->do_progress_updates ? "true" : "false");
Nandana Dutt402a8392019-06-14 14:25:13 +0100260 dprintf(fd, "last_percent_progress: %d\n", ds_->last_reported_percent_progress_);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700261 dprintf(fd, "progress:\n");
Nandana Duttd9737662019-01-29 15:59:38 +0000262 ds_->progress_->Dump(fd, " ");
263 dprintf(fd, "args: %s\n", ds_->options_->args.c_str());
Kedar Chitnis9fd8c052021-11-16 09:09:22 +0000264 dprintf(fd, "bugreport_mode: %s\n", ds_->options_->bugreport_mode_string.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000265 dprintf(fd, "version: %s\n", ds_->version_.c_str());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000266 dprintf(fd, "bugreport_dir: %s\n", destination.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000267 dprintf(fd, "screenshot_path: %s\n", ds_->screenshot_path_.c_str());
268 dprintf(fd, "log_path: %s\n", ds_->log_path_.c_str());
269 dprintf(fd, "tmp_path: %s\n", ds_->tmp_path_.c_str());
270 dprintf(fd, "path: %s\n", ds_->path_.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000271 dprintf(fd, "base_name: %s\n", ds_->base_name_.c_str());
272 dprintf(fd, "name: %s\n", ds_->name_.c_str());
273 dprintf(fd, "now: %ld\n", ds_->now_);
Nandana Duttd9737662019-01-29 15:59:38 +0000274 dprintf(fd, "notification title: %s\n", ds_->options_->notification_title.c_str());
275 dprintf(fd, "notification description: %s\n", ds_->options_->notification_description.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700276
277 return NO_ERROR;
278}
279} // namespace os
280} // namespace android