diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2020-01-08 22:28:35 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-01-08 22:28:35 +0000 |
commit | 22f2eb7208e8e7763b2e496b4138a26f566b934c (patch) | |
tree | 26d48e7cb95061090d2ed584ef0c05e4e47799b6 | |
parent | 804ce8e61ece941ca9335613c1ecb5c4d4fb9e4c (diff) | |
parent | 94eafe74a340aaa3b48540cd75cdd02939c6d223 (diff) |
Merge "Update Add/Remove Configuration"
-rw-r--r-- | apex/statsd/aidl/android/os/IStatsManagerService.aidl | 18 | ||||
-rw-r--r-- | apex/statsd/aidl/android/os/IStatsd.aidl | 8 | ||||
-rw-r--r-- | apex/statsd/service/java/com/android/server/stats/StatsManagerService.java | 42 | ||||
-rw-r--r-- | cmds/statsd/src/StatsService.cpp | 14 | ||||
-rw-r--r-- | cmds/statsd/src/StatsService.h | 4 | ||||
-rw-r--r-- | cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp | 6 | ||||
-rw-r--r-- | core/java/android/app/StatsManager.java | 8 |
7 files changed, 79 insertions, 21 deletions
diff --git a/apex/statsd/aidl/android/os/IStatsManagerService.aidl b/apex/statsd/aidl/android/os/IStatsManagerService.aidl index 2a3665ca9b5d..dec56345ec2f 100644 --- a/apex/statsd/aidl/android/os/IStatsManagerService.aidl +++ b/apex/statsd/aidl/android/os/IStatsManagerService.aidl @@ -107,4 +107,22 @@ interface IStatsManagerService { * Requires Manifest.permission.DUMP and Manifest.permission.PACKAGE_USAGE_STATS. */ byte[] getData(in long key, in String packageName); + + /** + * Sets a configuration with the specified config id and subscribes to updates for this + * configuration id. Broadcasts will be sent if this configuration needs to be collected. + * The configuration must be a wire-encoded StatsdConfig. The receiver for this data is + * registered in a separate function. + * + * Requires Manifest.permission.DUMP and Manifest.permission.PACKAGE_USAGE_STATS. + */ + void addConfiguration(in long configId, in byte[] config, in String packageName); + + /** + * Removes the configuration with the matching config id. No-op if this config id does not + * exist. + * + * Requires Manifest.permission.DUMP and Manifest.permission.PACKAGE_USAGE_STATS. + */ + void removeConfiguration(in long configId, in String packageName); }
\ No newline at end of file diff --git a/apex/statsd/aidl/android/os/IStatsd.aidl b/apex/statsd/aidl/android/os/IStatsd.aidl index c08abdd4cebe..c409f516de1b 100644 --- a/apex/statsd/aidl/android/os/IStatsd.aidl +++ b/apex/statsd/aidl/android/os/IStatsd.aidl @@ -99,14 +99,14 @@ interface IStatsd { byte[] getMetadata(); /** - * Sets a configuration with the specified config key and subscribes to updates for this + * Sets a configuration with the specified config id and subscribes to updates for this * configuration key. Broadcasts will be sent if this configuration needs to be collected. * The configuration must be a wire-encoded StatsdConfig. The receiver for this data is * registered in a separate function. * * Requires Manifest.permission.DUMP. */ - void addConfiguration(in long configKey, in byte[] config, in String packageName); + void addConfiguration(in long configId, in byte[] config, in int callingUid); /** * Registers the given pending intent for this config key. This intent is invoked when the @@ -143,12 +143,12 @@ interface IStatsd { void removeActiveConfigsChangedOperation(int callingUid); /** - * Removes the configuration with the matching config key. No-op if this config key does not + * Removes the configuration with the matching config id. No-op if this config id does not * exist. * * Requires Manifest.permission.DUMP. */ - void removeConfiguration(in long configKey, in String packageName); + void removeConfiguration(in long configId, in int callingUid); /** * Set the PendingIntentRef to be used when broadcasting subscriber diff --git a/apex/statsd/service/java/com/android/server/stats/StatsManagerService.java b/apex/statsd/service/java/com/android/server/stats/StatsManagerService.java index eff9bda618f9..b27d0f7699fc 100644 --- a/apex/statsd/service/java/com/android/server/stats/StatsManagerService.java +++ b/apex/statsd/service/java/com/android/server/stats/StatsManagerService.java @@ -301,6 +301,48 @@ public class StatsManagerService extends IStatsManagerService.Stub { throw new IllegalStateException("Failed to connect to statsd to getData"); } + @Override + public void addConfiguration(long configId, byte[] config, String packageName) + throws IllegalStateException { + enforceDumpAndUsageStatsPermission(packageName); + int callingUid = Binder.getCallingUid(); + final long token = Binder.clearCallingIdentity(); + try { + IStatsd statsd = waitForStatsd(); + if (statsd != null) { + statsd.addConfiguration(configId, config, callingUid); + return; + } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to addConfiguration with statsd"); + throw new IllegalStateException(e.getMessage(), e); + } finally { + Binder.restoreCallingIdentity(token); + } + throw new IllegalStateException("Failed to connect to statsd to addConfig"); + } + + @Override + public void removeConfiguration(long configId, String packageName) + throws IllegalStateException { + enforceDumpAndUsageStatsPermission(packageName); + int callingUid = Binder.getCallingUid(); + final long token = Binder.clearCallingIdentity(); + try { + IStatsd statsd = waitForStatsd(); + if (statsd != null) { + statsd.removeConfiguration(configId, callingUid); + return; + } + } catch (RemoteException e) { + Slog.e(TAG, "Failed to removeConfiguration with statsd"); + throw new IllegalStateException(e.getMessage(), e); + } finally { + Binder.restoreCallingIdentity(token); + } + throw new IllegalStateException("Failed to connect to statsd to removeConfig"); + } + void setStatsCompanionService(StatsCompanionService statsCompanionService) { mStatsCompanionService = statsCompanionService; } diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp index b2a5b50bb6eb..694c99df2194 100644 --- a/cmds/statsd/src/StatsService.cpp +++ b/cmds/statsd/src/StatsService.cpp @@ -1155,11 +1155,10 @@ Status StatsService::getMetadata(vector<uint8_t>* output) { } Status StatsService::addConfiguration(int64_t key, const vector <uint8_t>& config, - const String16& packageName) { - ENFORCE_DUMP_AND_USAGE_STATS(packageName); + const int32_t callingUid) { + ENFORCE_UID(AID_SYSTEM); - IPCThreadState* ipc = IPCThreadState::self(); - if (addConfigurationChecked(ipc->getCallingUid(), key, config)) { + if (addConfigurationChecked(callingUid, key, config)) { return Status::ok(); } else { ALOGE("Could not parse malformatted StatsdConfig"); @@ -1224,11 +1223,10 @@ Status StatsService::removeActiveConfigsChangedOperation(const int32_t callingUi return Status::ok(); } -Status StatsService::removeConfiguration(int64_t key, const String16& packageName) { - ENFORCE_DUMP_AND_USAGE_STATS(packageName); +Status StatsService::removeConfiguration(int64_t key, const int32_t callingUid) { + ENFORCE_UID(AID_SYSTEM); - IPCThreadState* ipc = IPCThreadState::self(); - ConfigKey configKey(ipc->getCallingUid(), key); + ConfigKey configKey(callingUid, key); mConfigManager->RemoveConfig(configKey); SubscriberReporter::getInstance().removeConfig(configKey); return Status::ok(); diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h index 56d87f25b7e5..c9a9072ecb92 100644 --- a/cmds/statsd/src/StatsService.h +++ b/cmds/statsd/src/StatsService.h @@ -115,7 +115,7 @@ public: */ virtual Status addConfiguration(int64_t key, const vector<uint8_t>& config, - const String16& packageName) override; + const int32_t callingUid) override; /** * Binder call to let clients register the data fetch operation for a configuration. @@ -145,7 +145,7 @@ public: * Binder call to allow clients to remove the specified configuration. */ virtual Status removeConfiguration(int64_t key, - const String16& packageName) override; + const int32_t callingUid) override; /** * Binder call to associate the given config's subscriberId with the given pendingIntentRef. diff --git a/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp b/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp index 0bc3ebb81ce6..16b51d99535b 100644 --- a/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/PartialBucket_e2e_test.cpp @@ -28,16 +28,16 @@ namespace statsd { #ifdef __ANDROID__ -const string kAndroid = "android"; const string kApp1 = "app1.sharing.1"; const int kConfigKey = 789130123; // Randomly chosen to avoid collisions with existing configs. +const int kCallingUid = 0; // Randomly chosen void SendConfig(StatsService& service, const StatsdConfig& config) { string str; config.SerializeToString(&str); std::vector<uint8_t> configAsVec(str.begin(), str.end()); bool success; - service.addConfiguration(kConfigKey, configAsVec, String16(kAndroid.c_str())); + service.addConfiguration(kConfigKey, configAsVec, kCallingUid); } ConfigMetricsReport GetReports(sp<StatsLogProcessor> processor, int64_t timestamp, @@ -50,7 +50,7 @@ ConfigMetricsReport GetReports(sp<StatsLogProcessor> processor, int64_t timestam ConfigMetricsReportList reports; reports.ParseFromArray(output.data(), output.size()); EXPECT_EQ(1, reports.reports_size()); - return reports.reports(0); + return reports.reports(kCallingUid); } StatsdConfig MakeConfig() { diff --git a/core/java/android/app/StatsManager.java b/core/java/android/app/StatsManager.java index 4e50a3fa17c1..84263749232d 100644 --- a/core/java/android/app/StatsManager.java +++ b/core/java/android/app/StatsManager.java @@ -157,11 +157,11 @@ public final class StatsManager { public void addConfig(long configKey, byte[] config) throws StatsUnavailableException { synchronized (sLock) { try { - IStatsd service = getIStatsdLocked(); + IStatsManagerService service = getIStatsManagerServiceLocked(); // can throw IllegalArgumentException service.addConfiguration(configKey, config, mContext.getOpPackageName()); } catch (RemoteException e) { - Slog.e(TAG, "Failed to connect to statsd when adding configuration"); + Slog.e(TAG, "Failed to connect to statsmanager when adding configuration"); throw new StatsUnavailableException("could not connect", e); } catch (SecurityException e) { throw new StatsUnavailableException(e.getMessage(), e); @@ -194,10 +194,10 @@ public final class StatsManager { public void removeConfig(long configKey) throws StatsUnavailableException { synchronized (sLock) { try { - IStatsd service = getIStatsdLocked(); + IStatsManagerService service = getIStatsManagerServiceLocked(); service.removeConfiguration(configKey, mContext.getOpPackageName()); } catch (RemoteException e) { - Slog.e(TAG, "Failed to connect to statsd when removing configuration"); + Slog.e(TAG, "Failed to connect to statsmanager when removing configuration"); throw new StatsUnavailableException("could not connect", e); } catch (SecurityException e) { throw new StatsUnavailableException(e.getMessage(), e); |