Skip to content

fix(enable): use sysfs to detect wireless NIC instead of name prefix#668

Open
GongHeng2017 wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:202605211910-eagle-fix
Open

fix(enable): use sysfs to detect wireless NIC instead of name prefix#668
GongHeng2017 wants to merge 1 commit into
linuxdeepin:develop/eaglefrom
GongHeng2017:202605211910-eagle-fix

Conversation

@GongHeng2017
Copy link
Copy Markdown
Contributor

@GongHeng2017 GongHeng2017 commented May 21, 2026

Replace hardcoded name prefix matching (wlan/wlp) with sysfs path checking (/sys/class/net//wireless and phy80211) for more reliable wireless NIC detection.

使用 sysfs 路径检测替代硬编码名称前缀匹配来判断无线网卡,提升检测准确性。

Log: 修复无线网卡检测逻辑,使用sysfs替代名称前缀匹配
PMS: https://pms.uniontech.com/bug-view-362409.html
Influence: 无线网卡启用/禁用操作不再依赖接口名称前缀,支持所有符合内核规范的无线网卡接口名称。

Summary by Sourcery

Detect wireless network interfaces via sysfs paths instead of hardcoded name prefixes when enabling or disabling devices.

Bug Fixes:

  • Fix wireless NIC detection so it no longer relies on interface name prefixes like wlan/wlp but uses kernel-provided sysfs indicators instead.

Enhancements:

  • Add a reusable helper to classify network interfaces as wireless based on /sys/class/net entries.
  • Update file copyright years to cover up to 2026.

Replace hardcoded name prefix matching (wlan/wlp) with sysfs path
checking (/sys/class/net/<name>/wireless and phy80211) for more
reliable wireless NIC detection.

使用 sysfs 路径检测替代硬编码名称前缀匹配来判断无线网卡,提升检测准确性。

Log: 修复无线网卡检测逻辑,使用sysfs替代名称前缀匹配
PMS: https://pms.uniontech.com/bug-view-362409.html
Influence: 无线网卡启用/禁用操作不再依赖接口名称前缀,支持所有符合内核规范的无线网卡接口名称。
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017

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
Copy link
Copy Markdown

sourcery-ai Bot commented May 21, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors wireless network interface detection in EnableUtils to rely on sysfs entries instead of hardcoded interface name prefixes, and encapsulates the new detection logic in a private helper while updating metadata headers.

File-Level Changes

Change Details Files
Replace wireless NIC detection based on interface name prefixes with sysfs-based detection and encapsulate it in a helper method.
  • Replace direct logicalName.startsWith("wlan"/"wlp") condition with a call to a new isWireless helper in ioctlOperateNetworkLogicalName
  • Add inclusion of QFileInfo to support filesystem-based checks
  • Implement static private isWireless method to test for /sys/class/net//wireless or phy80211 path existence
  • Declare isWireless as a private static method in the EnableUtils class header
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.h
Update SPDX copyright year range metadata.
  • Update SPDX-FileCopyrightText year range from 2019 ~ 2023 to 2019 - 2026 in implementation file
  • Update SPDX-FileCopyrightText year range from 2019 ~ 2023 to 2019 - 2026 in header file
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/enablecontrol/enableutils.h

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

@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码变更的主要目的是将原本通过硬编码字符串前缀(wlanwlp)判断无线网卡的方式,替换为通过读取 Linux sysfs 虚拟文件系统(/sys/class/net/...)来判断。这是一个非常好的改进方向,因为基于 sysfs 的判断比基于命名规则的判断更加准确和健壮(例如,某些自定义命名或非标准命名的无线网卡也能被正确识别)。

以下是对本次代码变更的详细审查和改进意见:

1. 语法与逻辑

  • 路径遍历漏洞风险isWireless 函数接收 logicalName 并直接拼接到文件路径中。如果 logicalName 来自不受信任的输入(例如包含 ../../ 等路径穿越字符),可能会导致程序去检查非预期的目录。虽然在设备管理器中,网卡名通常由内核生成且较为可信,但从防御性编程的角度来看,应当对输入进行校验。
  • 逻辑简化if (condition) return true; else return false; 是冗余的写法,可以直接返回条件表达式的结果。

2. 代码质量

  • 冗余的条件判断:如上所述,可以直接 return wirelessInfo.exists() || phyInfo.exists();,这样代码更简洁易读。
  • SPDX 版权年份格式:版权声明从 2019 ~ 2023 修改为了 2019 - 2026。虽然这不是代码逻辑问题,但通常开源项目(如 FSF 或 SPDX 规范)建议使用连字符 - 表示范围,这里的修改是符合规范的。不过,提前将版权年份更新到 2026 年可能不太符合常规实践(通常是随当年发布自然更新),建议确认是否符合团队的规范。

3. 代码性能

  • 文件系统访问开销QFileInfo::exists() 默认会触发一次 stat 系统调用。在当前代码中,如果 wirelessInfo.exists()true,仍然会构造 phyInfo 对象(尽管由于短路求值不会调用其 exists())。由于 Linux 的 /sys/class/net/<iface>/wirelessphy80211 通常同时存在或同时不存在,只检查其中一个通常就足够了。如果为了严谨两者都查,建议利用逻辑或的短路特性,避免不必要的对象构造。

4. 代码安全

  • 输入验证缺失:如前所述,logicalName 应当被验证为合法的 Linux 网络接口名称,防止路径注入。Linux 网络接口名只允许包含字母、数字和下划线,且长度通常不超过 15 个字符。
  • 符号链接检查:在 /sys 文件系统中,wirelessphy80211 通常是指向实际设备的符号链接。QFileInfo::exists() 能够正确处理符号链接并跟随它,因此这部分是安全的。

改进后的代码建议

基于以上审查意见,建议对 isWireless 函数进行如下改进:

bool EnableUtils::isWireless(const QString &logicalName)
{
    // 安全校验:确保 logicalName 是合法的 Linux 网络接口名
    // 合法字符仅包含字母、数字和下划线,且不为空,长度不超过 15 (IFNAMSIZ - 1)
    static QRegularExpression validIfnameRegex("^[a-zA-Z0-9_]{1,15}$");
    if (!validIfnameRegex.match(logicalName).hasMatch()) {
        qWarning() << "Invalid logical name received:" << logicalName;
        return false;
    }

    // 检查 /sys/class/net/<iface>/wireless 是否存在
    // 在 Linux 中,只要存在 wireless 目录或 phy80211 链接,即可判定为无线网卡
    // 优先检查 wireless,利用短路求值避免不必要的 QFileInfo 构造和 stat 调用
    if (QFileInfo::exists(QString("/sys/class/net/%1/wireless").arg(logicalName))) {
        return true;
    }

    // 备用检查 phy80211
    return QFileInfo::exists(QString("/sys/class/net/%1/phy80211").arg(logicalName));
}

改进点说明:

  1. 防御性校验:增加了正则表达式校验 ^[a-zA-Z0-9_]{1,15}$,彻底杜绝了路径穿越的可能性,同时也过滤了空字符串等异常输入。
  2. 性能优化:直接使用 QFileInfo::exists(QString) 静态方法,省去了先构造 QFileInfo 对象再调用 exists() 的开销;利用 if 逻辑判断,如果 wireless 存在则直接返回,不再去检查 phy80211,减少了磁盘 I/O。
  3. 逻辑简化:消除了 if-else return true/false 的冗余结构。

这个改进后的版本在安全性、健壮性和性能上都有所提升,且更符合 C++ 和 Qt 的最佳实践。

Copy link
Copy Markdown

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

  • The isWireless helper can be simplified to a single return statement (e.g., return wirelessInfo.exists() || phyInfo.exists();) to avoid the explicit if/else and keep the code concise.
  • Consider using QFile::exists() directly instead of constructing QFileInfo objects in isWireless, since you only need to check existence and not other file metadata.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `isWireless` helper can be simplified to a single return statement (e.g., `return wirelessInfo.exists() || phyInfo.exists();`) to avoid the explicit `if/else` and keep the code concise.
- Consider using `QFile::exists()` directly instead of constructing `QFileInfo` objects in `isWireless`, since you only need to check existence and not other file metadata.

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.

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