1 /**
2  * Copyright (c) 2022, 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 #ifndef CPP_WATCHDOG_SERVER_SRC_THREADPRIORITYCONTROLLER_H_
18 #define CPP_WATCHDOG_SERVER_SRC_THREADPRIORITYCONTROLLER_H_
19 
20 #include <aidl/android/automotive/watchdog/internal/ThreadPolicyWithPriority.h>
21 #include <android-base/result.h>
22 
23 #include <sched.h>
24 
25 namespace android {
26 namespace automotive {
27 namespace watchdog {
28 
29 class ThreadPriorityControllerInterface {
30 public:
31     virtual ~ThreadPriorityControllerInterface() = default;
32     virtual android::base::Result<void> setThreadPriority(int pid, int tid, int uid, int policy,
33                                                           int priority) = 0;
34     virtual android::base::Result<void> getThreadPriority(
35             int pid, int tid, int uid,
36             aidl::android::automotive::watchdog::internal::ThreadPolicyWithPriority* result) = 0;
37 };
38 
39 class ThreadPriorityController final : public ThreadPriorityControllerInterface {
40 public:
41     // An interface for stubbing system calls in unit testing.
42     class SystemCallsInterface {
43     public:
44         virtual int setScheduler(pid_t tid, int policy, const sched_param* param) = 0;
45         virtual int getScheduler(pid_t tid) = 0;
46         virtual int getParam(pid_t tid, sched_param* param) = 0;
47         virtual android::base::Result<std::tuple<uid_t, pid_t>> readPidStatusFileForPid(
48                 pid_t pid) = 0;
49 
50         virtual ~SystemCallsInterface() = default;
51     };
52 
ThreadPriorityController()53     ThreadPriorityController() : mSystemCallsInterface(std::make_unique<SystemCalls>()) {}
54 
ThreadPriorityController(std::unique_ptr<SystemCallsInterface> s)55     explicit ThreadPriorityController(std::unique_ptr<SystemCallsInterface> s) :
56           mSystemCallsInterface(std::move(s)) {}
57 
58     android::base::Result<void> setThreadPriority(int pid, int tid, int uid, int policy,
59                                                   int priority) override;
60     android::base::Result<void> getThreadPriority(
61             int pid, int tid, int uid,
62             aidl::android::automotive::watchdog::internal::ThreadPolicyWithPriority* result)
63             override;
64 
65 private:
66     class SystemCalls final : public SystemCallsInterface {
67         int setScheduler(pid_t tid, int policy, const sched_param* param) override;
68         int getScheduler(pid_t tid) override;
69         int getParam(pid_t tid, sched_param* param) override;
70         android::base::Result<std::tuple<uid_t, pid_t>> readPidStatusFileForPid(pid_t pid) override;
71     };
72 
73     std::unique_ptr<SystemCallsInterface> mSystemCallsInterface;
74 
75     android::base::Result<void> checkPidTidUid(pid_t pid, pid_t tid, uid_t uid);
76 };
77 
78 }  // namespace watchdog
79 }  // namespace automotive
80 }  // namespace android
81 
82 #endif  // CPP_WATCHDOG_SERVER_SRC_THREADPRIORITYCONTROLLER_H_
83