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> |
| 23 | #include <mutex> |
| 24 | #include <string> |
| 25 | |
| 26 | // Minimal controller description to be mmapped into process address space |
| 27 | class CgroupController { |
| 28 | public: |
| 29 | CgroupController() {} |
| 30 | CgroupController(uint32_t version, const std::string& name, const std::string& path); |
| 31 | |
| 32 | uint32_t version() const { return version_; } |
| 33 | const char* name() const { return name_; } |
| 34 | const char* path() const { return path_; } |
| 35 | |
| 36 | std::string GetTasksFilePath(const std::string& path) const; |
| 37 | std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const; |
| 38 | bool GetTaskGroup(int tid, std::string* group) const; |
| 39 | |
| 40 | private: |
| 41 | static constexpr size_t CGROUP_NAME_BUF_SZ = 16; |
| 42 | static constexpr size_t CGROUP_PATH_BUF_SZ = 32; |
| 43 | |
| 44 | uint32_t version_; |
| 45 | char name_[CGROUP_NAME_BUF_SZ]; |
| 46 | char path_[CGROUP_PATH_BUF_SZ]; |
| 47 | }; |
| 48 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 49 | struct CgroupFile { |
| 50 | static constexpr uint32_t FILE_VERSION_1 = 1; |
| 51 | static constexpr uint32_t FILE_CURR_VERSION = FILE_VERSION_1; |
| 52 | |
| 53 | uint32_t version_; |
| 54 | uint32_t controller_count_; |
| 55 | CgroupController controllers_[]; |
| 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(); |
| 64 | |
| 65 | const CgroupController* FindController(const std::string& name) const; |
| 66 | |
| 67 | private: |
| 68 | struct CgroupFile* cg_file_data_; |
| 69 | size_t cg_file_size_; |
| 70 | |
| 71 | CgroupMap(); |
| 72 | ~CgroupMap(); |
| 73 | |
| 74 | bool LoadRcFile(); |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 75 | void Print() const; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 76 | }; |