audit all openFd and dupping for close-on-exec

Made all uses of openFd and dup set the close-on-exec flag, with a few
exceptions when starting a git-annex daemon.

Made openFdWithMode be used everywhere, rather than openFd.
Adding a new parameter to it ensures I checked everything.
And will help to make sure this gets considered in the future when
opening fds.

In lockPidFile, the only thing that keeps the pid file locked, once
daemonize re-runs the command in a new session, is that the fd is
inherited.

In Utility.LogFile.redir, the new fd it dups to does not have the
close-on-exec flag set, because this is used to set up the stdout and
stderr fds, which need to be inherited by child processes.

Same in Assistant.startDaemon where the browser gets started with the
original stdout and stderr.

This does nothing about uses of openFile and similar!

Sponsored-By: mycroft
This commit is contained in:
Joey Hess 2025-09-04 15:45:28 -04:00
commit 033e4b086f
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
11 changed files with 51 additions and 34 deletions

View file

@ -53,12 +53,7 @@ openLock' lck = do
#ifndef mingw32_HOST_OS
-- On unix, git simply uses O_EXCL
h <- openFdWithMode (fromOsPath lck) ReadWrite (Just 0O666)
#if MIN_VERSION_unix(2,8,0)
(defaultFileFlags { exclusive = True, cloexec = True })
#else
(defaultFileFlags { exclusive = True })
setFdOption h CloseOnExec True
#endif
(defaultFileFlags { exclusive = True }) (CloseOnExecFlag True)
#else
-- It's not entirely clear how git manages locking on Windows,
-- since it's buried in the portability layer, and different