blob: bb089e69b1eabcb5592be0cc0eea82a458028c14 [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
Nandana Dutt197661d2018-11-16 16:40:21 +000042static binder::Status exception(uint32_t code, const std::string& msg) {
43 MYLOGE("%s (%d) ", msg.c_str(), code);
44 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
45}
46
47static binder::Status error(uint32_t code, const std::string& msg) {
48 MYLOGE("%s (%d) ", msg.c_str(), code);
49 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str()));
50}
51
Nandana Dutt2bbba942019-01-30 13:13:52 +000052// Takes ownership of data.
Nandana Dutt197661d2018-11-16 16:40:21 +000053static void* callAndNotify(void* data) {
Nandana Dutt2bbba942019-01-30 13:13:52 +000054 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
55 ds_info->ds->Run(ds_info->calling_uid, ds_info->calling_package);
Nandana Duttcc4ead82019-01-23 08:29:23 +000056 MYLOGD("Finished Run()\n");
Nandana Dutt197661d2018-11-16 16:40:21 +000057 return nullptr;
58}
59
Felipe Leme009ecbb2016-11-07 10:18:44 -080060class DumpstateToken : public BnDumpstateToken {};
Nandana Duttd2f5f082019-01-18 17:13:52 +000061
62} // namespace
Felipe Leme009ecbb2016-11-07 10:18:44 -080063
Nandana Duttd9737662019-01-29 15:59:38 +000064DumpstateService::DumpstateService() : ds_(nullptr) {
Felipe Leme75876a22016-10-27 16:31:27 -070065}
66
67char const* DumpstateService::getServiceName() {
68 return "dumpstate";
69}
70
71status_t DumpstateService::Start() {
72 IPCThreadState::self()->disableBackgroundScheduling(true);
73 status_t ret = BinderService<DumpstateService>::publish();
74 if (ret != android::OK) {
75 return ret;
76 }
77 sp<ProcessState> ps(ProcessState::self());
78 ps->startThreadPool();
79 ps->giveThreadPoolName();
80 return android::OK;
81}
82
Nandana Duttd9737662019-01-29 15:59:38 +000083// Note: this method is part of the old flow and is not expected to be used in combination
84// with startBugreport.
Felipe Leme75876a22016-10-27 16:31:27 -070085binder::Status DumpstateService::setListener(const std::string& name,
Felipe Leme009ecbb2016-11-07 10:18:44 -080086 const sp<IDumpstateListener>& listener,
Vishnu Nair20cf5032018-01-05 13:15:49 -080087 bool getSectionDetails,
Felipe Leme009ecbb2016-11-07 10:18:44 -080088 sp<IDumpstateToken>* returned_token) {
89 *returned_token = nullptr;
Felipe Leme75876a22016-10-27 16:31:27 -070090 if (name.empty()) {
91 MYLOGE("setListener(): name not set\n");
Felipe Leme75876a22016-10-27 16:31:27 -070092 return binder::Status::ok();
93 }
94 if (listener == nullptr) {
95 MYLOGE("setListener(): listener not set\n");
Felipe Leme75876a22016-10-27 16:31:27 -070096 return binder::Status::ok();
97 }
98 std::lock_guard<std::mutex> lock(lock_);
Nandana Duttd9737662019-01-29 15:59:38 +000099 if (ds_ == nullptr) {
100 ds_ = &(Dumpstate::GetInstance());
101 }
102 if (ds_->listener_ != nullptr) {
103 MYLOGE("setListener(%s): already set (%s)\n", name.c_str(), ds_->listener_name_.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700104 return binder::Status::ok();
105 }
Felipe Leme009ecbb2016-11-07 10:18:44 -0800106
Nandana Duttd9737662019-01-29 15:59:38 +0000107 ds_->listener_name_ = name;
108 ds_->listener_ = listener;
109 ds_->report_section_ = getSectionDetails;
Felipe Leme009ecbb2016-11-07 10:18:44 -0800110 *returned_token = new DumpstateToken();
111
Felipe Leme75876a22016-10-27 16:31:27 -0700112 return binder::Status::ok();
113}
114
Nandana Duttd2f5f082019-01-18 17:13:52 +0000115binder::Status DumpstateService::startBugreport(int32_t calling_uid,
116 const std::string& calling_package,
Nandana Dutt5c914202019-01-16 17:45:18 +0000117 const android::base::unique_fd& bugreport_fd,
Nandana Dutt54dbd672019-01-11 12:58:05 +0000118 const android::base::unique_fd& screenshot_fd,
Nandana Dutt05290ad2019-01-04 15:48:09 +0000119 int bugreport_mode,
Nandana Dutt54dbd672019-01-11 12:58:05 +0000120 const sp<IDumpstateListener>& listener) {
Nandana Dutt197661d2018-11-16 16:40:21 +0000121 MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
122
123 if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL &&
124 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE &&
125 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE &&
126 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR &&
127 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000128 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI &&
129 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) {
Nandana Dutt197661d2018-11-16 16:40:21 +0000130 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
131 StringPrintf("Invalid bugreport mode: %d", bugreport_mode));
132 }
133
Nandana Dutt54dbd672019-01-11 12:58:05 +0000134 if (bugreport_fd.get() == -1 || screenshot_fd.get() == -1) {
135 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Invalid file descriptor");
136 }
137
Nandana Dutt197661d2018-11-16 16:40:21 +0000138 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
Nandana Dutt54dbd672019-01-11 12:58:05 +0000139 options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd,
140 screenshot_fd);
141
Nandana Duttd9737662019-01-29 15:59:38 +0000142 // This is the bugreporting API flow, so ensure there is only one bugreport in progress at a
143 // time.
Nandana Dutt54dbd672019-01-11 12:58:05 +0000144 std::lock_guard<std::mutex> lock(lock_);
Nandana Duttd9737662019-01-29 15:59:38 +0000145 if (ds_ != nullptr) {
146 return exception(binder::Status::EX_SERVICE_SPECIFIC,
147 "There is already a bugreport in progress");
148 }
149 ds_ = &(Dumpstate::GetInstance());
150 ds_->SetOptions(std::move(options));
Nandana Dutt54dbd672019-01-11 12:58:05 +0000151 if (listener != nullptr) {
Nandana Duttd9737662019-01-29 15:59:38 +0000152 ds_->listener_ = listener;
Nandana Dutt54dbd672019-01-11 12:58:05 +0000153 }
Nandana Dutt197661d2018-11-16 16:40:21 +0000154
Nandana Dutt2bbba942019-01-30 13:13:52 +0000155 DumpstateInfo* ds_info = new DumpstateInfo();
156 ds_info->ds = ds_;
157 ds_info->calling_uid = calling_uid;
158 ds_info->calling_package = calling_package;
Nandana Duttd2f5f082019-01-18 17:13:52 +0000159
Nandana Dutt197661d2018-11-16 16:40:21 +0000160 pthread_t thread;
Nandana Dutt2bbba942019-01-30 13:13:52 +0000161 status_t err = pthread_create(&thread, nullptr, callAndNotify, ds_info);
Nandana Dutt197661d2018-11-16 16:40:21 +0000162 if (err != 0) {
Nandana Dutt2bbba942019-01-30 13:13:52 +0000163 delete ds_info;
164 ds_info = nullptr;
Nandana Dutt197661d2018-11-16 16:40:21 +0000165 return error(err, "Could not create a background thread.");
166 }
Nandana Duttcccbb5f2018-09-27 09:23:36 +0100167 return binder::Status::ok();
168}
169
Nandana Duttcc4ead82019-01-23 08:29:23 +0000170binder::Status DumpstateService::cancelBugreport() {
171 // This is a no-op since the cancellation is done from java side via setting sys properties.
172 // See BugreportManagerServiceImpl.
173 // TODO(b/111441001): maybe make native and java sides use different binder interface
174 // to avoid these annoyances.
175 return binder::Status::ok();
176}
177
Felipe Leme75876a22016-10-27 16:31:27 -0700178status_t DumpstateService::dump(int fd, const Vector<String16>&) {
Nandana Duttd9737662019-01-29 15:59:38 +0000179 if (ds_ == nullptr) {
180 dprintf(fd, "Bugreport not in progress yet");
181 return NO_ERROR;
182 }
183 std::string destination = ds_->options_->bugreport_fd.get() != -1
184 ? StringPrintf("[fd:%d]", ds_->options_->bugreport_fd.get())
185 : ds_->bugreport_internal_dir_.c_str();
186 dprintf(fd, "id: %d\n", ds_->id_);
187 dprintf(fd, "pid: %d\n", ds_->pid_);
188 dprintf(fd, "update_progress: %s\n", ds_->options_->do_progress_updates ? "true" : "false");
189 dprintf(fd, "update_progress_threshold: %d\n", ds_->update_progress_threshold_);
190 dprintf(fd, "last_updated_progress: %d\n", ds_->last_updated_progress_);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700191 dprintf(fd, "progress:\n");
Nandana Duttd9737662019-01-29 15:59:38 +0000192 ds_->progress_->Dump(fd, " ");
193 dprintf(fd, "args: %s\n", ds_->options_->args.c_str());
194 dprintf(fd, "extra_options: %s\n", ds_->options_->extra_options.c_str());
195 dprintf(fd, "version: %s\n", ds_->version_.c_str());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000196 dprintf(fd, "bugreport_dir: %s\n", destination.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000197 dprintf(fd, "screenshot_path: %s\n", ds_->screenshot_path_.c_str());
198 dprintf(fd, "log_path: %s\n", ds_->log_path_.c_str());
199 dprintf(fd, "tmp_path: %s\n", ds_->tmp_path_.c_str());
200 dprintf(fd, "path: %s\n", ds_->path_.c_str());
201 dprintf(fd, "extra_options: %s\n", ds_->options_->extra_options.c_str());
202 dprintf(fd, "base_name: %s\n", ds_->base_name_.c_str());
203 dprintf(fd, "name: %s\n", ds_->name_.c_str());
204 dprintf(fd, "now: %ld\n", ds_->now_);
205 dprintf(fd, "is_zipping: %s\n", ds_->IsZipping() ? "true" : "false");
206 dprintf(fd, "listener: %s\n", ds_->listener_name_.c_str());
207 dprintf(fd, "notification title: %s\n", ds_->options_->notification_title.c_str());
208 dprintf(fd, "notification description: %s\n", ds_->options_->notification_description.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700209
210 return NO_ERROR;
211}
212} // namespace os
213} // namespace android