summaryrefslogtreecommitdiff
path: root/Annex
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2014-08-20 11:25:07 -0400
committerGravatar Joey Hess <joey@kitenet.net>2014-08-20 11:37:02 -0400
commitb44e74ace2d74954c503930b9edb75d0aa9d7b52 (patch)
treef07fa949063d3e93987dad89e3348e6d8b692b76 /Annex
parent5fda2e3cb3f93771fb70a3ccbf88eeaf4e7f924a (diff)
Ensure that all lock fds are close-on-exec, fixing various problems with them being inherited by child processes such as git commands.
(With the exception of daemon pid locking.) This fixes at part of #758630. I reproduced the assistant locking eg, a removable drive's annex journal lock file and forking a long-running git-cat-file process that inherited that lock. This did not affect Windows. Considered doing a portable Utility.LockFile layer, but git-annex uses posix locks in several special ways that have no direct Windows equivilant, and it seems like it would mostly be a complication. This commit was sponsored by Protonet.
Diffstat (limited to 'Annex')
-rw-r--r--Annex/Content.hs4
-rw-r--r--Annex/LockFile.hs9
-rw-r--r--Annex/Ssh.hs1
-rw-r--r--Annex/Transfer.hs1
4 files changed, 13 insertions, 2 deletions
diff --git a/Annex/Content.hs b/Annex/Content.hs
index b51e15827..2a05c2dac 100644
--- a/Annex/Content.hs
+++ b/Annex/Content.hs
@@ -192,7 +192,9 @@ lockContent key a = do
v <- tryIO $ setLock fd (WriteLock, AbsoluteSeek, 0, 0)
case v of
Left _ -> alreadylocked
- Right _ -> return $ Just fd
+ Right _ -> do
+ setFdOption fd CloseOnExec True
+ return $ Just fd
unlock mlockfile mfd = do
maybe noop cleanuplockfile mlockfile
liftIO $ maybe noop closeFd mfd
diff --git a/Annex/LockFile.hs b/Annex/LockFile.hs
index dc4f82f98..7a5c4b0e1 100644
--- a/Annex/LockFile.hs
+++ b/Annex/LockFile.hs
@@ -37,6 +37,7 @@ lockFileShared file = go =<< fromLockPool file
mode <- annexFileMode
lockhandle <- liftIO $ noUmask mode $
openFd file ReadOnly (Just mode) defaultFileFlags
+ setFdOption lockhandle CloseOnExec True
liftIO $ waitToSetLock lockhandle (ReadLock, AbsoluteSeek, 0, 0)
#else
lockhandle <- liftIO $ waitToLock $ lockShared file
@@ -70,13 +71,19 @@ changeLockPool a = do
withExclusiveLock :: (Git.Repo -> FilePath) -> Annex a -> Annex a
withExclusiveLock getlockfile a = do
lockfile <- fromRepo getlockfile
+ liftIO $ hPutStrLn stderr (show ("locking", lockfile))
+ liftIO $ hFlush stderr
createAnnexDirectory $ takeDirectory lockfile
mode <- annexFileMode
- bracketIO (lock lockfile mode) unlock (const a)
+ r <- bracketIO (lock lockfile mode) unlock (const a)
+ liftIO $ hPutStrLn stderr (show ("unlocked", lockfile))
+ liftIO $ hFlush stderr
+ return r
where
#ifndef mingw32_HOST_OS
lock lockfile mode = do
l <- noUmask mode $ createFile lockfile mode
+ setFdOption l CloseOnExec True
waitToSetLock l (WriteLock, AbsoluteSeek, 0, 0)
return l
unlock = closeFd
diff --git a/Annex/Ssh.hs b/Annex/Ssh.hs
index 246ac338d..238af0588 100644
--- a/Annex/Ssh.hs
+++ b/Annex/Ssh.hs
@@ -153,6 +153,7 @@ sshCleanup = mapM_ cleanup =<< enumSocketFiles
mode <- annexFileMode
fd <- liftIO $ noUmask mode $
openFd lockfile ReadWrite (Just mode) defaultFileFlags
+ setFdOption fd CloseOnExec True
v <- liftIO $ tryIO $
setLock fd (WriteLock, AbsoluteSeek, 0, 0)
case v of
diff --git a/Annex/Transfer.hs b/Annex/Transfer.hs
index 5e98a87d9..ec17609ea 100644
--- a/Annex/Transfer.hs
+++ b/Annex/Transfer.hs
@@ -81,6 +81,7 @@ runTransfer' ignorelock t file shouldretry a = do
case mfd of
Nothing -> return (Nothing, False)
Just fd -> do
+ setFdOption fd CloseOnExec True
locked <- catchMaybeIO $
setLock fd (WriteLock, AbsoluteSeek, 0, 0)
if isNothing locked