1 /*
2 * Copyright (C) 2017 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 STATSD_DEBUG false // STOPSHIP if true
18 #include "Log.h"
19
20 #include "StatsService.h"
21 #include "flags/FlagProvider.h"
22 #include "socket/StatsSocketListener.h"
23
24 #include <android/binder_interface_utils.h>
25 #include <android/binder_process.h>
26 #include <android/binder_manager.h>
27 #include <utils/Looper.h>
28
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 using namespace android;
35 using namespace android::os::statsd;
36 using ::ndk::SharedRefBase;
37 using std::shared_ptr;
38 using std::make_shared;
39
40 shared_ptr<StatsService> gStatsService = nullptr;
41 sp<StatsSocketListener> gSocketListener = nullptr;
42 int gCtrlPipe[2];
43
signalHandler(int sig)44 void signalHandler(int sig) {
45 ALOGW("statsd terminated on receiving signal %d.", sig);
46 const char c = 'q';
47 write(gCtrlPipe[1], &c, 1);
48 }
49
registerSignalHandlers()50 void registerSignalHandlers()
51 {
52 struct sigaction sa;
53 sigemptyset(&sa.sa_mask);
54 sa.sa_flags = 0;
55
56 sa.sa_handler = SIG_IGN;
57 // ShellSubscriber uses SIGPIPE as a signal to detect the end of the
58 // client process. Don't prematurely exit(1) here. Instead, ignore the
59 // signal and allow the write call to return EPIPE.
60 sigaction(SIGPIPE, &sa, nullptr);
61
62 pipe2(gCtrlPipe, O_CLOEXEC);
63 sa.sa_handler = signalHandler;
64 sigaction(SIGTERM, &sa, nullptr);
65 }
66
main(int,char **)67 int main(int /*argc*/, char** /*argv*/) {
68 // Set up the looper
69 sp<Looper> looper(Looper::prepare(0 /* opts */));
70
71 // Set up the binder
72 ABinderProcess_setThreadPoolMaxThreadCount(9);
73 ABinderProcess_startThreadPool();
74
75 std::shared_ptr<LogEventQueue> eventQueue =
76 std::make_shared<LogEventQueue>(4000 /*buffer limit. Buffer is NOT pre-allocated*/);
77
78 // Initialize boot flags
79 FlagProvider::getInstance().initBootFlags({});
80
81 // Create the service
82 gStatsService = SharedRefBase::make<StatsService>(looper, eventQueue);
83 // TODO(b/149582373): Set DUMP_FLAG_PROTO once libbinder_ndk supports
84 // setting dumpsys priorities.
85 binder_status_t status = AServiceManager_addService(gStatsService->asBinder().get(), "stats");
86 if (status != STATUS_OK) {
87 ALOGE("Failed to add service as AIDL service");
88 return -1;
89 }
90
91 gStatsService->sayHiToStatsCompanion();
92
93 gStatsService->Startup();
94
95 gSocketListener = new StatsSocketListener(eventQueue);
96
97 ALOGI("Statsd starts to listen to socket.");
98 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
99 if (gSocketListener->startListener(600)) {
100 exit(1);
101 }
102
103 // Use self-pipe to notify this thread to gracefully quit
104 // when receiving SIGTERM
105 registerSignalHandlers();
106 std::thread([] {
107 while (true) {
108 char c;
109 int i = read(gCtrlPipe[0], &c, 1);
110 if (i < 0) {
111 if (errno == EINTR) continue;
112 }
113 gSocketListener->stopListener();
114 gStatsService->Terminate();
115 exit(1);
116 }
117 }).detach();
118
119 // Loop forever -- the reports run on this thread in a handler, and the
120 // binder calls remain responsive in their pool of one thread.
121 while (true) {
122 looper->pollAll(-1 /* timeoutMillis */);
123 }
124 ALOGW("statsd escaped from its loop.");
125
126 return 1;
127 }
128