Skip to content

feat(server): add D-Bus service and event relay dispatcher - #257

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
wangrong1069:pr0911-2
Sep 11, 2026
Merged

deepin-bot[bot] merged 1 commit into
linuxdeepin:develop/eaglefrom
wangrong1069:pr0911-2

Conversation

@wangrong1069

@wangrong1069 wangrong1069 commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Replace socket-based event dispatcher with a D-Bus service exporting org.deepin.Anything interface and a relay dispatcher that broadcasts raw fs_event structs to connected channels via a worker thread.

新增D-Bus服务接口org.deepin.Anything,提供SetAllowCaller和
GetEventChannel方法;新增事件中继分发器通过worker线程广播
原始fs_event;修复监听器GSource附加到错误GMainContext的问题;
fs_event结构src/dst合并为path字段,minor类型扩展为guint32。

Log: 新增D-Bus服务及事件中继分发器,替换旧的socket分发方式
PMS: BUG-370779
Influence: server服务改为D-Bus方式提供事件通道,影响事件监听和分发的整体流程

Summary by Sourcery

Replace the server's socket event-dispatcher interface with an authorized D-Bus service and worker-backed raw event relay.

New Features:

  • Expose the org.deepin.Anything system D-Bus service with caller authorization and event-channel retrieval methods.
  • Relay raw filesystem events to connected channels through a dedicated worker-backed dispatcher.

Bug Fixes:

  • Attach filesystem listener sources to their listener-specific GMainContext instead of the wrong default context.

Enhancements:

  • Simplify fs_event payloads by consolidating source and destination into path and widening minor device identifiers to guint32.
  • Replace the server's socket-based dispatcher integration with D-Bus channel provisioning and raw event broadcasting.

Build:

  • Install the D-Bus policy configuration and add optional CTest integration for server tests.

Deployment:

  • Add system D-Bus service and policy configuration for org.deepin.Anything.

Tests:

  • Add relay dispatcher tests covering lifecycle, channel creation and limits, event broadcasting, and shutdown behavior.

@github-actions

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "src/server/dbus-service.h": {
            "b": [
                " * An opaque D-Bus service object that exports the org.deepin.Anything"
            ]
        }
    }
}

@sourcery-ai

sourcery-ai Bot commented Sep 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

The server now acquires org.deepin.Anything on the system D-Bus and exposes authorized event channels backed by a worker-thread relay that broadcasts raw fs_event structs, while listeners adopt explicit thread-local GMainContexts and tests cover the new relay behavior.

Sequence diagram for D-Bus event channel acquisition and relay

sequenceDiagram
    participant Client
    participant DBus as org.deepin.Anything
    participant Relay as ServerEventRelayDispatcher
    participant Worker as Relay worker thread
    participant Channels as Event relay channels

    Client->>DBus: SetAllowCaller(unique_name)
    DBus-->>Client: Return success
    Client->>DBus: GetEventChannel()
    DBus->>Relay: server_event_relay_dispatcher_get_event_channel()
    Relay->>Worker: Queue request and wake worker
    Worker->>Channels: event_relay_dispatcher_get_event_channel()
    Channels-->>Worker: fd
    Worker-->>DBus: get_event_channel_callback(fd, event_protocol_id)
    DBUS-->>Client: Return Unix fd and protocol id
Loading

File-Level Changes

Change Details Files
Replace the server’s socket event-dispatch path with a system-bus D-Bus service and worker-backed raw event relay.
  • Export org.deepin.Anything with SetAllowCaller and permission-checked GetEventChannel methods.
  • Pass channel requests and fs_event payloads through a dedicated worker thread to serialize access to the relay library and broadcast raw events.
  • Wire service and relay lifecycle into server startup and shutdown, including the D-Bus policy and service installation files.
src/server/dbus-service.c
src/server/dbus-service.h
src/server/event-relay-dispatcher.c
src/server/event-relay-dispatcher.h
src/server/main.c
src/server/CMakeLists.txt
src/server/org.deepin.Anything.conf
src/logger/deepin-anything-logger.service
src/server/deepin-anything-server.service
Change the event wire model and listener ingestion to use a single path field with a widened minor device number.
  • Replace fs_event src/dst fields with path and change minor from guint8 to guint32.
  • Update generic-netlink and mmap-ring listeners and legacy conversion logic to consume the new representation.
src/server/fs-event.h
src/server/event-listener-genl.c
src/server/event-listener-mmap-ring.c
src/server/event-dispatcher.c
Attach listener I/O sources explicitly to their per-thread GMainContexts.
  • Create and attach GIO sources to the listener-owned contexts instead of relying on convenience APIs and the ambient default context.
  • Add source/context creation and attachment failure handling with cleanup paths.
src/server/event-listener-genl.c
src/server/event-listener-mmap-ring.c
Add automated coverage for relay lifecycle, channel creation, limits, shutdown, and raw-event broadcasting.
  • Enable optional server tests through CMake.
  • Exercise channel allocation, protocol IDs, failure behavior, event payload integrity, and multi-channel fan-out.
src/server/CMakeLists.txt
src/server/tests/CMakeLists.txt
src/server/tests/test_event_relay_dispatcher.c

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/server/dbus-service.c" line_range="144-145" />
<code_context>
+    gpointer key;
+    g_hash_table_iter_init(&ht_iter, service->allow_callers);
+    while (g_hash_table_iter_next(&ht_iter, &key, NULL)) {
+        if (!g_hash_table_contains(live_names, key)) {
+            g_hash_table_iter_remove(&ht_iter);
+            g_message("allow_callers: evicted stale entry %s", (gchar *)key);
+        }
+    }
</code_context>
<issue_to_address>
**issue (bug_risk):** g_hash_table_iter_remove frees the current allow-list key, then the callback logs that freed key through `key`, causing a use-after-free during stale-caller cleanup.

**Triggers:** When ListNames contains a caller that is no longer present in `allow_callers`.

**Suggested fix:** Copy the key for logging before removal, or log it before calling `g_hash_table_iter_remove`.

```suggestion
            g_message("allow_callers: evicted stale entry %s", (gchar *)key);
            g_hash_table_iter_remove(&ht_iter);
```
</issue_to_address>

### Comment 2
<location path="src/server/dbus-service.c" line_range="94-95" />
<code_context>
+            G_DBUS_CALL_FLAGS_NONE,
+            -1,                       /* timeout — use default         */
+            NULL,                     /* cancellable                   */
+            list_names_callback,
+            service);                 /* user_data — borrowed service  */
+    }
+
</code_context>
<issue_to_address>
**issue (bug_risk):** The asynchronous ListNames call stores a raw `AnythingDBusService *` as user data, but service destruction does not cancel or otherwise synchronize with that call; its callback dereferences the freed service through `service->allow_callers`.

**Triggers:** When the server shuts down while a SetAllowCaller-triggered ListNames request is still pending.

**Suggested fix:** Hold a reference to the service until the async callback completes, or cancel pending calls before freeing the service.
</issue_to_address>

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

Comment thread src/server/dbus-service.c Outdated
Comment thread src/server/dbus-service.c
Replace socket-based event dispatcher with a D-Bus service exporting
org.deepin.Anything interface and a relay dispatcher that broadcasts
raw fs_event structs to connected channels via a worker thread.

新增D-Bus服务接口org.deepin.Anything,提供SetAllowCaller和
GetEventChannel方法;新增事件中继分发器通过worker线程广播
原始fs_event;修复监听器GSource附加到错误GMainContext的问题;
fs_event结构src/dst合并为path字段,minor类型扩展为guint32。

Log: 新增D-Bus服务及事件中继分发器,替换旧的socket分发方式
PMS: BUG-370779
Influence: server服务改为D-Bus方式提供事件通道,影响事件监听和分发的整体流程
@github-actions

Copy link
Copy Markdown
  • 检测到敏感词export变动
详情
    {
    "export": {
        "src/server/dbus-service.h": {
            "b": [
                " * An opaque D-Bus service object that exports the org.deepin.Anything"
            ]
        }
    }
}

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 98 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 98 分,大于 70 分通过阈值,代码质量符合要求。新增D-Bus服务和事件中继分发器设计合理,GSource修复正确,安全配置完善。未发现安全漏洞,语法逻辑清晰,性能设计良好。仅存在轻微代码重复和可优化空间。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 代码结构完整,D-Bus服务、事件中继分发器、监听器GSource修复均实现正确。线程同步机制(GAsyncQueue + mutex/cond)实现正确,内存管理遵循GLib规范。GSource修复正确:从g_io_add_watch(附加到全局默认上下文)改为g_source_attach到线程局部上下文。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. src/server/event-listener-genl.c, src/server/event-listener-mmap-ring.c:166-177, 119-134 - 两个监听器文件中GSource创建+附加模式存在轻微代码重复(各约10行),可考虑抽取为公共辅助函数减少维护成本

建议: GSource创建模式可抽取为公共辅助函数以减少重复代码。注释非常完善,包括设计决策、内存所有权、线程安全说明。函数长度合理,模块划分清晰。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. src/server/event-relay-dispatcher.c:977-998 - server_event_relay_dispatcher_get_event_channel中每次请求需分配两个g_slice_new0(ChannelRequest + wakeup fs_event),存在轻微分配开销,g_slice分配器本身效率高,此开销在可接受范围内

建议: 事件入队使用GAsyncQueue(O(1)),设计合理。max_event_channel=30限制并发通道数,防止资源耗尽。allow_callers懒清理使用异步ListNames调用,不阻塞主线程。g_slice分配器本身效率很高,当前开销在可接受范围内。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: D-Bus安全配置合理:root可own,deepin-daemon组可调用,默认拒绝SetAllowCaller。GetEventChannel有应用层allow_callers检查作为深度防御。缓冲区操作有边界检查(safe_string_copy, g_strlcpy)。文件描述符管理正确(FDList dup后关闭原始fd)。线程安全:GAsyncQueue线程安全,allow_callers仅主线程访问(有文档说明)。日志不泄露敏感信息。


💡 改进建议代码示例

// 可选:抽取GSource创建为公共辅助函数
static guint
attach_io_source_to_context(GIOChannel *channel,
                            GIOCondition condition,
                            GSourceFunc callback,
                            gpointer data,
                            GMainContext *ctx)
{
    GSource *source = g_io_create_watch(channel, condition);
    if (source == NULL)
        return 0;
    g_source_set_callback(source, callback, data, NULL);
    guint id = g_source_attach(source, ctx);
    g_source_unref(source);
    return id;
}

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, wangrong1069

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

@wangrong1069

Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot

deepin-bot Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

This pr force merged! (status: unstable)

@deepin-bot
deepin-bot Bot merged commit 684c18a into linuxdeepin:develop/eagle Sep 11, 2026
16 of 17 checks passed
@wangrong1069
wangrong1069 deleted the pr0911-2 branch September 11, 2026 09:15
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.

3 participants