Skip to content

Commit 5ac8d9c

Browse files
committed
dbus: add StartTransientUnitAux and StartTransientUnitAuxContext for starting transient units with auxiliary units
the existing StartTransientUnit methods pass an empty slice to the list of auxiliary units, this adds new methods for specifying those auxiliary units. Fixes #488
1 parent d25876d commit 5ac8d9c

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

dbus/methods.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,21 @@ func (c *Conn) StartTransientUnit(name string, mode string, properties []Propert
194194
// unique. mode is the same as in StartUnitContext, properties contains properties
195195
// of the unit.
196196
func (c *Conn) StartTransientUnitContext(ctx context.Context, name string, mode string, properties []Property, ch chan<- string) (int, error) {
197-
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartTransientUnit", name, mode, properties, make([]PropertyCollection, 0))
197+
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartTransientUnit", name, mode, properties, make([]AuxiliaryUnit, 0))
198+
}
199+
200+
// Deprecated: use StartTransientUnitContext instead.
201+
func (c *Conn) StartTransientUnitAux(name string, mode string, properties []Property, aux []AuxiliaryUnit, ch chan<- string) (int, error) {
202+
return c.StartTransientUnitAuxContext(context.Background(), name, mode, properties, aux, ch)
203+
}
204+
205+
// StartTransientUnitContext may be used to create and start a transient unit, which
206+
// will be released as soon as it is not running or referenced anymore or the
207+
// system is rebooted. name is the unit name including suffix, and must be
208+
// unique. mode is the same as in StartUnitContext, properties contains properties
209+
// of the unit.
210+
func (c *Conn) StartTransientUnitAuxContext(ctx context.Context, name string, mode string, properties []Property, aux []AuxiliaryUnit, ch chan<- string) (int, error) {
211+
return c.startJob(ctx, ch, "org.freedesktop.systemd1.Manager.StartTransientUnit", name, mode, properties, aux)
198212
}
199213

200214
// Deprecated: use [KillUnitWithTarget] instead.

dbus/methods_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ func runStartTrUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
128128
return nil
129129
}
130130

131+
func runStartTrUnitWithAux(t *testing.T, conn *Conn, trTarget TrUnitProp, trAux TrUnitProp) error {
132+
reschan := make(chan string)
133+
_, err := conn.StartTransientUnitAux(trTarget.name, "replace", trTarget.props, []AuxiliaryUnit{{Name: trAux.name, Properties: trAux.props}}, reschan)
134+
if err != nil {
135+
return err
136+
}
137+
138+
job := <-reschan
139+
if job != "done" {
140+
return fmt.Errorf("Job is not done: %s", job)
141+
}
142+
143+
return nil
144+
}
145+
131146
func runStopUnit(t *testing.T, conn *Conn, trTarget TrUnitProp) error {
132147
reschan := make(chan string)
133148
_, err := conn.StopUnit(trTarget.name, "replace", reschan)
@@ -850,6 +865,11 @@ func TestSetUnitProperties(t *testing.T) {
850865
}
851866
}
852867

868+
type TrUnitSocketListenProp struct {
869+
Type string
870+
Address string
871+
}
872+
853873
// Ensure that oneshot transient unit starting and stopping works.
854874
func TestStartStopTransientUnitAll(t *testing.T) {
855875
testCases := []struct {
@@ -991,6 +1011,29 @@ func TestStartStopTransientUnitAll(t *testing.T) {
9911011
},
9921012
checkFunc: checkTransientUnitConflicts,
9931013
},
1014+
{
1015+
trTarget: TrUnitProp{
1016+
name: "testing-aux-unit.socket",
1017+
props: []Property{
1018+
{
1019+
Name: "Listen",
1020+
Value: dbus.MakeVariant([]TrUnitSocketListenProp{
1021+
{
1022+
Type: "Stream",
1023+
Address: path.Join(os.TempDir(), "go-systemd-test.sock"),
1024+
},
1025+
}),
1026+
},
1027+
},
1028+
},
1029+
trDep: TrUnitProp{
1030+
name: "testing-aux-unit.service",
1031+
props: []Property{
1032+
PropExecStart([]string{"/bin/sleep", "400"}, false),
1033+
},
1034+
},
1035+
checkFunc: checkTransientUnitAux,
1036+
},
9941037
}
9951038

9961039
for i, tt := range testCases {
@@ -1332,6 +1375,47 @@ func checkTransientUnitConflicts(t *testing.T, trTarget TrUnitProp, trDep TrUnit
13321375
return nil
13331376
}
13341377

1378+
func checkTransientUnitAux(t *testing.T, trTarget TrUnitProp, trDep TrUnitProp) error {
1379+
conn := setupConn(t)
1380+
1381+
// Start the target unit with dep as auxiliary unit
1382+
err := runStartTrUnitWithAux(t, conn, trTarget, trDep)
1383+
if err != nil {
1384+
return err
1385+
}
1386+
1387+
target := getUnitStatusSingle(conn, trTarget.name)
1388+
if target == nil || target.ActiveState != "active" {
1389+
return fmt.Errorf("Test unit %s not active, should be active", trTarget.name)
1390+
}
1391+
1392+
aux := getUnitStatusSingle(conn, trDep.name)
1393+
if aux == nil {
1394+
return fmt.Errorf("Test unit %s not found", trDep.name)
1395+
}
1396+
1397+
if aux.ActiveState == "active" {
1398+
return fmt.Errorf("Test unit %s active, should be inactive", trDep.name)
1399+
}
1400+
1401+
err = runStopUnit(t, conn, trTarget)
1402+
if err != nil {
1403+
return err
1404+
}
1405+
1406+
target = getUnitStatusSingle(conn, trTarget.name)
1407+
if target != nil {
1408+
return fmt.Errorf("Test unit %s found in list, should be stopped", trTarget.name)
1409+
}
1410+
1411+
aux = getUnitStatusSingle(conn, trDep.name)
1412+
if aux != nil {
1413+
return fmt.Errorf("Test unit %s found in list, should be stopped", trTarget.name)
1414+
}
1415+
1416+
return nil
1417+
}
1418+
13351419
func runSleep(t *testing.T) uint32 {
13361420
cmd := exec.Command("/bin/sleep", "400")
13371421
err := cmd.Start()

dbus/properties.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Property struct {
3939
Value dbus.Variant
4040
}
4141

42-
type PropertyCollection struct {
42+
type AuxiliaryUnit struct {
4343
Name string
4444
Properties []Property
4545
}

0 commit comments

Comments
 (0)