Skip to content

Commit c1b983a

Browse files
committed
Implement AddPid method
This will allow runtimes like runc to ask systemd to move the process into a proper unit, instead of using cgroupfs directly. Implementation for systemd requires [1] and systemd >= v238. [1]: coreos/go-systemd#458 Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 02c98c2 commit c1b983a

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

cgroups.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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 container's main 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

fs/fs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path"
8+
"strings"
79
"sync"
810

911
"golang.org/x/sys/unix"
@@ -139,6 +141,30 @@ func (m *Manager) Apply(pid int) (retErr error) {
139141
return retErr
140142
}
141143

144+
func (m *Manager) AddPid(subcgroup string, pid int) (retErr error) {
145+
m.mu.Lock()
146+
defer m.mu.Unlock()
147+
148+
c := m.cgroups
149+
150+
for _, dir := range m.paths {
151+
path := path.Join(dir, subcgroup)
152+
if !strings.HasPrefix(path, dir) {
153+
return fmt.Errorf("%s is not a sub cgroup path", subcgroup)
154+
}
155+
156+
if err := cgroups.WriteCgroupProc(path, pid); err != nil {
157+
if isIgnorableError(c.Rootless, err) && c.Path == "" {
158+
retErr = cgroups.ErrRootless
159+
continue
160+
}
161+
return err
162+
}
163+
}
164+
165+
return retErr
166+
}
167+
142168
func (m *Manager) Destroy() error {
143169
m.mu.Lock()
144170
defer m.mu.Unlock()

fs2/fs2.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"strings"
89

910
"github.com/opencontainers/cgroups"
@@ -83,6 +84,15 @@ func (m *Manager) Apply(pid int) error {
8384
return nil
8485
}
8586

87+
func (m *Manager) AddPid(subcgroup string, pid int) error {
88+
path := filepath.Join(m.dirPath, subcgroup)
89+
if !strings.HasPrefix(path, m.dirPath) {
90+
return fmt.Errorf("%s is not a sub cgroup path", subcgroup)
91+
}
92+
93+
return cgroups.WriteCgroupProc(path, pid)
94+
}
95+
8696
func (m *Manager) GetPids() ([]int, error) {
8797
return cgroups.GetPids(m.dirPath)
8898
}

systemd/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,12 @@ func stopUnit(cm *dbusConnManager, unitName string) error {
208208
return nil
209209
}
210210

211+
func addPid(cm *dbusConnManager, unitName, subcgroup string, pid int) error {
212+
return cm.retryOnDisconnect(func(c *systemdDbus.Conn) error {
213+
return c.AttachProcessesToUnit(context.TODO(), unitName, subcgroup, []uint32{uint32(pid)})
214+
})
215+
}
216+
211217
func resetFailedUnit(cm *dbusConnManager, name string) error {
212218
return cm.retryOnDisconnect(func(c *systemdDbus.Conn) error {
213219
return c.ResetFailedUnitContext(context.TODO(), name)

systemd/v1.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ func (m *LegacyManager) Apply(pid int) error {
215215
return nil
216216
}
217217

218+
func (m *LegacyManager) AddPid(subcgroup string, pid int) error {
219+
m.mu.Lock()
220+
defer m.mu.Unlock()
221+
222+
if err := addPid(m.dbus, getUnitName(m.cgroups), subcgroup, pid); err != nil {
223+
return err
224+
}
225+
// systemd only joins controllers it knows; use cgroupfs for the rest.
226+
fsMgr, err := fs.NewManager(m.cgroups, m.paths)
227+
if err != nil {
228+
return err
229+
}
230+
fsMgr.AddPid(subcgroup, pid)
231+
232+
return nil
233+
}
234+
218235
func (m *LegacyManager) Destroy() error {
219236
m.mu.Lock()
220237
defer m.mu.Unlock()

systemd/v2.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ func cgroupFilesToChown() ([]string, error) {
383383
return filesToChown, nil
384384
}
385385

386+
func (m *UnifiedManager) AddPid(subcgroup string, pid int) error {
387+
m.mu.Lock()
388+
defer m.mu.Unlock()
389+
390+
return addPid(m.dbus, getUnitName(m.cgroups), subcgroup, pid)
391+
}
392+
386393
func (m *UnifiedManager) Destroy() error {
387394
m.mu.Lock()
388395
defer m.mu.Unlock()

0 commit comments

Comments
 (0)