cgroup_file.cpp 3.3 KB

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