Merge "Set cg_file_data_ to null when initialization failed"

This commit is contained in:
Treehugger Robot 2019-03-09 00:15:05 +00:00 committed by Gerrit Code Review
commit f3762004a3
4 changed files with 42 additions and 25 deletions

View file

@ -117,7 +117,7 @@ static bool ReadDescriptorsFromFile(const std::string& file_name,
std::string json_doc; std::string json_doc;
if (!android::base::ReadFileToString(file_name, &json_doc)) { if (!android::base::ReadFileToString(file_name, &json_doc)) {
LOG(ERROR) << "Failed to read task profiles from " << file_name; PLOG(ERROR) << "Failed to read task profiles from " << file_name;
return false; return false;
} }
@ -185,7 +185,7 @@ static bool SetupCgroup(const CgroupDescriptor& descriptor) {
// mkdir <path> [mode] [owner] [group] // mkdir <path> [mode] [owner] [group]
if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) { if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) {
PLOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup"; LOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
return false; return false;
} }
@ -289,7 +289,7 @@ bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
std::string file_name = StringPrintf("/proc/%d/cgroup", tid); std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
std::string content; std::string content;
if (!android::base::ReadFileToString(file_name, &content)) { if (!android::base::ReadFileToString(file_name, &content)) {
LOG(ERROR) << "Failed to read " << file_name; PLOG(ERROR) << "Failed to read " << file_name;
return false; return false;
} }
@ -323,7 +323,7 @@ CgroupDescriptor::CgroupDescriptor(uint32_t version, const std::string& name,
CgroupMap::CgroupMap() : cg_file_data_(nullptr), cg_file_size_(0) { CgroupMap::CgroupMap() : cg_file_data_(nullptr), cg_file_size_(0) {
if (!LoadRcFile()) { if (!LoadRcFile()) {
PLOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed"; LOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed";
} }
} }
@ -360,27 +360,42 @@ bool CgroupMap::LoadRcFile() {
return false; return false;
} }
cg_file_size_ = sb.st_size; size_t file_size = sb.st_size;
if (cg_file_size_ < sizeof(CgroupFile)) { if (file_size < sizeof(CgroupFile)) {
PLOG(ERROR) << "Invalid file format " << cgroup_rc_path; LOG(ERROR) << "Invalid file format " << cgroup_rc_path;
return false; return false;
} }
cg_file_data_ = (CgroupFile*)mmap(nullptr, cg_file_size_, PROT_READ, MAP_SHARED, fd, 0); CgroupFile* file_data = (CgroupFile*)mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0);
if (cg_file_data_ == MAP_FAILED) { if (file_data == MAP_FAILED) {
PLOG(ERROR) << "Failed to mmap " << cgroup_rc_path; PLOG(ERROR) << "Failed to mmap " << cgroup_rc_path;
return false; return false;
} }
if (cg_file_data_->version_ != CgroupFile::FILE_CURR_VERSION) { if (file_data->version_ != CgroupFile::FILE_CURR_VERSION) {
PLOG(ERROR) << cgroup_rc_path << " file version mismatch"; LOG(ERROR) << cgroup_rc_path << " file version mismatch";
munmap(file_data, file_size);
return false; return false;
} }
if (file_size != sizeof(CgroupFile) + file_data->controller_count_ * sizeof(CgroupController)) {
LOG(ERROR) << cgroup_rc_path << " file has invalid size";
munmap(file_data, file_size);
return false;
}
cg_file_data_ = file_data;
cg_file_size_ = file_size;
return true; return true;
} }
void CgroupMap::Print() { void CgroupMap::Print() const {
if (!cg_file_data_) {
LOG(ERROR) << "CgroupMap::Print called for [" << getpid()
<< "] failed, RC file was not initialized properly";
return;
}
LOG(INFO) << "File version = " << cg_file_data_->version_; LOG(INFO) << "File version = " << cg_file_data_->version_;
LOG(INFO) << "File controller count = " << cg_file_data_->controller_count_; LOG(INFO) << "File controller count = " << cg_file_data_->controller_count_;
@ -397,7 +412,7 @@ bool CgroupMap::SetupCgroups() {
// load cgroups.json file // load cgroups.json file
if (!ReadDescriptors(&descriptors)) { if (!ReadDescriptors(&descriptors)) {
PLOG(ERROR) << "Failed to load cgroup description file"; LOG(ERROR) << "Failed to load cgroup description file";
return false; return false;
} }
@ -412,7 +427,7 @@ bool CgroupMap::SetupCgroups() {
// mkdir <CGROUPS_RC_DIR> 0711 system system // mkdir <CGROUPS_RC_DIR> 0711 system system
if (!Mkdir(CGROUPS_RC_DIR, 0711, "system", "system")) { if (!Mkdir(CGROUPS_RC_DIR, 0711, "system", "system")) {
PLOG(ERROR) << "Failed to create directory for <CGROUPS_RC_FILE> file"; LOG(ERROR) << "Failed to create directory for <CGROUPS_RC_FILE> file";
return false; return false;
} }
@ -428,7 +443,7 @@ bool CgroupMap::SetupCgroups() {
std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE); std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE);
// chmod 0644 <cgroup_rc_path> // chmod 0644 <cgroup_rc_path>
if (fchmodat(AT_FDCWD, cgroup_rc_path.c_str(), 0644, AT_SYMLINK_NOFOLLOW) < 0) { if (fchmodat(AT_FDCWD, cgroup_rc_path.c_str(), 0644, AT_SYMLINK_NOFOLLOW) < 0) {
LOG(ERROR) << "fchmodat() failed"; PLOG(ERROR) << "fchmodat() failed";
return false; return false;
} }
@ -437,6 +452,8 @@ bool CgroupMap::SetupCgroups() {
const CgroupController* CgroupMap::FindController(const std::string& name) const { const CgroupController* CgroupMap::FindController(const std::string& name) const {
if (!cg_file_data_) { if (!cg_file_data_) {
LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
<< "] failed, RC file was not initialized properly";
return nullptr; return nullptr;
} }

View file

@ -92,5 +92,5 @@ class CgroupMap {
~CgroupMap(); ~CgroupMap();
bool LoadRcFile(); bool LoadRcFile();
void Print(); void Print() const;
}; };

View file

@ -167,7 +167,7 @@ static int getCGroupSubsys(int tid, const char* subsys, std::string& subgroup) {
if (!controller) return -1; if (!controller) return -1;
if (!controller->GetTaskGroup(tid, &subgroup)) { if (!controller->GetTaskGroup(tid, &subgroup)) {
PLOG(ERROR) << "Failed to find cgroup for tid " << tid; LOG(ERROR) << "Failed to find cgroup for tid " << tid;
return -1; return -1;
} }
return 0; return 0;

View file

@ -119,7 +119,7 @@ bool SetAttributeAction::ExecuteForTask(int tid) const {
std::string path; std::string path;
if (!attribute_->GetPathForTask(tid, &path)) { if (!attribute_->GetPathForTask(tid, &path)) {
PLOG(ERROR) << "Failed to find cgroup for tid " << tid; LOG(ERROR) << "Failed to find cgroup for tid " << tid;
return false; return false;
} }
@ -174,7 +174,7 @@ bool SetCgroupAction::AddTidToCgroup(int tid, int fd) {
if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) { if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) {
// If the thread is in the process of exiting, don't flag an error // If the thread is in the process of exiting, don't flag an error
if (errno != ESRCH) { if (errno != ESRCH) {
PLOG(ERROR) << "JoinGroup failed to write '" << value << "'; fd=" << fd; PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd;
return false; return false;
} }
} }
@ -187,7 +187,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
if (fd_ >= 0) { if (fd_ >= 0) {
// fd is cached, reuse it // fd is cached, reuse it
if (!AddTidToCgroup(pid, fd_)) { if (!AddTidToCgroup(pid, fd_)) {
PLOG(ERROR) << "Failed to add task into cgroup"; LOG(ERROR) << "Failed to add task into cgroup";
return false; return false;
} }
return true; return true;
@ -206,7 +206,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
return false; return false;
} }
if (!AddTidToCgroup(pid, tmp_fd)) { if (!AddTidToCgroup(pid, tmp_fd)) {
PLOG(ERROR) << "Failed to add task into cgroup"; LOG(ERROR) << "Failed to add task into cgroup";
return false; return false;
} }
@ -219,7 +219,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
return true; return true;
} }
if (!AddTidToCgroup(pid, tmp_fd)) { if (!AddTidToCgroup(pid, tmp_fd)) {
PLOG(ERROR) << "Failed to add task into cgroup"; LOG(ERROR) << "Failed to add task into cgroup";
return false; return false;
} }
@ -232,7 +232,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
if (fd_ >= 0) { if (fd_ >= 0) {
// fd is cached, reuse it // fd is cached, reuse it
if (!AddTidToCgroup(tid, fd_)) { if (!AddTidToCgroup(tid, fd_)) {
PLOG(ERROR) << "Failed to add task into cgroup"; LOG(ERROR) << "Failed to add task into cgroup";
return false; return false;
} }
return true; return true;
@ -244,7 +244,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
} }
// application-dependent path can't be used with tid // application-dependent path can't be used with tid
PLOG(ERROR) << "Application profile can't be applied to a thread"; LOG(ERROR) << "Application profile can't be applied to a thread";
return false; return false;
#else #else
std::string tasks_path = controller_->GetTasksFilePath(path_); std::string tasks_path = controller_->GetTasksFilePath(path_);
@ -254,7 +254,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
return true; return true;
} }
if (!AddTidToCgroup(tid, tmp_fd)) { if (!AddTidToCgroup(tid, tmp_fd)) {
PLOG(ERROR) << "Failed to add task into cgroup"; LOG(ERROR) << "Failed to add task into cgroup";
return false; return false;
} }