Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0118b5a6 authored by Jeffrey Huang's avatar Jeffrey Huang
Browse files

Add APEX UIDs to PackageManager query

Because of internal implementation of one way binder calls, in order to
unblock the boot phase, we need to put both the binder call and the
filedescriptor in the background thread.

Original boot time: 17758.6ms
New boot time: 17608.0ms

Bug: 130773130
Test: DeviceBootTest.DeviceBootTest#SuccessiveBootTest
Change-Id: Ib3524418a387c5902af8139e1b2aee82525b8e04
parent a0222398
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -59,9 +59,6 @@ interface IStatsCompanionService {
    /** Cancel any alarm for the purpose of subscriber triggering. */
    oneway void cancelAlarmForSubscriberTriggering();

    /** Tells StatsCompaionService to grab the uid map snapshot and send it to statsd. */
    oneway void triggerUidSnapshot();

    /**
     * Ask StatsCompanionService if the given permission is allowed for a particular process
     * and user ID. statsd is incapable of doing this check itself because checkCallingPermission
+22 −38
Original line number Diff line number Diff line
@@ -153,7 +153,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
    }

    private static void informAllUidsLocked(Context context) throws RemoteException {
    private static void informAllUids(Context context) {
        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
        PackageManager pm = context.getPackageManager();
        final List<UserHandle> users = um.getUserHandles(true);
@@ -168,18 +168,26 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            Log.e(TAG, "Failed to create a pipe to send uid map data.", e);
            return;
        }
        sStatsd.informAllUidData(fds[0]);
        HandlerThread backgroundThread = new HandlerThread(
                "statsCompanionService.bg", THREAD_PRIORITY_BACKGROUND);
        backgroundThread.start();
        Handler handler = new Handler(backgroundThread.getLooper());
        handler.post(() -> {
            IStatsd statsd = getStatsdNonblocking();
            if (statsd == null) {
                return;
            }
            try {
                statsd.informAllUidData(fds[0]);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to send uid map to statsd");
            }
            try {
                fds[0].close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close the read side of the pipe.", e);
            }
            final ParcelFileDescriptor writeFd = fds[1];
        HandlerThread backgroundThread = new HandlerThread(
                "statsCompanionService.bg", THREAD_PRIORITY_BACKGROUND);
        backgroundThread.start();
        Handler handler = new Handler(backgroundThread.getLooper());
        handler.post(() -> {
            FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(writeFd);
            try {
                ProtoOutputStream output = new ProtoOutputStream(fout);
@@ -188,7 +196,8 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
                for (UserHandle userHandle : users) {
                    List<PackageInfo> pi =
                            pm.getInstalledPackagesAsUser(PackageManager.MATCH_UNINSTALLED_PACKAGES
                                            | PackageManager.MATCH_ANY_USER,
                                            | PackageManager.MATCH_ANY_USER
                                            | PackageManager.MATCH_APEX,
                                    userHandle.getIdentifier());
                    for (int j = 0; j < pi.size(); j++) {
                        if (pi.get(j).applicationInfo != null) {
@@ -319,19 +328,9 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
    private static final class UserUpdateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (sStatsdLock) {
                if (sStatsd == null) {
                    Log.w(TAG, "Could not access statsd for UserUpdateReceiver");
                    return;
                }
                try {
            // Pull the latest state of UID->app name, version mapping.
            // Needed since the new user basically has a version of every app.
                    informAllUidsLocked(context);
                } catch (RemoteException e) {
                    Log.e(TAG, "Failed to inform statsd latest update of all apps", e);
                }
            }
            informAllUids(context);
        }
    }

@@ -588,21 +587,6 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
        }
    }

    @Override // Binder call
    public void triggerUidSnapshot() {
        StatsCompanion.enforceStatsdCallingUid();
        synchronized (sStatsdLock) {
            final long token = Binder.clearCallingIdentity();
            try {
                informAllUidsLocked(mContext);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to trigger uid snapshot.", e);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }
    }

    @Override // Binder call
    public boolean checkPermission(String permission, int pid, int uid) {
        StatsCompanion.enforceStatsdCallingUid();
@@ -707,7 +691,7 @@ public class StatsCompanionService extends IStatsCompanionService.Stub {
            try {
                // Pull the latest state of UID->app name, version mapping when
                // statsd starts.
                informAllUidsLocked(mContext);
                informAllUids(mContext);
            } finally {
                Binder.restoreCallingIdentity(token);
            }