Helper for finding the cgroup of the specified pid for the specified subsystem.
| 1783 | // Helper for finding the cgroup of the specified pid for the |
| 1784 | // specified subsystem. |
| 1785 | Result<string> cgroup(pid_t pid, const string& subsystem) |
| 1786 | { |
| 1787 | // Determine cgroup for hierarchy with the subsystem attached. |
| 1788 | string path = path::join("/proc", stringify(pid), "cgroup"); |
| 1789 | |
| 1790 | Try<string> read = os::read(path); |
| 1791 | |
| 1792 | if (read.isError()) { |
| 1793 | return Error("Failed to read " + path + ": " + read.error()); |
| 1794 | } |
| 1795 | |
| 1796 | // Now determine the cgroup by parsing each line of the output which |
| 1797 | // should be of the form "N:subsystems:cgroup" where 'N' is the |
| 1798 | // hierarchy number and 'subsystems' are the attached subsystems and |
| 1799 | // 'cgroup' is the relative path to the cgroup from the hierarchy |
| 1800 | // path. |
| 1801 | Option<string> cgroup = None(); |
| 1802 | |
| 1803 | foreach (const string& line, strings::tokenize(read.get(), "\n")) { |
| 1804 | vector<string> tokens = strings::tokenize(line, ":"); |
| 1805 | |
| 1806 | // The second field is empty for cgroups v2 hierarchy. |
| 1807 | if (tokens.size() == 2) { |
| 1808 | continue; |
| 1809 | } else if (tokens.size() != 3) { |
| 1810 | return Error("Unexpected format in " + path); |
| 1811 | } |
| 1812 | |
| 1813 | foreach (const string& token, strings::tokenize(tokens[1], ",")) { |
| 1814 | if (subsystem == token) { |
| 1815 | cgroup = tokens[2]; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | return cgroup; |
| 1821 | } |
| 1822 | |
| 1823 | } // namespace internal { |
| 1824 |