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