Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.PackageManager; |
| 21 | import android.gsi.GsiProgress; |
| 22 | import android.gsi.IGsiService; |
David Anderson | 6414321 | 2019-07-12 16:24:09 -0700 | [diff] [blame] | 23 | import android.gsi.IGsid; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 24 | import android.os.Environment; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.IBinder.DeathRecipient; |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 27 | import android.os.ParcelFileDescriptor; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 30 | import android.os.SystemProperties; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 31 | import android.os.UserHandle; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 32 | import android.os.image.IDynamicSystemService; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 33 | import android.os.storage.StorageManager; |
| 34 | import android.os.storage.StorageVolume; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 35 | import android.util.Slog; |
| 36 | |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 37 | import java.io.File; |
| 38 | |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 39 | /** |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 40 | * DynamicSystemService implements IDynamicSystemService. It provides permission check before |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 41 | * passing requests to gsid |
| 42 | */ |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 43 | public class DynamicSystemService extends IDynamicSystemService.Stub implements DeathRecipient { |
| 44 | private static final String TAG = "DynamicSystemService"; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 45 | private static final String NO_SERVICE_ERROR = "no gsiservice"; |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 46 | private static final int GSID_ROUGH_TIMEOUT_MS = 8192; |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 47 | private static final String PATH_DEFAULT = "/data/gsi/"; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 48 | private Context mContext; |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 49 | private String mInstallPath, mDsuSlot; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 50 | private volatile IGsiService mGsiService; |
| 51 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 52 | DynamicSystemService(Context context) { |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 53 | mContext = context; |
| 54 | } |
| 55 | |
| 56 | private static IGsiService connect(DeathRecipient recipient) throws RemoteException { |
| 57 | IBinder binder = ServiceManager.getService("gsiservice"); |
| 58 | if (binder == null) { |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 59 | return null; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 60 | } |
| 61 | /** |
| 62 | * The init will restart gsiservice if it crashed and the proxy object will need to be |
| 63 | * re-initialized in this case. |
| 64 | */ |
| 65 | binder.linkToDeath(recipient, 0); |
David Anderson | 6414321 | 2019-07-12 16:24:09 -0700 | [diff] [blame] | 66 | |
| 67 | IGsid gsid = IGsid.Stub.asInterface(binder); |
| 68 | return gsid.getClient(); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** implements DeathRecipient */ |
| 72 | @Override |
| 73 | public void binderDied() { |
| 74 | Slog.w(TAG, "gsiservice died; reconnecting"); |
| 75 | synchronized (this) { |
| 76 | mGsiService = null; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private IGsiService getGsiService() throws RemoteException { |
| 81 | checkPermission(); |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 82 | |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 83 | if (!"running".equals(SystemProperties.get("init.svc.gsid"))) { |
| 84 | SystemProperties.set("ctl.start", "gsid"); |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | for (int sleepMs = 64; sleepMs <= (GSID_ROUGH_TIMEOUT_MS << 1); sleepMs <<= 1) { |
| 88 | synchronized (this) { |
| 89 | if (mGsiService == null) { |
| 90 | mGsiService = connect(this); |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 91 | } |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 92 | if (mGsiService != null) { |
| 93 | return mGsiService; |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 94 | } |
| 95 | } |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 96 | |
| 97 | try { |
| 98 | Slog.d(TAG, "GsiService is not ready, wait for " + sleepMs + "ms"); |
| 99 | Thread.sleep(sleepMs); |
| 100 | } catch (InterruptedException e) { |
| 101 | Slog.e(TAG, "Interrupted when waiting for GSID"); |
| 102 | return null; |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 103 | } |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 104 | } |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 105 | |
| 106 | throw new RemoteException(NO_SERVICE_ERROR); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private void checkPermission() { |
| 110 | if (mContext.checkCallingOrSelfPermission( |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 111 | android.Manifest.permission.MANAGE_DYNAMIC_SYSTEM) |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 112 | != PackageManager.PERMISSION_GRANTED) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 113 | throw new SecurityException("Requires MANAGE_DYNAMIC_SYSTEM permission"); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 118 | public boolean startInstallation(String dsuSlot) throws RemoteException { |
Howard Chen | 6ea5bed | 2019-10-04 18:15:14 +0800 | [diff] [blame] | 119 | IGsiService service = getGsiService(); |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 120 | // priority from high to low: sysprop -> sdcard -> /data |
| 121 | String path = SystemProperties.get("os.aot.path"); |
| 122 | if (path.isEmpty()) { |
| 123 | final int userId = UserHandle.myUserId(); |
| 124 | final StorageVolume[] volumes = |
| 125 | StorageManager.getVolumeList(userId, StorageManager.FLAG_FOR_WRITE); |
| 126 | for (StorageVolume volume : volumes) { |
| 127 | if (volume.isEmulated()) continue; |
| 128 | if (!volume.isRemovable()) continue; |
| 129 | if (!Environment.MEDIA_MOUNTED.equals(volume.getState())) continue; |
| 130 | File sdCard = volume.getPathFile(); |
| 131 | if (sdCard.isDirectory()) { |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 132 | path = new File(sdCard, dsuSlot).getPath(); |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | } |
| 136 | if (path.isEmpty()) { |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 137 | path = PATH_DEFAULT + dsuSlot; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 138 | } |
| 139 | Slog.i(TAG, "startInstallation -> " + path); |
| 140 | } |
Howard Chen | 6ea5bed | 2019-10-04 18:15:14 +0800 | [diff] [blame] | 141 | mInstallPath = path; |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 142 | mDsuSlot = dsuSlot; |
Howard Chen | 6ea5bed | 2019-10-04 18:15:14 +0800 | [diff] [blame] | 143 | if (service.openInstall(path) != 0) { |
| 144 | Slog.i(TAG, "Failed to open " + path); |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean createPartition(String name, long size, boolean readOnly) |
| 152 | throws RemoteException { |
Howard Chen | e176c56 | 2019-08-13 17:01:53 +0800 | [diff] [blame] | 153 | IGsiService service = getGsiService(); |
Howard Chen | 6ea5bed | 2019-10-04 18:15:14 +0800 | [diff] [blame] | 154 | if (service.createPartition(name, size, readOnly) != 0) { |
Howard Chen | e176c56 | 2019-08-13 17:01:53 +0800 | [diff] [blame] | 155 | Slog.i(TAG, "Failed to install " + name); |
| 156 | return false; |
| 157 | } |
| 158 | return true; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
Howard Chen | 6ea5bed | 2019-10-04 18:15:14 +0800 | [diff] [blame] | 162 | public boolean finishInstallation() throws RemoteException { |
| 163 | IGsiService service = getGsiService(); |
| 164 | if (service.closeInstall() != 0) { |
| 165 | Slog.i(TAG, "Failed to finish installation"); |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | @Override |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 172 | public GsiProgress getInstallationProgress() throws RemoteException { |
| 173 | return getGsiService().getInstallProgress(); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean abort() throws RemoteException { |
| 178 | return getGsiService().cancelGsiInstall(); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean isInUse() throws RemoteException { |
Howard Chen | 07fcb2f | 2019-12-19 11:40:16 +0800 | [diff] [blame] | 183 | return SystemProperties.getBoolean("ro.gsid.image_running", false); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public boolean isInstalled() throws RemoteException { |
| 188 | return getGsiService().isGsiInstalled(); |
| 189 | } |
| 190 | |
| 191 | @Override |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 192 | public boolean isEnabled() throws RemoteException { |
| 193 | return getGsiService().isGsiEnabled(); |
| 194 | } |
| 195 | |
| 196 | @Override |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 197 | public boolean remove() throws RemoteException { |
Howard Chen | 6ea5bed | 2019-10-04 18:15:14 +0800 | [diff] [blame] | 198 | IGsiService gsiService = getGsiService(); |
| 199 | String install_dir = gsiService.getInstalledGsiImageDir(); |
Howard Chen | c44ee15 | 2019-08-02 17:54:49 +0800 | [diff] [blame] | 200 | return getGsiService().removeGsi(); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Howard Chen | c44ee15 | 2019-08-02 17:54:49 +0800 | [diff] [blame] | 204 | public boolean setEnable(boolean enable, boolean oneShot) throws RemoteException { |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 205 | IGsiService gsiService = getGsiService(); |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 206 | if (enable) { |
Howard Chen | c6f64c4 | 2019-11-10 01:11:50 +0800 | [diff] [blame] | 207 | return gsiService.enableGsi(oneShot, mDsuSlot) == 0; |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 208 | } else { |
Howard Chen | c44ee15 | 2019-08-02 17:54:49 +0800 | [diff] [blame] | 209 | return gsiService.disableGsi(); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | @Override |
Howard Chen | ae615b3 | 2019-08-05 16:56:12 +0800 | [diff] [blame] | 214 | public boolean setAshmem(ParcelFileDescriptor ashmem, long size) { |
| 215 | try { |
| 216 | return getGsiService().setGsiAshmem(ashmem, size); |
| 217 | } catch (RemoteException e) { |
| 218 | throw new RuntimeException(e.toString()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public boolean submitFromAshmem(long size) { |
| 224 | try { |
| 225 | return getGsiService().commitGsiChunkFromAshmem(size); |
| 226 | } catch (RemoteException e) { |
| 227 | throw new RuntimeException(e.toString()); |
| 228 | } |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 229 | } |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 230 | } |