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; |
Eric Jeong | 0963dbd | 2020-04-17 16:50:05 -0700 | [diff] [blame] | 40 | sp<StatsSocketListener> gSocketListener = nullptr; |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 41 | |
Ruchir Rastogi | 04d75c3 | 2020-04-13 17:06:57 -0700 | [diff] [blame] | 42 | void 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-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 49 | } |
Ruchir Rastogi | 04d75c3 | 2020-04-13 17:06:57 -0700 | [diff] [blame] | 50 | |
Eric Jeong | 0963dbd | 2020-04-17 16:50:05 -0700 | [diff] [blame] | 51 | if (gSocketListener != nullptr) gSocketListener->stopListener(); |
Ruchir Rastogi | 04d75c3 | 2020-04-13 17:06:57 -0700 | [diff] [blame] | 52 | if (gStatsService != nullptr) gStatsService->Terminate(); |
Eric Jeong | 2d99718 | 2019-10-03 13:33:48 -0700 | [diff] [blame] | 53 | ALOGW("statsd terminated on receiving signal %d.", sig); |
| 54 | exit(1); |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ruchir Rastogi | 04d75c3 | 2020-04-13 17:06:57 -0700 | [diff] [blame] | 57 | void registerSignalHandlers() |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 58 | { |
| 59 | struct sigaction sa; |
| 60 | sigemptyset(&sa.sa_mask); |
| 61 | sa.sa_flags = 0; |
Ruchir Rastogi | 04d75c3 | 2020-04-13 17:06:57 -0700 | [diff] [blame] | 62 | sa.sa_handler = signalHandler; |
| 63 | sigaction(SIGPIPE, &sa, nullptr); |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 64 | sigaction(SIGHUP, &sa, nullptr); |
| 65 | sigaction(SIGINT, &sa, nullptr); |
| 66 | sigaction(SIGQUIT, &sa, nullptr); |
| 67 | sigaction(SIGTERM, &sa, nullptr); |
| 68 | } |
| 69 | |
Yao Chen | ef99c4f | 2017-09-22 16:26:54 -0700 | [diff] [blame] | 70 | int main(int /*argc*/, char** /*argv*/) { |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 71 | // Set up the looper |
| 72 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 73 | |
| 74 | // Set up the binder |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 75 | ABinderProcess_setThreadPoolMaxThreadCount(9); |
| 76 | ABinderProcess_startThreadPool(); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 77 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 78 | std::shared_ptr<LogEventQueue> eventQueue = |
| 79 | std::make_shared<LogEventQueue>(2000 /*buffer limit. Buffer is NOT pre-allocated*/); |
| 80 | |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 81 | // Create the service |
Ruchir Rastogi | e449b0c | 2020-02-10 17:40:09 -0800 | [diff] [blame] | 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) { |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 87 | ALOGE("Failed to add service as AIDL service"); |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 88 | return -1; |
| 89 | } |
Howard Ro | a46b658 | 2018-09-18 16:45:02 -0700 | [diff] [blame] | 90 | |
Ruchir Rastogi | 04d75c3 | 2020-04-13 17:06:57 -0700 | [diff] [blame] | 91 | registerSignalHandlers(); |
Bookatz | b487b55 | 2017-09-18 11:26:01 -0700 | [diff] [blame] | 92 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 93 | gStatsService->sayHiToStatsCompanion(); |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 94 | |
Yangster-mac | 97e7d20 | 2018-10-09 11:05:39 -0700 | [diff] [blame] | 95 | gStatsService->Startup(); |
| 96 | |
Eric Jeong | 0963dbd | 2020-04-17 16:50:05 -0700 | [diff] [blame] | 97 | gSocketListener = new StatsSocketListener(eventQueue); |
Yao Chen | 49954cd | 2018-04-18 13:48:02 -0700 | [diff] [blame] | 98 | |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 99 | ALOGI("Statsd starts to listen to socket."); |
| 100 | // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value |
Eric Jeong | 0963dbd | 2020-04-17 16:50:05 -0700 | [diff] [blame] | 101 | if (gSocketListener->startListener(600)) { |
Yao Chen | 0f86186 | 2019-03-27 11:51:15 -0700 | [diff] [blame] | 102 | exit(1); |
| 103 | } |
Joe Onorato | 5dcbc6c | 2017-08-29 15:13:58 -0700 | [diff] [blame] | 104 | |
| 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 | } |