blob: e0b8426a56d2a8cc8fea98282885c8f3ea3e7dc9 [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;
Philip Cuadra41950492017-03-24 14:09:19 -070023import android.util.Log;
Glenn Kasten07b04652012-04-23 15:00:43 -070024
25/**
26 * The implementation of the scheduling policy service interface.
27 *
28 * @hide
29 */
30public 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;
Glenn Kasten430c2542012-06-04 11:42:01 -070036 private static final int PRIORITY_MAX = 3;
Glenn Kasten07b04652012-04-23 15:00:43 -070037
38 public SchedulingPolicyService() {
39 }
40
Mikhail Naganova0cb18d2017-02-07 10:50:21 -080041 // TODO(b/35196900) We should pass the period in time units, rather
42 // than a fixed priority number.
43 public int requestPriority(int pid, int tid, int prio, boolean isForApp) {
Glenn Kasten07b04652012-04-23 15:00:43 -070044 //Log.i(TAG, "requestPriority(pid=" + pid + ", tid=" + tid + ", prio=" + prio + ")");
45
Andy Hunged0ea402015-10-30 14:11:46 -070046 // Verify that the caller uid is permitted, priority is in range,
47 // and that the callback thread specified by app belongs to the app that
48 // called mediaserver or audioserver.
49 // Once we've verified that the caller uid is permitted, we can trust the pid but
Glenn Kasten07b04652012-04-23 15:00:43 -070050 // we can't trust the tid. No need to explicitly check for pid == 0 || tid == 0,
51 // since if not the case then the getThreadGroupLeader() test will also fail.
Steven Moreland3d8166f2017-04-07 10:47:06 -070052 if (!isPermitted() || prio < PRIORITY_MIN ||
Glenn Kasten07b04652012-04-23 15:00:43 -070053 prio > PRIORITY_MAX || Process.getThreadGroupLeader(tid) != pid) {
Philip Cuadra41950492017-03-24 14:09:19 -070054 return PackageManager.PERMISSION_DENIED;
55 }
56 if (Binder.getCallingUid() != Process.BLUETOOTH_UID) {
57 try {
58 // make good use of our CAP_SYS_NICE capability
59 Process.setThreadGroup(tid, !isForApp ?
Glenn Kasten9a3f9532017-04-20 14:49:16 -070060 Process.THREAD_GROUP_AUDIO_SYS : Process.THREAD_GROUP_RT_APP);
Philip Cuadra41950492017-03-24 14:09:19 -070061 } catch (RuntimeException e) {
62 Log.e(TAG, "Failed setThreadGroup: " + e);
63 return PackageManager.PERMISSION_DENIED;
64 }
Glenn Kasten07b04652012-04-23 15:00:43 -070065 }
66 try {
Glenn Kasten07b04652012-04-23 15:00:43 -070067 // must be in this order or it fails the schedulability constraint
Tim Murray38ee3372016-08-18 11:13:26 -070068 Process.setThreadScheduler(tid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK,
Philip Cuadra41950492017-03-24 14:09:19 -070069 prio);
Glenn Kasten07b04652012-04-23 15:00:43 -070070 } catch (RuntimeException e) {
Philip Cuadra41950492017-03-24 14:09:19 -070071 Log.e(TAG, "Failed setThreadScheduler: " + e);
Glenn Kasten07b04652012-04-23 15:00:43 -070072 return PackageManager.PERMISSION_DENIED;
73 }
74 return PackageManager.PERMISSION_GRANTED;
75 }
76
Steven Moreland3d8166f2017-04-07 10:47:06 -070077 private boolean isPermitted() {
78 // schedulerservice hidl
79 if (Binder.getCallingPid() == Process.myPid()) {
80 return true;
81 }
82
83 switch (Binder.getCallingUid()) {
Andy Hunged0ea402015-10-30 14:11:46 -070084 case Process.AUDIOSERVER_UID: // fastcapture, fastmixer
Eino-Ville Talvala1f677fd2016-04-25 17:05:03 -070085 case Process.CAMERASERVER_UID: // camera high frame rate recording
Philip Cuadra41950492017-03-24 14:09:19 -070086 case Process.BLUETOOTH_UID: // Bluetooth audio playback
Andy Hunged0ea402015-10-30 14:11:46 -070087 return true;
88 default:
89 return false;
90 }
91 }
Glenn Kasten07b04652012-04-23 15:00:43 -070092}