Skip to content

Commit ce03692

Browse files
author
zorowk
committed
fix(auth): release daemon descriptors in session child
Close inherited daemon file descriptors in the forked session leader so old D-Bus connections cannot retain the display manager service names. Fail fast when D-Bus registration is unavailable and restore the original service start limit. Log: DDM 通过 fork 创建 session leader 时,子进程若继承 system D-Bus fd,旧 DDM 主进程退出后仍可能保留 org.deepin.DisplayManager 服务名,阻 止新 DDM 注册。session child 仅保留标准流和与父进程通信的 pipe,并关闭 其余 daemon fd;DDM 注册 D-Bus 服务或对象失败时立即退出,避免 systemd 显示 active 而 D-Bus 实际不可用。该修复不处理 systemd start-limit-hit; DDM 被限流停止时必须 reset-failed 后重新启动。 PMS: Influence: Affects session startup and DDM D-Bus ownership across daemon restarts.
1 parent 8487986 commit ce03692

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/daemon/Auth.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,54 @@
1717
#include <security/pam_appl.h>
1818
#include <signal.h>
1919
#include <unistd.h>
20+
#include <climits>
21+
#include <sys/syscall.h>
2022
#include <utmp.h>
2123
#include <utmpx.h>
2224

2325
namespace DDM {
2426

27+
#ifdef SYS_close_range
28+
bool tryCloseInheritedWithCloseRange(int preservedFd)
29+
{
30+
const unsigned int first = STDERR_FILENO + 1;
31+
const unsigned int last = UINT_MAX;
32+
33+
// No preserved FD in range: just close everything
34+
if (preservedFd <= static_cast<int>(first) || preservedFd > static_cast<int>(last))
35+
return syscall(SYS_close_range, first, last, 0) == 0;
36+
37+
// Close [first, preservedFd - 1]
38+
if (syscall(SYS_close_range,
39+
first,
40+
static_cast<unsigned int>(preservedFd - 1),
41+
0) == -1)
42+
return false;
43+
44+
// Close [preservedFd + 1, last]
45+
if (syscall(SYS_close_range,
46+
static_cast<unsigned int>(preservedFd + 1),
47+
last,
48+
0) == -1)
49+
return false;
50+
51+
return true;
52+
}
53+
#endif
54+
55+
void closeInheritedFileDescriptors(int preservedFd)
56+
{
57+
#ifdef SYS_close_range
58+
if (tryCloseInheritedWithCloseRange(preservedFd))
59+
return;
60+
#endif
61+
const long maxFd = sysconf(_SC_OPEN_MAX);
62+
for (int fd = STDERR_FILENO + 1; fd < maxFd; ++fd) {
63+
if (fd != preservedFd)
64+
close(fd);
65+
}
66+
}
67+
2568
///////////////////////////
2669
// utmp helper functions //
2770
///////////////////////////
@@ -255,6 +298,10 @@ namespace DDM {
255298
// Delete old signal handlers, in order to close old fds
256299
// which are shared with the parent process.
257300
delete daemonApp->signalHandler();
301+
// The session leader must not keep daemon-owned sockets alive.
302+
// In particular, an inherited system bus fd would retain DDM's
303+
// well-known names after the daemon exits.
304+
closeInheritedFileDescriptors(pipefd[1]);
258305

259306
// Restore default SIGINT and SIGTERM handlers. We need
260307
// the signal hander to terminate ourself, since we're

0 commit comments

Comments
 (0)