diff --git a/cmds/statsd/Android.bp b/cmds/statsd/Android.bp index f0b751db5ae94..53cbae00350fc 100644 --- a/cmds/statsd/Android.bp +++ b/cmds/statsd/Android.bp @@ -332,6 +332,8 @@ java_library { "src/stats_log.proto", "src/statsd_config.proto", "src/atoms.proto", + "src/shell/shell_config.proto", + "src/shell/shell_data.proto", ], static_libs: [ diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp index f2a46636c382c..3107b4dd7fce3 100644 --- a/cmds/statsd/src/StatsService.cpp +++ b/cmds/statsd/src/StatsService.cpp @@ -360,7 +360,11 @@ status_t StatsService::command(int in, int out, int err, Vector& args, if (mShellSubscriber == nullptr) { mShellSubscriber = new ShellSubscriber(mUidMap, mPullerManager); } - mShellSubscriber->startNewSubscription(in, out, resultReceiver); + int timeoutSec = -1; + if (argCount >= 2) { + timeoutSec = atoi(args[1].c_str()); + } + mShellSubscriber->startNewSubscription(in, out, resultReceiver, timeoutSec); return NO_ERROR; } } diff --git a/cmds/statsd/src/shell/ShellSubscriber.cpp b/cmds/statsd/src/shell/ShellSubscriber.cpp index 22883f3c205a0..52d5ffc33317c 100644 --- a/cmds/statsd/src/shell/ShellSubscriber.cpp +++ b/cmds/statsd/src/shell/ShellSubscriber.cpp @@ -30,7 +30,8 @@ namespace statsd { const static int FIELD_ID_ATOM = 1; -void ShellSubscriber::startNewSubscription(int in, int out, sp resultReceiver) { +void ShellSubscriber::startNewSubscription(int in, int out, sp resultReceiver, + int timeoutSec) { VLOG("start new shell subscription"); { std::lock_guard lock(mMutex); @@ -50,11 +51,18 @@ void ShellSubscriber::startNewSubscription(int in, int out, sp // Read config forever until EOF is reached. Clients may send multiple configs -- each new // config replace the previous one. readConfig(in); + VLOG("timeout : %d", timeoutSec); // Now we have read an EOF we now wait for the semaphore until the client exits. VLOG("Now wait for client to exit"); std::unique_lock lk(mMutex); - mShellDied.wait(lk, [this, resultReceiver] { return mResultReceiver != resultReceiver; }); + + if (timeoutSec > 0) { + mShellDied.wait_for(lk, timeoutSec * 1s, + [this, resultReceiver] { return mResultReceiver != resultReceiver; }); + } else { + mShellDied.wait(lk, [this, resultReceiver] { return mResultReceiver != resultReceiver; }); + } } void ShellSubscriber::updateConfig(const ShellSubscription& config) { diff --git a/cmds/statsd/src/shell/ShellSubscriber.h b/cmds/statsd/src/shell/ShellSubscriber.h index 5401f31ce68c0..8e54a8b000917 100644 --- a/cmds/statsd/src/shell/ShellSubscriber.h +++ b/cmds/statsd/src/shell/ShellSubscriber.h @@ -65,7 +65,8 @@ public: /** * Start a new subscription. */ - void startNewSubscription(int inFd, int outFd, sp resultReceiver); + void startNewSubscription(int inFd, int outFd, sp resultReceiver, + int timeoutSec); void binderDied(const wp& who); diff --git a/cmds/statsd/tests/shell/ShellSubscriber_test.cpp b/cmds/statsd/tests/shell/ShellSubscriber_test.cpp index a184f5672d65d..73d1fd7850e94 100644 --- a/cmds/statsd/tests/shell/ShellSubscriber_test.cpp +++ b/cmds/statsd/tests/shell/ShellSubscriber_test.cpp @@ -83,7 +83,7 @@ void runShellTest(ShellSubscription config, sp uidMap, // mimic a binder thread that a shell subscriber runs on. it would block. std::thread reader([&resultReceiver, &fds_config, &fds_data, &shellClient] { - shellClient->startNewSubscription(fds_config[0], fds_data[1], resultReceiver); + shellClient->startNewSubscription(fds_config[0], fds_data[1], resultReceiver, -1); }); reader.detach();