func (c *Conn) initConnection() error { var err error c.sysconn, 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.sysconn.Auth(methods) if err != nil { c.sysconn.Close() return err } err = c.sysconn.Hello() if err != nil { c.sysconn.Close() return err } c.sysobj = c.sysconn.Object("org.freedesktop.systemd1", dbus.ObjectPath("/org/freedesktop/systemd1")) // Setup the listeners on jobs so that we can get completions c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, "type='signal', interface='org.freedesktop.systemd1.Manager', member='JobRemoved'") c.initSubscription() c.initDispatch() 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 }