Skip to content

Commit f239902

Browse files
author
Luca Bruno
authored
Merge pull request #373 from msekletar/freezer
dbus: implement support for cgroup freezer APIs
2 parents 9a9fd21 + 8622d1b commit f239902

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

dbus/methods.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,14 @@ func (c *Conn) listJobsInternal(ctx context.Context) ([]JobStatus, error) {
828828

829829
return status, nil
830830
}
831+
832+
// Freeze the cgroup associated with the unit.
833+
// Note that FreezeUnit and ThawUnit are only supported on systems running with cgroup v2.
834+
func (c *Conn) FreezeUnit(ctx context.Context, unit string) error {
835+
return c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.FreezeUnit", 0, unit).Store()
836+
}
837+
838+
// Unfreeze the cgroup associated with the unit.
839+
func (c *Conn) ThawUnit(ctx context.Context, unit string) error {
840+
return c.sysobj.CallWithContext(ctx, "org.freedesktop.systemd1.Manager.ThawUnit", 0, unit).Store()
841+
}

dbus/methods_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package dbus
1616

1717
import (
18+
"context"
1819
"fmt"
1920
"os"
2021
"os/exec"
@@ -1600,3 +1601,59 @@ func TestUnitName(t *testing.T) {
16001601
}
16011602
}
16021603
}
1604+
1605+
func TestFreezer(t *testing.T) {
1606+
target := "freeze.service"
1607+
conn := setupConn(t)
1608+
defer conn.Close()
1609+
1610+
setupUnit(target, conn, t)
1611+
linkUnit(target, conn, t)
1612+
1613+
reschan := make(chan string)
1614+
_, err := conn.StartUnit(target, "replace", reschan)
1615+
if err != nil {
1616+
t.Fatal(err)
1617+
}
1618+
1619+
job := <-reschan
1620+
if job != "done" {
1621+
t.Fatal("Job is not done:", job)
1622+
}
1623+
1624+
if err := conn.FreezeUnit(context.Background(), target); err != nil {
1625+
// Don't fail the test if freezing units is not implemented at all (on older systemd versions) or
1626+
// not supported (on systems running with cgroup v1).
1627+
e, ok := err.(dbus.Error)
1628+
if ok && (e.Name == "org.freedesktop.DBus.Error.UnknownMethod" || e.Name == "org.freedesktop.DBus.Error.NotSupported") {
1629+
t.SkipNow()
1630+
}
1631+
t.Fatalf("failed to freeze unit %s: %s", target, err)
1632+
}
1633+
1634+
p, err := conn.GetUnitProperty(target, "FreezerState")
1635+
if err != nil {
1636+
t.Fatal(err)
1637+
}
1638+
1639+
v := p.Value.Value().(string)
1640+
if v != "frozen" {
1641+
t.Fatalf("unit is not frozen after calling FreezeUnit(), FreezerState=%s", v)
1642+
}
1643+
1644+
if err := conn.ThawUnit(context.Background(), target); err != nil {
1645+
t.Fatalf("failed to thaw unit %s: %s", target, err)
1646+
}
1647+
1648+
p, err = conn.GetUnitProperty(target, "FreezerState")
1649+
if err != nil {
1650+
t.Fatal(err)
1651+
}
1652+
1653+
v = p.Value.Value().(string)
1654+
if v != "running" {
1655+
t.Fatalf("unit is not frozen after calling ThawUnit(), FreezerState=%s", v)
1656+
}
1657+
1658+
runStopUnit(t, conn, TrUnitProp{target, nil})
1659+
}

fixtures/freeze.service

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Unit]
2+
Description=freeze unit test
3+
4+
[Service]
5+
ExecStart=/bin/sleep 400

0 commit comments

Comments
 (0)