Skip to content

fix: detect scripts and set Type=forking for systemd services#372

Closed
xujin177 wants to merge 1 commit into
linuxdeepin:masterfrom
xujin177:fix/cmd-launch
Closed

fix: detect scripts and set Type=forking for systemd services#372
xujin177 wants to merge 1 commit into
linuxdeepin:masterfrom
xujin177:fix/cmd-launch

Conversation

@xujin177

Copy link
Copy Markdown

When a program is a script that forks child processes (e.g., deepin- security-loader-exec), systemd under Type=simple may incorrectly terminate the service and kill child processes. By detecting script files and setting Type=forking, we ensure the service manager waits for the forking process to complete properly.

Log: Fixed script execution handling with proper systemd service type

Influence:

  1. Test executing script files that fork child processes
  2. Verify that scripts with shebang (#!) are correctly identified
  3. Ensure non-script executables still use default Type=simple
  4. Check that child processes survive parent script exit
  5. Validate systemd service lifecycle with forking scripts
  6. Test backward compatibility for existing binary executables

fix: 检测脚本并为 systemd 服务设置 Type=forking

当程序是一个会 fork 子进程的脚本(如 deepin-security-loader-exec)时,使 用 Type=simple 的 systemd 可能错误地终止服务并杀掉子进程。通过检测脚本文
件并设置 Type=forking,确保服务管理器正确等待 fork 进程完成。

Log: 修复了脚本执行时 systemd 服务类型的处理

Influence:

  1. 测试执行会 fork 子进程的脚本文件
  2. 验证带 shebang (#!) 的脚本能被正确识别
  3. 确保非脚本可执行文件仍使用默认的 Type=simple
  4. 检查子进程在父脚本退出后是否正常存活
  5. 验证 systemd 服务生命周期对 fork 脚本的处理
  6. 测试对现有二进制可执行文件的向后兼容性

PMS: BUG-366983

@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.

Sorry @xujin177, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

Hi @xujin177. Thanks for your PR. 😃

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: xujin177

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

Hi @xujin177. 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.

Comment thread src/dbus/applicationmanager1service.cpp Outdated
When a program is a script that forks child processes (e.g., deepin-
security-loader-exec), systemd under Type=simple may incorrectly
terminate the service and kill child processes. By detecting script
files and setting Type=forking, we ensure the service manager waits for
the forking process to complete properly.

Log: Fixed script execution handling with proper systemd service type

Influence:
1. Test executing script files that fork child processes
2. Verify that scripts with shebang (#!) are correctly identified
3. Ensure non-script executables still use default Type=simple
4. Check that child processes survive parent script exit
5. Validate systemd service lifecycle with forking scripts
6. Test backward compatibility for existing binary executables

fix: 检测脚本并为 systemd 服务设置 Type=forking

当程序是一个会 fork 子进程的脚本(如 deepin-security-loader-exec)时,使用 Type=simple 的 systemd 可能错误地终止服务并杀掉子进程。通过检测脚本文件并设置 Type=forking,确保服务管理器正确等待 fork 进程完成。

Log: 修复了脚本执行时 systemd 服务类型的处理

Influence:
1. 测试执行会 fork 子进程的脚本文件
2. 验证带 shebang (#!) 的脚本能被正确识别
3. 确保非脚本可执行文件仍使用默认的 Type=simple
4. 检查子进程在父脚本退出后是否正常存活
5. 验证 systemd 服务生命周期对 fork 脚本的处理
6. 测试对现有二进制可执行文件的向后兼容性

PMS: BUG-366983
// 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更合适吧

@xujin177 xujin177 closed this Jun 30, 2026
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.

4 participants