コード例 #1
0
ファイル: dbus.go プロジェクト: Clarifai/kubernetes
// NewSystemdConnection establishes a private, direct connection to systemd.
// This can be used for communicating with systemd without a dbus daemon.
// Callers should call Close() when done with the connection.
func NewSystemdConnection() (*Conn, error) {
	return newConnection(func() (*dbus.Conn, error) {
		// We skip Hello when talking directly to systemd.
		return dbusAuthConnection(func() (*dbus.Conn, error) {
			return dbus.Dial("unix:path=/run/systemd/private")
		})
	})
}
コード例 #2
0
func ConnectToDBus(bus string) (*DBusClient, error) {
	c := new(DBusClient)

	var err error
	switch bus {
	case "session":
		c.bus, err = dbus.SessionBus()

	case "system":
		c.bus, err = dbus.SystemBus()

	default:
		c.bus, err = dbus.Dial(bus)
	}

	if err != nil {
		return nil, err
	}
	if !c.bus.SupportsUnixFDs() {
		return nil, errors.New("DBus connection does not support file descriptors")
	}

	c.path = dbus.ObjectPath("/com/firelizzard/teasvc/Client")
	err = c.bus.Export(c, c.path, "com.firelizzard.teasvc.Client")
	if err != nil {
		return nil, err
	}

	c.sigchans = make(map[string](chan *dbus.Signal))
	chsig := make(chan *dbus.Signal, 10)

	go func() {
		for {
			sig := <-chsig
			ch, ok := c.sigchans[sig.Name]
			if !ok {
				log.Print("Unhandled signal: " + sig.Name)
			}

			select {
			case ch <- sig:
				// sent singal, done

			default:
				log.Print("Unhandled signal (full channel): " + sig.Name)
			}
		}
	}()
	c.bus.Signal(chsig)
	c.bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, "type='signal',interface='com.firelizzard.teasvc',member='Pong'")

	return c, nil
}
コード例 #3
0
func ExportToDBus(proc *Process, bus string) (*DBusServer, error) {
	s := new(DBusServer)
	s.proc = proc

	var err error
	switch bus {
	case "session":
		s.bus, err = dbus.SessionBus()

	case "system":
		s.bus, err = dbus.SystemBus()

	default:
		s.bus, err = dbus.Dial(bus)
	}

	if err != nil {
		return nil, err
	}
	if !s.bus.SupportsUnixFDs() {
		return nil, errors.New("DBus connection does not support file descriptors")
	}

	s.path = dbus.ObjectPath("/com/firelizzard/teasvc/Server")
	go s.handleSignals()

	err = s.bus.Export(s, s.path, "com.firelizzard.teasvc.Server")
	if err != nil {
		return nil, err
	}

	c := s.bus.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, "type='signal',interface='com.firelizzard.teasvc',member='Ping'")
	if c.Err != nil {
		return nil, c.Err
	}
	return s, nil
}