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; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 21 | import android.gsi.GsiInstallParams; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 22 | import android.gsi.GsiProgress; |
| 23 | import android.gsi.IGsiService; |
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 | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 27 | import android.os.RemoteException; |
| 28 | import android.os.ServiceManager; |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 29 | import android.os.SystemProperties; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 30 | import android.os.UserHandle; |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 31 | import android.os.image.IDynamicSystemService; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 32 | import android.os.storage.StorageManager; |
| 33 | import android.os.storage.StorageVolume; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 34 | import android.util.Slog; |
| 35 | |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 36 | import java.io.File; |
| 37 | |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 38 | /** |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 39 | * DynamicSystemService implements IDynamicSystemService. It provides permission check before |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 40 | * passing requests to gsid |
| 41 | */ |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 42 | public class DynamicSystemService extends IDynamicSystemService.Stub implements DeathRecipient { |
| 43 | private static final String TAG = "DynamicSystemService"; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 44 | private static final String NO_SERVICE_ERROR = "no gsiservice"; |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 45 | private static final int GSID_ROUGH_TIMEOUT_MS = 8192; |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 46 | private static final String PATH_DEFAULT = "/data/gsi"; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 47 | private Context mContext; |
| 48 | private volatile IGsiService mGsiService; |
| 49 | |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 50 | DynamicSystemService(Context context) { |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 51 | mContext = context; |
| 52 | } |
| 53 | |
| 54 | private static IGsiService connect(DeathRecipient recipient) throws RemoteException { |
| 55 | IBinder binder = ServiceManager.getService("gsiservice"); |
| 56 | if (binder == null) { |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 57 | return null; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 58 | } |
| 59 | /** |
| 60 | * The init will restart gsiservice if it crashed and the proxy object will need to be |
| 61 | * re-initialized in this case. |
| 62 | */ |
| 63 | binder.linkToDeath(recipient, 0); |
| 64 | return IGsiService.Stub.asInterface(binder); |
| 65 | } |
| 66 | |
| 67 | /** implements DeathRecipient */ |
| 68 | @Override |
| 69 | public void binderDied() { |
| 70 | Slog.w(TAG, "gsiservice died; reconnecting"); |
| 71 | synchronized (this) { |
| 72 | mGsiService = null; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private IGsiService getGsiService() throws RemoteException { |
| 77 | checkPermission(); |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 78 | |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 79 | if (!"running".equals(SystemProperties.get("init.svc.gsid"))) { |
| 80 | SystemProperties.set("ctl.start", "gsid"); |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | for (int sleepMs = 64; sleepMs <= (GSID_ROUGH_TIMEOUT_MS << 1); sleepMs <<= 1) { |
| 84 | synchronized (this) { |
| 85 | if (mGsiService == null) { |
| 86 | mGsiService = connect(this); |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 87 | } |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 88 | if (mGsiService != null) { |
| 89 | return mGsiService; |
Howard Chen | 1392c14 | 2019-03-05 14:23:47 +0800 | [diff] [blame] | 90 | } |
| 91 | } |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 92 | |
| 93 | try { |
| 94 | Slog.d(TAG, "GsiService is not ready, wait for " + sleepMs + "ms"); |
| 95 | Thread.sleep(sleepMs); |
| 96 | } catch (InterruptedException e) { |
| 97 | Slog.e(TAG, "Interrupted when waiting for GSID"); |
| 98 | return null; |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 99 | } |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 100 | } |
Po-Chien Hsueh | 7565fcc | 2019-04-11 16:36:54 +0800 | [diff] [blame] | 101 | |
| 102 | throw new RemoteException(NO_SERVICE_ERROR); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | private void checkPermission() { |
| 106 | if (mContext.checkCallingOrSelfPermission( |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 107 | android.Manifest.permission.MANAGE_DYNAMIC_SYSTEM) |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 108 | != PackageManager.PERMISSION_GRANTED) { |
Po-Chien Hsueh | 4e908c2 | 2019-03-07 11:57:17 +0800 | [diff] [blame] | 109 | throw new SecurityException("Requires MANAGE_DYNAMIC_SYSTEM permission"); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean startInstallation(long systemSize, long userdataSize) throws RemoteException { |
Howard Chen | 148d7ed | 2019-03-26 16:13:04 +0800 | [diff] [blame] | 115 | // priority from high to low: sysprop -> sdcard -> /data |
| 116 | String path = SystemProperties.get("os.aot.path"); |
| 117 | if (path.isEmpty()) { |
| 118 | final int userId = UserHandle.myUserId(); |
| 119 | final StorageVolume[] volumes = |
| 120 | StorageManager.getVolumeList(userId, StorageManager.FLAG_FOR_WRITE); |
| 121 | for (StorageVolume volume : volumes) { |
| 122 | if (volume.isEmulated()) continue; |
| 123 | if (!volume.isRemovable()) continue; |
| 124 | if (!Environment.MEDIA_MOUNTED.equals(volume.getState())) continue; |
| 125 | File sdCard = volume.getPathFile(); |
| 126 | if (sdCard.isDirectory()) { |
| 127 | path = sdCard.getPath(); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | if (path.isEmpty()) { |
| 132 | path = PATH_DEFAULT; |
| 133 | } |
| 134 | Slog.i(TAG, "startInstallation -> " + path); |
| 135 | } |
| 136 | GsiInstallParams installParams = new GsiInstallParams(); |
| 137 | installParams.installDir = path; |
| 138 | installParams.gsiSize = systemSize; |
| 139 | installParams.userdataSize = userdataSize; |
| 140 | return getGsiService().beginGsiInstall(installParams) == 0; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public GsiProgress getInstallationProgress() throws RemoteException { |
| 145 | return getGsiService().getInstallProgress(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public boolean abort() throws RemoteException { |
| 150 | return getGsiService().cancelGsiInstall(); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean isInUse() throws RemoteException { |
| 155 | return getGsiService().isGsiRunning(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean isInstalled() throws RemoteException { |
| 160 | return getGsiService().isGsiInstalled(); |
| 161 | } |
| 162 | |
| 163 | @Override |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 164 | public boolean isEnabled() throws RemoteException { |
| 165 | return getGsiService().isGsiEnabled(); |
| 166 | } |
| 167 | |
| 168 | @Override |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 169 | public boolean remove() throws RemoteException { |
| 170 | return getGsiService().removeGsiInstall(); |
| 171 | } |
| 172 | |
| 173 | @Override |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 174 | public boolean setEnable(boolean enable) throws RemoteException { |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 175 | IGsiService gsiService = getGsiService(); |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 176 | if (enable) { |
David Anderson | 8ee0674 | 2019-02-27 13:45:34 -0800 | [diff] [blame] | 177 | final int status = gsiService.getGsiBootStatus(); |
| 178 | final boolean singleBoot = (status == IGsiService.BOOT_STATUS_SINGLE_BOOT); |
| 179 | return gsiService.setGsiBootable(singleBoot) == 0; |
Howard Chen | 72324e4 | 2019-03-29 16:38:39 +0800 | [diff] [blame] | 180 | } else { |
| 181 | return gsiService.disableGsiInstall(); |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean write(byte[] buf) throws RemoteException { |
| 187 | return getGsiService().commitGsiChunkFromMemory(buf); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public boolean commit() throws RemoteException { |
David Anderson | 8ee0674 | 2019-02-27 13:45:34 -0800 | [diff] [blame] | 192 | return getGsiService().setGsiBootable(true) == 0; |
Howard Chen | f17f42b | 2019-01-07 14:10:44 +0800 | [diff] [blame] | 193 | } |
| 194 | } |