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 anything unexpected happens.
| 120 | // @return A map from subsystem names to SubsystemInfo instances if |
| 121 | // succeeds. Error if anything unexpected happens. |
| 122 | static Try<map<string, SubsystemInfo>> subsystems() |
| 123 | { |
| 124 | // TODO(benh): Use os::read to get better error information. |
| 125 | ifstream file("/proc/cgroups"); |
| 126 | |
| 127 | if (!file.is_open()) { |
| 128 | return Error("Failed to open /proc/cgroups"); |
| 129 | } |
| 130 | |
| 131 | map<string, SubsystemInfo> infos; |
| 132 | |
| 133 | while (!file.eof()) { |
| 134 | string line; |
| 135 | getline(file, line); |
| 136 | |
| 137 | if (file.fail()) { |
| 138 | if (!file.eof()) { |
| 139 | return Error("Failed to read /proc/cgroups"); |
| 140 | } |
| 141 | } else { |
| 142 | if (line.empty()) { |
| 143 | // Skip empty lines. |
| 144 | continue; |
| 145 | } else if (line.find_first_of('#') == 0) { |
| 146 | // Skip the first line which starts with '#' (contains titles). |
| 147 | continue; |
| 148 | } else { |
| 149 | // Parse line to get subsystem info. |
| 150 | string name; |
| 151 | int hierarchy; |
| 152 | int cgroups; |
| 153 | bool enabled; |
| 154 | |
| 155 | istringstream ss(line); |
| 156 | ss >> dec >> name >> hierarchy >> cgroups >> enabled; |
| 157 | |
| 158 | // Check for any read/parse errors. |
| 159 | if (ss.fail() && !ss.eof()) { |
| 160 | return Error("Failed to parse /proc/cgroups"); |
| 161 | } |
| 162 | |
| 163 | infos[name] = SubsystemInfo(name, hierarchy, cgroups, enabled); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return infos; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | // Mount a cgroups virtual file system (with proper subsystems |