• 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 #pragma once
18 
19 #include <sys/types.h>
20 
21 #include <string>
22 
23 #include <android/cgrouprc.h>
24 
25 // Convenient wrapper of an ACgroupController pointer.
26 class CgroupController {
27   public:
28     // Does not own controller
CgroupController(const ACgroupController * controller)29     explicit CgroupController(const ACgroupController* controller)
30         : controller_(controller) {}
31 
32     uint32_t version() const;
33     const char* name() const;
34     const char* path() const;
35 
36     bool HasValue() const;
37     bool IsUsable();
38 
39     std::string GetTasksFilePath(const std::string& path) const;
40     std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const;
41     bool GetTaskGroup(pid_t tid, std::string* group) const;
42 
43   private:
44     enum ControllerState {
45         UNKNOWN = 0,
46         USABLE = 1,
47         MISSING = 2,
48     };
49 
50     const ACgroupController* controller_ = nullptr;
51     ControllerState state_ = ControllerState::UNKNOWN;
52 };
53 
54 class CgroupMap {
55   public:
56     // Selinux policy ensures only init process can successfully use this function
57     static bool SetupCgroups();
58 
59     static CgroupMap& GetInstance();
60     CgroupController FindController(const std::string& name) const;
61     CgroupController FindControllerByPath(const std::string& path) const;
62     int ActivateControllers(const std::string& path) const;
63 
64   private:
65     bool loaded_ = false;
66     CgroupMap();
67     bool LoadRcFile();
68     void Print() const;
69 };
70