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