blob: 6ddd2e73be691ce0a495159ce09fef21dd6b3afa [file] [log] [blame]
Yifan Hong1b6f12f2021-06-03 20:01:57 -07001/*
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
25namespace {
26class Service : public android::BBinder {
27public:
28 Service(std::string_view descriptor) : mDescriptor(descriptor.data(), descriptor.size()) {}
29 const android::String16& getInterfaceDescriptor() const override { return mDescriptor; }
30
31private:
32 android::String16 mDescriptor;
33};
34} // namespace
35
36int 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}