blob: cd9c4e5b947b82835c05b7d2e90f09f8eb9a9390 [file] [log] [blame]
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07001/*
yro0feae942017-11-15 14:38:48 -08002 * Copyright (C) 2017 The Android Open Source Project
Joe Onorato5dcbc6c2017-08-29 15:13:58 -07003 *
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
Yao Chen49954cd2018-04-18 13:48:02 -070017#define DEBUG false // STOPSHIP if true
Joe Onorato9fc9edf2017-10-15 20:08:52 -070018#include "Log.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070019
Yao Chenef99c4f2017-09-22 16:26:54 -070020#include "StatsService.h"
Yao Chen49954cd2018-04-18 13:48:02 -070021#include "socket/StatsSocketListener.h"
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070022
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080023#include <android/binder_interface_utils.h>
24#include <android/binder_process.h>
25#include <android/binder_manager.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070026#include <utils/Looper.h>
George Burgess IVef8262c2018-04-23 09:32:41 -070027
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070028#include <stdio.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070029#include <sys/stat.h>
Yao Chenef99c4f2017-09-22 16:26:54 -070030#include <sys/types.h>
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070031#include <unistd.h>
32
33using namespace android;
Bookatz906a35c2017-09-20 15:26:44 -070034using namespace android::os::statsd;
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080035using ::ndk::SharedRefBase;
36using std::shared_ptr;
37using std::make_shared;
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070038
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080039shared_ptr<StatsService> gStatsService = nullptr;
Eric Jeong0963dbd2020-04-17 16:50:05 -070040sp<StatsSocketListener> gSocketListener = nullptr;
Yangster-mac97e7d202018-10-09 11:05:39 -070041
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070042void signalHandler(int sig) {
43 if (sig == SIGPIPE) {
44 // ShellSubscriber uses SIGPIPE as a signal to detect the end of the
45 // client process. Don't prematurely exit(1) here. Instead, ignore the
46 // signal and allow the write call to return EPIPE.
47 ALOGI("statsd received SIGPIPE. Ignoring signal.");
48 return;
Yangster-mac97e7d202018-10-09 11:05:39 -070049 }
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070050
Eric Jeong0963dbd2020-04-17 16:50:05 -070051 if (gSocketListener != nullptr) gSocketListener->stopListener();
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070052 if (gStatsService != nullptr) gStatsService->Terminate();
Eric Jeong2d997182019-10-03 13:33:48 -070053 ALOGW("statsd terminated on receiving signal %d.", sig);
54 exit(1);
Yangster-mac97e7d202018-10-09 11:05:39 -070055}
56
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070057void registerSignalHandlers()
Yangster-mac97e7d202018-10-09 11:05:39 -070058{
59 struct sigaction sa;
60 sigemptyset(&sa.sa_mask);
61 sa.sa_flags = 0;
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070062 sa.sa_handler = signalHandler;
63 sigaction(SIGPIPE, &sa, nullptr);
Yangster-mac97e7d202018-10-09 11:05:39 -070064 sigaction(SIGHUP, &sa, nullptr);
65 sigaction(SIGINT, &sa, nullptr);
66 sigaction(SIGQUIT, &sa, nullptr);
67 sigaction(SIGTERM, &sa, nullptr);
68}
69
Yao Chenef99c4f2017-09-22 16:26:54 -070070int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070071 // Set up the looper
72 sp<Looper> looper(Looper::prepare(0 /* opts */));
73
74 // Set up the binder
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080075 ABinderProcess_setThreadPoolMaxThreadCount(9);
76 ABinderProcess_startThreadPool();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070077
Yao Chen0f861862019-03-27 11:51:15 -070078 std::shared_ptr<LogEventQueue> eventQueue =
79 std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/);
80
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070081 // Create the service
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080082 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) {
Howard Roa46b6582018-09-18 16:45:02 -070087 ALOGE("Failed to add service as AIDL service");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070088 return -1;
89 }
Howard Roa46b6582018-09-18 16:45:02 -070090
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070091 registerSignalHandlers();
Bookatzb487b552017-09-18 11:26:01 -070092
Yangster-mac97e7d202018-10-09 11:05:39 -070093 gStatsService->sayHiToStatsCompanion();
Yao Chen49954cd2018-04-18 13:48:02 -070094
Yangster-mac97e7d202018-10-09 11:05:39 -070095 gStatsService->Startup();
96
Eric Jeong0963dbd2020-04-17 16:50:05 -070097 gSocketListener = new StatsSocketListener(eventQueue);
Yao Chen49954cd2018-04-18 13:48:02 -070098
Yao Chen0f861862019-03-27 11:51:15 -070099 ALOGI("Statsd starts to listen to socket.");
100 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
Eric Jeong0963dbd2020-04-17 16:50:05 -0700101 if (gSocketListener->startListener(600)) {
Yao Chen0f861862019-03-27 11:51:15 -0700102 exit(1);
103 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700104
105 // Loop forever -- the reports run on this thread in a handler, and the
106 // binder calls remain responsive in their pool of one thread.
107 while (true) {
108 looper->pollAll(-1 /* timeoutMillis */);
109 }
110 ALOGW("statsd escaped from its loop.");
111
112 return 1;
113}