• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/mman.h>
18 #include <sys/stat.h>
19 
20 #include <memory>
21 
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/unique_fd.h>
25 #include <android/cgrouprc.h>
26 #include <processgroup/processgroup.h>
27 
28 #include "cgrouprc_internal.h"
29 
30 using android::base::StringPrintf;
31 using android::base::unique_fd;
32 
33 using android::cgrouprc::format::CgroupController;
34 using android::cgrouprc::format::CgroupFile;
35 
LoadRcFile()36 static CgroupFile* LoadRcFile() {
37     struct stat sb;
38 
39     unique_fd fd(TEMP_FAILURE_RETRY(open(CGROUPS_RC_PATH, O_RDONLY | O_CLOEXEC)));
40     if (fd < 0) {
41         PLOG(ERROR) << "open() failed for " << CGROUPS_RC_PATH;
42         return nullptr;
43     }
44 
45     if (fstat(fd, &sb) < 0) {
46         PLOG(ERROR) << "fstat() failed for " << CGROUPS_RC_PATH;
47         return nullptr;
48     }
49 
50     size_t file_size = sb.st_size;
51     if (file_size < sizeof(CgroupFile)) {
52         LOG(ERROR) << "Invalid file format " << CGROUPS_RC_PATH;
53         return nullptr;
54     }
55 
56     CgroupFile* file_data = (CgroupFile*)mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0);
57     if (file_data == MAP_FAILED) {
58         PLOG(ERROR) << "Failed to mmap " << CGROUPS_RC_PATH;
59         return nullptr;
60     }
61 
62     if (file_data->version_ != CgroupFile::FILE_CURR_VERSION) {
63         LOG(ERROR) << CGROUPS_RC_PATH << " file version mismatch";
64         munmap(file_data, file_size);
65         return nullptr;
66     }
67 
68     auto expected = sizeof(CgroupFile) + file_data->controller_count_ * sizeof(CgroupController);
69     if (file_size != expected) {
70         LOG(ERROR) << CGROUPS_RC_PATH << " file has invalid size, expected " << expected
71                    << ", actual " << file_size;
72         munmap(file_data, file_size);
73         return nullptr;
74     }
75 
76     return file_data;
77 }
78 
GetInstance()79 static CgroupFile* GetInstance() {
80     // Deliberately leak this object (not munmap) to avoid a race between destruction on
81     // process exit and concurrent access from another thread.
82     static auto* file = LoadRcFile();
83     return file;
84 }
85 
ACgroupFile_getVersion()86 uint32_t ACgroupFile_getVersion() {
87     auto file = GetInstance();
88     if (file == nullptr) return 0;
89     return file->version_;
90 }
91 
ACgroupFile_getControllerCount()92 uint32_t ACgroupFile_getControllerCount() {
93     auto file = GetInstance();
94     if (file == nullptr) return 0;
95     return file->controller_count_;
96 }
97 
ACgroupFile_getController(uint32_t index)98 const ACgroupController* ACgroupFile_getController(uint32_t index) {
99     auto file = GetInstance();
100     if (file == nullptr) return nullptr;
101     CHECK(index < file->controller_count_);
102     // Although the object is not actually an ACgroupController object, all ACgroupController_*
103     // functions implicitly convert ACgroupController* back to CgroupController* before invoking
104     // member functions.
105     return static_cast<ACgroupController*>(&file->controllers_[index]);
106 }
107