mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-03 11:31:12 +02:00
Saves 360 KB (19951800 => 19591352 on linux/amd64 --extra-small --box binary) Updates #12614 Updates #17206 Change-Id: Iafd5b2536dd735111b447546cba335a7a64379ed Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux && !android && !ts_omit_dbus
|
|
|
|
package dns
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
)
|
|
|
|
func init() {
|
|
optDBusPing.Set(dbusPing)
|
|
optDBusReadString.Set(dbusReadString)
|
|
}
|
|
|
|
func dbusPing(name, objectPath string) error {
|
|
conn, err := dbus.SystemBus()
|
|
if err != nil {
|
|
// DBus probably not running.
|
|
return err
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
|
|
obj := conn.Object(name, dbus.ObjectPath(objectPath))
|
|
call := obj.CallWithContext(ctx, "org.freedesktop.DBus.Peer.Ping", 0)
|
|
return call.Err
|
|
}
|
|
|
|
// dbusReadString reads a string property from the provided name and object
|
|
// path. property must be in "interface.member" notation.
|
|
func dbusReadString(name, objectPath, iface, member string) (string, error) {
|
|
conn, err := dbus.SystemBus()
|
|
if err != nil {
|
|
// DBus probably not running.
|
|
return "", err
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
|
|
obj := conn.Object(name, dbus.ObjectPath(objectPath))
|
|
|
|
var result dbus.Variant
|
|
err = obj.CallWithContext(ctx, "org.freedesktop.DBus.Properties.Get", 0, iface, member).Store(&result)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if s, ok := result.Value().(string); ok {
|
|
return s, nil
|
|
}
|
|
return result.String(), nil
|
|
}
|