1 /**
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 <memory>
22
23 #include <android-base/stringprintf.h>
24 #include "android/os/BnDumpstate.h"
25
26 #include "DumpstateInternal.h"
27
28 using android::base::StringPrintf;
29
30 namespace android {
31 namespace os {
32
33 namespace {
34
35 struct DumpstateInfo {
36 public:
37 Dumpstate* ds = nullptr;
38 int32_t calling_uid = -1;
39 std::string calling_package;
40 };
41
exception(uint32_t code,const std::string & msg)42 static 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
47 // Creates a bugreport and exits, thus preserving the oneshot nature of the service.
48 // Note: takes ownership of data.
dumpstate_thread_main(void * data)49 [[noreturn]] static void* dumpstate_thread_main(void* data) {
50 std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
51 ds_info->ds->Run(ds_info->calling_uid, ds_info->calling_package);
52 MYLOGD("Finished taking a bugreport. Exiting.\n");
53 exit(0);
54 }
55
signalErrorAndExit(sp<IDumpstateListener> listener,int error_code)56 [[noreturn]] static void signalErrorAndExit(sp<IDumpstateListener> listener, int error_code) {
57 listener->onError(error_code);
58 exit(0);
59 }
60
61 } // namespace
62
DumpstateService()63 DumpstateService::DumpstateService() : ds_(nullptr) {
64 }
65
getServiceName()66 char const* DumpstateService::getServiceName() {
67 return "dumpstate";
68 }
69
Start()70 status_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
startBugreport(int32_t calling_uid,const std::string & calling_package,android::base::unique_fd bugreport_fd,android::base::unique_fd screenshot_fd,int bugreport_mode,const sp<IDumpstateListener> & listener,bool is_screenshot_requested)82 binder::Status DumpstateService::startBugreport(int32_t calling_uid,
83 const std::string& calling_package,
84 android::base::unique_fd bugreport_fd,
85 android::base::unique_fd screenshot_fd,
86 int bugreport_mode,
87 const sp<IDumpstateListener>& listener,
88 bool is_screenshot_requested) {
89 MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
90
91 // Ensure there is only one bugreport in progress at a time.
92 std::lock_guard<std::mutex> lock(lock_);
93 if (ds_ != nullptr) {
94 MYLOGE("Error! There is already a bugreport in progress. Returning.");
95 if (listener != nullptr) {
96 listener->onError(IDumpstateListener::BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);
97 }
98 return exception(binder::Status::EX_SERVICE_SPECIFIC,
99 "There is already a bugreport in progress");
100 }
101
102 // From here on, all conditions that indicate we are done with this incoming request should
103 // result in exiting the service to free it up for next invocation.
104 if (listener == nullptr) {
105 MYLOGE("Invalid input: no listener");
106 exit(0);
107 }
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 &&
114 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI &&
115 bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) {
116 MYLOGE("Invalid input: bad bugreport mode: %d", bugreport_mode);
117 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
118 }
119
120 std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
121 options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd,
122 screenshot_fd, is_screenshot_requested);
123
124 if (bugreport_fd.get() == -1 || (options->do_screenshot && screenshot_fd.get() == -1)) {
125 MYLOGE("Invalid filedescriptor");
126 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
127 }
128
129
130 ds_ = &(Dumpstate::GetInstance());
131 ds_->SetOptions(std::move(options));
132 ds_->listener_ = listener;
133
134 DumpstateInfo* ds_info = new DumpstateInfo();
135 ds_info->ds = ds_;
136 ds_info->calling_uid = calling_uid;
137 ds_info->calling_package = calling_package;
138
139 pthread_t thread;
140 // Initialize dumpstate
141 ds_->Initialize();
142 status_t err = pthread_create(&thread, nullptr, dumpstate_thread_main, ds_info);
143 if (err != 0) {
144 delete ds_info;
145 ds_info = nullptr;
146 MYLOGE("Could not create a thread");
147 signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
148 }
149 return binder::Status::ok();
150 }
151
cancelBugreport()152 binder::Status DumpstateService::cancelBugreport() {
153 std::lock_guard<std::mutex> lock(lock_);
154 ds_->Cancel();
155 return binder::Status::ok();
156 }
157
dump(int fd,const Vector<String16> &)158 status_t DumpstateService::dump(int fd, const Vector<String16>&) {
159 std::lock_guard<std::mutex> lock(lock_);
160 if (ds_ == nullptr) {
161 dprintf(fd, "Bugreport not in progress yet");
162 return NO_ERROR;
163 }
164 std::string destination = ds_->options_->bugreport_fd.get() != -1
165 ? StringPrintf("[fd:%d]", ds_->options_->bugreport_fd.get())
166 : ds_->bugreport_internal_dir_.c_str();
167 dprintf(fd, "id: %d\n", ds_->id_);
168 dprintf(fd, "pid: %d\n", ds_->pid_);
169 dprintf(fd, "update_progress: %s\n", ds_->options_->do_progress_updates ? "true" : "false");
170 dprintf(fd, "last_percent_progress: %d\n", ds_->last_reported_percent_progress_);
171 dprintf(fd, "progress:\n");
172 ds_->progress_->Dump(fd, " ");
173 dprintf(fd, "args: %s\n", ds_->options_->args.c_str());
174 dprintf(fd, "bugreport_mode: %s\n", ds_->options_->bugreport_mode.c_str());
175 dprintf(fd, "version: %s\n", ds_->version_.c_str());
176 dprintf(fd, "bugreport_dir: %s\n", destination.c_str());
177 dprintf(fd, "screenshot_path: %s\n", ds_->screenshot_path_.c_str());
178 dprintf(fd, "log_path: %s\n", ds_->log_path_.c_str());
179 dprintf(fd, "tmp_path: %s\n", ds_->tmp_path_.c_str());
180 dprintf(fd, "path: %s\n", ds_->path_.c_str());
181 dprintf(fd, "base_name: %s\n", ds_->base_name_.c_str());
182 dprintf(fd, "name: %s\n", ds_->name_.c_str());
183 dprintf(fd, "now: %ld\n", ds_->now_);
184 dprintf(fd, "is_zipping: %s\n", ds_->IsZipping() ? "true" : "false");
185 dprintf(fd, "notification title: %s\n", ds_->options_->notification_title.c_str());
186 dprintf(fd, "notification description: %s\n", ds_->options_->notification_description.c_str());
187
188 return NO_ERROR;
189 }
190 } // namespace os
191 } // namespace android
192