Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 1 | /* |
yro | 0feae94 | 2017-11-15 14:38:48 -0800 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 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 | |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 17 | #define DEBUG false // STOPSHIP if true |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 19 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 20 | #include "StatsService.h" |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 21 | #include "socket/StatsSocketListener.h" |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 22 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 23 | #include <android/binder_interface_utils.h> |
| 24 | #include <android/binder_process.h> |
| 25 | #include <android/binder_manager.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 26 | #include <utils/Looper.h> |
George Burgess IV | ef8262c | 2018-04-23 09:32:41 -0700 | [diff] [blame] | 27 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 28 | #include <stdio.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 29 | #include <sys/stat.h> |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 30 | #include <sys/types.h> |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
| 32 | |
| 33 | using namespace android; |
Bookatz | 906a35c | 2017-09-20 15:26:44 -0700 | [diff] [blame] | 34 | using namespace android::os::statsd; |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 35 | using ::ndk::SharedRefBase; |
| 36 | using std::shared_ptr; |
| 37 | using std::make_shared; |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 38 | |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 39 | shared_ptr<StatsService> gStatsService = nullptr; |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 40 | |
| 41 | void sigHandler(int sig) { |
| 42 | if (gStatsService != nullptr) { |
| 43 | gStatsService->Terminate(); |
| 44 | } |
Eric Jeong | 2d99718 | 2019-10-03 13:33:48 -0700 | [diff] [blame] | 45 | ALOGW("statsd terminated on receiving signal %d.", sig); |
| 46 | exit(1); |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void registerSigHandler() |
| 50 | { |
| 51 | struct sigaction sa; |
| 52 | sigemptyset(&sa.sa_mask); |
| 53 | sa.sa_flags = 0; |
| 54 | sa.sa_handler = sigHandler; |
| 55 | sigaction(SIGHUP, &sa, nullptr); |
| 56 | sigaction(SIGINT, &sa, nullptr); |
| 57 | sigaction(SIGQUIT, &sa, nullptr); |
| 58 | sigaction(SIGTERM, &sa, nullptr); |
| 59 | } |
| 60 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 61 | int main(int /*argc*/, char** /*argv*/) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 62 | // Set up the looper |
| 63 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 64 | |
| 65 | // Set up the binder |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 66 | ABinderProcess_setThreadPoolMaxThreadCount(9); |
| 67 | ABinderProcess_startThreadPool(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 68 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 69 | std::shared_ptr<LogEventQueue> eventQueue = |
| 70 | std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/); |
| 71 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 72 | // Create the service |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 73 | gStatsService = SharedRefBase::make<StatsService>(looper, eventQueue); |
| 74 | // TODO(b/149582373): Set DUMP_FLAG_PROTO once libbinder_ndk supports |
| 75 | // setting dumpsys priorities. |
| 76 | binder_status_t status = AServiceManager_addService(gStatsService->asBinder().get(), "stats"); |
| 77 | if (status != STATUS_OK) { |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 78 | ALOGE("Failed to add service as AIDL service"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 79 | return -1; |
| 80 | } |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 81 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 82 | registerSigHandler(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 83 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 84 | gStatsService->sayHiToStatsCompanion(); |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 85 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 86 | gStatsService->Startup(); |
| 87 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 88 | sp<StatsSocketListener> socketListener = new StatsSocketListener(eventQueue); |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 89 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 90 | ALOGI("Statsd starts to listen to socket."); |
| 91 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
| 92 | if (socketListener->startListener(600)) { |
| 93 | exit(1); |
| 94 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 95 | |
| 96 | // Loop forever -- the reports run on this thread in a handler, and the |
| 97 | // binder calls remain responsive in their pool of one thread. |
| 98 | while (true) { |
| 99 | looper->pollAll(-1 /* timeoutMillis */); |
| 100 | } |
| 101 | ALOGW("statsd escaped from its loop."); |
| 102 | |
| 103 | return 1; |
| 104 | } |