libprocessgroup_setup: use libcgrouprc_format

Use CgroupController definition from libcgrouprc_format, not
libprocessgroup, because the wire format will be removed
from libprocessgroup later.

Bug: 123664216
Test: builds
Change-Id: If5e2301a1b65c3f6a51a1661cfeeed4e299f634e
Merged-In: If5e2301a1b65c3f6a51a1661cfeeed4e299f634e
This commit is contained in:
Yifan Hong 2019-03-22 17:21:58 -07:00
parent 6f9ce2e548
commit d8ce1fb1d1
4 changed files with 28 additions and 9 deletions

View File

@ -61,6 +61,7 @@ cc_defaults {
static_libs: [
"libseccomp_policy",
"libavb",
"libcgrouprc_format",
"libprotobuf-cpp-lite",
"libpropertyinfoserializer",
"libpropertyinfoparser",

View File

@ -25,9 +25,12 @@ cc_library_shared {
],
shared_libs: [
"libbase",
"libprocessgroup",
"libcgrouprc",
"libjsoncpp",
],
static_libs: [
"libcgrouprc_format",
],
header_libs: [
"libprocessgroup_headers",
],

View File

@ -16,8 +16,10 @@
#pragma once
// TODO(b/123664216): Use libcgrouprc_format
#include "../cgroup_map.h"
#include <processgroup/format/cgroup_controller.h>
namespace android {
namespace cgrouprc {
// Complete controller description for mounting cgroups
class CgroupDescriptor {
@ -25,14 +27,17 @@ class CgroupDescriptor {
CgroupDescriptor(uint32_t version, const std::string& name, const std::string& path,
mode_t mode, const std::string& uid, const std::string& gid);
const CgroupController* controller() const { return &controller_; }
const format::CgroupController* controller() const { return &controller_; }
mode_t mode() const { return mode_; }
std::string uid() const { return uid_; }
std::string gid() const { return gid_; }
private:
CgroupController controller_;
format::CgroupController controller_;
mode_t mode_ = 0;
std::string uid_;
std::string gid_;
};
} // namespace cgrouprc
} // namespace android

View File

@ -37,6 +37,7 @@
#include <android-base/unique_fd.h>
#include <json/reader.h>
#include <json/value.h>
#include <processgroup/format/cgroup_file.h>
#include <processgroup/processgroup.h>
#include <processgroup/setup.h>
@ -46,6 +47,9 @@ using android::base::GetBoolProperty;
using android::base::StringPrintf;
using android::base::unique_fd;
namespace android {
namespace cgrouprc {
static constexpr const char* CGROUPS_DESC_FILE = "/etc/cgroups.json";
static constexpr const char* CGROUPS_DESC_VENDOR_FILE = "/vendor/etc/cgroups.json";
@ -185,7 +189,7 @@ static bool ReadDescriptors(std::map<std::string, CgroupDescriptor>* descriptors
#if defined(__ANDROID__)
static bool SetupCgroup(const CgroupDescriptor& descriptor) {
const CgroupController* controller = descriptor.controller();
const format::CgroupController* controller = descriptor.controller();
// mkdir <path> [mode] [owner] [group]
if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) {
@ -244,8 +248,8 @@ static bool WriteRcFile(const std::map<std::string, CgroupDescriptor>& descripto
return false;
}
CgroupFile fl;
fl.version_ = CgroupFile::FILE_CURR_VERSION;
format::CgroupFile fl;
fl.version_ = format::CgroupFile::FILE_CURR_VERSION;
fl.controller_count_ = descriptors.size();
int ret = TEMP_FAILURE_RETRY(write(fd, &fl, sizeof(fl)));
if (ret < 0) {
@ -254,7 +258,8 @@ static bool WriteRcFile(const std::map<std::string, CgroupDescriptor>& descripto
}
for (const auto& [name, descriptor] : descriptors) {
ret = TEMP_FAILURE_RETRY(write(fd, descriptor.controller(), sizeof(CgroupController)));
ret = TEMP_FAILURE_RETRY(
write(fd, descriptor.controller(), sizeof(format::CgroupController)));
if (ret < 0) {
PLOG(ERROR) << "write() failed for " << CGROUPS_RC_PATH;
return false;
@ -269,7 +274,12 @@ CgroupDescriptor::CgroupDescriptor(uint32_t version, const std::string& name,
const std::string& gid)
: controller_(version, name, path), mode_(mode), uid_(uid), gid_(gid) {}
} // namespace cgrouprc
} // namespace android
bool CgroupSetupCgroups() {
using namespace android::cgrouprc;
std::map<std::string, CgroupDescriptor> descriptors;
if (getpid() != 1) {