blob: 93504124291a841ee3f35705411c7f9e8a735f96 [file] [log] [blame]
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08001/*
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/cdefs.h>
20#include <sys/types.h>
21
22#include <map>
Yifan Hong53e0deb2019-03-22 17:01:08 -070023#include <memory>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080024#include <mutex>
25#include <string>
Yifan Hong53e0deb2019-03-22 17:01:08 -070026#include <vector>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080027
Yifan Hong53e0deb2019-03-22 17:01:08 -070028#include <android/cgrouprc.h>
29
30// Convenient wrapper of an ACgroupController pointer.
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080031class CgroupController {
32 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070033 // Does not own controller
34 explicit CgroupController(const ACgroupController* controller) : controller_(controller) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080035
Yifan Hong53e0deb2019-03-22 17:01:08 -070036 uint32_t version() const;
37 const char* name() const;
38 const char* path() const;
39
40 bool HasValue() const;
Suren Baghdasaryanfa7a05f2019-05-08 17:59:55 -070041 bool IsUsable() const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080042
43 std::string GetTasksFilePath(const std::string& path) const;
44 std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const;
45 bool GetTaskGroup(int tid, std::string* group) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080046 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070047 const ACgroupController* controller_ = nullptr;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080048};
49
50class CgroupMap {
51 public:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080052 // Selinux policy ensures only init process can successfully use this function
53 static bool SetupCgroups();
54
55 static CgroupMap& GetInstance();
Yifan Hong53e0deb2019-03-22 17:01:08 -070056 CgroupController FindController(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080057
58 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070059 bool loaded_ = false;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060 CgroupMap();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080061 bool LoadRcFile();
Wei Wangd71d3012019-03-07 11:59:12 -080062 void Print() const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080063};