Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 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 <android-base/stringprintf.h> |
| 22 | |
| 23 | #include "android/os/BnDumpstate.h" |
| 24 | |
Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 25 | #include "DumpstateInternal.h" |
| 26 | |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 27 | using android::base::StringPrintf; |
| 28 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace os { |
| 31 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 32 | namespace { |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 33 | |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 34 | struct DumpstateInfo { |
| 35 | public: |
| 36 | Dumpstate* ds = nullptr; |
| 37 | int32_t calling_uid = -1; |
| 38 | std::string calling_package; |
| 39 | }; |
| 40 | |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 41 | static binder::Status exception(uint32_t code, const std::string& msg) { |
| 42 | MYLOGE("%s (%d) ", msg.c_str(), code); |
| 43 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 44 | } |
| 45 | |
| 46 | static binder::Status error(uint32_t code, const std::string& msg) { |
| 47 | MYLOGE("%s (%d) ", msg.c_str(), code); |
| 48 | return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); |
| 49 | } |
| 50 | |
| 51 | static void* callAndNotify(void* data) { |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 52 | DumpstateInfo& ds_info = *static_cast<DumpstateInfo*>(data); |
| 53 | ds_info.ds->Run(ds_info.calling_uid, ds_info.calling_package); |
Nandana Dutt | cc4ead8 | 2019-01-23 08:29:23 +0000 | [diff] [blame] | 54 | MYLOGD("Finished Run()\n"); |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
| 57 | |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 58 | class DumpstateToken : public BnDumpstateToken {}; |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 59 | |
| 60 | } // namespace |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 61 | |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 62 | DumpstateService::DumpstateService() : ds_(nullptr) { |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | char const* DumpstateService::getServiceName() { |
| 66 | return "dumpstate"; |
| 67 | } |
| 68 | |
| 69 | status_t DumpstateService::Start() { |
| 70 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 71 | status_t ret = BinderService<DumpstateService>::publish(); |
| 72 | if (ret != android::OK) { |
| 73 | return ret; |
| 74 | } |
| 75 | sp<ProcessState> ps(ProcessState::self()); |
| 76 | ps->startThreadPool(); |
| 77 | ps->giveThreadPoolName(); |
| 78 | return android::OK; |
| 79 | } |
| 80 | |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 81 | // Note: this method is part of the old flow and is not expected to be used in combination |
| 82 | // with startBugreport. |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 83 | binder::Status DumpstateService::setListener(const std::string& name, |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 84 | const sp<IDumpstateListener>& listener, |
Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 85 | bool getSectionDetails, |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 86 | sp<IDumpstateToken>* returned_token) { |
| 87 | *returned_token = nullptr; |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 88 | if (name.empty()) { |
| 89 | MYLOGE("setListener(): name not set\n"); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 90 | return binder::Status::ok(); |
| 91 | } |
| 92 | if (listener == nullptr) { |
| 93 | MYLOGE("setListener(): listener not set\n"); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 94 | return binder::Status::ok(); |
| 95 | } |
| 96 | std::lock_guard<std::mutex> lock(lock_); |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 97 | if (ds_ == nullptr) { |
| 98 | ds_ = &(Dumpstate::GetInstance()); |
| 99 | } |
| 100 | if (ds_->listener_ != nullptr) { |
| 101 | MYLOGE("setListener(%s): already set (%s)\n", name.c_str(), ds_->listener_name_.c_str()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 102 | return binder::Status::ok(); |
| 103 | } |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 104 | |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 105 | ds_->listener_name_ = name; |
| 106 | ds_->listener_ = listener; |
| 107 | ds_->report_section_ = getSectionDetails; |
Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 108 | *returned_token = new DumpstateToken(); |
| 109 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 110 | return binder::Status::ok(); |
| 111 | } |
| 112 | |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 113 | binder::Status DumpstateService::startBugreport(int32_t calling_uid, |
| 114 | const std::string& calling_package, |
Nandana Dutt | 5c91420 | 2019-01-16 17:45:18 +0000 | [diff] [blame] | 115 | const android::base::unique_fd& bugreport_fd, |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 116 | const android::base::unique_fd& screenshot_fd, |
Nandana Dutt | 05290ad | 2019-01-04 15:48:09 +0000 | [diff] [blame] | 117 | int bugreport_mode, |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 118 | const sp<IDumpstateListener>& listener) { |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 119 | MYLOGI("startBugreport() with mode: %d\n", bugreport_mode); |
| 120 | |
| 121 | if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL && |
| 122 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE && |
| 123 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE && |
| 124 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR && |
| 125 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY && |
Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 126 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI && |
| 127 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) { |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 128 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 129 | StringPrintf("Invalid bugreport mode: %d", bugreport_mode)); |
| 130 | } |
| 131 | |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 132 | if (bugreport_fd.get() == -1 || screenshot_fd.get() == -1) { |
| 133 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Invalid file descriptor"); |
| 134 | } |
| 135 | |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 136 | std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>(); |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 137 | options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd, |
| 138 | screenshot_fd); |
| 139 | |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 140 | // This is the bugreporting API flow, so ensure there is only one bugreport in progress at a |
| 141 | // time. |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 142 | std::lock_guard<std::mutex> lock(lock_); |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 143 | if (ds_ != nullptr) { |
| 144 | return exception(binder::Status::EX_SERVICE_SPECIFIC, |
| 145 | "There is already a bugreport in progress"); |
| 146 | } |
| 147 | ds_ = &(Dumpstate::GetInstance()); |
| 148 | ds_->SetOptions(std::move(options)); |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 149 | if (listener != nullptr) { |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 150 | ds_->listener_ = listener; |
Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 151 | } |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 152 | |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 153 | DumpstateInfo ds_info; |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 154 | ds_info.ds = ds_; |
Nandana Dutt | d2f5f08 | 2019-01-18 17:13:52 +0000 | [diff] [blame] | 155 | ds_info.calling_uid = calling_uid; |
| 156 | ds_info.calling_package = calling_package; |
| 157 | |
Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 158 | pthread_t thread; |
| 159 | status_t err = pthread_create(&thread, nullptr, callAndNotify, &ds_); |
| 160 | if (err != 0) { |
| 161 | return error(err, "Could not create a background thread."); |
| 162 | } |
Nandana Dutt | cccbb5f | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 163 | return binder::Status::ok(); |
| 164 | } |
| 165 | |
Nandana Dutt | cc4ead8 | 2019-01-23 08:29:23 +0000 | [diff] [blame] | 166 | binder::Status DumpstateService::cancelBugreport() { |
| 167 | // This is a no-op since the cancellation is done from java side via setting sys properties. |
| 168 | // See BugreportManagerServiceImpl. |
| 169 | // TODO(b/111441001): maybe make native and java sides use different binder interface |
| 170 | // to avoid these annoyances. |
| 171 | return binder::Status::ok(); |
| 172 | } |
| 173 | |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 174 | status_t DumpstateService::dump(int fd, const Vector<String16>&) { |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 175 | if (ds_ == nullptr) { |
| 176 | dprintf(fd, "Bugreport not in progress yet"); |
| 177 | return NO_ERROR; |
| 178 | } |
| 179 | std::string destination = ds_->options_->bugreport_fd.get() != -1 |
| 180 | ? StringPrintf("[fd:%d]", ds_->options_->bugreport_fd.get()) |
| 181 | : ds_->bugreport_internal_dir_.c_str(); |
| 182 | dprintf(fd, "id: %d\n", ds_->id_); |
| 183 | dprintf(fd, "pid: %d\n", ds_->pid_); |
| 184 | dprintf(fd, "update_progress: %s\n", ds_->options_->do_progress_updates ? "true" : "false"); |
| 185 | dprintf(fd, "update_progress_threshold: %d\n", ds_->update_progress_threshold_); |
| 186 | dprintf(fd, "last_updated_progress: %d\n", ds_->last_updated_progress_); |
Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 187 | dprintf(fd, "progress:\n"); |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 188 | ds_->progress_->Dump(fd, " "); |
| 189 | dprintf(fd, "args: %s\n", ds_->options_->args.c_str()); |
| 190 | dprintf(fd, "extra_options: %s\n", ds_->options_->extra_options.c_str()); |
| 191 | dprintf(fd, "version: %s\n", ds_->version_.c_str()); |
Nandana Dutt | 9a76d20 | 2019-01-21 15:56:48 +0000 | [diff] [blame] | 192 | dprintf(fd, "bugreport_dir: %s\n", destination.c_str()); |
Nandana Dutt | d973766 | 2019-01-29 15:59:38 +0000 | [diff] [blame^] | 193 | dprintf(fd, "screenshot_path: %s\n", ds_->screenshot_path_.c_str()); |
| 194 | dprintf(fd, "log_path: %s\n", ds_->log_path_.c_str()); |
| 195 | dprintf(fd, "tmp_path: %s\n", ds_->tmp_path_.c_str()); |
| 196 | dprintf(fd, "path: %s\n", ds_->path_.c_str()); |
| 197 | dprintf(fd, "extra_options: %s\n", ds_->options_->extra_options.c_str()); |
| 198 | dprintf(fd, "base_name: %s\n", ds_->base_name_.c_str()); |
| 199 | dprintf(fd, "name: %s\n", ds_->name_.c_str()); |
| 200 | dprintf(fd, "now: %ld\n", ds_->now_); |
| 201 | dprintf(fd, "is_zipping: %s\n", ds_->IsZipping() ? "true" : "false"); |
| 202 | dprintf(fd, "listener: %s\n", ds_->listener_name_.c_str()); |
| 203 | dprintf(fd, "notification title: %s\n", ds_->options_->notification_title.c_str()); |
| 204 | dprintf(fd, "notification description: %s\n", ds_->options_->notification_description.c_str()); |
Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 205 | |
| 206 | return NO_ERROR; |
| 207 | } |
| 208 | } // namespace os |
| 209 | } // namespace android |