Skip to content
Closed
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
18 changes: 15 additions & 3 deletions src/dbus/applicationmanager1service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,19 @@ QDBusObjectPath ApplicationManager1Service::executeCommand(const QString &progra
// 1. Property: Description
properties.append({"Description", QDBusVariant(QString("Run: %1").arg(program))});

// 2. Property: ExecStart (Type: a(sasb))
// 2. Property: Type (Type: s)
// 脚本中可能调用会 fork 的子进程(如 deepin-security-loader-exec),导致主进程提前退出,
// systemd 在 Type=simple 下会误判服务结束并杀掉子进程,因此脚本使用 Type=forking
QFile programFile(program);
if (programFile.open(QIODevice::ReadOnly)) {
if (programFile.readLine().startsWith("#!")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

性能/内存隐患 (readLine 处理二进制文件):
如果传入的 program 是一个几十兆的 ELF 二进制文件(而不是文本脚本),该文件内部可能很大一段都没有换行符 \n。调用 QFile::readLine() 会让 Qt 持续将二进制数据读入内存,直到遇到偶然的换行符或达到最大限制。这不仅拖慢了启动速度,还白白浪费了内存。

建议修复: 检查 Shebang 只需要读前 2 个字节。应改为:
if (programFile.read(2) == "#!")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

用peek更合适吧

properties.append({"Type", QDBusVariant(QStringLiteral("forking"))});
qInfo() << "Detected script, using Type=forking for systemd service";
}
programFile.close();
}

// 3. Property: ExecStart (Type: a(sasb))
// Systemd 要求 ExecStart 是一个结构体数组,因为一个服务可以有多个 ExecStart 命令
SystemdExecCommand execCmd;
execCmd.path = program; // 二进制路径
Expand All @@ -1007,10 +1019,10 @@ QDBusObjectPath ApplicationManager1Service::executeCommand(const QString &progra
execStartList << execCmd;
properties.append({"ExecStart", QDBusVariant(QVariant::fromValue(execStartList))});

// 3. Property: Environment (Type: as)
// 4. Property: Environment (Type: as)
properties.append({"Environment", QDBusVariant(environment)});

// 4. Property: WorkingDirectory (Type: s)
// 5. Property: WorkingDirectory (Type: s)
if (!workdir.isEmpty()) {
properties.append({"WorkingDirectory", QDBusVariant(workdir)});
}
Expand Down
Loading