Skip to content

Instantly share code, notes, and snippets.

@89luca89
Created May 5, 2022 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 89luca89/bd78f5e716a7bd9206f802756e6c10de to your computer and use it in GitHub Desktop.
Save 89luca89/bd78f5e716a7bd9206f802756e6c10de to your computer and use it in GitHub Desktop.
Example notification daemon for rebootmgr notify strategy
package main
import (
"io"
"log"
"net"
"os"
"reflect"
"strconv"
"github.com/godbus/dbus/v5"
)
var sockAddr string = "/run/user/" +
strconv.Itoa(os.Geteuid()) +
"/rebootnotification.socket"
func notifySend() {
conn, err := dbus.ConnectSessionBus()
if err != nil {
panic(err)
}
defer conn.Close()
obj := conn.Object(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
)
call := obj.Call(
"org.freedesktop.Notifications.Notify",
0,
"",
uint32(0),
"",
"Test",
"This is a test of the DBus bindings for go.",
[]string{},
map[string]dbus.Variant{},
int32(5000),
)
if call.Err != nil {
panic(call.Err)
}
}
func handleMessage(c net.Conn) {
log.Printf("Client connected [%s]", c.RemoteAddr().Network())
inputBuffer := make([]byte, 1024)
data, err := c.Read(inputBuffer)
if err != nil {
panic("Receiving error")
}
if reflect.DeepEqual(inputBuffer[:data-2], []byte("Reboot")) {
notifySend()
}
io.Copy(c, c)
c.Close()
}
func main() {
if err := os.RemoveAll(sockAddr); err != nil {
log.Fatal(err)
}
l, err := net.Listen("unix", sockAddr)
if err != nil {
log.Fatal("listen error:", err)
}
defer l.Close()
for {
// Accept new connections, dispatching them to echoServer
// in a goroutine.
conn, err := l.Accept()
if err != nil {
log.Fatal("accept error:", err)
}
go handleMessage(conn)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment