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; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 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; |
Glenn Kasten | 430c254 | 2012-06-04 11:42:01 -0700 | [diff] [blame] | 35 | private static final int PRIORITY_MAX = 3; |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 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 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 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 |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 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. |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 49 | if (!isPermittedCallingUid() || prio < PRIORITY_MIN || |
Glenn Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 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, prio); |
| 59 | } catch (RuntimeException e) { |
| 60 | return PackageManager.PERMISSION_DENIED; |
| 61 | } |
| 62 | return PackageManager.PERMISSION_GRANTED; |
| 63 | } |
| 64 | |
Andy Hung | ed0ea40 | 2015-10-30 14:11:46 -0700 | [diff] [blame] | 65 | 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 Kasten | 07b0465 | 2012-04-23 15:00:43 -0700 | [diff] [blame] | 75 | } |