| 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 |  | 
|  | 49 | // Complete controller description for mounting cgroups | 
|  | 50 | class CgroupDescriptor { | 
|  | 51 | public: | 
|  | 52 | CgroupDescriptor(uint32_t version, const std::string& name, const std::string& path, | 
|  | 53 | mode_t mode, const std::string& uid, const std::string& gid); | 
|  | 54 |  | 
|  | 55 | const CgroupController* controller() const { return &controller_; } | 
|  | 56 | mode_t mode() const { return mode_; } | 
|  | 57 | std::string uid() const { return uid_; } | 
|  | 58 | std::string gid() const { return gid_; } | 
|  | 59 |  | 
|  | 60 | private: | 
|  | 61 | CgroupController controller_; | 
|  | 62 | mode_t mode_; | 
|  | 63 | std::string uid_; | 
|  | 64 | std::string gid_; | 
|  | 65 | }; | 
|  | 66 |  | 
|  | 67 | struct CgroupFile { | 
|  | 68 | static constexpr uint32_t FILE_VERSION_1 = 1; | 
|  | 69 | static constexpr uint32_t FILE_CURR_VERSION = FILE_VERSION_1; | 
|  | 70 |  | 
|  | 71 | uint32_t version_; | 
|  | 72 | uint32_t controller_count_; | 
|  | 73 | CgroupController controllers_[]; | 
|  | 74 | }; | 
|  | 75 |  | 
|  | 76 | class CgroupMap { | 
|  | 77 | public: | 
|  | 78 | static constexpr const char* CGROUPS_RC_FILE = "cgroup.rc"; | 
|  | 79 |  | 
|  | 80 | // Selinux policy ensures only init process can successfully use this function | 
|  | 81 | static bool SetupCgroups(); | 
|  | 82 |  | 
|  | 83 | static CgroupMap& GetInstance(); | 
|  | 84 |  | 
|  | 85 | const CgroupController* FindController(const std::string& name) const; | 
|  | 86 |  | 
|  | 87 | private: | 
|  | 88 | struct CgroupFile* cg_file_data_; | 
|  | 89 | size_t cg_file_size_; | 
|  | 90 |  | 
|  | 91 | CgroupMap(); | 
|  | 92 | ~CgroupMap(); | 
|  | 93 |  | 
|  | 94 | bool LoadRcFile(); | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 95 | void Print() const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 96 | }; |