Skip to content

Commit 2356c76

Browse files
Implement DisableUnitFiles
1 parent 3207a34 commit 2356c76

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

dbus/methods.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,50 @@ type EnableUnitFileChange struct {
266266
Destination string // Destination of the symlink
267267
}
268268

269+
// DisableUnitFiles() may be used to disable one or more units in the system (by
270+
// removing symlinks to them from /etc or /run).
271+
//
272+
// It takes a list of unit files to disable (either just file names or full
273+
// absolute paths if the unit files are residing outside the usual unit
274+
// search paths), and one boolean: whether the unit was enabled for runtime
275+
// only (true, /run), or persistently (false, /etc).
276+
//
277+
// This call returns an array with the changes made. The changes list
278+
// consists of structures with three strings: the type of the change (one of
279+
// symlink or unlink), the file name of the symlink and the destination of the
280+
// symlink.
281+
func (c *Conn) DisableUnitFiles(files []string, runtime bool) ([]DisableUnitFileChange, error) {
282+
result := make([][]interface{}, 0)
283+
err := c.sysobj.Call("DisableUnitFiles", 0, files, runtime).Store(&result)
284+
if err != nil {
285+
return nil, err
286+
}
287+
288+
resultInterface := make([]interface{}, len(result))
289+
for i := range result {
290+
resultInterface[i] = result[i]
291+
}
292+
293+
changes := make([]DisableUnitFileChange, len(result))
294+
changesInterface := make([]interface{}, len(changes))
295+
for i := range changes {
296+
changesInterface[i] = &changes[i]
297+
}
298+
299+
err = dbus.Store(resultInterface, changesInterface...)
300+
if err != nil {
301+
return nil, err
302+
}
303+
304+
return changes, nil
305+
}
306+
307+
type DisableUnitFileChange struct {
308+
Type string // Type of the change (one of symlink or unlink)
309+
Filename string // File name of the symlink
310+
Destination string // Destination of the symlink
311+
}
312+
269313
// Reload instructs systemd to scan for and reload unit files. This is
270314
// equivalent to a 'systemctl daemon-reload'.
271315
func (c *Conn) Reload() (string, error) {

dbus/methods_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,37 @@ func TestStartStopUnit(t *testing.T) {
121121
}
122122
}
123123

124+
// Enables a unit and then immediately tears it down
125+
func TestEnableDisableUnit(t *testing.T) {
126+
target := "enable-disable.service"
127+
conn := setupConn(t)
128+
129+
setupUnit(target, conn, t)
130+
131+
abs, err := filepath.Abs("../fixtures/" + target)
132+
if err != nil {
133+
t.Fatal(err)
134+
}
135+
136+
path := filepath.Join("/run/systemd/system/", target)
137+
138+
// 2. Disable the unit
139+
changes, err := conn.DisableUnitFiles([]string{abs}, true)
140+
if err != nil {
141+
t.Fatal(err)
142+
}
143+
144+
if len(changes) != 1 {
145+
t.Fatalf("Changes should include the path, %v", changes)
146+
}
147+
if changes[0].Filename != path {
148+
t.Fatalf("Change should include correct filename, %+v", changes[0])
149+
}
150+
if changes[0].Destination != "" {
151+
t.Fatalf("Change destination should be empty, %+v", changes[0])
152+
}
153+
}
154+
124155
// TestGetUnitProperties reads the `-.mount` which should exist on all systemd
125156
// systems and ensures that one of its properties is valid.
126157
func TestGetUnitProperties(t *testing.T) {

fixtures/enable-disable.service

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

0 commit comments

Comments
 (0)