| 73 | } |
| 74 | |
| 75 | func initCgroup(cfg *cgroupConfig) error { |
| 76 | |
| 77 | logger.Debugf("Initializing cgroup") |
| 78 | baseGroup := cfg.Group |
| 79 | //subsystem := cfg.Subsystem |
| 80 | |
| 81 | // If baseGroup is empty, it implies using the cgroup of the current process |
| 82 | // otherwise, it refers to a absolute group path |
| 83 | if baseGroup != "" { |
| 84 | baseGroup = filepath.Join("/", baseGroup) |
| 85 | } |
| 86 | |
| 87 | cfg.isUnified = cgroups.Mode() == cgroups.Unified |
| 88 | |
| 89 | if cfg.isUnified { |
| 90 | logger.Debugf("Cgroup V2 detected") |
| 91 | g := baseGroup |
| 92 | if g == "" { |
| 93 | logger.Debugf("Detecting my cgroup path") |
| 94 | var err error |
| 95 | if g, err = cgv2.NestedGroupPath(""); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | } |
| 99 | logger.Infof("Using cgroup path: %s", g) |
| 100 | |
| 101 | var err error |
| 102 | if cfg.cgMgrV2, err = cgv2.Load(g); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | if baseGroup == "" { |
| 106 | logger.Debugf("Creating a sub group and move all processes into it") |
| 107 | wkrMgr, err := cfg.cgMgrV2.NewChild("__worker", nil) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | for { |
| 112 | logger.Debugf("Reading pids") |
| 113 | procs, err := cfg.cgMgrV2.Procs(false) |
| 114 | if err != nil { |
| 115 | logger.Errorf("Cannot read pids in that group") |
| 116 | return err |
| 117 | } |
| 118 | if len(procs) == 0 { |
| 119 | break |
| 120 | } |
| 121 | for _, p := range procs { |
| 122 | if err := wkrMgr.AddProc(p); err != nil { |
| 123 | if errors.Is(err, syscall.ESRCH) { |
| 124 | logger.Debugf("Write pid %d to sub group failed: process vanished, ignoring", p) |
| 125 | } else { |
| 126 | return err |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } else { |
| 132 | logger.Debugf("Trying to create a sub group in that group") |