• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.util.Log;
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;
36     private static final int PRIORITY_MAX = 3;
37 
SchedulingPolicyService()38     public SchedulingPolicyService() {
39     }
40 
41     // TODO(b/35196900) We should pass the period in time units, rather
42     // than a fixed priority number.
requestPriority(int pid, int tid, int prio, boolean isForApp)43     public int requestPriority(int pid, int tid, int prio, boolean isForApp) {
44         //Log.i(TAG, "requestPriority(pid=" + pid + ", tid=" + tid + ", prio=" + prio + ")");
45 
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
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.
52         if (!isPermitted() || prio < PRIORITY_MIN ||
53                 prio > PRIORITY_MAX || Process.getThreadGroupLeader(tid) != pid) {
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 ?
60                   Process.THREAD_GROUP_AUDIO_SYS : Process.THREAD_GROUP_RT_APP);
61             } catch (RuntimeException e) {
62                 Log.e(TAG, "Failed setThreadGroup: " + e);
63                 return PackageManager.PERMISSION_DENIED;
64            }
65         }
66         try {
67             // must be in this order or it fails the schedulability constraint
68             Process.setThreadScheduler(tid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK,
69                                        prio);
70         } catch (RuntimeException e) {
71             Log.e(TAG, "Failed setThreadScheduler: " + e);
72             return PackageManager.PERMISSION_DENIED;
73         }
74         return PackageManager.PERMISSION_GRANTED;
75     }
76 
isPermitted()77     private boolean isPermitted() {
78         // schedulerservice hidl
79         if (Binder.getCallingPid() == Process.myPid()) {
80             return true;
81         }
82 
83         switch (Binder.getCallingUid()) {
84         case Process.AUDIOSERVER_UID: // fastcapture, fastmixer
85         case Process.CAMERASERVER_UID: // camera high frame rate recording
86         case Process.BLUETOOTH_UID: // Bluetooth audio playback
87             return true;
88         default:
89             return false;
90         }
91     }
92 }
93