blob: 2c845e6e566217bf625d9f5a521e1012728f4bc7 [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
Nandana Dutt16d1aee2019-02-15 16:13:53 +000047// Creates a bugreport and exits, thus preserving the oneshot nature of the service.
48// Note: takes ownership of data.
49[[noreturn]] static void* dumpstate_thread_main(void* data) {
Nandana Dutt2bbba942019-01-30 13:13:52 +000050 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
51 ds_info->ds->Run(ds_info->calling_uid, ds_info->calling_package);
Nandana Dutt16d1aee2019-02-15 16:13:53 +000052 MYLOGD("Finished taking a bugreport. Exiting.\n");
53 exit(0);
54}
55
56[[noreturn]] static void signalErrorAndExit(sp<IDumpstateListener> listener, int error_code) {
57 listener->onError(error_code);
58 exit(0);
Nandana Dutt197661d2018-11-16 16:40:21 +000059}
60
Nandana Duttd2f5f082019-01-18 17:13:52 +000061} // namespace
Felipe Leme009ecbb2016-11-07 10:18:44 -080062
Nandana Duttd9737662019-01-29 15:59:38 +000063DumpstateService::DumpstateService() : ds_(nullptr) {
Felipe Leme75876a22016-10-27 16:31:27 -070064}
65
66char const* DumpstateService::getServiceName() {
67 return "dumpstate";
68}
69
70status_t DumpstateService::Start() {
71 IPCThreadState::self()->disableBackgroundScheduling(true);
72 status_t ret = BinderService<DumpstateService>::publish();
73 if (ret != android::OK) {
74 return ret;
75 }
76 sp<ProcessState> ps(ProcessState::self());
77 ps->startThreadPool();
78 ps->giveThreadPoolName();
79 return android::OK;
80}
81
Nandana Duttd2f5f082019-01-18 17:13:52 +000082binder::Status DumpstateService::startBugreport(int32_t calling_uid,
83 const std::string& calling_package,
Nandana Dutt5c914202019-01-16 17:45:18 +000084 const android::base::unique_fd& bugreport_fd,
Nandana Dutt54dbd672019-01-11 12:58:05 +000085 const android::base::unique_fd& screenshot_fd,
Nandana Dutt05290ad2019-01-04 15:48:09 +000086 int bugreport_mode,
Nandana Dutt54dbd672019-01-11 12:58:05 +000087 const sp<IDumpstateListener>& listener) {
Nandana Dutt197661d2018-11-16 16:40:21 +000088 MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
89
Abhijeet Kaured5d6a62019-10-07 15:02:05 +010090 // Ensure there is only one bugreport in progress at a time.
Nandana Dutt16d1aee2019-02-15 16:13:53 +000091 std::lock_guard<std::mutex> lock(lock_);
92 if (ds_ != nullptr) {
93 MYLOGE("Error! There is already a bugreport in progress. Returning.");
94 if (listener != nullptr) {
Nandana Dutt0eb86bf2019-02-21 16:10:10 +000095 listener->onError(IDumpstateListener::BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);
Nandana Dutt16d1aee2019-02-15 16:13:53 +000096 }
97 return exception(binder::Status::EX_SERVICE_SPECIFIC,
98 "There is already a bugreport in progress");
99 }
100
101 // From here on, all conditions that indicate we are done with this incoming request should
102 // result in exiting the service to free it up for next invocation.
103 if (listener == nullptr) {
104 MYLOGE("Invalid input: no listener");
105 exit(0);
106 }
107
Nandana Dutt197661d2018-11-16 16:40:21 +0000108 if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL &&
109 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE &&
110 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE &&
111 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR &&
112 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY &&
Abhijeet Kaur904e0e02018-12-05 14:03:01 +0000113 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI &&
114 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000115 MYLOGE("Invalid input: bad bugreport mode: %d", bugreport_mode);
116 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
Nandana Dutt197661d2018-11-16 16:40:21 +0000117 }
118
Abhijeet Kaur1f3d8862019-03-28 11:04:43 +0000119 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
120 options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd,
121 screenshot_fd);
122
123 if (bugreport_fd.get() == -1 || (options->do_fb && screenshot_fd.get() == -1)) {
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000124 MYLOGE("Invalid filedescriptor");
125 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
Nandana Dutt54dbd672019-01-11 12:58:05 +0000126 }
127
Nandana Dutt54dbd672019-01-11 12:58:05 +0000128
Nandana Duttd9737662019-01-29 15:59:38 +0000129 ds_ = &(Dumpstate::GetInstance());
130 ds_->SetOptions(std::move(options));
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000131 ds_->listener_ = listener;
Nandana Dutt197661d2018-11-16 16:40:21 +0000132
Nandana Dutt2bbba942019-01-30 13:13:52 +0000133 DumpstateInfo* ds_info = new DumpstateInfo();
134 ds_info->ds = ds_;
135 ds_info->calling_uid = calling_uid;
136 ds_info->calling_package = calling_package;
Nandana Duttd2f5f082019-01-18 17:13:52 +0000137
Nandana Dutt197661d2018-11-16 16:40:21 +0000138 pthread_t thread;
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000139 status_t err = pthread_create(&thread, nullptr, dumpstate_thread_main, ds_info);
Nandana Dutt197661d2018-11-16 16:40:21 +0000140 if (err != 0) {
Nandana Dutt2bbba942019-01-30 13:13:52 +0000141 delete ds_info;
142 ds_info = nullptr;
Nandana Dutt16d1aee2019-02-15 16:13:53 +0000143 MYLOGE("Could not create a thread");
144 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
Nandana Dutt197661d2018-11-16 16:40:21 +0000145 }
Nandana Duttcccbb5f2018-09-27 09:23:36 +0100146 return binder::Status::ok();
147}
148
Nandana Duttcc4ead82019-01-23 08:29:23 +0000149binder::Status DumpstateService::cancelBugreport() {
150 // This is a no-op since the cancellation is done from java side via setting sys properties.
151 // See BugreportManagerServiceImpl.
152 // TODO(b/111441001): maybe make native and java sides use different binder interface
153 // to avoid these annoyances.
154 return binder::Status::ok();
155}
156
Felipe Leme75876a22016-10-27 16:31:27 -0700157status_t DumpstateService::dump(int fd, const Vector<String16>&) {
Nandana Duttd9737662019-01-29 15:59:38 +0000158 if (ds_ == nullptr) {
159 dprintf(fd, "Bugreport not in progress yet");
160 return NO_ERROR;
161 }
162 std::string destination = ds_->options_->bugreport_fd.get() != -1
163 ? StringPrintf("[fd:%d]", ds_->options_->bugreport_fd.get())
164 : ds_->bugreport_internal_dir_.c_str();
165 dprintf(fd, "id: %d\n", ds_->id_);
166 dprintf(fd, "pid: %d\n", ds_->pid_);
167 dprintf(fd, "update_progress: %s\n", ds_->options_->do_progress_updates ? "true" : "false");
Nandana Dutt402a8392019-06-14 14:25:13 +0100168 dprintf(fd, "last_percent_progress: %d\n", ds_->last_reported_percent_progress_);
Felipe Leme7447d7c2016-11-03 18:12:22 -0700169 dprintf(fd, "progress:\n");
Nandana Duttd9737662019-01-29 15:59:38 +0000170 ds_->progress_->Dump(fd, " ");
171 dprintf(fd, "args: %s\n", ds_->options_->args.c_str());
172 dprintf(fd, "extra_options: %s\n", ds_->options_->extra_options.c_str());
173 dprintf(fd, "version: %s\n", ds_->version_.c_str());
Nandana Dutt9a76d202019-01-21 15:56:48 +0000174 dprintf(fd, "bugreport_dir: %s\n", destination.c_str());
Nandana Duttd9737662019-01-29 15:59:38 +0000175 dprintf(fd, "screenshot_path: %s\n", ds_->screenshot_path_.c_str());
176 dprintf(fd, "log_path: %s\n", ds_->log_path_.c_str());
177 dprintf(fd, "tmp_path: %s\n", ds_->tmp_path_.c_str());
178 dprintf(fd, "path: %s\n", ds_->path_.c_str());
179 dprintf(fd, "extra_options: %s\n", ds_->options_->extra_options.c_str());
180 dprintf(fd, "base_name: %s\n", ds_->base_name_.c_str());
181 dprintf(fd, "name: %s\n", ds_->name_.c_str());
182 dprintf(fd, "now: %ld\n", ds_->now_);
183 dprintf(fd, "is_zipping: %s\n", ds_->IsZipping() ? "true" : "false");
Nandana Duttd9737662019-01-29 15:59:38 +0000184 dprintf(fd, "notification title: %s\n", ds_->options_->notification_title.c_str());
185 dprintf(fd, "notification description: %s\n", ds_->options_->notification_description.c_str());
Felipe Leme75876a22016-10-27 16:31:27 -0700186
187 return NO_ERROR;
188}
189} // namespace os
190} // namespace android