src/linux/cgroups.cpp
// Return information about subsystems on the current machine. We get
// information from /proc/cgroups file. Each line in it describes a subsystem.
// @return  A map from subsystem names to SubsystemInfo instances if succeeds.
//          Error if any unexpected happens.
Return information about subsystems on the current machine. We get
information from /proc/cgroups file. Each line in it describes a subsystem.
Return information about subsystems on the current machine . We get information from / proc / cgroups file . Each line in it describes a subsystem .
static Try<std::map<std::string, SubsystemInfo> > subsystems()
{
  std::ifstream file("/proc/cgroups");
  if (!file.is_open()) {
    return Try<std::map<std::string, SubsystemInfo> >::error(
        "Failed to open /proc/cgroups");
  }
  std::map<std::string, SubsystemInfo> infos;
  while (!file.eof()) {
    std::string line;
    std::getline(file, line);
    if (file.fail()) {
      if (!file.eof()) {
        file.close();
        return Try<std::map<std::string, SubsystemInfo> >::error(
            "Failed to read /proc/cgroups");
      }
    } else {
      if (line.empty()) {
        continue;
      } else if (line.find_first_of('#') == 0) {
        continue;
      } else {
        std::string name;
        int hierarchy;
        int cgroups;
        bool enabled;
        std::istringstream ss(line);
        ss >> std::dec >> name >> hierarchy >> cgroups >> enabled;
        if (ss.fail() && !ss.eof()) {
          file.close();
          return Try<std::map<std::string, SubsystemInfo> >::error(
              "Failed to parse /proc/cgroups");
        }
        infos[name] = SubsystemInfo(name, hierarchy, cgroups, enabled);
      }
    }
  }
  file.close();
  return infos;
}
static Try < std :: map < std :: string , SubsystemInfo > > subsystems ( ) { std :: ifstream file ( " " ) ; if ( ! file . is_open ( ) ) { return Try < std :: map < std :: string , SubsystemInfo > > :: error ( " " ) ; } std :: map < std :: string , SubsystemInfo > infos ; while ( ! file . eof ( ) ) { std :: string line ; std :: getline ( file , line ) ; if ( file . fail ( ) ) { if ( ! file . eof ( ) ) { file . close ( ) ; return Try < std :: map < std :: string , SubsystemInfo > > :: error ( " " ) ; } } else { if ( line . empty ( ) ) { continue ; } else if ( line . find_first_of ( ' ' ) == 0 ) { continue ; } else { std :: string name ; int hierarchy ; int cgroups ; bool enabled ; std :: istringstream ss ( line ) ; ss >> std :: dec >> name >> hierarchy >> cgroups >> enabled ; if ( ss . fail ( ) && ! ss . eof ( ) ) { file . close ( ) ; return Try < std :: map < std :: string , SubsystemInfo > > :: error ( " " ) ; } infos [ name ] = SubsystemInfo ( name , hierarchy , cgroups , enabled ) ; } } } file . close ( ) ; return infos ; }
Return information about subsystems on the current machine.
Return information about subsystems on the current machine .
// Skip empty lines. // Skip the first line which starts with '#' (contains titles). // Parse line to get subsystem info. // Check for any read/parse errors.