我正在尝试创建/启动一个使用会话总线的服务。它不一定是会话总线,我只是想让它工作,不知道两者之间的区别,就像免责声明一样。
基本上,我正在查看sdbus-cpp库并遵循它们的示例。
我的服务档案:
[Unit]
Description=org.sdbuscpp.concatenator
[Service]
Type=simple
ExecStart=/home/john/dev/dbus/server/build/dbus-server
StandardOutput=journal
User=john
[Install]
WantedBy=multi-user.target
我的配置:
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<policy user="john">
<allow own="org.sdbuscpp.concatenator"/>
<allow send_destination="org.sdbuscpp"/>
<allow send_interface="org.sdbuscpp.concatenator"/>
</policy>
</busconfig>
以及来自GitHub的示例代码:
#include <sdbus-c++/sdbus-c++.h>
#include <string>
#include <vector>
// Yeah, global variable is ugly, but this is just an example and we want to access
// the concatenator instance from within the concatenate method handler to be able
// to emit signals.
sdbus::IObject* g_concatenator{};
void concatenate(sdbus::MethodCall call)
{
// Deserialize the collection of numbers from the message
std::vector<int> numbers;
call >> numbers;
// Deserialize separator from the message
std::string separator;
call >> separator;
// Return error if there are no numbers in the collection
if (numbers.empty())
{
throw sdbus::Error("org.sdbuscpp.Concatenator.Error", "No numbers provided");
}
std::string result;
for (auto number : numbers)
{
result += (result.empty() ? std::string() : separator) + std::to_string(number);
}
// Serialize resulting string to the reply and send the reply to the caller
auto reply = call.createReply();
reply << result;
reply.send();
// Emit 'concatenated' signal
const char* interfaceName = "org.sdbuscpp.Concatenator";
auto signal = g_concatenator->createSignal(interfaceName, "concatenated");
signal << result;
g_concatenator->emitSignal(signal);
}
int main()
{
// Create D-Bus connection to the system bus and requests name on it.
const char* serviceName = "org.sdbuscpp.concatenator";
// auto connection = sdbus::createSystemBusConnection(serviceName);
auto connection = sdbus::createSessionBusConnection(serviceName);
// Create concatenator D-Bus object.
const char* objectPath = "/org/sdbuscpp/concatenator";
auto concatenator = sdbus::createObject(*connection, objectPath);
g_concatenator = concatenator.get();
// Register D-Bus methods and signals on the concatenator object, and exports the object.
const char* interfaceName = "org.sdbuscpp.Concatenator";
concatenator->registerMethod(interfaceName, "concatenate", "ais", "s", &concatenate);
concatenator->registerSignal(interfaceName, "concatenated", "s");
concatenator->finishRegistration();
// Run the I/O event loop on the bus connection.
connection->enterProcessingLoop();
}
这就是我开始这项服务时得到的结果:
Feb 19 17:59:01 box systemd[1]: Started org.sdbuscpp.concatenator.
Feb 19 17:59:02 box dbus-server[828]: terminate called after throwing an instance of 'sdbus::Error'
Feb 19 17:59:02 box dbus-server[828]: what(): [org.freedesktop.DBus.Error.FileNotFound] Failed to open bus (No such file or directory)
Feb 19 17:59:02 box systemd[1]: org.sdbuscpp.concatenator.service: Main process exited, code=dumped, status=6/ABRT
Feb 19 17:59:02 box systemd[1]: org.sdbuscpp.concatenator.service: Failed with result 'core-dump'.
如果我将代码更改为使用系统总线,服务就会启动,但是当我发现使用d-脚的服务时,我会在单击它之后得到一条错误消息。
我做错了什么/理解错了什么?我在运行Ubuntu 19.10。
发布于 2022-08-25 08:19:14
我把掘墓人头盔戴上了..。
也许这对其他读者是有帮助的:
执行
dbus-launch
在终端中应该修复所描述的问题。
https://unix.stackexchange.com/questions/568543
复制相似问题