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 DEBUG false // STOPSHIP if true 18 #include "Log.h" 19 20 #include "StatsService.h" 21 #include "socket/StatsSocketListener.h" 22 23 #include <android/binder_interface_utils.h> 24 #include <android/binder_process.h> 25 #include <android/binder_manager.h> 26 #include <utils/Looper.h> 27 28 #include <stdio.h> 29 #include <sys/stat.h> 30 #include <sys/types.h> 31 #include <unistd.h> 32 33 using namespace android; 34 using namespace android::os::statsd; 35 using ::ndk::SharedRefBase; 36 using std::shared_ptr; 37 using std::make_shared; 38 39 shared_ptr<StatsService> gStatsService = nullptr; 40 sp<StatsSocketListener> gSocketListener = nullptr; 41 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; 49 } 50 51 if (gSocketListener != nullptr) gSocketListener->stopListener(); 52 if (gStatsService != nullptr) gStatsService->Terminate(); 53 ALOGW("statsd terminated on receiving signal %d.", sig); 54 exit(1); 55 } 56 57 void registerSignalHandlers() 58 { 59 struct sigaction sa; 60 sigemptyset(&sa.sa_mask); 61 sa.sa_flags = 0; 62 sa.sa_handler = signalHandler; 63 sigaction(SIGPIPE, &sa, nullptr); 64 sigaction(SIGHUP, &sa, nullptr); 65 sigaction(SIGINT, &sa, nullptr); 66 sigaction(SIGQUIT, &sa, nullptr); 67 sigaction(SIGTERM, &sa, nullptr); 68 } 69 70 int main(int /*argc*/, char** /*argv*/) { 71 // Set up the looper 72 sp<Looper> looper(Looper::prepare(0 /* opts */)); 73 74 // Set up the binder 75 ABinderProcess_setThreadPoolMaxThreadCount(9); 76 ABinderProcess_startThreadPool(); 77 78 std::shared_ptr<LogEventQueue> eventQueue = 79 std::make_shared<LogEventQueue>(4000 /*buffer limit. Buffer is NOT pre-allocated*/); 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 registerSignalHandlers(); 92 93 gStatsService->sayHiToStatsCompanion(); 94 95 gStatsService->Startup(); 96 97 gSocketListener = new StatsSocketListener(eventQueue); 98 99 ALOGI("Statsd starts to listen to socket."); 100 // Backlog and /proc/sys/net/unix/max_dgram_qlen set to large value 101 if (gSocketListener->startListener(600)) { 102 exit(1); 103 } 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 } 114