cgroup_map.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #pragma once
  17. #include <sys/cdefs.h>
  18. #include <sys/types.h>
  19. #include <map>
  20. #include <memory>
  21. #include <mutex>
  22. #include <string>
  23. #include <vector>
  24. #include <android/cgrouprc.h>
  25. // Convenient wrapper of an ACgroupController pointer.
  26. class CgroupController {
  27. public:
  28. // Does not own controller
  29. explicit CgroupController(const ACgroupController* controller)
  30. : controller_(controller), state_(UNKNOWN) {}
  31. uint32_t version() const;
  32. const char* name() const;
  33. const char* path() const;
  34. bool HasValue() const;
  35. bool IsUsable();
  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. private:
  40. enum ControllerState {
  41. UNKNOWN = 0,
  42. USABLE = 1,
  43. MISSING = 2,
  44. };
  45. const ACgroupController* controller_ = nullptr;
  46. ControllerState state_;
  47. };
  48. class CgroupMap {
  49. public:
  50. // Selinux policy ensures only init process can successfully use this function
  51. static bool SetupCgroups();
  52. static CgroupMap& GetInstance();
  53. CgroupController FindController(const std::string& name) const;
  54. private:
  55. bool loaded_ = false;
  56. CgroupMap();
  57. bool LoadRcFile();
  58. void Print() const;
  59. };