2024-06-13 13:24:24 +02:00
|
|
|
//go:build (linux && !android) || freebsd
|
2023-06-12 14:43:55 +02:00
|
|
|
|
2022-11-23 13:39:42 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-30 09:58:56 +01:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2022-11-23 13:39:42 +01:00
|
|
|
"github.com/godbus/dbus/v5"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const dbusDefaultFlag = 0
|
|
|
|
|
|
|
|
func isDbusListenerRunning(dest string, path dbus.ObjectPath) bool {
|
|
|
|
obj, closeConn, err := getDbusObject(dest, path)
|
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
log.Tracef("error getting dbus object: %s", err)
|
2022-11-23 13:39:42 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer closeConn()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-01-30 09:58:56 +01:00
|
|
|
if err = obj.CallWithContext(ctx, "org.freedesktop.DBus.Peer.Ping", 0).Store(); err != nil {
|
|
|
|
log.Tracef("error calling dbus: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2022-11-23 13:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDbusObject(dest string, path dbus.ObjectPath) (dbus.BusObject, func(), error) {
|
|
|
|
conn, err := dbus.SystemBus()
|
|
|
|
if err != nil {
|
2024-01-30 09:58:56 +01:00
|
|
|
return nil, nil, fmt.Errorf("get dbus: %w", err)
|
2022-11-23 13:39:42 +01:00
|
|
|
}
|
|
|
|
obj := conn.Object(dest, path)
|
|
|
|
|
|
|
|
closeFunc := func() {
|
|
|
|
closeErr := conn.Close()
|
|
|
|
if closeErr != nil {
|
|
|
|
log.Warnf("got an error closing dbus connection, err: %s", closeErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj, closeFunc, nil
|
|
|
|
}
|