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 "carpowerpolicyd"
18
19 #include "CarPowerPolicyServer.h"
20
21 #include <android-base/result.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/ProcessState.h>
24 #include <log/log.h>
25 #include <utils/Looper.h>
26 #include <utils/StrongPointer.h>
27
28 #include <signal.h>
29
30 using android::IPCThreadState;
31 using android::Looper;
32 using android::ProcessState;
33 using android::sp;
34 using android::frameworks::automotive::powerpolicy::CarPowerPolicyServer;
35
36 namespace {
37
38 const size_t kMaxBinderThreadCount = 2;
39
sigHandler(int sig)40 void sigHandler(int sig) {
41 IPCThreadState::self()->stopProcess();
42 CarPowerPolicyServer::terminateService();
43 ALOGW("powerpolicy daemon terminated on receiving signal %d.", sig);
44 exit(1);
45 }
46
registerSigHandler()47 void registerSigHandler() {
48 struct sigaction sa;
49 sigemptyset(&sa.sa_mask);
50 sa.sa_flags = 0;
51 sa.sa_handler = sigHandler;
52 sigaction(SIGQUIT, &sa, nullptr);
53 sigaction(SIGTERM, &sa, nullptr);
54 }
55
56 } // namespace
57
main(int,char **)58 int main(int /*argc*/, char** /*argv*/) {
59 registerSigHandler();
60
61 // Set up the binder
62 sp<ProcessState> ps(ProcessState::self());
63 ps->setThreadPoolMaxThreadCount(kMaxBinderThreadCount);
64 ps->startThreadPool();
65 ps->giveThreadPoolName();
66 IPCThreadState::self()->disableBackgroundScheduling(true);
67
68 // Set up the looper
69 sp<Looper> looper(Looper::prepare(/*opts=*/0));
70
71 // Start the services
72 auto result = CarPowerPolicyServer::startService(looper);
73 if (!result.ok()) {
74 ALOGE("Failed to start service: %s", result.error().message().c_str());
75 CarPowerPolicyServer::terminateService();
76 exit(result.error().code());
77 }
78
79 while (true) {
80 looper->pollAll(/*timeoutMillis=*/-1);
81 }
82 ALOGW("Car power policy server escaped from its loop.");
83
84 return 0;
85 }
86