summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-11-24 00:27:56 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2022-11-24 00:27:56 +0000
commit6cb94177f8be3f4ae8f1a38131300beedc103e93 (patch)
tree235cc23b6c69f9d34b7dd020070e8d3b444b91ea
parentdbb07873da6ad8ee71ae321d2581b4959af51642 (diff)
parent992c86cfa9b615e8e62426a5014a0b10501a37b0 (diff)
downloadStatsD-android13-d4-s2-release.tar.gz
Change-Id: If31a718db195477f0e61b8cca0032d9e4cd1c79d
-rw-r--r--statsd/src/main.cpp44
1 files changed, 27 insertions, 17 deletions
diff --git a/statsd/src/main.cpp b/statsd/src/main.cpp
index bd2c0e42..67bb65db 100644
--- a/statsd/src/main.cpp
+++ b/statsd/src/main.cpp
@@ -39,20 +39,12 @@ using std::make_shared;
shared_ptr<StatsService> gStatsService = nullptr;
sp<StatsSocketListener> gSocketListener = nullptr;
+int gCtrlPipe[2];
void signalHandler(int sig) {
- if (sig == SIGPIPE) {
- // ShellSubscriber uses SIGPIPE as a signal to detect the end of the
- // client process. Don't prematurely exit(1) here. Instead, ignore the
- // signal and allow the write call to return EPIPE.
- ALOGI("statsd received SIGPIPE. Ignoring signal.");
- return;
- }
-
- if (gSocketListener != nullptr) gSocketListener->stopListener();
- if (gStatsService != nullptr) gStatsService->Terminate();
ALOGW("statsd terminated on receiving signal %d.", sig);
- exit(1);
+ const char c = 'q';
+ write(gCtrlPipe[1], &c, 1);
}
void registerSignalHandlers()
@@ -60,11 +52,15 @@ void registerSignalHandlers()
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
- sa.sa_handler = signalHandler;
+
+ sa.sa_handler = SIG_IGN;
+ // ShellSubscriber uses SIGPIPE as a signal to detect the end of the
+ // client process. Don't prematurely exit(1) here. Instead, ignore the
+ // signal and allow the write call to return EPIPE.
sigaction(SIGPIPE, &sa, nullptr);
- sigaction(SIGHUP, &sa, nullptr);
- sigaction(SIGINT, &sa, nullptr);
- sigaction(SIGQUIT, &sa, nullptr);
+
+ pipe2(gCtrlPipe, O_CLOEXEC);
+ sa.sa_handler = signalHandler;
sigaction(SIGTERM, &sa, nullptr);
}
@@ -92,8 +88,6 @@ int main(int /*argc*/, char** /*argv*/) {
return -1;
}
- registerSignalHandlers();
-
gStatsService->sayHiToStatsCompanion();
gStatsService->Startup();
@@ -106,6 +100,22 @@ int main(int /*argc*/, char** /*argv*/) {
exit(1);
}
+ // Use self-pipe to notify this thread to gracefully quit
+ // when receiving SIGTERM
+ registerSignalHandlers();
+ std::thread([] {
+ while (true) {
+ char c;
+ int i = read(gCtrlPipe[0], &c, 1);
+ if (i < 0) {
+ if (errno == EINTR) continue;
+ }
+ gSocketListener->stopListener();
+ gStatsService->Terminate();
+ exit(1);
+ }
+ }).detach();
+
// Loop forever -- the reports run on this thread in a handler, and the
// binder calls remain responsive in their pool of one thread.
while (true) {