blob: d24ed9c6cc729550543cbf29568dbb0d9c0dde7f [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
21#include <android-base/stringprintf.h>
22
23#include "android/os/BnDumpstate.h"
24
Felipe Lemef0292972016-11-22 13:57:05 -080025#include "DumpstateInternal.h"
26
Nandana Dutt197661d2018-11-16 16:40:21 +000027using android::base::StringPrintf;
28
Felipe Leme75876a22016-10-27 16:31:27 -070029namespace android {
30namespace os {
31
Felipe Leme009ecbb2016-11-07 10:18:44 -080032namespace {
Nandana Dutt197661d2018-11-16 16:40:21 +000033
34static binder::Status exception(uint32_t code, const std::string& msg) {
35 MYLOGE("%s (%d) ", msg.c_str(), code);
36 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
37}
38
39static binder::Status error(uint32_t code, const std::string& msg) {
40 MYLOGE("%s (%d) ", msg.c_str(), code);
41 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str()));
42}
43
44static void* callAndNotify(void* data) {
45 Dumpstate& ds = *static_cast<Dumpstate*>(data);
Nandana Dutt197661d2018-11-16 16:40:21 +000046 ds.Run();
Nandana Duttcc4ead82019-01-23 08:29:23 +000047 MYLOGD("Finished Run()\n");
Nandana Dutt197661d2018-11-16 16:40:21 +000048 return nullptr;
49}
50
Felipe Leme009ecbb2016-11-07 10:18:44 -080051class DumpstateToken : public BnDumpstateToken {};
52}
53
Felipe Leme75876a22016-10-27 16:31:27 -070054DumpstateService::DumpstateService() : ds_(Dumpstate::GetInstance()) {
55}
56
57char const* DumpstateService::getServiceName() {
58 return "dumpstate";
59}
60
61status_t DumpstateService::Start() {
62 IPCThreadState::self()->disableBackgroundScheduling(true);
63 status_t ret = BinderService<DumpstateService>::publish();
64 if (ret != android::OK) {
65 return ret;
66 }
67 sp<ProcessState> ps(ProcessState::self());
68 ps->startThreadPool();
69 ps->giveThreadPoolName();
70 return android::OK;
71}
72
73binder::Status DumpstateService::setListener(const std::string& name,
Felipe Leme009ecbb2016-11-07 10:18:44 -080074 const sp<IDumpstateListener>& listener,
Vishnu Nair20cf5032018-01-05 13:15:49 -080075 bool getSectionDetails,
Felipe Leme009ecbb2016-11-07 10:18:44 -080076 sp<IDumpstateToken>* returned_token) {
77 *returned_token = nullptr;
Felipe Leme75876a22016-10-27 16:31:27 -070078 if (name.empty()) {
79 MYLOGE("setListener(): name not set\n");
Felipe Leme75876a22016-10-27 16:31:27 -070080 return binder::Status::ok();
81 }
82 if (listener == nullptr) {
83 MYLOGE("setListener(): listener not set\n");
Felipe Leme75876a22016-10-27 16:31:27 -070084 return binder::Status::ok();
85 }
86 std::lock_guard<std::mutex> lock(lock_);
87 if (ds_.listener_ != nullptr) {
88 MYLOGE("setListener(%s): already set (%s)\n", name.c_str(), ds_.listener_name_.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -070089 return binder::Status::ok();
90 }
Felipe Leme009ecbb2016-11-07 10:18:44 -080091
Felipe Leme75876a22016-10-27 16:31:27 -070092 ds_.listener_name_ = name;
93 ds_.listener_ = listener;
Vishnu Nair20cf5032018-01-05 13:15:49 -080094 ds_.report_section_ = getSectionDetails;
Felipe Leme009ecbb2016-11-07 10:18:44 -080095 *returned_token = new DumpstateToken();
96
Felipe Leme75876a22016-10-27 16:31:27 -070097 return binder::Status::ok();
98}
99
Nandana Dutt5c914202019-01-16 17:45:18 +0000100// TODO(b/111441001): Hook up to consent service & copy final br only if user approves.
101binder::Status DumpstateService::startBugreport(int32_t /* calling_uid */,
102 const std::string& /* calling_package */,
103 const android::base::unique_fd& bugreport_fd,
Nandana Dutt54dbd672019-01-11 12:58:05 +0000104 const android::base::unique_fd& screenshot_fd,
Nandana Dutt05290ad2019-01-04 15:48:09 +0000105 int bugreport_mode,
Nandana Dutt54dbd672019-01-11 12:58:05 +0000106 const sp<IDumpstateListener>& listener) {
Nandana Dutt197661d2018-11-16 16:40:21 +0000107 MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
108
109 if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL &&
110 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE &&
111 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE &&
112 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR &&
113 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000114 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI &&
115 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) {
Nandana Dutt197661d2018-11-16 16:40:21 +0000116 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
117 StringPrintf("Invalid bugreport mode: %d", bugreport_mode));
118 }
119
Nandana Dutt54dbd672019-01-11 12:58:05 +0000120 if (bugreport_fd.get() == -1 || screenshot_fd.get() == -1) {
121 return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Invalid file descriptor");
122 }
123
Nandana Dutt197661d2018-11-16 16:40:21 +0000124 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
Nandana Dutt54dbd672019-01-11 12:58:05 +0000125 options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd,
126 screenshot_fd);
127
128 std::lock_guard<std::mutex> lock(lock_);
129 // TODO(b/111441001): Disallow multiple simultaneous bugreports.
Nandana Dutt197661d2018-11-16 16:40:21 +0000130 ds_.SetOptions(std::move(options));
Nandana Dutt54dbd672019-01-11 12:58:05 +0000131 if (listener != nullptr) {
132 ds_.listener_ = listener;
133 }
Nandana Dutt197661d2018-11-16 16:40:21 +0000134
135 pthread_t thread;
136 status_t err = pthread_create(&thread, nullptr, callAndNotify, &ds_);
137 if (err != 0) {
138 return error(err, "Could not create a background thread.");
139 }
Nandana Duttcccbb5f2018-09-27 09:23:36 +0100140 return binder::Status::ok();
141}
142
Nandana Duttcc4ead82019-01-23 08:29:23 +0000143binder::Status DumpstateService::cancelBugreport() {
144 // This is a no-op since the cancellation is done from java side via setting sys properties.
145 // See BugreportManagerServiceImpl.
146 // TODO(b/111441001): maybe make native and java sides use different binder interface
147 // to avoid these annoyances.
148 return binder::Status::ok();
149}
150
Felipe Leme75876a22016-10-27 16:31:27 -0700151status_t DumpstateService::dump(int fd, const Vector<String16>&) {
Felipe Leme7447d7c2016-11-03 18:12:22 -0700152 dprintf(fd, "id: %d\n", ds_.id_);
Felipe Leme75876a22016-10-27 16:31:27 -0700153 dprintf(fd, "pid: %d\n", ds_.pid_);
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100154 dprintf(fd, "update_progress: %s\n", ds_.options_->do_progress_updates ? "true" : "false");
Felipe Leme009ecbb2016-11-07 10:18:44 -0800155 dprintf(fd, "update_progress_threshold: %d\n", ds_.update_progress_threshold_);
156 dprintf(fd, "last_updated_progress: %d\n", ds_.last_updated_progress_);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700157 dprintf(fd, "progress:\n");
158 ds_.progress_->Dump(fd, " ");
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100159 dprintf(fd, "args: %s\n", ds_.options_->args.c_str());
160 dprintf(fd, "extra_options: %s\n", ds_.options_->extra_options.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700161 dprintf(fd, "version: %s\n", ds_.version_.c_str());
162 dprintf(fd, "bugreport_dir: %s\n", ds_.bugreport_dir_.c_str());
Nandana Dutt979388e2018-11-30 16:48:55 +0000163 dprintf(fd, "bugreport_internal_dir_: %s\n", ds_.bugreport_internal_dir_.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700164 dprintf(fd, "screenshot_path: %s\n", ds_.screenshot_path_.c_str());
165 dprintf(fd, "log_path: %s\n", ds_.log_path_.c_str());
166 dprintf(fd, "tmp_path: %s\n", ds_.tmp_path_.c_str());
Felipe Leme7447d7c2016-11-03 18:12:22 -0700167 dprintf(fd, "path: %s\n", ds_.path_.c_str());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100168 dprintf(fd, "extra_options: %s\n", ds_.options_->extra_options.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700169 dprintf(fd, "base_name: %s\n", ds_.base_name_.c_str());
170 dprintf(fd, "name: %s\n", ds_.name_.c_str());
171 dprintf(fd, "now: %ld\n", ds_.now_);
172 dprintf(fd, "is_zipping: %s\n", ds_.IsZipping() ? "true" : "false");
173 dprintf(fd, "listener: %s\n", ds_.listener_name_.c_str());
Nandana Dutt5fb117b2018-09-27 09:23:36 +0100174 dprintf(fd, "notification title: %s\n", ds_.options_->notification_title.c_str());
175 dprintf(fd, "notification description: %s\n", ds_.options_->notification_description.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700176
177 return NO_ERROR;
178}
179} // namespace os
180} // namespace android