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 #include <iterator> 18 19 #include <android-base/logging.h> 20 #include <android/cgrouprc.h> 21 #include <processgroup/util.h> 22 23 #include "cgrouprc_internal.h" 24 LoadDescriptors()25static CgroupDescriptorMap* LoadDescriptors() { 26 CgroupDescriptorMap* descriptors = new CgroupDescriptorMap; 27 if (!ReadDescriptors(descriptors)) { 28 LOG(ERROR) << "Failed to load cgroup description file"; 29 return nullptr; 30 } 31 return descriptors; 32 } 33 GetInstance()34static const CgroupDescriptorMap* GetInstance() { 35 // Deliberately leak this object (not munmap) to avoid a race between destruction on 36 // process exit and concurrent access from another thread. 37 static const CgroupDescriptorMap* descriptors = LoadDescriptors(); 38 return descriptors; 39 } 40 ACgroupFile_getVersion()41uint32_t ACgroupFile_getVersion() { 42 static constexpr uint32_t FILE_VERSION_1 = 1; 43 auto descriptors = GetInstance(); 44 if (descriptors == nullptr) return 0; 45 // There has only ever been one version, and there will be no more since cgroup.rc is no more 46 return FILE_VERSION_1; 47 } 48 ACgroupFile_getControllerCount()49uint32_t ACgroupFile_getControllerCount() { 50 auto descriptors = GetInstance(); 51 if (descriptors == nullptr) return 0; 52 return descriptors->size(); 53 } 54 ACgroupFile_getController(uint32_t index)55const ACgroupController* ACgroupFile_getController(uint32_t index) { 56 auto descriptors = GetInstance(); 57 if (descriptors == nullptr) return nullptr; 58 CHECK(index < descriptors->size()); 59 // Although the object is not actually an ACgroupController object, all ACgroupController_* 60 // functions implicitly convert ACgroupController* back to CgroupController* before invoking 61 // member functions. 62 const CgroupController* p = std::next(descriptors->begin(), index)->second.controller(); 63 return static_cast<const ACgroupController*>(p); 64 } 65