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 the caller uid is permitted, priority is in range, 44 // and that the callback thread specified by app belongs to the app that 45 // called mediaserver or audioserver. 46 // Once we've verified that the caller uid is permitted, 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 (!isPermittedCallingUid() || 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 | Process.SCHED_RESET_ON_FORK, 59 prio); 60 } catch (RuntimeException e) { 61 return PackageManager.PERMISSION_DENIED; 62 } 63 return PackageManager.PERMISSION_GRANTED; 64 } 65 66 private boolean isPermittedCallingUid() { 67 final int callingUid = Binder.getCallingUid(); 68 switch (callingUid) { 69 case Process.AUDIOSERVER_UID: // fastcapture, fastmixer 70 case Process.CAMERASERVER_UID: // camera high frame rate recording 71 return true; 72 default: 73 return false; 74 } 75 } 76 } 77