IpSecService.java revision 1afbef40c68373f3871eed087c546cfe1911ee36
1/* 2 * Copyright (C) 2017 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; 18 19import static android.Manifest.permission.DUMP; 20 21import android.content.Context; 22import android.net.IIpSecService; 23import android.net.INetd; 24import android.net.util.NetdService; 25import android.os.RemoteException; 26import android.util.Log; 27import android.util.Slog; 28 29import java.io.FileDescriptor; 30import java.io.PrintWriter; 31 32/** @hide */ 33public class IpSecService extends IIpSecService.Stub { 34 private static final String TAG = "IpSecService"; 35 private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); 36 private static final String NETD_SERVICE_NAME = "netd"; 37 38 /** Binder context for this service */ 39 private final Context mContext; 40 41 private Object mLock = new Object(); 42 43 private static final int NETD_FETCH_TIMEOUT = 5000; //ms 44 45 /** 46 * Constructs a new IpSecService instance 47 * 48 * @param context Binder context for this service 49 */ 50 private IpSecService(Context context) { 51 mContext = context; 52 } 53 54 static IpSecService create(Context context) throws InterruptedException { 55 final IpSecService service = new IpSecService(context); 56 service.connectNativeNetdService(); 57 return service; 58 } 59 60 public void systemReady() { 61 if (isNetdAlive()) { 62 Slog.d(TAG, "IpSecService is ready"); 63 } else { 64 Slog.wtf(TAG, "IpSecService not ready: failed to connect to NetD Native Service!"); 65 } 66 } 67 68 private void connectNativeNetdService() { 69 // Avoid blocking the system server to do this 70 Thread t = 71 new Thread( 72 new Runnable() { 73 @Override 74 public void run() { 75 synchronized (mLock) { 76 NetdService.get(NETD_FETCH_TIMEOUT); 77 } 78 } 79 }); 80 t.run(); 81 } 82 83 INetd getNetdInstance() { 84 final INetd netd = NetdService.getInstance(); 85 if (netd == null) { 86 throw new RemoteException("Failed to Get Netd Instance").rethrowFromSystemServer(); 87 } 88 return netd; 89 } 90 91 boolean isNetdAlive() { 92 synchronized (mLock) { 93 final INetd netd = getNetdInstance(); 94 if (netd == null) { 95 return false; 96 } 97 98 try { 99 return netd.isAlive(); 100 } catch (RemoteException re) { 101 return false; 102 } 103 } 104 } 105 106 @Override 107 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 108 mContext.enforceCallingOrSelfPermission(DUMP, TAG); 109 110 pw.println("IpSecService Log:"); 111 pw.println("NetdNativeService Connection: " + (isNetdAlive() ? "alive" : "dead")); 112 pw.println(); 113 } 114} 115