blob: e3945334aeca9c3ae442d77f4a0784b19c21b9b2 [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;
Yangster-mac97e7d202018-10-09 11:05:39 -070040
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070041void signalHandler(int sig) {
42 if (sig == SIGPIPE) {
43 // ShellSubscriber uses SIGPIPE as a signal to detect the end of the
44 // client process. Don't prematurely exit(1) here. Instead, ignore the
45 // signal and allow the write call to return EPIPE.
46 ALOGI("statsd received SIGPIPE. Ignoring signal.");
47 return;
Yangster-mac97e7d202018-10-09 11:05:39 -070048 }
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070049
50 if (gStatsService != nullptr) gStatsService->Terminate();
Eric Jeong2d997182019-10-03 13:33:48 -070051 ALOGW("statsd terminated on receiving signal %d.", sig);
52 exit(1);
Yangster-mac97e7d202018-10-09 11:05:39 -070053}
54
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070055void registerSignalHandlers()
Yangster-mac97e7d202018-10-09 11:05:39 -070056{
57 struct sigaction sa;
58 sigemptyset(&sa.sa_mask);
59 sa.sa_flags = 0;
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070060 sa.sa_handler = signalHandler;
61 sigaction(SIGPIPE, &sa, nullptr);
Yangster-mac97e7d202018-10-09 11:05:39 -070062 sigaction(SIGHUP, &sa, nullptr);
63 sigaction(SIGINT, &sa, nullptr);
64 sigaction(SIGQUIT, &sa, nullptr);
65 sigaction(SIGTERM, &sa, nullptr);
66}
67
Yao Chenef99c4f2017-09-22 16:26:54 -070068int main(int /*argc*/, char** /*argv*/) {
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070069 // Set up the looper
70 sp<Looper> looper(Looper::prepare(0 /* opts */));
71
72 // Set up the binder
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080073 ABinderProcess_setThreadPoolMaxThreadCount(9);
74 ABinderProcess_startThreadPool();
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070075
Yao Chen0f861862019-03-27 11:51:15 -070076 std::shared_ptr<LogEventQueue> eventQueue =
77 std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/);
78
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070079 // Create the service
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080080 gStatsService = SharedRefBase::make<StatsService>(looper, eventQueue);
81 // TODO(b/149582373): Set DUMP_FLAG_PROTO once libbinder_ndk supports
82 // setting dumpsys priorities.
83 binder_status_t status = AServiceManager_addService(gStatsService->asBinder().get(), "stats");
84 if (status != STATUS_OK) {
Howard Roa46b6582018-09-18 16:45:02 -070085 ALOGE("Failed to add service as AIDL service");
Joe Onorato5dcbc6c2017-08-29 15:13:58 -070086 return -1;
87 }
Howard Roa46b6582018-09-18 16:45:02 -070088
Ruchir Rastogi04d75c32020-04-13 17:06:57 -070089 registerSignalHandlers();
Bookatzb487b552017-09-18 11:26:01 -070090
Yangster-mac97e7d202018-10-09 11:05:39 -070091 gStatsService->sayHiToStatsCompanion();
Yao Chen49954cd2018-04-18 13:48:02 -070092
Yangster-mac97e7d202018-10-09 11:05:39 -070093 gStatsService->Startup();
94
Yao Chen0f861862019-03-27 11:51:15 -070095 sp<StatsSocketListener> socketListener = new StatsSocketListener(eventQueue);
Yao Chen49954cd2018-04-18 13:48:02 -070096
Yao Chen0f861862019-03-27 11:51:15 -070097 ALOGI("Statsd starts to listen to socket.");
98 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value
99 if (socketListener->startListener(600)) {
100 exit(1);
101 }
Joe Onorato5dcbc6c2017-08-29 15:13:58 -0700102
103 // Loop forever -- the reports run on this thread in a handler, and the
104 // binder calls remain responsive in their pool of one thread.
105 while (true) {
106 looper->pollAll(-1 /* timeoutMillis */);
107 }
108 ALOGW("statsd escaped from its loop.");
109
110 return 1;
111}