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 android.os; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.os.Binder; 22 import android.os.Process; 23 import android.util.Log; 24 25 /** 26 * The implementation of the scheduling policy service interface. 27 * 28 * @hide 29 */ 30 public class SchedulingPolicyService extends ISchedulingPolicyService.Stub { 31 32 private static final String TAG = "SchedulingPolicyService"; 33 34 // Minimum and maximum values allowed for requestPriority parameter prio 35 private static final int PRIORITY_MIN = 1; 36 private static final int PRIORITY_MAX = 3; 37 SchedulingPolicyService()38 public SchedulingPolicyService() { 39 } 40 requestPriority(int pid, int tid, int prio)41 public int requestPriority(int pid, int tid, int prio) { 42 //Log.i(TAG, "requestPriority(pid=" + pid + ", tid=" + tid + ", prio=" + prio + ")"); 43 44 // Verify that caller is mediaserver, priority is in range, and that the 45 // callback thread specified by app belongs to the app that called mediaserver. 46 // Once we've verified that the caller is mediaserver, we can trust the pid but 47 // we can't trust the tid. No need to explicitly check for pid == 0 || tid == 0, 48 // since if not the case then the getThreadGroupLeader() test will also fail. 49 if (Binder.getCallingUid() != Process.MEDIA_UID || prio < PRIORITY_MIN || 50 prio > PRIORITY_MAX || Process.getThreadGroupLeader(tid) != pid) { 51 return PackageManager.PERMISSION_DENIED; 52 } 53 try { 54 // make good use of our CAP_SYS_NICE capability 55 Process.setThreadGroup(tid, Binder.getCallingPid() == pid ? 56 Process.THREAD_GROUP_AUDIO_SYS : Process.THREAD_GROUP_AUDIO_APP); 57 // must be in this order or it fails the schedulability constraint 58 Process.setThreadScheduler(tid, Process.SCHED_FIFO, prio); 59 } catch (RuntimeException e) { 60 return PackageManager.PERMISSION_DENIED; 61 } 62 return PackageManager.PERMISSION_GRANTED; 63 } 64 65 } 66