File tree Expand file tree Collapse file tree 6 files changed +92
-0
lines changed
Expand file tree Collapse file tree 6 files changed +92
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,11 @@ type Manager interface {
2929 // can be used to merely create a cgroup.
3030 Apply (pid int ) error
3131
32+ // AddPid adds a process with a given pid to an existing cgroup.
33+ // The subcgroup argument is either empty, or a path relative to
34+ // a cgroup under under the manager's cgroup.
35+ AddPid (subcgroup string , pid int ) error
36+
3237 // GetPids returns the PIDs of all processes inside the cgroup.
3338 GetPids () ([]int , error )
3439
Original file line number Diff line number Diff line change 44 "errors"
55 "fmt"
66 "os"
7+ "path"
8+ "strings"
79 "sync"
810
911 "golang.org/x/sys/unix"
@@ -139,6 +141,33 @@ func (m *Manager) Apply(pid int) (retErr error) {
139141 return retErr
140142}
141143
144+ // AddPid adds a process with a given pid to an existing cgroup.
145+ // The subcgroup argument is either empty, or a path relative to
146+ // a cgroup under under the manager's cgroup.
147+ func (m * Manager ) AddPid (subcgroup string , pid int ) (retErr error ) {
148+ m .mu .Lock ()
149+ defer m .mu .Unlock ()
150+
151+ c := m .cgroups
152+
153+ for _ , dir := range m .paths {
154+ path := path .Join (dir , subcgroup )
155+ if ! strings .HasPrefix (path , dir ) {
156+ return fmt .Errorf ("bad sub cgroup path: %s" , subcgroup )
157+ }
158+
159+ if err := cgroups .WriteCgroupProc (path , pid ); err != nil {
160+ if isIgnorableError (c .Rootless , err ) && c .Path == "" {
161+ retErr = cgroups .ErrRootless
162+ continue
163+ }
164+ return err
165+ }
166+ }
167+
168+ return retErr
169+ }
170+
142171func (m * Manager ) Destroy () error {
143172 m .mu .Lock ()
144173 defer m .mu .Unlock ()
Original file line number Diff line number Diff line change 44 "errors"
55 "fmt"
66 "os"
7+ "path/filepath"
78 "strings"
89
910 "github.com/opencontainers/cgroups"
@@ -83,6 +84,18 @@ func (m *Manager) Apply(pid int) error {
8384 return nil
8485}
8586
87+ // AddPid adds a process with a given pid to an existing cgroup.
88+ // The subcgroup argument is either empty, or a path relative to
89+ // a cgroup under under the manager's cgroup.
90+ func (m * Manager ) AddPid (subcgroup string , pid int ) error {
91+ path := filepath .Join (m .dirPath , subcgroup )
92+ if ! strings .HasPrefix (path , m .dirPath ) {
93+ return fmt .Errorf ("bad sub cgroup path: %s" , subcgroup )
94+ }
95+
96+ return cgroups .WriteCgroupProc (path , pid )
97+ }
98+
8699func (m * Manager ) GetPids () ([]int , error ) {
87100 return cgroups .GetPids (m .dirPath )
88101}
Original file line number Diff line number Diff line change 66 "fmt"
77 "math"
88 "os"
9+ "path"
910 "strconv"
1011 "strings"
1112 "sync"
@@ -208,6 +209,20 @@ func stopUnit(cm *dbusConnManager, unitName string) error {
208209 return nil
209210}
210211
212+ func addPid (cm * dbusConnManager , unitName , subcgroup string , pid int ) error {
213+ absSubcgroup := subcgroup
214+ if ! path .IsAbs (absSubcgroup ) {
215+ absSubcgroup = "/" + subcgroup
216+ }
217+ if absSubcgroup != path .Clean (absSubcgroup ) {
218+ return fmt .Errorf ("bad sub cgroup path: %s" , subcgroup )
219+ }
220+
221+ return cm .retryOnDisconnect (func (c * systemdDbus.Conn ) error {
222+ return c .AttachProcessesToUnit (context .TODO (), unitName , absSubcgroup , []uint32 {uint32 (pid )})
223+ })
224+ }
225+
211226func resetFailedUnit (cm * dbusConnManager , name string ) error {
212227 return cm .retryOnDisconnect (func (c * systemdDbus.Conn ) error {
213228 return c .ResetFailedUnitContext (context .TODO (), name )
Original file line number Diff line number Diff line change @@ -215,6 +215,26 @@ func (m *LegacyManager) Apply(pid int) error {
215215 return nil
216216}
217217
218+ // AddPid adds a process with a given pid to an existing cgroup.
219+ // The subcgroup argument is either empty, or a path relative to
220+ // a cgroup under under the manager's cgroup.
221+ func (m * LegacyManager ) AddPid (subcgroup string , pid int ) error {
222+ m .mu .Lock ()
223+ defer m .mu .Unlock ()
224+
225+ if err := addPid (m .dbus , getUnitName (m .cgroups ), subcgroup , pid ); err != nil {
226+ return err
227+ }
228+ // systemd only joins controllers it knows; use cgroupfs for the rest.
229+ fsMgr , err := fs .NewManager (m .cgroups , m .paths )
230+ if err != nil {
231+ return err
232+ }
233+ fsMgr .AddPid (subcgroup , pid )
234+
235+ return nil
236+ }
237+
218238func (m * LegacyManager ) Destroy () error {
219239 m .mu .Lock ()
220240 defer m .mu .Unlock ()
Original file line number Diff line number Diff line change @@ -383,6 +383,16 @@ func cgroupFilesToChown() ([]string, error) {
383383 return filesToChown , nil
384384}
385385
386+ // AddPid adds a process with a given pid to an existing cgroup.
387+ // The subcgroup argument is either empty, or a path relative to
388+ // a cgroup under under the manager's cgroup.
389+ func (m * UnifiedManager ) AddPid (subcgroup string , pid int ) error {
390+ m .mu .Lock ()
391+ defer m .mu .Unlock ()
392+
393+ return addPid (m .dbus , getUnitName (m .cgroups ), subcgroup , pid )
394+ }
395+
386396func (m * UnifiedManager ) Destroy () error {
387397 m .mu .Lock ()
388398 defer m .mu .Unlock ()
You can’t perform that action at this time.
0 commit comments