CgroupSetupCgroups: don't leak fd.

- If file is unlinked, the mmapped region is still there.
- If file is truncated, a SIGBUS will be seen, and holding
  the fd doesn't help.

Test: boots (sanity)
Bug: 123664216
Change-Id: I6683804bc795fab6798891a4471e5fe58fbffe13
This commit is contained in:
Yifan Hong 2019-04-02 15:07:48 -07:00
parent 25d42eeaa6
commit 8df75fc304
1 changed files with 2 additions and 7 deletions

View File

@ -235,14 +235,9 @@ static bool SetupCgroup(const CgroupDescriptor&) {
#endif
// WARNING: This function should be called only from SetupCgroups and only once.
// It intentionally leaks an FD, so additional invocation will result in additional leak.
static bool WriteRcFile(const std::map<std::string, CgroupDescriptor>& descriptors) {
// WARNING: We are intentionally leaking the FD to keep the file open forever.
// Let init keep the FD open to prevent file mappings from becoming invalid in
// case the file gets deleted somehow.
int fd = TEMP_FAILURE_RETRY(open(CGROUPS_RC_PATH, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
S_IRUSR | S_IRGRP | S_IROTH));
unique_fd fd(TEMP_FAILURE_RETRY(open(CGROUPS_RC_PATH, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
S_IRUSR | S_IRGRP | S_IROTH)));
if (fd < 0) {
PLOG(ERROR) << "open() failed for " << CGROUPS_RC_PATH;
return false;