1 /*
2  * Copyright (C) 2021 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 <sysexits.h>
18 
19 #include <android-base/logging.h>
20 #include <binder/IBinder.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 
25 namespace {
26 class Service : public android::BBinder {
27 public:
Service(std::string_view descriptor)28     Service(std::string_view descriptor) : mDescriptor(descriptor.data(), descriptor.size()) {}
getInterfaceDescriptor() const29     const android::String16& getInterfaceDescriptor() const override { return mDescriptor; }
30 
31 private:
32     android::String16 mDescriptor;
33 };
34 } // namespace
35 
main(int argc,char ** argv)36 int main(int argc, char** argv) {
37     if (argc != 3) {
38         std::cerr << "usage: " << argv[0] << " <service-name> <interface-descriptor>" << std::endl;
39         return EX_USAGE;
40     }
41     auto name = argv[1];
42     auto descriptor = argv[2];
43 
44     auto sm = android::defaultServiceManager();
45     CHECK(sm != nullptr);
46     auto service = android::sp<Service>::make(descriptor);
47     auto status = sm->addService(android::String16(name), service);
48     CHECK_EQ(android::OK, status) << android::statusToString(status);
49     std::cout << "running..." << std::endl;
50     android::ProcessState::self()->startThreadPool();
51     android::IPCThreadState::self()->joinThreadPool();
52     LOG(ERROR) << "joinThreadPool exits";
53     return EX_SOFTWARE;
54 }
55