Skip to content

feat: add systemd units and plugin groups for tray loader - #518

Open
yixinshark wants to merge 1 commit into
linuxdeepin:masterfrom
yixinshark:feat/tray-plugin-systemd-services
Open

yixinshark wants to merge 1 commit into
linuxdeepin:masterfrom
yixinshark:feat/tray-plugin-systemd-services

Conversation

@yixinshark

@yixinshark yixinshark commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Pms: TASK-395865

Summary

  • Add dde-tray-loader.target and a template instance service with lifetime coupled to dde-shell@DDE.service
  • Group tray plugins via DConfig into per-group loader instances with --group/--check-group support
  • Unset DDE_CURRENT_COMPOSITOR so loaders connect to the dock's internal dockplugin compositor

Test plan

  • Verify dde-tray-loader.target starts after dde-shell@DDE.service is ready
  • Verify each plugin group runs in its own loader instance and empty groups skip startup
  • Verify loader processes stop and restart together with the dock shell

Summary by Sourcery

Introduce lifecycle-managed, independently isolated tray plugin loader groups for the dock shell.

New Features:

  • Add systemd user target and per-group template services for tray plugin loaders tied to the dock shell lifecycle.
  • Support loading tray plugins by DConfig-defined groups with validation and empty-group handling.

Bug Fixes:

  • Ensure tray loaders use the dock's internal compositor environment rather than inheriting the current compositor setting.

Enhancements:

  • Add group-aware loader commands and centralized plugin grouping configuration.

Build:

  • Install the new systemd units, wants directory, and tray plugin grouping configuration.

Add dde-tray-loader.target and a template instance service with lifetime
coupled to dde-shell@DDE.service, group tray plugins via DConfig into
per-group loader instances, add --group/--check-group support, and unset
DDE_CURRENT_COMPOSITOR so loaders connect to the dock's internal
dockplugin compositor instead of the outer session compositor.

新增 dde-tray-loader.target 及模板实例服务,生命周期与
dde-shell@DDE.service 绑定;通过 DConfig 将托盘插件分组为独立加载器实例,
新增 --group/--check-group 支持;并清除 DDE_CURRENT_COMPOSITOR,使加载器
连接 dock 内部的 dockplugin 合成器而非外层会话合成器。

Log: add systemd units and plugin groups for tray loader
Pms: TASK-395865
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: yixinshark

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Sep 21, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR moves tray plugin startup into four independently managed loader instances launched through a new systemd user target, with lifecycle coupling to dde-shell@DDE.service. Loader instances resolve plugins from a DConfig-owned grouping schema, skip genuinely empty groups via --check-group, report configuration failures distinctly, and unset the dock compositor environment so they connect to the dock's internal compositor.

Sequence diagram for grouped tray loader startup

sequenceDiagram
    participant Systemd as systemd
    participant Loader as trayplugin-loader
    participant Config as DConfig
    participant Dock as Dock compositor

    Systemd->>Loader: --check-group groupName
    Loader->>Loader: isValidGroup(groupName)
    Loader->>Config: pluginPathsForGroup(groupName)
    Config-->>Loader: plugin paths or config failure
    alt group is empty
        Loader-->>Systemd: exit 1
    else config failure
        Loader-->>Systemd: exit 255
    else group has plugins
        Systemd->>Loader: --group groupName
        Loader->>Config: pluginPathsForGroup(groupName)
        Loader->>Loader: pluginManager loads plugin paths
        Loader->>Loader: qunsetenv QT_SCALE_FACTOR
        Loader->>Loader: qunsetenv DDE_CURRENT_COMPOSITOR
        Loader->>Dock: connect to internal dockplugin compositor
    end
Loading

File-Level Changes

Change Details Files
Add user-systemd orchestration for group-specific tray-loader processes tied to the dock shell lifecycle.
  • Install a target, templated loader service, and one wants symlink per supported plugin group.
  • Generate the service command from the configured libexec install path.
  • Couple target startup and loader process environment/lifecycle to dde-shell@DDE.service, including compositor environment handling.
CMakeLists.txt
systemd/dde-tray-loader.target
systemd/dde-tray-loader@.service.in
systemd/dde-tray-loader.target.wants/dde-tray-loader@crashProneTrayPlugins.service
systemd/dde-tray-loader.target.wants/dde-tray-loader@otherTrayPlugins.service
systemd/dde-tray-loader.target.wants/dde-tray-loader@selfMaintenanceTrayPlugins.service
systemd/dde-tray-loader.target.wants/dde-tray-loader@subprojectTrayPlugins.service
debian/dde-tray-loader.install
Introduce DConfig-backed plugin grouping and expose group-aware loader execution.
  • Define four recognized groups and assign unlisted plugins to the fallback group while prioritizing crash-prone membership.
  • Scan standard or TRAY_DEBUG_PLUGIN_PATH plugin directories and distinguish config failures from empty groups.
  • Add --group validation/loading and --check-group exit behavior for systemd ExecCondition use.
  • Preserve explicit display-group overrides and support legacy direct -p loading.
src/loader/trayplugingroups.h
src/loader/trayplugingroups.cpp
src/loader/configs/org.deepin.dde.dock.plugin.groups.json
src/loader/main.cpp
src/loader/CMakeLists.txt
Package the new tray-loader runtime and configuration for Debian.
  • Update package metadata and install rules for systemd units and the loader configuration.
debian/control
debian/dde-tray-loader.install

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/loader/trayplugingroups.cpp" line_range="11" />
<code_context>
+        dirs << pluginDirs();
+
+    QStringList pluginPaths;
+    for (const auto &pluginDir : std::as_const(dirs)) {
+        QDir dir(pluginDir);
+        if (!dir.exists()) {
</code_context>
<issue_to_address>
**issue (bug_risk):** The new source uses `std::as_const` without including `<utility>`, where it is declared. A conforming build therefore fails to compile `trayplugingroups.cpp` with `std::as_const` not declared.

**Suggested fix:** Add `#include <utility>` to `trayplugingroups.cpp`.

```suggestion
#include <QScopedPointer>
#include <utility>
```
</issue_to_address>

### Comment 2
<location path="src/loader/trayplugingroups.cpp" line_range="24-27" />
<code_context>
+    // Keep in sync with the directories previously scanned by dde-shell
+    // (panels/dock/loadtrayplugins.h).
+    static const QStringList dirs = {
+        "/usr/lib/dde-dock/plugins/",
+        "/usr/lib/dde-dock/plugins/quick-trays/",
+        "/usr/lib/dde-dock/plugins/system-trays/"
+    };
+    return dirs;
+}
</code_context>
<issue_to_address>
**issue (bug_risk):** The group scanner only searches absolute `/usr/lib/dde-dock/...` directories, while the plugins are installed relative to the configured CMake install prefix. When the project is installed with a prefix other than `/usr`, every group is reported empty and no tray plugins are started.

**Triggers:** When the package is installed under a non-`/usr` prefix.

**Suggested fix:** Derive the plugin directories from configured install paths, for example by substituting `CMAKE_INSTALL_PREFIX`/`CMAKE_INSTALL_LIBDIR` into generated code or passing the paths through the service environment.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨


#include <QDir>
#include <QLoggingCategory>
#include <QScopedPointer>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The new source uses std::as_const without including <utility>, where it is declared. A conforming build therefore fails to compile trayplugingroups.cpp with std::as_const not declared.

Suggested fix: Add #include <utility> to trayplugingroups.cpp.

Suggested change
#include <QScopedPointer>
#include <QScopedPointer>
#include <utility>

Comment on lines +24 to +27
"/usr/lib/dde-dock/plugins/",
"/usr/lib/dde-dock/plugins/quick-trays/",
"/usr/lib/dde-dock/plugins/system-trays/"
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): The group scanner only searches absolute /usr/lib/dde-dock/... directories, while the plugins are installed relative to the configured CMake install prefix. When the project is installed with a prefix other than /usr, every group is reported empty and no tray plugins are started.

Triggers: When the package is installed under a non-/usr prefix.

Suggested fix: Derive the plugin directories from configured install paths, for example by substituting CMAKE_INSTALL_PREFIX/CMAKE_INSTALL_LIBDIR into generated code or passing the paths through the service environment.

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

AI 代码审查报告

项目: linuxdeepin/dde-tray-loader
PR: #518 - feat: add systemd units and plugin groups for tray loader
提交: 4fa4340 - feat: add systemd units and plugin groups for tray loader
分支: feat/tray-plugin-systemd-services → master
作者: yixinshark
审查时间: 2026-09-21 16:40:00
分析模式: 全量分析
平台: GitHub


总体评分

维度 得分 满分 状态
语法逻辑 25 25
代码质量 25 25
代码性能 20 20
代码安全 30 30
总分 100 100 ✓ 优秀

总体评价: 代码审查通过。未发现安全问题,代码结构清晰,注释完整,逻辑正确。本次提交为 dde-tray-loader 添加了 systemd 用户单元和基于 DConfig 的插件分组机制,将托盘插件按组隔离到独立进程中运行,生命周期与 dock shell 绑定。


漏洞统计

指标 数量
当前漏洞总数 0
新增漏洞 0
修复漏洞 0
持平漏洞 0

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个


维度1:语法逻辑(25/25)✓

评价词: 语法正确,逻辑清晰

分析内容

本次提交涉及 14 个文件的变更,包括新增 C++ 源文件(trayplugingroups.h/cpp)、修改 main.cpp 入口逻辑、新增 systemd 单元文件和 DConfig 配置 schema。

main.cpp 变更分析:

  1. --check-group 早期退出路径(第79-90行):在初始化 GUI 或连接 Wayland compositor 之前,创建轻量级 QCoreApplication 检查组成员身份。退出码区分明确:

    • 返回 0:组内有插件,继续启动
    • 返回 1:空组,跳过启动
    • 返回 255:错误(无效组名或配置加载失败)
  2. 参数互斥验证(第184行):parser.isSet(pluginPathsOption) == parser.isSet(pluginGroupLoadOption) 实现 XOR 逻辑,当 -p--group 同时设置或都未设置时报错。逻辑正确。

  3. --group 模式(第191-211行):验证组名 → 加载 DConfig 配置 → 检查配置状态 → 检查空组。流程清晰,错误处理完善:

    • 配置加载失败返回 2(不掩盖为空组)
    • 空组返回 0(优雅退出)
  4. 显示名称优先级(第237-241行):-g 显式覆盖 → --group 名称 → 第一个插件名称。逻辑正确。

trayplugingroups.cpp 分析:

  1. pluginDirs()(第19-29行):返回 static const QStringList,初始化一次。正确。
  2. allPluginPaths()(第31-56行):扫描 3 个插件目录,处理不存在目录的情况。逻辑正确。
  3. pluginPathsForGroup()(第68-104行):DConfig 创建带 null 检查,组分配优先级正确(crashProne > selfMaintenance > subproject > other)。
  4. isValidGroup()(第60-66行):白名单验证 4 个预定义组名。正确。

systemd 单元文件分析:

  1. dde-tray-loader.target:BindsTo/After dde-shell@DDE.service,PartOf dde-session-core.target。依赖关系正确。
  2. dde-tray-loader@.service.in:ExecCondition 在 ExecStart 前检查空组,Restart=on-abnormal 配合 StartLimitBurst=6 限制重启次数。逻辑一致。

无语法错误,无逻辑缺陷,边界处理完善。


维度2:代码质量(25/25)✓

评价词: 代码结构清晰,注释完整

分析内容

注释完整性(5/5):

  • main.cpp:ExecCondition 行为说明(第77-78行)、显示名称优先级注释(第235-236行)、配置失败处理注释(第203行)、空组处理注释(第208行)
  • trayplugingroups.cpp:DConfig schema 归属说明(第70-73行)、插件分组优先级注释(第90行)、目录不存在警告日志
  • trayplugingroups.h:函数文档完整,包含参数说明和返回值说明
  • systemd 单元文件:详细注释解释 BindsTo/PartOf 传递关系、ExecCondition 设计意图、Restart 策略原因
  • DConfig JSON:中英文双语 name/description,每个组都有清晰描述

代码重复(5/5):

  • pluginPathsForGroup()isValidGroup()--check-group--group 两个代码路径中复用,是正确的函数复用而非代码重复
  • 无复制粘贴代码

结构合理性(5/5):

  • 新代码组织为独立文件(trayplugingroups.h/cpp),命名空间 loader 封装
  • 函数大小合理,最大函数 pluginPathsForGroup() 约 35 行
  • 模块耦合度低:trayplugingroups 模块仅依赖 DConfig 和 Qt 核心模块

调试信息清理(5/5):

  • 无残留调试代码
  • TRAY_DEBUG_PLUGIN_PATH 环境变量是合法的开发/测试特性,变量名明确标识用途
  • qCWarning/qCritical/qDebug 是生产级日志,使用 Q_LOGGING_CATEGORY 分类

维度3:代码性能(20/20)✓

评价词: 性能良好,资源使用合理

分析内容

资源使用:

  • pluginDirs() 使用 static const,仅初始化一次,避免重复构造
  • DConfig 使用 QScopedPointer 创建,确保 RAII 资源清理,无内存泄漏
  • std::as_const(dirs) 防止不必要的 Qt 容器拷贝

算法复杂度:

  • allPluginPaths():O(d*n) 其中 d=3 个目录,n=每个目录的 .so 文件数。仅调用一次。
  • pluginPathsForGroup():O(mk) 其中 m=总插件数(约30),k=每个分组的插件数。对每个插件做最多 3 次 QStringList::contains() 线性查找。总比较次数约 303*15=1350 次字符串比较,可忽略不计。
  • 如需优化可使用 QSet<QString> 将查找降为 O(1),但当前规模下无必要。

无性能瓶颈:

  • 无频繁系统调用
  • 无不必要的内存拷贝
  • 无阻塞主线程的操作
  • DConfig 访问是快速的本地配置读取

维度4:代码安全(30/30)✓

存在0个安全漏洞

安全分析

1. 命令注入风险评估:无风险

  • systemd %i 实例名来自 .target.wants 目录下的符号链接文件名,由包安装控制
  • isValidGroup() 对组名进行白名单验证,仅接受 4 个预定义字符串
  • 即使攻击者创建恶意符号链接,--check-group 路径会拒绝无效组名并返回 255

2. 路径遍历风险评估:无风险

  • 插件目录为硬编码路径(/usr/lib/dde-dock/plugins/ 等)
  • TRAY_DEBUG_PLUGIN_PATH 环境变量需要本地访问权限,不构成安全漏洞

3. 敏感信息泄露评估:无风险

  • UnsetEnvironment=DDE_CURRENT_COMPOSITOR 是良好的安全实践,防止环境变量泄漏到子进程
  • 无硬编码密钥、密码或 token
  • DConfig JSON 中无敏感信息

4. 输入验证:完善

  • isValidGroup() 白名单验证组名
  • argc != 3 检查参数数量
  • dConfig->isValid() 检查 DConfig 有效性
  • configOk 标志区分配置失败和空组

5. 缓冲区安全:安全

  • 使用 Qt QString/StringList 进行字符串操作,边界安全
  • 无原始 C 字符串操作
  • sprintf/strcpy 等不安全函数

6. systemd 安全配置:良好

  • Restart=on-abnormal 配合 StartLimitBurst=6 防止无限重启循环
  • RestartPreventExitStatus=SIGTERM SIGKILL 防止正常终止触发重启
  • Slice=session.slice 正确的 cgroup 归属

文件分析清单

文件 类型 分析状态 说明
src/loader/main.cpp C++ 源码 新增 --group/--check-group 逻辑,参数互斥验证,显示名称优先级
src/loader/trayplugingroups.cpp C++ 源码(新增) 插件分组逻辑,DConfig 读取,目录扫描
src/loader/trayplugingroups.h C++ 头文件(新增) 组名常量定义,函数声明
src/loader/configs/org.deepin.dde.dock.plugin.groups.json DConfig 配置 3 个分组定义,权限设置合理
systemd/dde-tray-loader@.service.in systemd 模板服务 ExecCondition/ExecStart/Restart 策略
systemd/dde-tray-loader.target systemd 目标 BindsTo/After/PartOf 依赖关系
CMakeLists.txt 构建系统 configure_file/install systemd 单元
src/loader/CMakeLists.txt 构建系统 添加新源文件和 DConfig meta
debian/control 打包配置 添加 Breaks 依赖
debian/dde-tray-loader.install 安装配置 安装 systemd 单元文件
systemd/dde-tray-loader.target.wants/* 符号链接 跳过 4 个指向 @.service 的符号链接

审查结论

本次提交为 dde-tray-loader 添加了 systemd 用户单元和基于 DConfig 的插件分组机制,实现了以下功能:

  1. systemd 生命周期管理:通过 dde-tray-loader.target 和模板服务 dde-tray-loader@.service 将托盘插件加载器进程的生命周期与 dock shell 绑定
  2. 插件分组隔离:通过 DConfig 配置将插件分为 4 组(selfMaintenance/subproject/crashProne/other),每组在独立进程中运行
  3. 空组跳过:通过 ExecCondition 在启动 GUI 前检查空组,避免不必要的资源消耗
  4. 环境隔离UnsetEnvironment=DDE_CURRENT_COMPOSITOR 确保加载器连接到 dock 内部 compositor

代码质量优秀,注释详尽,逻辑清晰,无安全漏洞。建议合入。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants