Set cg_file_data_ to null when initialization failed

Cannot really do calls if initialization failed.
Also fix LOG usage.

But: 124774415
Test: Build
Change-Id: Ifd7db90cfee51ba5830e1d4db8f8d19b5bc9feba
This commit is contained in:
Wei Wang 2019-03-07 11:59:12 -08:00
parent 2ba61b775a
commit d71d30179c
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;
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;
}
@ -185,7 +185,7 @@ static bool SetupCgroup(const CgroupDescriptor& descriptor) {
// mkdir <path> [mode] [owner] [group]
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;
}
@ -289,7 +289,7 @@ bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
std::string content;
if (!android::base::ReadFileToString(file_name, &content)) {
LOG(ERROR) << "Failed to read " << file_name;
PLOG(ERROR) << "Failed to read " << file_name;
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) {
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;
}
cg_file_size_ = sb.st_size;
if (cg_file_size_ < sizeof(CgroupFile)) {
PLOG(ERROR) << "Invalid file format " << cgroup_rc_path;
size_t file_size = sb.st_size;
if (file_size < sizeof(CgroupFile)) {
LOG(ERROR) << "Invalid file format " << cgroup_rc_path;
return false;
}
cg_file_data_ = (CgroupFile*)mmap(nullptr, cg_file_size_, PROT_READ, MAP_SHARED, fd, 0);
if (cg_file_data_ == MAP_FAILED) {
CgroupFile* file_data = (CgroupFile*)mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0);
if (file_data == MAP_FAILED) {
PLOG(ERROR) << "Failed to mmap " << cgroup_rc_path;
return false;
}
if (cg_file_data_->version_ != CgroupFile::FILE_CURR_VERSION) {
PLOG(ERROR) << cgroup_rc_path << " file version mismatch";
if (file_data->version_ != CgroupFile::FILE_CURR_VERSION) {
LOG(ERROR) << cgroup_rc_path << " file version mismatch";
munmap(file_data, file_size);
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;
}
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 controller count = " << cg_file_data_->controller_count_;
@ -397,7 +412,7 @@ bool CgroupMap::SetupCgroups() {
// load cgroups.json file
if (!ReadDescriptors(&descriptors)) {
PLOG(ERROR) << "Failed to load cgroup description file";
LOG(ERROR) << "Failed to load cgroup description file";
return false;
}
@ -412,7 +427,7 @@ bool CgroupMap::SetupCgroups() {
// 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;
}
@ -428,7 +443,7 @@ bool CgroupMap::SetupCgroups() {
std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE);
// chmod 0644 <cgroup_rc_path>
if (fchmodat(AT_FDCWD, cgroup_rc_path.c_str(), 0644, AT_SYMLINK_NOFOLLOW) < 0) {
LOG(ERROR) << "fchmodat() failed";
PLOG(ERROR) << "fchmodat() failed";
return false;
}
@ -437,6 +452,8 @@ bool CgroupMap::SetupCgroups() {
const CgroupController* CgroupMap::FindController(const std::string& name) const {
if (!cg_file_data_) {
LOG(ERROR) << "CgroupMap::FindController called for [" << getpid()
<< "] failed, RC file was not initialized properly";
return nullptr;
}

View file

@ -92,5 +92,5 @@ class CgroupMap {
~CgroupMap();
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->GetTaskGroup(tid, &subgroup)) {
PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
LOG(ERROR) << "Failed to find cgroup for tid " << tid;
return -1;
}
return 0;

View file

@ -119,7 +119,7 @@ bool SetAttributeAction::ExecuteForTask(int tid) const {
std::string 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;
}
@ -174,7 +174,7 @@ bool SetCgroupAction::AddTidToCgroup(int tid, int fd) {
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 (errno != ESRCH) {
PLOG(ERROR) << "JoinGroup failed to write '" << value << "'; fd=" << fd;
PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd;
return false;
}
}
@ -187,7 +187,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
if (fd_ >= 0) {
// fd is cached, reuse it
if (!AddTidToCgroup(pid, fd_)) {
PLOG(ERROR) << "Failed to add task into cgroup";
LOG(ERROR) << "Failed to add task into cgroup";
return false;
}
return true;
@ -206,7 +206,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
return false;
}
if (!AddTidToCgroup(pid, tmp_fd)) {
PLOG(ERROR) << "Failed to add task into cgroup";
LOG(ERROR) << "Failed to add task into cgroup";
return false;
}
@ -219,7 +219,7 @@ bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
return true;
}
if (!AddTidToCgroup(pid, tmp_fd)) {
PLOG(ERROR) << "Failed to add task into cgroup";
LOG(ERROR) << "Failed to add task into cgroup";
return false;
}
@ -232,7 +232,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
if (fd_ >= 0) {
// fd is cached, reuse it
if (!AddTidToCgroup(tid, fd_)) {
PLOG(ERROR) << "Failed to add task into cgroup";
LOG(ERROR) << "Failed to add task into cgroup";
return false;
}
return true;
@ -244,7 +244,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
}
// 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;
#else
std::string tasks_path = controller_->GetTasksFilePath(path_);
@ -254,7 +254,7 @@ bool SetCgroupAction::ExecuteForTask(int tid) const {
return true;
}
if (!AddTidToCgroup(tid, tmp_fd)) {
PLOG(ERROR) << "Failed to add task into cgroup";
LOG(ERROR) << "Failed to add task into cgroup";
return false;
}