1 /*
2 * Copyright (C) 2007 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 <dlfcn.h>
18 #include <pthread.h>
19
20 #include <chrono>
21 #include <thread>
22
23 #include <jni.h>
24 #include <nativehelper/JNIHelp.h>
25
26 #include <android/binder_manager.h>
27 #include <android/binder_stability.h>
28 #include <android/hidl/manager/1.2/IServiceManager.h>
29 #include <binder/IServiceManager.h>
30 #include <hidl/HidlTransportSupport.h>
31 #include <incremental_service.h>
32
33 #include <memtrackproxy/MemtrackProxy.h>
34 #include <schedulerservice/SchedulingPolicyService.h>
35 #include <sensorservicehidl/SensorManager.h>
36 #include <stats/StatsAidl.h>
37 #include <stats/StatsHal.h>
38
39 #include <bionic/malloc.h>
40 #include <bionic/reserved_signals.h>
41
42 #include <android-base/properties.h>
43 #include <utils/Log.h>
44 #include <utils/misc.h>
45 #include <utils/AndroidThreads.h>
46
47 using namespace std::chrono_literals;
48
49 namespace {
50
startStatsAidlService()51 static void startStatsAidlService() {
52 using aidl::android::frameworks::stats::IStats;
53 using aidl::android::frameworks::stats::StatsHal;
54
55 std::shared_ptr<StatsHal> statsService = ndk::SharedRefBase::make<StatsHal>();
56
57 const std::string instance = std::string() + IStats::descriptor + "/default";
58 const binder_exception_t err =
59 AServiceManager_addService(statsService->asBinder().get(), instance.c_str());
60 LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register AIDL %s: %d", instance.c_str(), err);
61 }
62
startStatsHidlService()63 static void startStatsHidlService() {
64 using android::frameworks::stats::V1_0::IStats;
65 using android::frameworks::stats::V1_0::implementation::StatsHal;
66
67 android::sp<IStats> statsHal = new StatsHal();
68 const android::status_t err = statsHal->registerAsService();
69 ALOGW_IF(err != android::OK, "Cannot register HIDL %s: %d", IStats::descriptor, err);
70 }
71
72 } // namespace
73
74 namespace android {
75
android_server_SystemServer_startIStatsService(JNIEnv *,jobject)76 static void android_server_SystemServer_startIStatsService(JNIEnv* /* env */, jobject /* clazz */) {
77 startStatsHidlService();
78 startStatsAidlService();
79 }
80
android_server_SystemServer_startMemtrackProxyService(JNIEnv * env,jobject)81 static void android_server_SystemServer_startMemtrackProxyService(JNIEnv* env,
82 jobject /* clazz */) {
83 using aidl::android::hardware::memtrack::MemtrackProxy;
84
85 const char* memtrackProxyService = "memtrack.proxy";
86
87 std::shared_ptr<MemtrackProxy> memtrack_proxy = ndk::SharedRefBase::make<MemtrackProxy>();
88 auto binder = memtrack_proxy->asBinder();
89
90 AIBinder_forceDowngradeToLocalStability(binder.get());
91
92 const binder_exception_t err = AServiceManager_addService(binder.get(), memtrackProxyService);
93 LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register %s: %d", memtrackProxyService, err);
94 }
95
android_server_SystemServer_startHidlServices(JNIEnv * env,jobject)96 static void android_server_SystemServer_startHidlServices(JNIEnv* env, jobject /* clazz */) {
97 using ::android::frameworks::schedulerservice::V1_0::ISchedulingPolicyService;
98 using ::android::frameworks::schedulerservice::V1_0::implementation::SchedulingPolicyService;
99 using ::android::frameworks::sensorservice::V1_0::ISensorManager;
100 using ::android::frameworks::sensorservice::V1_0::implementation::SensorManager;
101 using ::android::hardware::configureRpcThreadpool;
102 using ::android::hidl::manager::V1_0::IServiceManager;
103
104 status_t err;
105
106 configureRpcThreadpool(5, false /* callerWillJoin */);
107
108 JavaVM *vm;
109 LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM");
110
111 sp<ISensorManager> sensorService = new SensorManager(vm);
112 err = sensorService->registerAsService();
113 LOG_ALWAYS_FATAL_IF(err != OK, "Cannot register %s: %d", ISensorManager::descriptor, err);
114
115 sp<ISchedulingPolicyService> schedulingService = new SchedulingPolicyService();
116 if (IServiceManager::Transport::HWBINDER ==
117 hardware::defaultServiceManager1_2()->getTransport(ISchedulingPolicyService::descriptor,
118 "default")) {
119 err = schedulingService->registerAsService("default");
120 LOG_ALWAYS_FATAL_IF(err != OK, "Cannot register %s: %d",
121 ISchedulingPolicyService::descriptor, err);
122 } else {
123 ALOGW("%s is deprecated. Skipping registration.", ISchedulingPolicyService::descriptor);
124 }
125 }
126
android_server_SystemServer_initZygoteChildHeapProfiling(JNIEnv *,jobject)127 static void android_server_SystemServer_initZygoteChildHeapProfiling(JNIEnv* /* env */,
128 jobject /* clazz */) {
129 android_mallopt(M_INIT_ZYGOTE_CHILD_PROFILING, nullptr, 0);
130 }
131
android_server_SystemServer_fdtrackAbort(JNIEnv *,jobject)132 static void android_server_SystemServer_fdtrackAbort(JNIEnv*, jobject) {
133 sigval val;
134 val.sival_int = 1;
135 sigqueue(getpid(), BIONIC_SIGNAL_FDTRACK, val);
136 }
137
android_server_SystemServer_startIncrementalService(JNIEnv * env,jclass klass,jobject self)138 static jlong android_server_SystemServer_startIncrementalService(JNIEnv* env, jclass klass,
139 jobject self) {
140 return Incremental_IncrementalService_Start(env);
141 }
142
android_server_SystemServer_setIncrementalServiceSystemReady(JNIEnv * env,jclass klass,jlong handle)143 static void android_server_SystemServer_setIncrementalServiceSystemReady(JNIEnv* env, jclass klass,
144 jlong handle) {
145 Incremental_IncrementalService_OnSystemReady(handle);
146 }
147
148 /*
149 * JNI registration.
150 */
151 static const JNINativeMethod gMethods[] = {
152 /* name, signature, funcPtr */
153 {"startIStatsService", "()V", (void*)android_server_SystemServer_startIStatsService},
154 {"startMemtrackProxyService", "()V",
155 (void*)android_server_SystemServer_startMemtrackProxyService},
156 {"startHidlServices", "()V", (void*)android_server_SystemServer_startHidlServices},
157 {"initZygoteChildHeapProfiling", "()V",
158 (void*)android_server_SystemServer_initZygoteChildHeapProfiling},
159 {"fdtrackAbort", "()V", (void*)android_server_SystemServer_fdtrackAbort},
160 {"startIncrementalService", "()J",
161 (void*)android_server_SystemServer_startIncrementalService},
162 {"setIncrementalServiceSystemReady", "(J)V",
163 (void*)android_server_SystemServer_setIncrementalServiceSystemReady},
164 };
165
register_android_server_SystemServer(JNIEnv * env)166 int register_android_server_SystemServer(JNIEnv* env)
167 {
168 return jniRegisterNativeMethods(env, "com/android/server/SystemServer",
169 gMethods, NELEM(gMethods));
170 }
171
172 }; // namespace android
173