示例#1
0
func (c *Conn) initConnection() error {
	var err error
	c.conn, err = dbus.SystemBusPrivate()
	if err != nil {
		return err
	}

	// Only use EXTERNAL method, and hardcode the uid (not username)
	// to avoid a username lookup (which requires a dynamically linked
	// libc)
	methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(os.Getuid()))}

	err = c.conn.Auth(methods)
	if err != nil {
		c.conn.Close()
		return err
	}

	err = c.conn.Hello()
	if err != nil {
		c.conn.Close()
		return err
	}

	c.object = c.conn.Object("org.freedesktop.login1", dbus.ObjectPath(dbusPath))

	return nil
}
func runSendNeedReboot(args []string) int {
	conn, err := dbus.SystemBusPrivate()
	if err != nil {
		fmt.Fprintln(os.Stderr, "error connecting to system dbus:", err)
		return 1
	}

	methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(os.Getuid()))}
	err = conn.Auth(methods)
	if err != nil {
		conn.Close()
		fmt.Fprintln(os.Stderr, "error authing to system dbus:", err)
		return 1
	}

	err = conn.Hello()
	if err != nil {
		fmt.Fprintln(os.Stderr, "error sending hello:", err)
		conn.Close()
		return 1
	}

	err = conn.Emit("/com/coreos/update1",
		"com.coreos.update1.Manager.StatusUpdate",
		int64(0), float64(0),
		string("UPDATE_STATUS_UPDATED_NEED_REBOOT"),
		string(""), int64(0))

	if err != nil {
		fmt.Fprintln(os.Stderr, "error emitting signal:", err)
		conn.Close()
		return 1
	}

	// TODO(philips): figure out a way to make conn.Close() block until
	// everything is flushed.
	time.Sleep(time.Second)
	conn.Close()

	return 0
}
示例#3
0
func New() (c *Client, err error) {
	c = new(Client)

	c.conn, err = dbus.SystemBusPrivate()
	if err != nil {
		return nil, err
	}

	methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(os.Getuid()))}
	err = c.conn.Auth(methods)
	if err != nil {
		c.conn.Close()
		return nil, err
	}

	err = c.conn.Hello()
	if err != nil {
		c.conn.Close()
		return nil, err
	}

	c.object = c.conn.Object("com.coreos.update1", dbus.ObjectPath(dbusPath))

	// Setup the filter for the StatusUpdate signals
	match := fmt.Sprintf("type='signal',interface='%s',member='%s'", dbusInterface, dbusMember)

	call := c.conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, match)
	if call.Err != nil {
		return nil, err
	}

	c.ch = make(chan *dbus.Signal, signalBuffer)
	c.conn.Signal(c.ch)

	return c, nil
}