blob: 16143d3ae9e0111302e172e9964844071a30be18 [file] [log] [blame]
Mårten Kongstadeabc9e92015-12-15 16:40:23 +01001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.om;
18
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020019import static android.content.Context.IDMAP_SERVICE;
20import static android.text.format.DateUtils.SECOND_IN_MILLIS;
21
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010022import static com.android.server.om.OverlayManagerService.DEBUG;
23import static com.android.server.om.OverlayManagerService.TAG;
24
25import android.annotation.NonNull;
26import android.content.om.OverlayInfo;
27import android.content.pm.PackageInfo;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020028import android.os.IBinder;
29import android.os.IIdmap2;
30import android.os.RemoteException;
31import android.os.ServiceManager;
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010032import android.os.UserHandle;
33import android.util.Slog;
34
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020035import com.android.internal.os.BackgroundThread;
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010036import com.android.server.pm.Installer;
37
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010038import java.io.File;
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010039
40/**
41 * Handle the creation and deletion of idmap files.
42 *
43 * The actual work is performed by the idmap binary, launched through Installer
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020044 * and installd (or idmap2).
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010045 *
46 * Note: this class is subclassed in the OMS unit tests, and hence not marked as final.
47 */
48class IdmapManager {
Mårten Kongstadb87b50722018-09-21 09:58:10 +020049 private static final boolean FEATURE_FLAG_IDMAP2 = true;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020050
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010051 private final Installer mInstaller;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020052 private IIdmap2 mIdmap2Service;
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010053
54 IdmapManager(final Installer installer) {
55 mInstaller = installer;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020056 if (FEATURE_FLAG_IDMAP2) {
57 connectToIdmap2d();
58 }
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010059 }
60
61 boolean createIdmap(@NonNull final PackageInfo targetPackage,
62 @NonNull final PackageInfo overlayPackage, int userId) {
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010063 if (DEBUG) {
64 Slog.d(TAG, "create idmap for " + targetPackage.packageName + " and "
65 + overlayPackage.packageName);
66 }
67 final int sharedGid = UserHandle.getSharedAppGid(targetPackage.applicationInfo.uid);
68 final String targetPath = targetPackage.applicationInfo.getBaseCodePath();
69 final String overlayPath = overlayPackage.applicationInfo.getBaseCodePath();
70 try {
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020071 if (FEATURE_FLAG_IDMAP2) {
Mårten Kongstadef0695d2018-12-04 14:36:48 +010072 if (mIdmap2Service.verifyIdmap(overlayPath, userId)) {
73 return true;
74 }
75 return mIdmap2Service.createIdmap(targetPath, overlayPath, userId) != null;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020076 } else {
77 mInstaller.idmap(targetPath, overlayPath, sharedGid);
Mårten Kongstadef0695d2018-12-04 14:36:48 +010078 return true;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020079 }
80 } catch (Exception e) {
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010081 Slog.w(TAG, "failed to generate idmap for " + targetPath + " and "
82 + overlayPath + ": " + e.getMessage());
83 return false;
84 }
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010085 }
86
87 boolean removeIdmap(@NonNull final OverlayInfo oi, final int userId) {
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010088 if (DEBUG) {
89 Slog.d(TAG, "remove idmap for " + oi.baseCodePath);
90 }
91 try {
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020092 if (FEATURE_FLAG_IDMAP2) {
Mårten Kongstadef0695d2018-12-04 14:36:48 +010093 return mIdmap2Service.removeIdmap(oi.baseCodePath, userId);
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020094 } else {
95 mInstaller.removeIdmap(oi.baseCodePath);
Mårten Kongstadef0695d2018-12-04 14:36:48 +010096 return true;
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020097 }
98 } catch (Exception e) {
Mårten Kongstadeabc9e92015-12-15 16:40:23 +010099 Slog.w(TAG, "failed to remove idmap for " + oi.baseCodePath + ": " + e.getMessage());
100 return false;
101 }
Mårten Kongstadeabc9e92015-12-15 16:40:23 +0100102 }
103
104 boolean idmapExists(@NonNull final OverlayInfo oi) {
Mårten Kongstad06a1ac82018-09-20 13:09:47 +0200105 return new File(getIdmapPath(oi.baseCodePath, oi.userId)).isFile();
Mårten Kongstadeabc9e92015-12-15 16:40:23 +0100106 }
107
108 boolean idmapExists(@NonNull final PackageInfo overlayPackage, final int userId) {
Mårten Kongstad06a1ac82018-09-20 13:09:47 +0200109 return new File(getIdmapPath(overlayPackage.applicationInfo.getBaseCodePath(), userId))
110 .isFile();
Mårten Kongstadeabc9e92015-12-15 16:40:23 +0100111 }
112
Mårten Kongstad06a1ac82018-09-20 13:09:47 +0200113 private @NonNull String getIdmapPath(@NonNull final String overlayPackagePath,
114 final int userId) {
115 if (FEATURE_FLAG_IDMAP2) {
116 try {
117 return mIdmap2Service.getIdmapPath(overlayPackagePath, userId);
118 } catch (Exception e) {
119 Slog.w(TAG, "failed to get idmap path for " + overlayPackagePath + ": "
120 + e.getMessage());
121 return "";
122 }
123 } else {
124 final StringBuilder sb = new StringBuilder("/data/resource-cache/");
125 sb.append(overlayPackagePath.substring(1).replace('/', '@'));
126 sb.append("@idmap");
127 return sb.toString();
128 }
129 }
130
131 private void connectToIdmap2d() {
132 IBinder binder = ServiceManager.getService(IDMAP_SERVICE);
133 if (binder != null) {
134 try {
135 binder.linkToDeath(new IBinder.DeathRecipient() {
136 @Override
137 public void binderDied() {
138 Slog.w(TAG, "service '" + IDMAP_SERVICE + "' died; reconnecting...");
139 connectToIdmap2d();
140 }
141
142 }, 0);
143 } catch (RemoteException e) {
144 binder = null;
145 }
146 }
147 if (binder != null) {
148 mIdmap2Service = IIdmap2.Stub.asInterface(binder);
149 if (DEBUG) {
150 Slog.d(TAG, "service '" + IDMAP_SERVICE + "' connected");
151 }
152 } else {
153 Slog.w(TAG, "service '" + IDMAP_SERVICE + "' not found; trying again...");
154 BackgroundThread.getHandler().postDelayed(() -> {
155 connectToIdmap2d();
156 }, SECOND_IN_MILLIS);
157 }
Mårten Kongstadeabc9e92015-12-15 16:40:23 +0100158 }
Mårten Kongstadeabc9e92015-12-15 16:40:23 +0100159}