summaryrefslogtreecommitdiffstats
path: root/services/core/java/com/android/server/trust
diff options
context:
space:
mode:
authorAdrian Roos <roosa@google.com>2014-07-25 13:22:38 +0000
committerAndroid Git Automerger <android-git-automerger@android.com>2014-07-25 13:22:38 +0000
commitb42fe576f6f4b194866fde51175cd7062a4a435d (patch)
tree4f304f623cb4eb90f270a6ab0b38b6bd0597b986 /services/core/java/com/android/server/trust
parent73b941f761270c71616fe44fa0727aa79345604c (diff)
parent7a2cb299a0fffba8ad4eb044a57c37d0e42d380b (diff)
downloadframeworks_base-b42fe576f6f4b194866fde51175cd7062a4a435d.zip
frameworks_base-b42fe576f6f4b194866fde51175cd7062a4a435d.tar.gz
frameworks_base-b42fe576f6f4b194866fde51175cd7062a4a435d.tar.bz2
am 5de758d3: am f8a01f5f: am 6468d5c2: Merge "Restart trust agents when updated or when they are dead" into lmp-dev
* commit '5de758d3868b036f5067ad9f86ee7b02331e4818': Restart trust agents when updated or when they are dead
Diffstat (limited to 'services/core/java/com/android/server/trust')
-rw-r--r--services/core/java/com/android/server/trust/TrustAgentWrapper.java51
-rw-r--r--services/core/java/com/android/server/trust/TrustArchive.java2
-rw-r--r--services/core/java/com/android/server/trust/TrustManagerService.java68
3 files changed, 105 insertions, 16 deletions
diff --git a/services/core/java/com/android/server/trust/TrustAgentWrapper.java b/services/core/java/com/android/server/trust/TrustAgentWrapper.java
index f18939f..51009af 100644
--- a/services/core/java/com/android/server/trust/TrustAgentWrapper.java
+++ b/services/core/java/com/android/server/trust/TrustAgentWrapper.java
@@ -24,6 +24,7 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
+import android.os.SystemClock;
import android.os.UserHandle;
import android.util.Log;
import android.util.Slog;
@@ -41,6 +42,13 @@ public class TrustAgentWrapper {
private static final int MSG_GRANT_TRUST = 1;
private static final int MSG_REVOKE_TRUST = 2;
private static final int MSG_TRUST_TIMEOUT = 3;
+ private static final int MSG_RESTART_TIMEOUT = 4;
+
+ /**
+ * Time in uptime millis that we wait for the service connection, both when starting
+ * and when the service disconnects.
+ */
+ private static final long RESTART_TIMEOUT_MILLIS = 5 * 60000;
/**
* Long extra for {@link #MSG_GRANT_TRUST}
@@ -53,6 +61,8 @@ public class TrustAgentWrapper {
private final ComponentName mName;
private ITrustAgentService mTrustAgentService;
+ private boolean mBound;
+ private long mScheduledRestartUptimeMillis;
// Trust state
private boolean mTrusted;
@@ -95,6 +105,10 @@ public class TrustAgentWrapper {
}
mTrustManagerService.updateTrust(mUserId);
break;
+ case MSG_RESTART_TIMEOUT:
+ unbind();
+ mTrustManagerService.resetAgent(mName, mUserId);
+ break;
}
}
};
@@ -123,6 +137,7 @@ public class TrustAgentWrapper {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (DEBUG) Log.v(TAG, "TrustAgent started : " + name.flattenToString());
+ mHandler.removeMessages(MSG_RESTART_TIMEOUT);
mTrustAgentService = ITrustAgentService.Stub.asInterface(service);
mTrustManagerService.mArchive.logAgentConnected(mUserId, name);
setCallback(mCallback);
@@ -134,6 +149,9 @@ public class TrustAgentWrapper {
mTrustAgentService = null;
mTrustManagerService.mArchive.logAgentDied(mUserId, name);
mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
+ if (mBound) {
+ scheduleRestart();
+ }
}
};
@@ -144,9 +162,12 @@ public class TrustAgentWrapper {
mTrustManagerService = trustManagerService;
mUserId = user.getIdentifier();
mName = intent.getComponent();
- if (!context.bindServiceAsUser(intent, mConnection, Context.BIND_AUTO_CREATE, user)) {
- if (DEBUG) Log.v(TAG, "can't bind to TrustAgent " + mName.flattenToShortString());
- // TODO: retry somehow?
+ // Schedules a restart for when connecting times out. If the connection succeeds,
+ // the restart is canceled in mCallback's onConnected.
+ scheduleRestart();
+ mBound = context.bindServiceAsUser(intent, mConnection, Context.BIND_AUTO_CREATE, user);
+ if (!mBound) {
+ Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
}
}
@@ -184,14 +205,38 @@ public class TrustAgentWrapper {
}
public void unbind() {
+ if (!mBound) {
+ return;
+ }
if (DEBUG) Log.v(TAG, "TrustAgent unbound : " + mName.flattenToShortString());
mTrustManagerService.mArchive.logAgentStopped(mUserId, mName);
mContext.unbindService(mConnection);
+ mBound = false;
mTrustAgentService = null;
mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
+ mHandler.removeMessages(MSG_RESTART_TIMEOUT);
}
public boolean isConnected() {
return mTrustAgentService != null;
}
+
+ public boolean isBound() {
+ return mBound;
+ }
+
+ /**
+ * If not connected, returns the time at which the agent is restarted.
+ *
+ * @return restart time in uptime millis.
+ */
+ public long getScheduledRestartUptimeMillis() {
+ return mScheduledRestartUptimeMillis;
+ }
+
+ private void scheduleRestart() {
+ mHandler.removeMessages(MSG_RESTART_TIMEOUT);
+ mScheduledRestartUptimeMillis = SystemClock.uptimeMillis() + RESTART_TIMEOUT_MILLIS;
+ mHandler.sendEmptyMessageAtTime(MSG_RESTART_TIMEOUT, mScheduledRestartUptimeMillis);
+ }
}
diff --git a/services/core/java/com/android/server/trust/TrustArchive.java b/services/core/java/com/android/server/trust/TrustArchive.java
index 56950d2..5e32d86 100644
--- a/services/core/java/com/android/server/trust/TrustArchive.java
+++ b/services/core/java/com/android/server/trust/TrustArchive.java
@@ -130,7 +130,7 @@ public class TrustArchive {
}
}
- private static String formatDuration(long duration) {
+ public static String formatDuration(long duration) {
StringBuilder sb = new StringBuilder();
TimeUtils.formatDuration(duration, sb);
return sb.toString();
diff --git a/services/core/java/com/android/server/trust/TrustManagerService.java b/services/core/java/com/android/server/trust/TrustManagerService.java
index 60a8090..14436aa 100644
--- a/services/core/java/com/android/server/trust/TrustManagerService.java
+++ b/services/core/java/com/android/server/trust/TrustManagerService.java
@@ -45,6 +45,7 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
+import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.trust.TrustAgentService;
@@ -99,12 +100,6 @@ public class TrustManagerService extends SystemService {
private UserManager mUserManager;
- /**
- * Cache for {@link #refreshAgentList()}
- */
- private final ArraySet<AgentInfo> mObsoleteAgents = new ArraySet<AgentInfo>();
-
-
public TrustManagerService(Context context) {
super(context);
mContext = context;
@@ -168,8 +163,8 @@ public class TrustManagerService extends SystemService {
List<UserInfo> userInfos = mUserManager.getUsers(true /* excludeDying */);
LockPatternUtils lockPatternUtils = new LockPatternUtils(mContext);
- mObsoleteAgents.clear();
- mObsoleteAgents.addAll(mActiveAgents);
+ ArraySet<AgentInfo> obsoleteAgents = new ArraySet<>();
+ obsoleteAgents.addAll(mActiveAgents);
for (UserInfo userInfo : userInfos) {
int disabledFeatures = lockPatternUtils.getDevicePolicyManager()
@@ -208,14 +203,14 @@ public class TrustManagerService extends SystemService {
new Intent().setComponent(name), userInfo.getUserHandle());
mActiveAgents.add(agentInfo);
} else {
- mObsoleteAgents.remove(agentInfo);
+ obsoleteAgents.remove(agentInfo);
}
}
}
boolean trustMayHaveChanged = false;
- for (int i = 0; i < mObsoleteAgents.size(); i++) {
- AgentInfo info = mObsoleteAgents.valueAt(i);
+ for (int i = 0; i < obsoleteAgents.size(); i++) {
+ AgentInfo info = obsoleteAgents.valueAt(i);
if (info.agent.isTrusted()) {
trustMayHaveChanged = true;
}
@@ -228,6 +223,43 @@ public class TrustManagerService extends SystemService {
}
}
+ private void removeAgentsOfPackage(String packageName) {
+ boolean trustMayHaveChanged = false;
+ for (int i = mActiveAgents.size() - 1; i >= 0; i--) {
+ AgentInfo info = mActiveAgents.valueAt(i);
+ if (packageName.equals(info.component.getPackageName())) {
+ Log.i(TAG, "Resetting agent " + info.component.flattenToShortString());
+ if (info.agent.isTrusted()) {
+ trustMayHaveChanged = true;
+ }
+ info.agent.unbind();
+ mActiveAgents.removeAt(i);
+ }
+ }
+ if (trustMayHaveChanged) {
+ updateTrustAll();
+ }
+ }
+
+ public void resetAgent(ComponentName name, int userId) {
+ boolean trustMayHaveChanged = false;
+ for (int i = mActiveAgents.size() - 1; i >= 0; i--) {
+ AgentInfo info = mActiveAgents.valueAt(i);
+ if (name.equals(info.component) && userId == info.userId) {
+ Log.i(TAG, "Resetting agent " + info.component.flattenToShortString());
+ if (info.agent.isTrusted()) {
+ trustMayHaveChanged = true;
+ }
+ info.agent.unbind();
+ mActiveAgents.removeAt(i);
+ }
+ }
+ if (trustMayHaveChanged) {
+ updateTrust(userId);
+ }
+ refreshAgentList();
+ }
+
private ComponentName getSettingsComponentName(PackageManager pm, ResolveInfo resolveInfo) {
if (resolveInfo == null || resolveInfo.serviceInfo == null
|| resolveInfo.serviceInfo.metaData == null) return null;
@@ -448,11 +480,18 @@ public class TrustManagerService extends SystemService {
if (info.userId != user.id) { continue; }
boolean trusted = info.agent.isTrusted();
fout.print(" "); fout.println(info.component.flattenToShortString());
- fout.print(" connected=" + dumpBool(info.agent.isConnected()));
+ fout.print(" bound=" + dumpBool(info.agent.isBound()));
+ fout.print(", connected=" + dumpBool(info.agent.isConnected()));
fout.println(", trusted=" + dumpBool(trusted));
if (trusted) {
fout.println(" message=\"" + info.agent.getMessage() + "\"");
}
+ if (!info.agent.isConnected()) {
+ String restartTime = TrustArchive.formatDuration(
+ info.agent.getScheduledRestartUptimeMillis()
+ - SystemClock.uptimeMillis());
+ fout.println(" restartScheduledAt=" + restartTime);
+ }
if (!simpleNames.add(TrustArchive.getSimpleName(info.component))) {
duplicateSimpleNames = true;
}
@@ -501,6 +540,11 @@ public class TrustManagerService extends SystemService {
// We're interested in all changes, even if just some components get enabled / disabled.
return true;
}
+
+ @Override
+ public void onPackageDisappeared(String packageName, int reason) {
+ removeAgentsOfPackage(packageName);
+ }
};
private class DevicePolicyReceiver extends BroadcastReceiver {