| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 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 | #pragma once | 
|  | 18 |  | 
|  | 19 | #include <sys/cdefs.h> | 
|  | 20 | #include <sys/types.h> | 
|  | 21 |  | 
|  | 22 | #include <map> | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 23 | #include <memory> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 24 | #include <mutex> | 
|  | 25 | #include <string> | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 26 | #include <vector> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 27 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 28 | #include <android/cgrouprc.h> | 
|  | 29 |  | 
|  | 30 | // Convenient wrapper of an ACgroupController pointer. | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 31 | class CgroupController { | 
|  | 32 | public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 33 | // Does not own controller | 
| Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 34 | explicit CgroupController(const ACgroupController* controller) | 
|  | 35 | : controller_(controller), state_(UNKNOWN) {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 36 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 37 | uint32_t version() const; | 
|  | 38 | const char* name() const; | 
|  | 39 | const char* path() const; | 
|  | 40 |  | 
|  | 41 | bool HasValue() const; | 
| Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 42 | bool IsUsable(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | std::string GetTasksFilePath(const std::string& path) const; | 
|  | 45 | std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const; | 
|  | 46 | bool GetTaskGroup(int tid, std::string* group) const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 47 | private: | 
| Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 48 | enum ControllerState { | 
|  | 49 | UNKNOWN = 0, | 
|  | 50 | USABLE = 1, | 
|  | 51 | MISSING = 2, | 
|  | 52 | }; | 
|  | 53 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 54 | const ACgroupController* controller_ = nullptr; | 
| Suren Baghdasaryan | 25eb1bf | 2019-06-26 11:08:50 -0700 | [diff] [blame] | 55 | ControllerState state_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
|  | 58 | class CgroupMap { | 
|  | 59 | public: | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 60 | // Selinux policy ensures only init process can successfully use this function | 
|  | 61 | static bool SetupCgroups(); | 
|  | 62 |  | 
|  | 63 | static CgroupMap& GetInstance(); | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 64 | CgroupController FindController(const std::string& name) const; | 
| Suren Baghdasaryan | 9e3ace5 | 2021-06-16 17:01:19 -0700 | [diff] [blame] | 65 | CgroupController FindControllerByPath(const std::string& path) const; | 
| Suren Baghdasaryan | 25ad3f9 | 2021-07-30 13:42:39 -0700 | [diff] [blame] | 66 | int ActivateControllers(const std::string& path) const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 67 |  | 
|  | 68 | private: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 69 | bool loaded_ = false; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 70 | CgroupMap(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 71 | bool LoadRcFile(); | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 72 | void Print() const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 73 | }; |