From bb1e6834e8a1cd6fd5b88b5a1962e9d813a080fc Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 20 Oct 2025 20:26:49 +0200 Subject: Correctly append or remove FD_CLOEXEC Rather than just setting the flags to 0 or FD_CLOEXEC, read the current value and append or remove as needed. --- apt-pkg/contrib/fileutl.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index fc4e047a5..69e6dc430 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -803,7 +803,8 @@ std::string flNormalize(std::string file) /*{{{*/ /* */ void SetCloseExec(int Fd,bool Close) { - if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0) + auto flags = fcntl(Fd, F_GETFD); + if (flags < 0 || fcntl(Fd, F_SETFD, Close ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC) != 0) { cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl; exit(100); @@ -922,7 +923,10 @@ pid_t ExecFork(std::set KeepFDs) signal(SIGTSTP,SIG_DFL); auto safeSetCloseExec = [](int fd, bool close) { - if (fcntl(fd, F_SETFD, close ? FD_CLOEXEC : 0) == 0 || errno == EBADF) + auto flags = fcntl(fd, F_GETFD); + if (flags < 0 && errno == EBADF) + return; + if (flags >= 0 && fcntl(fd, F_SETFD, close ? flags | FD_CLOEXEC : flags & ~FD_CLOEXEC) == 0) return; cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl; _Exit(100); -- cgit v1.2.3-70-g09d2