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 #define LOG_TAG "carwatchdogd"
18
19 #include "ServiceManager.h"
20
21 #include <android-base/chrono_utils.h>
22 #include <android-base/properties.h>
23 #include <android-base/result.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/IServiceManager.h>
26 #include <binder/ProcessState.h>
27 #include <log/log.h>
28 #include <utils/Looper.h>
29
30 #include <signal.h>
31
32 #include <thread> // NOLINT(build/c++11)
33
34 using ::android::IPCThreadState;
35 using ::android::Looper;
36 using ::android::ProcessState;
37 using ::android::sp;
38 using ::android::automotive::watchdog::ServiceManager;
39 using ::android::base::Result;
40
41 namespace {
42
43 const size_t kMaxBinderThreadCount = 16;
44
sigHandler(int sig)45 void sigHandler(int sig) {
46 IPCThreadState::self()->stopProcess();
47 ServiceManager::terminateServices();
48 ALOGW("car watchdog server terminated on receiving signal %d.", sig);
49 exit(1);
50 }
51
registerSigHandler()52 void registerSigHandler() {
53 struct sigaction sa;
54 sigemptyset(&sa.sa_mask);
55 sa.sa_flags = 0;
56 sa.sa_handler = sigHandler;
57 sigaction(SIGQUIT, &sa, nullptr);
58 sigaction(SIGTERM, &sa, nullptr);
59 }
60
61 } // namespace
62
main(int,char **)63 int main(int /*argc*/, char** /*argv*/) {
64 // Set up the looper
65 sp<Looper> looper(Looper::prepare(/*opts=*/0));
66
67 // Start the services
68 auto result = ServiceManager::startServices(looper);
69 if (!result.ok()) {
70 ALOGE("Failed to start services: %s", result.error().message().c_str());
71 ServiceManager::terminateServices();
72 exit(result.error().code());
73 }
74
75 registerSigHandler();
76
77 // Wait for the service manager before starting binder mediator.
78 while (android::base::GetProperty("init.svc.servicemanager", "") != "running") {
79 // Poll frequent enough so the CarWatchdogDaemonHelper can connect to the daemon during
80 // system boot up.
81 std::this_thread::sleep_for(250ms);
82 }
83
84 // Set up the binder
85 sp<ProcessState> ps(ProcessState::self());
86 ps->setThreadPoolMaxThreadCount(kMaxBinderThreadCount);
87 ps->startThreadPool();
88 ps->giveThreadPoolName();
89 IPCThreadState::self()->disableBackgroundScheduling(true);
90
91 result = ServiceManager::startBinderMediator();
92 if (!result.ok()) {
93 ALOGE("Failed to start binder mediator: %s", result.error().message().c_str());
94 ServiceManager::terminateServices();
95 exit(result.error().code());
96 }
97
98 // Loop forever -- the health check runs on this thread in a handler, and the binder calls
99 // remain responsive in their pool of threads.
100 while (true) {
101 looper->pollAll(/*timeoutMillis=*/-1);
102 }
103 ALOGW("Car watchdog server escaped from its loop.");
104
105 return 0;
106 }
107