blob: 80faf4730274ef7739dc18b9732fc6e68e107bcf [file] [log] [blame]
Glenn Kasten07b04652012-04-23 15:00:43 -07001/*
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
Jeff Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.os;
Glenn Kasten07b04652012-04-23 15:00:43 -070018
Glenn Kasten07b04652012-04-23 15:00:43 -070019import android.content.pm.PackageManager;
20import android.os.Binder;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080021import android.os.ISchedulingPolicyService;
Glenn Kasten07b04652012-04-23 15:00:43 -070022import android.os.Process;
Glenn Kasten07b04652012-04-23 15:00:43 -070023
24/**
25 * The implementation of the scheduling policy service interface.
26 *
27 * @hide
28 */
29public 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;
Glenn Kasten430c2542012-06-04 11:42:01 -070035 private static final int PRIORITY_MAX = 3;
Glenn Kasten07b04652012-04-23 15:00:43 -070036
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
Andy Hunged0ea402015-10-30 14:11:46 -070043 // 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
Glenn Kasten07b04652012-04-23 15:00:43 -070047 // 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.
Andy Hunged0ea402015-10-30 14:11:46 -070049 if (!isPermittedCallingUid() || prio < PRIORITY_MIN ||
Glenn Kasten07b04652012-04-23 15:00:43 -070050 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
Andy Hunged0ea402015-10-30 14:11:46 -070065 private boolean isPermittedCallingUid() {
66 final int callingUid = Binder.getCallingUid();
67 switch (callingUid) {
68 case Process.AUDIOSERVER_UID: // fastcapture, fastmixer
69 case Process.MEDIA_UID: // camera
70 return true;
71 default:
72 return false;
73 }
74 }
Glenn Kasten07b04652012-04-23 15:00:43 -070075}