xref: /aosp_15_r20/system/extras/partition_tools/lpdumpd.cc (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
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>
23 #include <android-base/properties.h>
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 
run(const std::vector<std::string> & args,std::string * aidl_return)43     Status run(const std::vector<std::string>& args, std::string* aidl_return) override {
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());
48         }
49         LOG(DEBUG) << "Dumping with args: " << base::Join(args, " ");
50         std::stringstream output;
51         std::stringstream error;
52         int ret = LpdumpMain((int)local_argv.size(), local_argv.data(), output, error);
53         std::string error_str = error.str();
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 {
61             return Status::fromServiceSpecificError(ret, error_str.c_str());
62         }
63     }
64 };
65 
66 }  // namespace lpdump
67 }  // namespace android
68 
main(int,char **)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 }
79