Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.connectivity; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.pm.ApplicationInfo; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.res.Resources; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.Canvas; |
| 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.net.INetworkManagementEventObserver; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 30 | import android.net.LocalSocket; |
| 31 | import android.net.LocalSocketAddress; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 33 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 34 | import android.os.Process; |
| 35 | import android.os.SystemClock; |
| 36 | import android.os.SystemProperties; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 37 | import android.util.Log; |
| 38 | |
| 39 | import com.android.internal.R; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 40 | import com.android.internal.net.VpnConfig; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 41 | import com.android.server.ConnectivityService.VpnCallback; |
| 42 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 43 | import java.io.OutputStream; |
| 44 | import java.nio.charset.Charsets; |
| 45 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 46 | /** |
| 47 | * @hide |
| 48 | */ |
| 49 | public class Vpn extends INetworkManagementEventObserver.Stub { |
| 50 | |
| 51 | private final static String TAG = "Vpn"; |
| 52 | private final static String VPN = android.Manifest.permission.VPN; |
| 53 | |
| 54 | private final Context mContext; |
| 55 | private final VpnCallback mCallback; |
| 56 | |
| 57 | private String mPackageName; |
| 58 | private String mInterfaceName; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 59 | |
| 60 | private LegacyVpnRunner mLegacyVpnRunner; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 61 | |
| 62 | public Vpn(Context context, VpnCallback callback) { |
| 63 | mContext = context; |
| 64 | mCallback = callback; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Prepare for a VPN application. |
| 69 | * |
| 70 | * @param packageName The package name of the new VPN application. |
| 71 | * @return The name of the current prepared package. |
| 72 | */ |
| 73 | public synchronized String prepare(String packageName) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 74 | // Return the current prepared package if the new one is null. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 75 | if (packageName == null) { |
| 76 | return mPackageName; |
| 77 | } |
| 78 | |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 79 | // Check the permission of the caller. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 80 | PackageManager pm = mContext.getPackageManager(); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 81 | VpnConfig.enforceCallingPackage(pm.getNameForUid(Binder.getCallingUid())); |
| 82 | |
| 83 | // Check the permission of the given package. |
| 84 | if (packageName.isEmpty()) { |
| 85 | packageName = null; |
| 86 | } else if (pm.checkPermission(VPN, packageName) != PackageManager.PERMISSION_GRANTED) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 87 | throw new SecurityException(packageName + " does not have " + VPN); |
| 88 | } |
| 89 | |
| 90 | // Reset the interface and hide the notification. |
| 91 | if (mInterfaceName != null) { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 92 | jniResetInterface(mInterfaceName); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 93 | mCallback.restore(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 94 | hideNotification(); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 95 | mInterfaceName = null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 98 | // Notify the package being revoked. |
| 99 | if (mPackageName != null) { |
| 100 | Intent intent = new Intent(VpnConfig.ACTION_VPN_REVOKED); |
| 101 | intent.setPackage(mPackageName); |
| 102 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 103 | mContext.sendBroadcast(intent); |
| 104 | } |
| 105 | |
| 106 | Log.i(TAG, "Switched from " + mPackageName + " to " + packageName); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 107 | mPackageName = packageName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 108 | return mPackageName; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Protect a socket from routing changes by binding it to the given |
Chia-chi Yeh | 3f3337a | 2011-06-17 16:34:32 -0700 | [diff] [blame] | 113 | * interface. The socket IS closed by this method. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 114 | * |
| 115 | * @param socket The socket to be bound. |
| 116 | * @param name The name of the interface. |
| 117 | */ |
| 118 | public void protect(ParcelFileDescriptor socket, String name) { |
Chia-chi Yeh | 3f3337a | 2011-06-17 16:34:32 -0700 | [diff] [blame] | 119 | try { |
| 120 | mContext.enforceCallingPermission(VPN, "protect"); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 121 | jniProtectSocket(socket.getFd(), name); |
Chia-chi Yeh | 3f3337a | 2011-06-17 16:34:32 -0700 | [diff] [blame] | 122 | } finally { |
| 123 | try { |
| 124 | socket.close(); |
| 125 | } catch (Exception e) { |
| 126 | // ignore |
| 127 | } |
| 128 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Configure a TUN interface and return its file descriptor. |
| 133 | * |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 134 | * @param config The parameters to configure the interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 135 | * @return The file descriptor of the interface. |
| 136 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 137 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 138 | // Check the permission of the caller. |
| 139 | mContext.enforceCallingPermission(VPN, "establish"); |
| 140 | |
| 141 | // Check if the caller is already prepared. |
| 142 | PackageManager pm = mContext.getPackageManager(); |
| 143 | ApplicationInfo app = null; |
| 144 | try { |
| 145 | app = pm.getApplicationInfo(mPackageName, 0); |
| 146 | } catch (Exception e) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 147 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 148 | } |
| 149 | if (Binder.getCallingUid() != app.uid) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 150 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 153 | // Load the label. |
| 154 | String label = app.loadLabel(pm).toString(); |
| 155 | |
| 156 | // Load the icon and convert it into a bitmap. |
| 157 | Drawable icon = app.loadIcon(pm); |
| 158 | Bitmap bitmap = null; |
| 159 | if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) { |
| 160 | int width = mContext.getResources().getDimensionPixelSize( |
| 161 | android.R.dimen.notification_large_icon_width); |
| 162 | int height = mContext.getResources().getDimensionPixelSize( |
| 163 | android.R.dimen.notification_large_icon_height); |
| 164 | icon.setBounds(0, 0, width, height); |
| 165 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 166 | icon.draw(new Canvas(bitmap)); |
| 167 | } |
| 168 | |
| 169 | // Create the interface and abort if any of the following steps fails. |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 170 | ParcelFileDescriptor descriptor = |
| 171 | ParcelFileDescriptor.adoptFd(jniCreateInterface(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 172 | try { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 173 | String name = jniGetInterfaceName(descriptor.getFd()); |
| 174 | if (jniSetAddresses(name, config.addresses) < 1) { |
| 175 | throw new IllegalArgumentException("At least one address must be specified"); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 176 | } |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 177 | if (config.routes != null) { |
| 178 | jniSetRoutes(name, config.routes); |
| 179 | } |
| 180 | if (mInterfaceName != null && !mInterfaceName.equals(name)) { |
| 181 | jniResetInterface(mInterfaceName); |
| 182 | } |
| 183 | mInterfaceName = name; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 184 | } catch (RuntimeException e) { |
| 185 | try { |
| 186 | descriptor.close(); |
| 187 | } catch (Exception ex) { |
| 188 | // ignore |
| 189 | } |
| 190 | throw e; |
| 191 | } |
| 192 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 193 | String dnsServers = (config.dnsServers == null) ? "" : config.dnsServers.trim(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 194 | mCallback.override(dnsServers.isEmpty() ? null : dnsServers.split(" ")); |
| 195 | |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 196 | config.packageName = mPackageName; |
| 197 | config.interfaceName = mInterfaceName; |
Chia-chi Yeh | 383e052 | 2011-07-01 00:13:25 -0700 | [diff] [blame] | 198 | showNotification(config, label, bitmap); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 199 | return descriptor; |
| 200 | } |
| 201 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 202 | // INetworkManagementEventObserver.Stub |
Mike J. Chen | f59c7d0 | 2011-06-23 15:33:15 -0700 | [diff] [blame] | 203 | public void interfaceStatusChanged(String name, boolean up) { |
| 204 | } |
| 205 | |
| 206 | // INetworkManagementEventObserver.Stub |
| 207 | public void interfaceLinkStateChanged(String name, boolean up) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // INetworkManagementEventObserver.Stub |
| 211 | public void interfaceAdded(String name) { |
| 212 | } |
| 213 | |
| 214 | // INetworkManagementEventObserver.Stub |
| 215 | public synchronized void interfaceRemoved(String name) { |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 216 | if (name.equals(mInterfaceName) && jniCheckInterface(name) == 0) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 217 | hideNotification(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 218 | mCallback.restore(); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 219 | mInterfaceName = null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 223 | private void showNotification(VpnConfig config, String label, Bitmap icon) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 224 | NotificationManager nm = (NotificationManager) |
| 225 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 226 | |
| 227 | if (nm != null) { |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 228 | String title = (label == null) ? mContext.getString(R.string.vpn_title) : |
| 229 | mContext.getString(R.string.vpn_title_long, label); |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 230 | String text = (config.sessionName == null) ? mContext.getString(R.string.vpn_text) : |
| 231 | mContext.getString(R.string.vpn_text_long, config.sessionName); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 232 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 233 | long identity = Binder.clearCallingIdentity(); |
| 234 | Notification notification = new Notification.Builder(mContext) |
| 235 | .setSmallIcon(R.drawable.vpn_connected) |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 236 | .setLargeIcon(icon) |
| 237 | .setContentTitle(title) |
Chia-chi Yeh | f8905fd | 2011-06-14 16:35:02 -0700 | [diff] [blame] | 238 | .setContentText(text) |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 239 | .setContentIntent(VpnConfig.getIntentForNotification(mContext, config)) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 240 | .setDefaults(Notification.DEFAULT_ALL) |
| 241 | .setOngoing(true) |
| 242 | .getNotification(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 243 | nm.notify(R.drawable.vpn_connected, notification); |
| 244 | Binder.restoreCallingIdentity(identity); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | private void hideNotification() { |
| 249 | NotificationManager nm = (NotificationManager) |
| 250 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 251 | |
| 252 | if (nm != null) { |
| 253 | long identity = Binder.clearCallingIdentity(); |
| 254 | nm.cancel(R.drawable.vpn_connected); |
| 255 | Binder.restoreCallingIdentity(identity); |
| 256 | } |
| 257 | } |
| 258 | |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 259 | private native int jniCreateInterface(int mtu); |
| 260 | private native String jniGetInterfaceName(int fd); |
| 261 | private native int jniSetAddresses(String name, String addresses); |
| 262 | private native int jniSetRoutes(String name, String routes); |
| 263 | private native void jniResetInterface(String name); |
| 264 | private native int jniCheckInterface(String name); |
| 265 | private native void jniProtectSocket(int fd, String name); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 266 | |
| 267 | /** |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 268 | * Handle legacy VPN requests. This method stops the daemons and restart |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 269 | * them if their arguments are not null. Heavy things are offloaded to |
| 270 | * another thread, so callers will not be blocked too long. |
| 271 | * |
| 272 | * @param raoocn The arguments to be passed to racoon. |
| 273 | * @param mtpd The arguments to be passed to mtpd. |
| 274 | */ |
| 275 | public synchronized void doLegacyVpn(String[] racoon, String[] mtpd) { |
| 276 | // Currently only system user is allowed. |
| 277 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 278 | throw new SecurityException("Unauthorized Caller"); |
| 279 | } |
| 280 | |
| 281 | // If the previous runner is still alive, interrupt it. |
| 282 | if (mLegacyVpnRunner != null && mLegacyVpnRunner.isAlive()) { |
| 283 | mLegacyVpnRunner.interrupt(); |
| 284 | } |
| 285 | |
| 286 | // Start a new runner and we are done! |
| 287 | mLegacyVpnRunner = new LegacyVpnRunner( |
| 288 | new String[] {"racoon", "mtpd"}, racoon, mtpd); |
| 289 | mLegacyVpnRunner.start(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Bringing up a VPN connection takes time, and that is all this thread |
| 294 | * does. Here we have plenty of time. The only thing we need to take |
| 295 | * care of is responding to interruptions as soon as possible. Otherwise |
| 296 | * requests will be piled up. This can be done in a Handler as a state |
| 297 | * machine, but it is much easier to read in the current form. |
| 298 | */ |
| 299 | private class LegacyVpnRunner extends Thread { |
| 300 | private static final String TAG = "LegacyVpnRunner"; |
| 301 | |
| 302 | private static final String NONE = "--"; |
| 303 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 304 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 305 | private final String[][] mArguments; |
| 306 | private long mTimer = -1; |
| 307 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 308 | public LegacyVpnRunner(String[] daemons, String[]... arguments) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 309 | super(TAG); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 310 | mDaemons = daemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 311 | mArguments = arguments; |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | public void run() { |
| 316 | // Wait for the previous thread since it has been interrupted. |
| 317 | Log.v(TAG, "wait"); |
| 318 | synchronized (TAG) { |
| 319 | Log.v(TAG, "run"); |
| 320 | execute(); |
| 321 | Log.v(TAG, "exit"); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | private void checkpoint(boolean yield) throws InterruptedException { |
| 326 | long now = SystemClock.elapsedRealtime(); |
| 327 | if (mTimer == -1) { |
| 328 | mTimer = now; |
| 329 | Thread.sleep(1); |
| 330 | } else if (now - mTimer <= 30000) { |
| 331 | Thread.sleep(yield ? 200 : 1); |
| 332 | } else { |
| 333 | throw new InterruptedException("timeout"); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private void execute() { |
| 338 | // Catch all exceptions so we can clean up few things. |
| 339 | try { |
| 340 | // Initialize the timer. |
| 341 | checkpoint(false); |
| 342 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 343 | // First stop the daemons. |
| 344 | for (String daemon : mDaemons) { |
| 345 | SystemProperties.set("ctl.stop", daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 348 | // Wait for the daemons to stop. |
| 349 | for (String daemon : mDaemons) { |
| 350 | String key = "init.svc." + daemon; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 351 | while (!"stopped".equals(SystemProperties.get(key))) { |
| 352 | checkpoint(true); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // Reset the properties. |
| 357 | SystemProperties.set("vpn.dns", NONE); |
| 358 | SystemProperties.set("vpn.via", NONE); |
| 359 | while (!NONE.equals(SystemProperties.get("vpn.dns")) || |
| 360 | !NONE.equals(SystemProperties.get("vpn.via"))) { |
| 361 | checkpoint(true); |
| 362 | } |
| 363 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 364 | // Check if we need to restart some daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 365 | boolean restart = false; |
| 366 | for (String[] arguments : mArguments) { |
| 367 | restart = restart || (arguments != null); |
| 368 | } |
| 369 | if (!restart) { |
| 370 | return; |
| 371 | } |
| 372 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 373 | // Start the daemon with arguments. |
| 374 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 375 | String[] arguments = mArguments[i]; |
| 376 | if (arguments == null) { |
| 377 | continue; |
| 378 | } |
| 379 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 380 | // Start the daemon. |
| 381 | String daemon = mDaemons[i]; |
| 382 | SystemProperties.set("ctl.start", daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 383 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 384 | // Wait for the daemon to start. |
| 385 | String key = "init.svc." + daemon; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 386 | while (!"running".equals(SystemProperties.get(key))) { |
| 387 | checkpoint(true); |
| 388 | } |
| 389 | |
| 390 | // Create the control socket. |
| 391 | LocalSocket socket = new LocalSocket(); |
| 392 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 393 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 394 | |
| 395 | // Wait for the socket to connect. |
| 396 | while (true) { |
| 397 | try { |
| 398 | socket.connect(address); |
| 399 | break; |
| 400 | } catch (Exception e) { |
| 401 | // ignore |
| 402 | } |
| 403 | checkpoint(true); |
| 404 | } |
| 405 | socket.setSoTimeout(500); |
| 406 | |
| 407 | // Send over the arguments. |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 408 | OutputStream out = socket.getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 409 | for (String argument : arguments) { |
| 410 | byte[] bytes = argument.getBytes(Charsets.UTF_8); |
| 411 | if (bytes.length >= 0xFFFF) { |
| 412 | throw new IllegalArgumentException("argument too large"); |
| 413 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 414 | out.write(bytes.length >> 8); |
| 415 | out.write(bytes.length); |
| 416 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 417 | checkpoint(false); |
| 418 | } |
| 419 | |
| 420 | // Send End-Of-Arguments. |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 421 | out.write(0xFF); |
| 422 | out.write(0xFF); |
| 423 | out.flush(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 424 | socket.close(); |
| 425 | } |
| 426 | |
| 427 | // Now here is the beast from the old days. We check few |
| 428 | // properties to figure out the current status. Ideally we |
| 429 | // can read things back from the sockets and get rid of the |
| 430 | // properties, but we have no time... |
| 431 | while (NONE.equals(SystemProperties.get("vpn.dns")) || |
| 432 | NONE.equals(SystemProperties.get("vpn.via"))) { |
| 433 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 434 | // Check if a running daemon is dead. |
| 435 | for (int i = 0; i < mDaemons.length; ++i) { |
| 436 | String daemon = mDaemons[i]; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 437 | if (mArguments[i] != null && !"running".equals( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 438 | SystemProperties.get("init.svc." + daemon))) { |
| 439 | throw new IllegalArgumentException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | checkpoint(true); |
| 443 | } |
| 444 | |
| 445 | // Great! Now we are connected! |
| 446 | Log.i(TAG, "connected!"); |
| 447 | // TODO: |
| 448 | |
| 449 | } catch (Exception e) { |
| 450 | Log.i(TAG, e.getMessage()); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame^] | 451 | for (String daemon : mDaemons) { |
| 452 | SystemProperties.set("ctl.stop", daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 457 | } |