apply_raw.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // +build linux
  2. package fs
  3. import (
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "sync"
  12. "github.com/opencontainers/runc/libcontainer/cgroups"
  13. "github.com/opencontainers/runc/libcontainer/configs"
  14. libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
  15. )
  16. var (
  17. subsystems = subsystemSet{
  18. &CpusetGroup{},
  19. &DevicesGroup{},
  20. &MemoryGroup{},
  21. &CpuGroup{},
  22. &CpuacctGroup{},
  23. &PidsGroup{},
  24. &BlkioGroup{},
  25. &HugetlbGroup{},
  26. &NetClsGroup{},
  27. &NetPrioGroup{},
  28. &PerfEventGroup{},
  29. &FreezerGroup{},
  30. &NameGroup{GroupName: "name=systemd", Join: true},
  31. }
  32. CgroupProcesses = "cgroup.procs"
  33. HugePageSizes, _ = cgroups.GetHugePageSize()
  34. )
  35. var errSubsystemDoesNotExist = errors.New("cgroup: subsystem does not exist")
  36. type subsystemSet []subsystem
  37. func (s subsystemSet) Get(name string) (subsystem, error) {
  38. for _, ss := range s {
  39. if ss.Name() == name {
  40. return ss, nil
  41. }
  42. }
  43. return nil, errSubsystemDoesNotExist
  44. }
  45. type subsystem interface {
  46. // Name returns the name of the subsystem.
  47. Name() string
  48. // Returns the stats, as 'stats', corresponding to the cgroup under 'path'.
  49. GetStats(path string, stats *cgroups.Stats) error
  50. // Removes the cgroup represented by 'cgroupData'.
  51. Remove(*cgroupData) error
  52. // Creates and joins the cgroup represented by 'cgroupData'.
  53. Apply(*cgroupData) error
  54. // Set the cgroup represented by cgroup.
  55. Set(path string, cgroup *configs.Cgroup) error
  56. }
  57. type Manager struct {
  58. mu sync.Mutex
  59. Cgroups *configs.Cgroup
  60. Paths map[string]string
  61. }
  62. // The absolute path to the root of the cgroup hierarchies.
  63. var cgroupRootLock sync.Mutex
  64. var cgroupRoot string
  65. // Gets the cgroupRoot.
  66. func getCgroupRoot() (string, error) {
  67. cgroupRootLock.Lock()
  68. defer cgroupRootLock.Unlock()
  69. if cgroupRoot != "" {
  70. return cgroupRoot, nil
  71. }
  72. root, err := cgroups.FindCgroupMountpointDir()
  73. if err != nil {
  74. return "", err
  75. }
  76. if _, err := os.Stat(root); err != nil {
  77. return "", err
  78. }
  79. cgroupRoot = root
  80. return cgroupRoot, nil
  81. }
  82. type cgroupData struct {
  83. root string
  84. innerPath string
  85. config *configs.Cgroup
  86. pid int
  87. }
  88. func (m *Manager) Apply(pid int) (err error) {
  89. if m.Cgroups == nil {
  90. return nil
  91. }
  92. var c = m.Cgroups
  93. d, err := getCgroupData(m.Cgroups, pid)
  94. if err != nil {
  95. return err
  96. }
  97. if c.Paths != nil {
  98. paths := make(map[string]string)
  99. for name, path := range c.Paths {
  100. _, err := d.path(name)
  101. if err != nil {
  102. if cgroups.IsNotFound(err) {
  103. continue
  104. }
  105. return err
  106. }
  107. paths[name] = path
  108. }
  109. m.Paths = paths
  110. return cgroups.EnterPid(m.Paths, pid)
  111. }
  112. m.mu.Lock()
  113. defer m.mu.Unlock()
  114. paths := make(map[string]string)
  115. for _, sys := range subsystems {
  116. if err := sys.Apply(d); err != nil {
  117. return err
  118. }
  119. // TODO: Apply should, ideally, be reentrant or be broken up into a separate
  120. // create and join phase so that the cgroup hierarchy for a container can be
  121. // created then join consists of writing the process pids to cgroup.procs
  122. p, err := d.path(sys.Name())
  123. if err != nil {
  124. if cgroups.IsNotFound(err) {
  125. continue
  126. }
  127. return err
  128. }
  129. paths[sys.Name()] = p
  130. }
  131. m.Paths = paths
  132. return nil
  133. }
  134. func (m *Manager) Destroy() error {
  135. if m.Cgroups.Paths != nil {
  136. return nil
  137. }
  138. m.mu.Lock()
  139. defer m.mu.Unlock()
  140. if err := cgroups.RemovePaths(m.Paths); err != nil {
  141. return err
  142. }
  143. m.Paths = make(map[string]string)
  144. return nil
  145. }
  146. func (m *Manager) GetPaths() map[string]string {
  147. m.mu.Lock()
  148. paths := m.Paths
  149. m.mu.Unlock()
  150. return paths
  151. }
  152. func (m *Manager) GetStats() (*cgroups.Stats, error) {
  153. m.mu.Lock()
  154. defer m.mu.Unlock()
  155. stats := cgroups.NewStats()
  156. for name, path := range m.Paths {
  157. sys, err := subsystems.Get(name)
  158. if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
  159. continue
  160. }
  161. if err := sys.GetStats(path, stats); err != nil {
  162. return nil, err
  163. }
  164. }
  165. return stats, nil
  166. }
  167. func (m *Manager) Set(container *configs.Config) error {
  168. for _, sys := range subsystems {
  169. // Generate fake cgroup data.
  170. d, err := getCgroupData(container.Cgroups, -1)
  171. if err != nil {
  172. return err
  173. }
  174. // Get the path, but don't error out if the cgroup wasn't found.
  175. path, err := d.path(sys.Name())
  176. if err != nil && !cgroups.IsNotFound(err) {
  177. return err
  178. }
  179. if err := sys.Set(path, container.Cgroups); err != nil {
  180. return err
  181. }
  182. }
  183. if m.Paths["cpu"] != "" {
  184. if err := CheckCpushares(m.Paths["cpu"], container.Cgroups.Resources.CpuShares); err != nil {
  185. return err
  186. }
  187. }
  188. return nil
  189. }
  190. // Freeze toggles the container's freezer cgroup depending on the state
  191. // provided
  192. func (m *Manager) Freeze(state configs.FreezerState) error {
  193. d, err := getCgroupData(m.Cgroups, 0)
  194. if err != nil {
  195. return err
  196. }
  197. dir, err := d.path("freezer")
  198. if err != nil {
  199. return err
  200. }
  201. prevState := m.Cgroups.Resources.Freezer
  202. m.Cgroups.Resources.Freezer = state
  203. freezer, err := subsystems.Get("freezer")
  204. if err != nil {
  205. return err
  206. }
  207. err = freezer.Set(dir, m.Cgroups)
  208. if err != nil {
  209. m.Cgroups.Resources.Freezer = prevState
  210. return err
  211. }
  212. return nil
  213. }
  214. func (m *Manager) GetPids() ([]int, error) {
  215. dir, err := getCgroupPath(m.Cgroups)
  216. if err != nil {
  217. return nil, err
  218. }
  219. return cgroups.GetPids(dir)
  220. }
  221. func (m *Manager) GetAllPids() ([]int, error) {
  222. dir, err := getCgroupPath(m.Cgroups)
  223. if err != nil {
  224. return nil, err
  225. }
  226. return cgroups.GetAllPids(dir)
  227. }
  228. func getCgroupPath(c *configs.Cgroup) (string, error) {
  229. d, err := getCgroupData(c, 0)
  230. if err != nil {
  231. return "", err
  232. }
  233. return d.path("devices")
  234. }
  235. func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {
  236. root, err := getCgroupRoot()
  237. if err != nil {
  238. return nil, err
  239. }
  240. if (c.Name != "" || c.Parent != "") && c.Path != "" {
  241. return nil, fmt.Errorf("cgroup: either Path or Name and Parent should be used")
  242. }
  243. // XXX: Do not remove this code. Path safety is important! -- cyphar
  244. cgPath := libcontainerUtils.CleanPath(c.Path)
  245. cgParent := libcontainerUtils.CleanPath(c.Parent)
  246. cgName := libcontainerUtils.CleanPath(c.Name)
  247. innerPath := cgPath
  248. if innerPath == "" {
  249. innerPath = filepath.Join(cgParent, cgName)
  250. }
  251. return &cgroupData{
  252. root: root,
  253. innerPath: innerPath,
  254. config: c,
  255. pid: pid,
  256. }, nil
  257. }
  258. func (raw *cgroupData) parentPath(subsystem, mountpoint, root string) (string, error) {
  259. // Use GetThisCgroupDir instead of GetInitCgroupDir, because the creating
  260. // process could in container and shared pid namespace with host, and
  261. // /proc/1/cgroup could point to whole other world of cgroups.
  262. initPath, err := cgroups.GetThisCgroupDir(subsystem)
  263. if err != nil {
  264. return "", err
  265. }
  266. // This is needed for nested containers, because in /proc/self/cgroup we
  267. // see pathes from host, which don't exist in container.
  268. relDir, err := filepath.Rel(root, initPath)
  269. if err != nil {
  270. return "", err
  271. }
  272. return filepath.Join(mountpoint, relDir), nil
  273. }
  274. func (raw *cgroupData) path(subsystem string) (string, error) {
  275. mnt, root, err := cgroups.FindCgroupMountpointAndRoot(subsystem)
  276. // If we didn't mount the subsystem, there is no point we make the path.
  277. if err != nil {
  278. return "", err
  279. }
  280. // If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
  281. if filepath.IsAbs(raw.innerPath) {
  282. // Sometimes subsystems can be mounted togethger as 'cpu,cpuacct'.
  283. return filepath.Join(raw.root, filepath.Base(mnt), raw.innerPath), nil
  284. }
  285. parentPath, err := raw.parentPath(subsystem, mnt, root)
  286. if err != nil {
  287. return "", err
  288. }
  289. return filepath.Join(parentPath, raw.innerPath), nil
  290. }
  291. func (raw *cgroupData) join(subsystem string) (string, error) {
  292. path, err := raw.path(subsystem)
  293. if err != nil {
  294. return "", err
  295. }
  296. if err := os.MkdirAll(path, 0755); err != nil {
  297. return "", err
  298. }
  299. if err := writeFile(path, CgroupProcesses, strconv.Itoa(raw.pid)); err != nil {
  300. return "", err
  301. }
  302. return path, nil
  303. }
  304. func writeFile(dir, file, data string) error {
  305. // Normally dir should not be empty, one case is that cgroup subsystem
  306. // is not mounted, we will get empty dir, and we want it fail here.
  307. if dir == "" {
  308. return fmt.Errorf("no such directory for %s.", file)
  309. }
  310. if err := ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700); err != nil {
  311. return fmt.Errorf("failed to write %v to %v: %v", data, file, err)
  312. }
  313. return nil
  314. }
  315. func readFile(dir, file string) (string, error) {
  316. data, err := ioutil.ReadFile(filepath.Join(dir, file))
  317. return string(data), err
  318. }
  319. func removePath(p string, err error) error {
  320. if err != nil {
  321. return err
  322. }
  323. if p != "" {
  324. return os.RemoveAll(p)
  325. }
  326. return nil
  327. }
  328. func CheckCpushares(path string, c int64) error {
  329. var cpuShares int64
  330. if c == 0 {
  331. return nil
  332. }
  333. fd, err := os.Open(filepath.Join(path, "cpu.shares"))
  334. if err != nil {
  335. return err
  336. }
  337. defer fd.Close()
  338. _, err = fmt.Fscanf(fd, "%d", &cpuShares)
  339. if err != nil && err != io.EOF {
  340. return err
  341. }
  342. if c > cpuShares {
  343. return fmt.Errorf("The maximum allowed cpu-shares is %d", cpuShares)
  344. } else if c < cpuShares {
  345. return fmt.Errorf("The minimum allowed cpu-shares is %d", cpuShares)
  346. }
  347. return nil
  348. }