git-annex/Utility/OpenFd.hs
Joey Hess 033e4b086f
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
2025-09-04 16:01:41 -04:00

33 lines
798 B
Haskell

{- openFd wrapper to support old versions of unix package.
-
- Copyright 2023-2025 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
module Utility.OpenFd where
#ifndef mingw32_HOST_OS
import System.Posix.IO.ByteString
import System.Posix.Types
import Utility.RawFilePath
newtype CloseOnExecFlag = CloseOnExecFlag Bool
openFdWithMode :: RawFilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> CloseOnExecFlag -> IO Fd
openFdWithMode f openmode filemode flags (CloseOnExecFlag closeonexec) = do
#if MIN_VERSION_unix(2,8,0)
openFd f openmode (flags { creat = filemode, cloexec = closeonexec })
#else
fd <- openFd f openmode filemode flags
when closeonexec $
setFdOption fd CloseOnExec True
return fd
#endif
#endif