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/strings.h>
24 #include <android/lpdump/BnLpdump.h>
25 #include <android/lpdump/ILpdump.h>
26 #include <binder/IPCThreadState.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/ProcessState.h>
29 
30 int LpdumpMain(int argc, char* argv[], std::ostream&, std::ostream&);
31 
32 namespace android {
33 namespace lpdump {
34 
35 using binder::Status;
36 
37 class Lpdump : public BnLpdump {
38   public:
39     Lpdump() = default;
40     virtual ~Lpdump() = default;
41 
run(const std::vector<std::string> & args,std::string * aidl_return)42     Status run(const std::vector<std::string>& args, std::string* aidl_return) override {
43         std::vector<char*> local_argv;
44         std::vector<std::string> local_args = args;
45         for (auto& arg : local_args) {
46             local_argv.push_back(arg.data());
47         }
48         LOG(DEBUG) << "Dumping with args: " << base::Join(args, " ");
49         std::stringstream output;
50         std::stringstream error;
51         int ret = LpdumpMain((int)local_argv.size(), local_argv.data(), output, error);
52         std::string error_str = error.str();
53         if (ret == 0) {
54             if (!error_str.empty()) {
55                 LOG(WARNING) << error_str;
56             }
57             *aidl_return = output.str();
58             return Status::ok();
59         } else {
60             return Status::fromServiceSpecificError(ret, error_str.c_str());
61         }
62     }
63 };
64 
65 }  // namespace lpdump
66 }  // namespace android
67 
main(int,char **)68 int main(int, char**) {
69     using namespace android;
70 
71     sp<lpdump::Lpdump> service = new lpdump::Lpdump();
72     defaultServiceManager()->addService(String16("lpdump_service"), service);
73     LOG(VERBOSE) << "lpdumpd starting";
74     ProcessState::self()->startThreadPool();
75     IPCThreadState::self()->joinThreadPool();
76     return 0;
77 }
78