Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include <limits> |
| 18 | #include <sstream> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <android-base/logging.h> |
David Anderson | b694b1d | 2023-07-14 09:02:55 -0700 | [diff] [blame] | 23 | #include <android-base/properties.h> |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 24 | #include <android-base/strings.h> |
| 25 | #include <android/lpdump/BnLpdump.h> |
| 26 | #include <android/lpdump/ILpdump.h> |
| 27 | #include <binder/IPCThreadState.h> |
| 28 | #include <binder/IServiceManager.h> |
| 29 | #include <binder/ProcessState.h> |
| 30 | |
| 31 | int LpdumpMain(int argc, char* argv[], std::ostream&, std::ostream&); |
| 32 | |
| 33 | namespace android { |
| 34 | namespace lpdump { |
| 35 | |
| 36 | using binder::Status; |
| 37 | |
| 38 | class Lpdump : public BnLpdump { |
| 39 | public: |
| 40 | Lpdump() = default; |
| 41 | virtual ~Lpdump() = default; |
| 42 | |
| 43 | Status run(const std::vector<std::string>& args, std::string* aidl_return) override { |
David Anderson | 6b7a582 | 2019-11-19 17:04:39 -0800 | [diff] [blame] | 44 | std::vector<char*> local_argv; |
| 45 | std::vector<std::string> local_args = args; |
| 46 | for (auto& arg : local_args) { |
| 47 | local_argv.push_back(arg.data()); |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 48 | } |
| 49 | LOG(DEBUG) << "Dumping with args: " << base::Join(args, " "); |
| 50 | std::stringstream output; |
Yifan Hong | 9960b98 | 2019-03-28 10:42:05 -0700 | [diff] [blame] | 51 | std::stringstream error; |
David Anderson | 6b7a582 | 2019-11-19 17:04:39 -0800 | [diff] [blame] | 52 | int ret = LpdumpMain((int)local_argv.size(), local_argv.data(), output, error); |
Yifan Hong | 9960b98 | 2019-03-28 10:42:05 -0700 | [diff] [blame] | 53 | std::string error_str = error.str(); |
Sandeep Dhavale | acb7546 | 2024-08-19 13:56:40 -0700 | [diff] [blame] | 54 | if (ret == 0) { |
| 55 | if (!error_str.empty()) { |
| 56 | LOG(WARNING) << error_str; |
| 57 | } |
| 58 | *aidl_return = output.str(); |
| 59 | return Status::ok(); |
| 60 | } else { |
Yifan Hong | 9960b98 | 2019-03-28 10:42:05 -0700 | [diff] [blame] | 61 | return Status::fromServiceSpecificError(ret, error_str.c_str()); |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 62 | } |
Yifan Hong | b84fb57 | 2019-03-15 14:19:08 -0700 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | } // namespace lpdump |
| 67 | } // namespace android |
| 68 | |
| 69 | int main(int, char**) { |
| 70 | using namespace android; |
| 71 | |
| 72 | sp<lpdump::Lpdump> service = new lpdump::Lpdump(); |
| 73 | defaultServiceManager()->addService(String16("lpdump_service"), service); |
| 74 | LOG(VERBOSE) << "lpdumpd starting"; |
| 75 | ProcessState::self()->startThreadPool(); |
| 76 | IPCThreadState::self()->joinThreadPool(); |
| 77 | return 0; |
| 78 | } |