1 /* 2 * Copyright (C) 2012 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.Intent; 21 import android.content.pm.PackageManager; 22 import android.os.Binder; 23 import android.os.Handler; 24 import android.os.IBinder; 25 import android.os.IUpdateLock; 26 import android.os.RemoteException; 27 import android.os.SystemClock; 28 import android.os.TokenWatcher; 29 import android.os.UpdateLock; 30 import android.util.Slog; 31 32 import java.io.FileDescriptor; 33 import java.io.PrintWriter; 34 35 public class UpdateLockService extends IUpdateLock.Stub { 36 static final boolean DEBUG = false; 37 static final String TAG = "UpdateLockService"; 38 39 // signatureOrSystem required to use update locks 40 static final String PERMISSION = "android.permission.UPDATE_LOCK"; 41 42 Context mContext; 43 LockWatcher mLocks; 44 45 class LockWatcher extends TokenWatcher { LockWatcher(Handler h, String tag)46 LockWatcher(Handler h, String tag) { 47 super(h, tag); 48 } 49 acquired()50 public void acquired() { 51 if (DEBUG) { 52 Slog.d(TAG, "first acquire; broadcasting convenient=false"); 53 } 54 sendLockChangedBroadcast(false); 55 } released()56 public void released() { 57 if (DEBUG) { 58 Slog.d(TAG, "last release; broadcasting convenient=true"); 59 } 60 sendLockChangedBroadcast(true); 61 } 62 } 63 UpdateLockService(Context context)64 UpdateLockService(Context context) { 65 mContext = context; 66 mLocks = new LockWatcher(new Handler(), "UpdateLocks"); 67 68 // Consider just-booting to be a reasonable time to allow 69 // interruptions for update installation etc. 70 sendLockChangedBroadcast(true); 71 } 72 sendLockChangedBroadcast(boolean state)73 void sendLockChangedBroadcast(boolean state) { 74 // Safe early during boot because this broadcast only goes to registered receivers. 75 long oldIdent = Binder.clearCallingIdentity(); 76 try { 77 Intent intent = new Intent(UpdateLock.UPDATE_LOCK_CHANGED) 78 .putExtra(UpdateLock.NOW_IS_CONVENIENT, state) 79 .putExtra(UpdateLock.TIMESTAMP, System.currentTimeMillis()) 80 .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); 81 mContext.sendStickyBroadcast(intent); 82 } finally { 83 Binder.restoreCallingIdentity(oldIdent); 84 } 85 } 86 87 @Override acquireUpdateLock(IBinder token, String tag)88 public void acquireUpdateLock(IBinder token, String tag) throws RemoteException { 89 if (DEBUG) { 90 Slog.d(TAG, "acquire(" + token + ") by " + makeTag(tag)); 91 } 92 93 mContext.enforceCallingOrSelfPermission(PERMISSION, "acquireUpdateLock"); 94 mLocks.acquire(token, makeTag(tag)); 95 } 96 97 @Override releaseUpdateLock(IBinder token)98 public void releaseUpdateLock(IBinder token) throws RemoteException { 99 if (DEBUG) { 100 Slog.d(TAG, "release(" + token + ')'); 101 } 102 103 mContext.enforceCallingOrSelfPermission(PERMISSION, "releaseUpdateLock"); 104 mLocks.release(token); 105 }; 106 makeTag(String tag)107 private String makeTag(String tag) { 108 return "{tag=" + tag 109 + " uid=" + Binder.getCallingUid() 110 + " pid=" + Binder.getCallingPid() + '}'; 111 } 112 113 @Override dump(FileDescriptor fd, PrintWriter pw, String[] args)114 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 115 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 116 != PackageManager.PERMISSION_GRANTED) { 117 pw.println("Permission Denial: can't dump update lock service from from pid=" 118 + Binder.getCallingPid() 119 + ", uid=" + Binder.getCallingUid()); 120 return; 121 } 122 123 mLocks.dump(pw); 124 } 125 } 126