// WaitOnDevices waits for the devices named in devs to be plugged before returning. func WaitOnDevices(devs []string, stage string) error { conn, err := dbus.NewSystemdConnection() if err != nil { return err } devUnits := []string{} for _, d := range devs { devUnits = append(devUnits, unit.UnitNamePathEscape(d)+".device") } unitName := unit.UnitNameEscape(fmt.Sprintf("ignition_%s.service", stage)) props := []dbus.Property{ { Name: "DefaultDependencies", Value: godbus.MakeVariant(false), }, dbus.PropExecStart([]string{"/bin/true"}, false), // XXX(vc): we apparently are required to ExecStart _something_ dbus.PropAfter(devUnits...), dbus.PropRequires(devUnits...), } res := make(chan string) if _, err = conn.StartTransientUnit(unitName, "replace", props, res); err != nil { return fmt.Errorf("failed creating transient unit %s: %v", unitName, err) } s := <-res if s != "done" { return fmt.Errorf("transient unit %s %s", unitName, s) } return nil }
// GetAll implements org.freedesktop.DBus.Properties.GetAll. func (p *Properties) GetAll(iface string) (map[string]dbus.Variant, *dbus.Error) { p.mut.RLock() defer p.mut.RUnlock() m, ok := p.m[iface] if !ok { return nil, ErrIfaceNotFound } rm := make(map[string]dbus.Variant, len(m)) for k, v := range m { rm[k] = dbus.MakeVariant(v.Value) } return rm, nil }
// Get implements org.freedesktop.DBus.Properties.Get. func (p *Properties) Get(iface, property string) (dbus.Variant, *dbus.Error) { p.mut.RLock() defer p.mut.RUnlock() m, ok := p.m[iface] if !ok { return dbus.Variant{}, ErrIfaceNotFound } prop, ok := m[property] if !ok { return dbus.Variant{}, ErrPropNotFound } return dbus.MakeVariant(prop.Value), nil }
// PropExecStart sets the ExecStart service property. The first argument is a // slice with the binary path to execute followed by the arguments to pass to // the executed command. See // http://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart= func PropExecStart(command []string, uncleanIsFailure bool) Property { execStarts := []execStart{ execStart{ Path: command[0], Args: command, UncleanIsFailure: uncleanIsFailure, }, } return Property{ Name: "ExecStart", Value: dbus.MakeVariant(execStarts), } }
// set sets the given property and emits PropertyChanged if appropiate. p.mut // must already be locked. func (p *Properties) set(iface, property string, v interface{}) { prop := p.m[iface][property] prop.Value = v switch prop.Emit { case EmitFalse: // do nothing case EmitInvalidates: p.conn.Emit(p.path, "org.freedesktop.DBus.Properties.PropertiesChanged", iface, map[string]dbus.Variant{}, []string{property}) case EmitTrue: p.conn.Emit(p.path, "org.freedesktop.DBus.Properties.PropertiesChanged", iface, map[string]dbus.Variant{property: dbus.MakeVariant(v)}, []string{}) default: panic("invalid value for EmitType") } }
// TestSetUnitProperties changes a cgroup setting on the `tmp.mount` // which should exist on all systemd systems and ensures that the // property was set. func TestSetUnitProperties(t *testing.T) { conn := setupConn(t) unit := "tmp.mount" if err := conn.SetUnitProperties(unit, true, Property{"CPUShares", dbus.MakeVariant(uint64(1023))}); err != nil { t.Fatal(err) } info, err := conn.GetUnitTypeProperties(unit, "Mount") if err != nil { t.Fatal(err) } value := info["CPUShares"].(uint64) if value != 1023 { t.Fatal("CPUShares of unit is not 1023:", value) } }
func propDependency(name string, units []string) Property { return Property{ Name: name, Value: dbus.MakeVariant(units), } }
// PropDescription sets the Description unit property. See // http://www.freedesktop.org/software/systemd/man/systemd.unit#Description= func PropDescription(desc string) Property { return Property{ Name: "Description", Value: dbus.MakeVariant(desc), } }
// PropRemainAfterExit sets the RemainAfterExit service property. See // http://www.freedesktop.org/software/systemd/man/systemd.service.html#RemainAfterExit= func PropRemainAfterExit(b bool) Property { return Property{ Name: "RemainAfterExit", Value: dbus.MakeVariant(b), } }
// PropSlice sets the Slice unit property. See // http://www.freedesktop.org/software/systemd/man/systemd.resource-control.html#Slice= func PropSlice(slice string) Property { return Property{ Name: "Slice", Value: dbus.MakeVariant(slice), } }