Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handling of DBus addresses with options #925

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

#include "40_host_ipc.h"

#include <linux/limits.h>

Check warning on line 7 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <linux/limits.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <cstdlib>

Check warning on line 9 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstdlib> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <filesystem>

Check warning on line 10 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <filesystem> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream>

Check warning on line 11 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <iostream> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string>

Check warning on line 12 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <string> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <sys/stat.h>

Check warning on line 14 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <sys/stat.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <unistd.h>

Check warning on line 15 in libs/oci-cfg-generators/src/linglong/oci-cfg-generators/40_host_ipc.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <unistd.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace linglong::generator {

Expand Down Expand Up @@ -61,24 +63,41 @@
auto mountTemplate =
ocppi::runtime::config::types::Mount{ .options = string_list{ "rbind" }, .type = "bind" };

// TODO 应该参考规范文档实现更完善的地址解析支持
// https://dbus.freedesktop.org/doc/dbus-specification.html#addresses
[dbusMount = mountTemplate, &mounts, &env]() mutable {
auto *systemBusEnv = getenv("DBUS_SYSTEM_BUS_ADDRESS"); // NOLINT

// https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-types:~:text=the%20default%20locations.-,System%20message%20bus,-A%20computer%20may
black-desk marked this conversation as resolved.
Show resolved Hide resolved
std::string systemBus{ "/var/run/dbus/system_bus_socket" };
if (systemBusEnv != nullptr && std::filesystem::exists(systemBusEnv)) {
systemBus = systemBusEnv;
// default value from
// https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-types-system
std::string systemBusEnv = "unix:path=/var/run/dbus/system_bus_socket";
if (auto cStr = std::getenv("DBUS_SYSTEM_BUS_ADDRESS"); cStr != nullptr) {
systemBusEnv = cStr;
}
// address 可能是 unix:path=/xxxx,giud=xxx 这种格式
// 所以先将options部分提取出来,挂载时不需要关心
std::string options;
auto optionsPos = systemBusEnv.find(",");
if (optionsPos != std::string::npos) {
options = systemBusEnv.substr(optionsPos);
systemBusEnv.resize(optionsPos);
}
auto systemBus = std::string_view{ systemBusEnv };
auto suffix = std::string_view{ "unix:path=" };
if (systemBus.rfind(suffix, 0) != 0U) {
std::cerr << "Unexpected DBUS_SYSTEM_BUS_ADDRESS=" << systemBus << std::endl;
return;
}

if (!std::filesystem::exists(systemBus)) {
std::cerr << "D-Bus system bus socket not found at " << systemBus << std::endl;
auto socketPath = std::filesystem::path(systemBus.substr(suffix.size()));
if (!std::filesystem::exists(socketPath)) {
std::cerr << "D-Bus session bus socket not found at " << socketPath << std::endl;
return;
}

dbusMount.destination = "/run/dbus/system_bus_socket";
dbusMount.source = std::move(systemBus);
dbusMount.source = std::move(socketPath);
mounts.emplace_back(std::move(dbusMount));
env.emplace_back("DBUS_SYSTEM_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket");
// 将提取的options再拼到容器中的环境变量
env.emplace_back("DBUS_SYSTEM_BUS_ADDRESS=unix:path=/run/dbus/system_bus_socket" + options);
}();

mounts.push_back(ocppi::runtime::config::types::Mount{
Expand Down Expand Up @@ -155,13 +174,25 @@
});
}();

// TODO 应该参考规范文档实现更完善的地址解析支持
// https://dbus.freedesktop.org/doc/dbus-specification.html#addresses
[&cognitiveXDGRuntimeDir, &mounts, &env]() {
auto *sessionBusEnv = getenv("DBUS_SESSION_BUS_ADDRESS"); // NOLINT
if (sessionBusEnv == nullptr) {
std::string sessionBusEnv;
if (auto cStr = std::getenv("DBUS_SESSION_BUS_ADDRESS"); cStr != nullptr) {
sessionBusEnv = cStr;
}
if (sessionBusEnv.empty()) {
std::cerr << "Couldn't get DBUS_SESSION_BUS_ADDRESS" << std::endl;
return;
}

// address 可能是 unix:path=/xxxx,giud=xxx 这种格式
// 所以先将options部分提取出来,挂载时不需要关心
std::string options;
auto optionsPos = sessionBusEnv.find(",");
if (optionsPos != std::string::npos) {
options = sessionBusEnv.substr(optionsPos);
sessionBusEnv.resize(optionsPos);
}
auto sessionBus = std::string_view{ sessionBusEnv };
auto suffix = std::string_view{ "unix:path=" };
if (sessionBus.rfind(suffix, 0) != 0U) {
Expand All @@ -182,9 +213,9 @@
.source = socketPath,
.type = "bind",
});

// 将提取的options再拼到容器中的环境变量
env.emplace_back(std::string{ "DBUS_SESSION_BUS_ADDRESS=" }
+ "unix:path=" + cognitiveSessionBus.string());
+ "unix:path=" + cognitiveSessionBus.string() + options);
}();

[&hostXDGRuntimeDir, &cognitiveXDGRuntimeDir, &mounts]() {
Expand Down
10 changes: 10 additions & 0 deletions tools/test-linglong.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ ll-builder export
#运行编译后的可执行程序
ll-builder run

# 测试dbus环境变量没有问题
ll-builder run --exec export | grep DBUS_SESSION_BUS_ADDRESS
ll-builder run --exec export | grep DBUS_SYSTEM_BUS_ADDRESS
# 测试session dbus环境变量包含参数时能正常处理
export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS,test=1
ll-builder run --exec export | grep DBUS_SESSION_BUS_ADDRESS | grep test=1
# 测试system dbus环境变量包含参数时能正常处理
export DBUS_SYSTEM_BUS_ADDRESS="unix:path=/var/run/dbus/system_bus_socket,test=2"
ll-builder run --exec export | grep DBUS_SYSTEM_BUS_ADDRESS | grep test=2

#运行并安装uab
sudo ll-cli uninstall org.dde.demo || true
./org.dde.demo_x86_64_0.0.0.1_main.uab
Expand Down
Loading