2018-12-21 20:41:50 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <map>
|
2019-03-23 01:01:08 +01:00
|
|
|
#include <memory>
|
2018-12-21 20:41:50 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2019-03-23 01:01:08 +01:00
|
|
|
#include <vector>
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
#include <android/cgrouprc.h>
|
|
|
|
|
|
|
|
// Convenient wrapper of an ACgroupController pointer.
|
2018-12-21 20:41:50 +01:00
|
|
|
class CgroupController {
|
|
|
|
public:
|
2019-03-23 01:01:08 +01:00
|
|
|
// Does not own controller
|
2019-06-26 20:08:50 +02:00
|
|
|
explicit CgroupController(const ACgroupController* controller)
|
|
|
|
: controller_(controller), state_(UNKNOWN) {}
|
2019-03-23 01:01:08 +01:00
|
|
|
|
|
|
|
uint32_t version() const;
|
|
|
|
const char* name() const;
|
|
|
|
const char* path() const;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
bool HasValue() const;
|
2019-06-26 20:08:50 +02:00
|
|
|
bool IsUsable();
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
std::string GetTasksFilePath(const std::string& path) const;
|
|
|
|
std::string GetProcsFilePath(const std::string& path, uid_t uid, pid_t pid) const;
|
|
|
|
bool GetTaskGroup(int tid, std::string* group) const;
|
|
|
|
private:
|
2019-06-26 20:08:50 +02:00
|
|
|
enum ControllerState {
|
|
|
|
UNKNOWN = 0,
|
|
|
|
USABLE = 1,
|
|
|
|
MISSING = 2,
|
|
|
|
};
|
|
|
|
|
2019-03-23 01:01:08 +01:00
|
|
|
const ACgroupController* controller_ = nullptr;
|
2019-06-26 20:08:50 +02:00
|
|
|
ControllerState state_;
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class CgroupMap {
|
|
|
|
public:
|
|
|
|
// Selinux policy ensures only init process can successfully use this function
|
|
|
|
static bool SetupCgroups();
|
|
|
|
|
|
|
|
static CgroupMap& GetInstance();
|
2019-03-23 01:01:08 +01:00
|
|
|
CgroupController FindController(const std::string& name) const;
|
2021-06-17 02:01:19 +02:00
|
|
|
CgroupController FindControllerByPath(const std::string& path) const;
|
2021-07-30 22:42:39 +02:00
|
|
|
int ActivateControllers(const std::string& path) const;
|
2018-12-21 20:41:50 +01:00
|
|
|
|
|
|
|
private:
|
2019-03-23 01:01:08 +01:00
|
|
|
bool loaded_ = false;
|
2018-12-21 20:41:50 +01:00
|
|
|
CgroupMap();
|
|
|
|
bool LoadRcFile();
|
2019-03-07 20:59:12 +01:00
|
|
|
void Print() const;
|
2018-12-21 20:41:50 +01:00
|
|
|
};
|