1 /*
2 * Copyright (C) 2020 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 <android-base/logging.h>
18 #include <binder/IBinder.h>
19 #include <binder/IPCThreadState.h>
20 #include <binder/IServiceManager.h>
21 #include <binder/LazyServiceRegistrar.h>
22 #include "LazyTestService.h"
23
24 using android::BBinder;
25 using android::IBinder;
26 using android::IPCThreadState;
27 using android::OK;
28 using android::sp;
29 using android::binder::LazyServiceRegistrar;
30 using android::binder::LazyTestService;
31
setupDoubleLazyServer()32 void setupDoubleLazyServer() {
33 sp<LazyTestService> service1 = sp<LazyTestService>::make();
34 sp<LazyTestService> service2 = sp<LazyTestService>::make();
35
36 // Simulate another callback here, to test to make sure the actual instance
37 // we are relying on doesn't have its state messed up when multiple client
38 // callbacks are registered.
39 // DO NOT COPY - INTENTIONALLY TESTING BAD BEHAVIOR
40 auto extra = LazyServiceRegistrar::createExtraTestInstance();
41 extra.forcePersist(true); // don't allow this instance to handle process lifetime
42 CHECK_EQ(OK, extra.registerService(service1, "aidl_lazy_test_1"));
43 // DO NOT COPY - INTENTIONALLY TESTING BAD BEHAVIOR
44
45 auto lazyRegistrar = LazyServiceRegistrar::getInstance();
46 CHECK_EQ(OK, lazyRegistrar.registerService(service1, "aidl_lazy_test_1"));
47 CHECK_EQ(OK, lazyRegistrar.registerService(service2, "aidl_lazy_test_2"));
48 }
49
setupQuitterServer()50 void setupQuitterServer() {
51 auto lazyRegistrar = LazyServiceRegistrar::getInstance();
52 lazyRegistrar.setActiveServicesCallback([](bool hasClients) mutable -> bool {
53 // intentional bad behavior, for instance, simulating a system
54 // shutdown at this time
55 if (hasClients) exit(EXIT_SUCCESS);
56 return false;
57 });
58
59 sp<LazyTestService> service = sp<LazyTestService>::make();
60 CHECK_EQ(OK, lazyRegistrar.registerService(service, "aidl_lazy_test_quit"));
61 }
62
main(int argc,char ** argv)63 int main(int argc, char** argv) {
64 if (argc == 1) {
65 setupDoubleLazyServer();
66 } else if (argc == 2 && argv[1] == std::string("quit")) {
67 setupQuitterServer();
68 } else {
69 LOG_ALWAYS_FATAL("usage: %s [quit]", argv[0]);
70 }
71
72 IPCThreadState::self()->joinThreadPool();
73 return 1;
74 }
75