Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.os; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 18 | |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 19 | import android.content.pm.PackageManager; |
| 20 | import android.os.Binder; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 21 | import android.os.ISchedulingPolicyService; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 22 | import android.os.Process; |
Philip Cuadra | 4195049 | 2017-03-24 14:09:19 -0700 | [diff] [blame] | 23 | import android.util.Log; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 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; |
Glenn Kasten | 430c254 | 2012-06-04 11:42:01 -0700 | [diff] [blame] | 36 | private static final int PRIORITY_MAX = 3; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 37 | |
| 38 | public SchedulingPolicyService() { |
| 39 | } |
| 40 | |
Mikhail Naganov | a0cb18d | 2017-02-07 10:50:21 -0800 | [diff] [blame] | 41 | // 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 Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 44 | //Log.i(TAG, "requestPriority(pid=" + pid + ", tid=" + tid + ", prio=" + prio + ")"); |
| 45 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 46 | // 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 Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 50 | // 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 Moreland | 3d8166f | 2017-04-07 10:47:06 -0700 | [diff] [blame] | 52 | if (!isPermitted() || prio < PRIORITY_MIN || |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 53 | prio > PRIORITY_MAX || Process.getThreadGroupLeader(tid) != pid) { |
Philip Cuadra | 4195049 | 2017-03-24 14:09:19 -0700 | [diff] [blame] | 54 | 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 Kasten | 9a3f953 | 2017-04-20 14:49:16 -0700 | [diff] [blame] | 60 | Process.THREAD_GROUP_AUDIO_SYS : Process.THREAD_GROUP_RT_APP); |
Philip Cuadra | 4195049 | 2017-03-24 14:09:19 -0700 | [diff] [blame] | 61 | } catch (RuntimeException e) { |
| 62 | Log.e(TAG, "Failed setThreadGroup: " + e); |
| 63 | return PackageManager.PERMISSION_DENIED; |
| 64 | } |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 65 | } |
| 66 | try { |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 67 | // must be in this order or it fails the schedulability constraint |
Tim Murray | 38ee337 | 2016-08-18 11:13:26 -0700 | [diff] [blame] | 68 | Process.setThreadScheduler(tid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, |
Philip Cuadra | 4195049 | 2017-03-24 14:09:19 -0700 | [diff] [blame] | 69 | prio); |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 70 | } catch (RuntimeException e) { |
Philip Cuadra | 4195049 | 2017-03-24 14:09:19 -0700 | [diff] [blame] | 71 | Log.e(TAG, "Failed setThreadScheduler: " + e); |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 72 | return PackageManager.PERMISSION_DENIED; |
| 73 | } |
| 74 | return PackageManager.PERMISSION_GRANTED; |
| 75 | } |
| 76 | |
Steven Moreland | 3d8166f | 2017-04-07 10:47:06 -0700 | [diff] [blame] | 77 | private boolean isPermitted() { |
| 78 | // schedulerservice hidl |
| 79 | if (Binder.getCallingPid() == Process.myPid()) { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | switch (Binder.getCallingUid()) { |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 84 | case Process.AUDIOSERVER_UID: // fastcapture, fastmixer |
Eino-Ville Talvala | 1f677fd | 2016-04-25 17:05:03 -0700 | [diff] [blame] | 85 | case Process.CAMERASERVER_UID: // camera high frame rate recording |
Philip Cuadra | 4195049 | 2017-03-24 14:09:19 -0700 | [diff] [blame] | 86 | case Process.BLUETOOTH_UID: // Bluetooth audio playback |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 87 | return true; |
| 88 | default: |
| 89 | return false; |
| 90 | } |
| 91 | } |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 92 | } |