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 <binder/IPCThreadState.h>
22 #include <binder/ProcessState.h>
23 #include <log/log.h>
24 #include <utils/Looper.h>
25 
26 using ::android::IPCThreadState;
27 using ::android::Looper;
28 using ::android::ProcessState;
29 using ::android::sp;
30 using ::android::automotive::watchdog::ServiceManager;
31 
32 const size_t kMaxBinderThreadCount = 16;
33 
main(int,char **)34 int main(int /*argc*/, char** /*argv*/) {
35     // Set up the binder
36     sp<ProcessState> ps(ProcessState::self());
37     ps->setThreadPoolMaxThreadCount(kMaxBinderThreadCount);
38     ps->startThreadPool();
39     ps->giveThreadPoolName();
40     IPCThreadState::self()->disableBackgroundScheduling(true);
41 
42     sp<Looper> mainLooper(Looper::prepare(/*opts=*/0));
43 
44     auto result = ServiceManager::getInstance()->startServices(mainLooper);
45     if (!result.ok()) {
46         ALOGE("Failed to start services: %s", result.error().message().c_str());
47         ServiceManager::terminate();
48         exit(result.error().code());
49     }
50 
51     // Loop forever -- the health check runs on this thread in a handler, and the binder calls
52     // remain responsive in their pool of threads.
53     while (true) {
54         mainLooper->pollAll(/*timeoutMillis=*/-1);
55     }
56     ALOGW("Car watchdog server escaped from its loop.");
57     ServiceManager::terminate();
58 
59     return 0;
60 }
61