Skip to content

Conversation

@calsys456
Copy link
Contributor

@calsys456 calsys456 commented Jan 26, 2026

This is more robustic.

Summary by Sourcery

Enhancements:

  • Replace D-Bus based login1 session lookup with sd-login based session and TTY state inspection in the TTY usage check helper.

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: calsys456

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

@deepin-ci-robot
Copy link

Hi @calsys456. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 26, 2026

Reviewer's Guide

Refactors TTY-in-use detection to use systemd sd-login APIs instead of org.freedesktop.login1 D-Bus interfaces, along with minor include and header ordering cleanups in Display.cpp.

Sequence diagram for TTY in-use check via systemd sd-login APIs

sequenceDiagram
    participant Display as DDM_Display_isTtyInUse
    participant SdLogin as sd_login_library
    participant Logind as systemd_logind_service

    Display->>SdLogin: sd_get_sessions(sessions)
    SdLogin->>Logind: query_active_sessions
    Logind-->>SdLogin: list_of_sessions
    loop for each session in sessions
        Display->>SdLogin: sd_session_get_tty(session, tty)
        SdLogin->>Logind: get_tty_for_session
        Logind-->>SdLogin: tty
        Display->>SdLogin: sd_session_get_state(session, state)
        SdLogin->>Logind: get_state_for_session
        Logind-->>SdLogin: state
        alt tty equals desiredTty and state is not closing
            Display-->>Display: return true
        end
    end
    Display-->>Display: return false
Loading

File-Level Changes

Change Details Files
Replace org.freedesktop.login1 D-Bus based TTY usage detection with systemd sd-login API calls and clean up related includes.
  • Reordered and cleaned up local header includes, adding config, Login1Manager, and VirtualTerminal near the top and moving Messages include earlier.
  • Reorganized Qt and system headers, adding systemd/sd-login.h and standard C headers like fcntl and adjusting ordering for consistency.
  • Reimplemented isTtyInUse to enumerate sessions via sd_get_sessions and query per-session TTY and state via sd_session_get_tty and sd_session_get_state instead of using OrgFreedesktopLogin1* D-Bus interfaces.
  • Introduced RAII-like cleanup using std::unique_ptr with custom deleters to free session, TTY, and state strings allocated by sd-login APIs.
  • Simplified debug logging when a TTY is in use to log only the session ID rather than full D-Bus user and session metadata.
src/daemon/Display.cpp

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

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, and left some high level feedback:

  • The RAII guards in isTtyInUse capture sessions, tty, and state by value at construction time (when they are still nullptr), so the deleters will never free the actual buffers returned by sd_get_sessions/sd_session_get_*; instead, manage the lifetime by wrapping the actual pointer variables (e.g., via a small helper struct or a std::unique_ptr<T*, Deleter> that owns and mutates the same pointer).
  • The return values of sd_get_sessions, sd_session_get_tty, and sd_session_get_state are not checked before use; these calls should be validated and tty/state treated as potentially null before comparing with desiredTty or passing to strcmp to avoid undefined behavior on error paths.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The RAII guards in `isTtyInUse` capture `sessions`, `tty`, and `state` by value at construction time (when they are still `nullptr`), so the deleters will never free the actual buffers returned by `sd_get_sessions`/`sd_session_get_*`; instead, manage the lifetime by wrapping the actual pointer variables (e.g., via a small helper struct or a `std::unique_ptr<T*, Deleter>` that owns and mutates the same pointer).
- The return values of `sd_get_sessions`, `sd_session_get_tty`, and `sd_session_get_state` are not checked before use; these calls should be validated and `tty`/`state` treated as potentially null before comparing with `desiredTty` or passing to `strcmp` to avoid undefined behavior on error paths.

## Individual Comments

### Comment 1
<location> `src/daemon/Display.cpp:61-64` </location>
<code_context>
-                    qDebug() << "tty" << desiredTty << "already in use by" << session.user().path.path() << session.state()
-                                      << session.display() << session.desktop() << session.vTNr();
-                    return true;
+        char **sessions = nullptr;
+        std::unique_ptr<void, std::function<void(void *)>> guard(
+            nullptr,
+            [sessions]([[maybe_unused]] void *) {
+                if (sessions) {
+                    for (char **s = sessions; s && *s; ++s)
</code_context>

<issue_to_address>
**issue (bug_risk):** RAII guards capture `sessions`/`tty`/`state` by value, so they never see the updated pointers and won't free anything.

Because `guard` and `guard2` capture `sessions`, `tty`, and `state` by value, they store the initial `nullptr` values. When you later assign to those variables, the captured copies in the deleter are not updated, so nothing gets freed and the allocations from `sd_get_sessions` / `sd_session_get_tty` / `sd_session_get_state` are leaked. Capture by reference instead (e.g. `[&sessions]`, `[&tty, &state]`), or encapsulate the raw pointers in a small RAII struct and use that as the `unique_ptr` deleter target instead of capturing variables in a `std::function`.
</issue_to_address>

### Comment 2
<location> `src/daemon/Display.cpp:83-85` </location>
<code_context>
+                    if (state)
+                        free(state);
+                });
+            sd_session_get_tty(*s, &tty);
+            sd_session_get_state(*s, &state);
+            if (desiredTty == tty && strcmp(state, "closing") != 0) {
+                qDebug() << "tty" << desiredTty << "already in use by session" << *s;
+                return true;
</code_context>

<issue_to_address>
**issue (bug_risk):** Return codes and nullability from sd-login APIs are not checked, which can lead to UB via `strcmp` on a null pointer.

`sd_get_sessions`, `sd_session_get_tty`, and `sd_session_get_state` return values are ignored, so `tty`/`state` may be null. That makes `strcmp(state, "closing")` undefined if `state` is null, and comparing `desiredTty` to a null `tty` is almost certainly unintended. Please check each `sd_*` return value, handle errors by skipping/handling the session, and guard against `state == nullptr` (and invalid `tty`) before use.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces the login1 D-Bus–based session/TTY lookup with systemd sd-login APIs to determine whether a VT/TTY is already in use, aiming to make VT selection more robust.

Changes:

  • Reworked isTtyInUse() to iterate sessions via sd_get_sessions() and inspect sd_session_get_tty() / sd_session_get_state().
  • Updated includes accordingly (adds systemd/sd-login.h, reshuffles related headers).

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment on lines +80 to +82
if (sd_session_get_tty(*s, &tty) < 0 || sd_session_get_state(*s, &state) < 0)
continue;
if (desiredTty == tty && strcmp(state, "closing") != 0) {
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

This function uses strcmp()/free() but doesn’t include <cstring> / <cstdlib> (or <string.h> / <stdlib.h>), so it relies on transitive includes. Please add the explicit standard headers (and consider std::strcmp/std::free) to avoid build breaks when upstream headers change.

这里使用了 strcmp()/free(),但没有显式包含 <cstring> / <cstdlib>(或 <string.h> / <stdlib.h>),目前依赖间接包含。建议补齐标准头文件(并考虑使用 std::strcmp/std::free),避免上游头文件变更导致编译失败。

Copilot uses AI. Check for mistakes.
Comment on lines +41 to +42
#include <QDBusMessage>
#include <QDBusReply>
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

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

#include <QDBusMessage> and #include <QDBusReply> appear unused in this file (no references outside the includes). Please remove them to keep dependencies minimal.

#include <QDBusMessage>#include <QDBusReply> 在此文件中看起来未被使用(除 include 外没有引用)。建议移除以保持依赖最小化。

Suggested change
#include <QDBusMessage>
#include <QDBusReply>

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants