| 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 |  | 
|  | 34 | static 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 |  | 
|  | 39 | static 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 |  | 
|  | 44 | static void* callAndNotify(void* data) { | 
|  | 45 | Dumpstate& ds = *static_cast<Dumpstate*>(data); | 
|  | 46 | // TODO(111441001): Return status on listener. | 
|  | 47 | ds.Run(); | 
|  | 48 | MYLOGE("Finished Run()\n"); | 
|  | 49 | return nullptr; | 
|  | 50 | } | 
|  | 51 |  | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 52 | class DumpstateToken : public BnDumpstateToken {}; | 
|  | 53 | } | 
|  | 54 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 55 | DumpstateService::DumpstateService() : ds_(Dumpstate::GetInstance()) { | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | char const* DumpstateService::getServiceName() { | 
|  | 59 | return "dumpstate"; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | status_t DumpstateService::Start() { | 
|  | 63 | IPCThreadState::self()->disableBackgroundScheduling(true); | 
|  | 64 | status_t ret = BinderService<DumpstateService>::publish(); | 
|  | 65 | if (ret != android::OK) { | 
|  | 66 | return ret; | 
|  | 67 | } | 
|  | 68 | sp<ProcessState> ps(ProcessState::self()); | 
|  | 69 | ps->startThreadPool(); | 
|  | 70 | ps->giveThreadPoolName(); | 
|  | 71 | return android::OK; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | binder::Status DumpstateService::setListener(const std::string& name, | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 75 | const sp<IDumpstateListener>& listener, | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 76 | bool getSectionDetails, | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 77 | sp<IDumpstateToken>* returned_token) { | 
|  | 78 | *returned_token = nullptr; | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 79 | if (name.empty()) { | 
|  | 80 | MYLOGE("setListener(): name not set\n"); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 81 | return binder::Status::ok(); | 
|  | 82 | } | 
|  | 83 | if (listener == nullptr) { | 
|  | 84 | MYLOGE("setListener(): listener not set\n"); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 85 | return binder::Status::ok(); | 
|  | 86 | } | 
|  | 87 | std::lock_guard<std::mutex> lock(lock_); | 
|  | 88 | if (ds_.listener_ != nullptr) { | 
|  | 89 | 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] | 90 | return binder::Status::ok(); | 
|  | 91 | } | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 92 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 93 | ds_.listener_name_ = name; | 
|  | 94 | ds_.listener_ = listener; | 
| Vishnu Nair | 20cf503 | 2018-01-05 13:15:49 -0800 | [diff] [blame] | 95 | ds_.report_section_ = getSectionDetails; | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 96 | *returned_token = new DumpstateToken(); | 
|  | 97 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 98 | return binder::Status::ok(); | 
|  | 99 | } | 
|  | 100 |  | 
| Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 101 | binder::Status DumpstateService::startBugreport(const android::base::unique_fd& bugreport_fd, | 
|  | 102 | const android::base::unique_fd& screenshot_fd, | 
| Nandana Dutt | 05290ad | 2019-01-04 15:48:09 +0000 | [diff] [blame] | 103 | int bugreport_mode, | 
| Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 104 | const sp<IDumpstateListener>& listener) { | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 105 | MYLOGI("startBugreport() with mode: %d\n", bugreport_mode); | 
|  | 106 |  | 
|  | 107 | if (bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_FULL && | 
|  | 108 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_INTERACTIVE && | 
|  | 109 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_REMOTE && | 
|  | 110 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WEAR && | 
|  | 111 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_TELEPHONY && | 
| Abhijeet Kaur | 904e0e0 | 2018-12-05 14:03:01 +0000 | [diff] [blame] | 112 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_WIFI && | 
|  | 113 | bugreport_mode != Dumpstate::BugreportMode::BUGREPORT_DEFAULT) { | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 114 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, | 
|  | 115 | StringPrintf("Invalid bugreport mode: %d", bugreport_mode)); | 
|  | 116 | } | 
|  | 117 |  | 
| Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 118 | if (bugreport_fd.get() == -1 || screenshot_fd.get() == -1) { | 
|  | 119 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Invalid file descriptor"); | 
|  | 120 | } | 
|  | 121 |  | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 122 | std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>(); | 
| Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 123 | options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd, | 
|  | 124 | screenshot_fd); | 
|  | 125 |  | 
|  | 126 | std::lock_guard<std::mutex> lock(lock_); | 
|  | 127 | // TODO(b/111441001): Disallow multiple simultaneous bugreports. | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 128 | ds_.SetOptions(std::move(options)); | 
| Nandana Dutt | 54dbd67 | 2019-01-11 12:58:05 +0000 | [diff] [blame] | 129 | if (listener != nullptr) { | 
|  | 130 | ds_.listener_ = listener; | 
|  | 131 | } | 
| Nandana Dutt | 197661d | 2018-11-16 16:40:21 +0000 | [diff] [blame] | 132 |  | 
|  | 133 | pthread_t thread; | 
|  | 134 | status_t err = pthread_create(&thread, nullptr, callAndNotify, &ds_); | 
|  | 135 | if (err != 0) { | 
|  | 136 | return error(err, "Could not create a background thread."); | 
|  | 137 | } | 
| Nandana Dutt | cccbb5f | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 138 | return binder::Status::ok(); | 
|  | 139 | } | 
|  | 140 |  | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 141 | status_t DumpstateService::dump(int fd, const Vector<String16>&) { | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 142 | dprintf(fd, "id: %d\n", ds_.id_); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 143 | dprintf(fd, "pid: %d\n", ds_.pid_); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 144 | dprintf(fd, "update_progress: %s\n", ds_.options_->do_progress_updates ? "true" : "false"); | 
| Felipe Leme | 009ecbb | 2016-11-07 10:18:44 -0800 | [diff] [blame] | 145 | dprintf(fd, "update_progress_threshold: %d\n", ds_.update_progress_threshold_); | 
|  | 146 | dprintf(fd, "last_updated_progress: %d\n", ds_.last_updated_progress_); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 147 | dprintf(fd, "progress:\n"); | 
|  | 148 | ds_.progress_->Dump(fd, "  "); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 149 | dprintf(fd, "args: %s\n", ds_.options_->args.c_str()); | 
|  | 150 | dprintf(fd, "extra_options: %s\n", ds_.options_->extra_options.c_str()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 151 | dprintf(fd, "version: %s\n", ds_.version_.c_str()); | 
|  | 152 | dprintf(fd, "bugreport_dir: %s\n", ds_.bugreport_dir_.c_str()); | 
| Nandana Dutt | 979388e | 2018-11-30 16:48:55 +0000 | [diff] [blame] | 153 | dprintf(fd, "bugreport_internal_dir_: %s\n", ds_.bugreport_internal_dir_.c_str()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 154 | dprintf(fd, "screenshot_path: %s\n", ds_.screenshot_path_.c_str()); | 
|  | 155 | dprintf(fd, "log_path: %s\n", ds_.log_path_.c_str()); | 
|  | 156 | dprintf(fd, "tmp_path: %s\n", ds_.tmp_path_.c_str()); | 
| Felipe Leme | 7447d7c | 2016-11-03 18:12:22 -0700 | [diff] [blame] | 157 | dprintf(fd, "path: %s\n", ds_.path_.c_str()); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 158 | dprintf(fd, "extra_options: %s\n", ds_.options_->extra_options.c_str()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 159 | dprintf(fd, "base_name: %s\n", ds_.base_name_.c_str()); | 
|  | 160 | dprintf(fd, "name: %s\n", ds_.name_.c_str()); | 
|  | 161 | dprintf(fd, "now: %ld\n", ds_.now_); | 
|  | 162 | dprintf(fd, "is_zipping: %s\n", ds_.IsZipping() ? "true" : "false"); | 
|  | 163 | dprintf(fd, "listener: %s\n", ds_.listener_name_.c_str()); | 
| Nandana Dutt | 5fb117b | 2018-09-27 09:23:36 +0100 | [diff] [blame] | 164 | dprintf(fd, "notification title: %s\n", ds_.options_->notification_title.c_str()); | 
|  | 165 | dprintf(fd, "notification description: %s\n", ds_.options_->notification_description.c_str()); | 
| Felipe Leme | 75876a2 | 2016-10-27 16:31:27 -0700 | [diff] [blame] | 166 |  | 
|  | 167 | return NO_ERROR; | 
|  | 168 | } | 
|  | 169 | }  // namespace os | 
|  | 170 | }  // namespace android |