go - 使用 Golang 启动 D-Bus 服务

标签 go dbus

我现在正在使用适用于 golang 的 D-Bus API 调用并启动一个 systemd 服务(我的只是调用一个 shell 脚本)。

我在/usr/share/dbus-1/system-services/org.freedesktop.hello.service 做了一个 D-Bus 服务

[D-BUS Service]
Name=org.freedesktop.hello
Exec=/bin/false
User=root
SystemdService=hello.service

还有/lib/systemd/system/hello.service 中的 systemd 服务

[Unit]
Description=Hello

[Service]
Type=dbus
BusName=org.freedesktop.hello
ExecStart=/opt/hello.sh

我正在尝试使用下面的代码实现相同的结果,这很有效。

sudo gdbus call --system --dest org.freedesktop.hello --object-path /org/freedesktop/hello --method org.freedesktop.DBus.Introspectable.Introspect

但是我在 Golang 中不断收到错误,

The name org.freedesktop.hello was not provided by any .service files

我现在的代码是

package main

import (
    "encoding/json"
    "github.com/godbus/dbus"
    "os"
    "github.com/godbus/dbus/introspect"
)

func main() {
    conn, error1 := dbus.SessionBus()
    if error1 != nil {
        panic(error1)
    }
    node, err2 := introspect.Call(conn.Object("org.freedesktop.hello", "/org/freedesktop/hello"))
    if err2 != nil {
        panic(err2)
    }
    data, _ := json.MarshalIndent(node, "", "   ")
    os.Stdout.Write(data)
}

关于这个东西的信息不多,所以我想得到一些帮助。谢谢!

最佳答案

sudo gdbus call --system ...

那是在系统总线上。

...
conn, error1 := dbus.SessionBus()
...

这是在 session 总线上。

尝试使用像 dbus.SystemBus() 这样的东西。

关于go - 使用 Golang 启动 D-Bus 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47320709/

相关文章:

arrays - 为什么在使用 base64 编码字节数组时会出现 "index out of range"错误?

linux - C 中的 D-Bus 教程与 wpa_supplicant 通信

c++ - 等待 DBus 服务在 Qt 中可用

oop - 去OOP实现

go - 如何在 Go 中将时间戳格式化为 GMT?

regex - Golang 编译正则表达式以删除 "以及字符串中 @ 之后和包括的任何内容

go - 如何将interface{}转换为bytes.Buffer

gnome - 用 Onboard -> Hide/Show via DBus 替换 Gnomes 虚拟键盘不起作用

c++ - GDBusProxy - 在就绪回调中获取输入参数

c++ - QDbus:在同一路径上注册多个对象