2015-11-12 19:38:02 +00:00
|
|
|
{- pid-based lock files
|
|
|
|
-
|
|
|
|
- Copyright 2015 Joey Hess <id@joeyh.name>
|
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.LockFile.PidLock (
|
|
|
|
LockHandle,
|
|
|
|
tryLock,
|
|
|
|
waitLock,
|
2015-11-12 20:31:34 +00:00
|
|
|
dropLock,
|
2015-11-12 19:38:02 +00:00
|
|
|
LockStatus(..),
|
|
|
|
getLockStatus,
|
2015-11-12 20:31:34 +00:00
|
|
|
checkLocked,
|
|
|
|
checkSaneLock,
|
2015-11-12 19:38:02 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Utility.PartialPrelude
|
|
|
|
import Utility.Exception
|
|
|
|
import Utility.Applicative
|
|
|
|
import Utility.Directory
|
|
|
|
import Utility.Monad
|
|
|
|
import Utility.Path
|
|
|
|
import Utility.FileMode
|
|
|
|
import Utility.LockFile.LockStatus
|
2015-11-12 21:12:54 +00:00
|
|
|
import Utility.ThreadScheduler
|
2017-05-15 22:10:13 +00:00
|
|
|
import Utility.Hash
|
|
|
|
import Utility.FileSystemEncoding
|
2015-11-12 19:38:02 +00:00
|
|
|
import qualified Utility.LockFile.Posix as Posix
|
|
|
|
|
|
|
|
import System.IO
|
|
|
|
import System.Posix
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.List
|
|
|
|
import Network.BSD
|
|
|
|
import System.FilePath
|
2015-12-19 21:42:45 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
2015-11-12 19:38:02 +00:00
|
|
|
|
|
|
|
type LockFile = FilePath
|
|
|
|
|
2015-11-16 15:36:11 +00:00
|
|
|
data LockHandle = LockHandle LockFile FileStatus SideLockHandle
|
2015-11-13 18:44:53 +00:00
|
|
|
|
2015-11-16 15:36:11 +00:00
|
|
|
type SideLockHandle = Maybe (LockFile, Posix.LockHandle)
|
2015-11-12 19:38:02 +00:00
|
|
|
|
|
|
|
data PidLock = PidLock
|
|
|
|
{ lockingPid :: ProcessID
|
|
|
|
, lockingHost :: HostName
|
|
|
|
}
|
|
|
|
deriving (Eq, Read, Show)
|
|
|
|
|
|
|
|
mkPidLock :: IO PidLock
|
|
|
|
mkPidLock = PidLock
|
|
|
|
<$> getProcessID
|
|
|
|
<*> getHostName
|
|
|
|
|
|
|
|
readPidLock :: LockFile -> IO (Maybe PidLock)
|
|
|
|
readPidLock lockfile = (readish =<<) <$> catchMaybeIO (readFile lockfile)
|
|
|
|
|
|
|
|
-- To avoid races when taking over a stale pid lock, a side lock is used.
|
2015-11-16 15:36:11 +00:00
|
|
|
-- This is a regular posix exclusive lock.
|
2015-11-13 18:44:53 +00:00
|
|
|
trySideLock :: LockFile -> (SideLockHandle -> IO a) -> IO a
|
2015-11-12 19:38:02 +00:00
|
|
|
trySideLock lockfile a = do
|
2015-11-13 18:04:29 +00:00
|
|
|
sidelock <- sideLockFile lockfile
|
2015-11-12 19:38:02 +00:00
|
|
|
mlck <- catchDefaultIO Nothing $
|
|
|
|
withUmask nullFileMode $
|
|
|
|
Posix.tryLockExclusive (Just mode) sidelock
|
2015-11-16 15:36:11 +00:00
|
|
|
-- Check the lock we just took, in case we opened a side lock file
|
|
|
|
-- belonging to another process that will have since deleted it.
|
|
|
|
case mlck of
|
|
|
|
Nothing -> a Nothing
|
|
|
|
Just lck -> ifM (Posix.checkSaneLock sidelock lck)
|
|
|
|
( a (Just (sidelock, lck))
|
|
|
|
, a Nothing
|
|
|
|
)
|
2015-11-12 19:38:02 +00:00
|
|
|
where
|
2015-11-13 18:49:30 +00:00
|
|
|
-- Let all users write to the lock file in /dev/shm or /tmp,
|
2015-11-12 19:38:02 +00:00
|
|
|
-- so that other users can reuse it to take the lock.
|
2015-11-13 18:49:30 +00:00
|
|
|
-- Since /dev/shm and /tmp are sticky dirs, a user cannot
|
|
|
|
-- delete another user's lock file there, so could not
|
|
|
|
-- delete a stale lock.
|
2015-11-12 19:38:02 +00:00
|
|
|
mode = combineModes (readModes ++ writeModes)
|
|
|
|
|
2015-11-16 15:36:11 +00:00
|
|
|
dropSideLock :: SideLockHandle -> IO ()
|
|
|
|
dropSideLock Nothing = return ()
|
|
|
|
dropSideLock (Just (f, h)) = do
|
|
|
|
-- Delete the file first, to ensure that any process that is trying
|
|
|
|
-- to take the side lock will only succeed once the file is
|
|
|
|
-- deleted, and so will be able to immediately see that it's taken
|
|
|
|
-- a stale lock.
|
|
|
|
_ <- tryIO $ removeFile f
|
|
|
|
Posix.dropLock h
|
|
|
|
|
|
|
|
-- The side lock is put in /dev/shm. This will work on most any
|
|
|
|
-- Linux system, even if its whole root filesystem doesn't support posix
|
|
|
|
-- locks. /tmp is used as a fallback.
|
2015-11-13 18:04:29 +00:00
|
|
|
sideLockFile :: LockFile -> IO LockFile
|
|
|
|
sideLockFile lockfile = do
|
|
|
|
f <- absPath lockfile
|
|
|
|
let base = intercalate "_" (splitDirectories (makeRelative "/" f))
|
|
|
|
let shortbase = reverse $ take 32 $ reverse base
|
2017-05-15 22:10:13 +00:00
|
|
|
let md5sum = if base == shortbase
|
|
|
|
then ""
|
|
|
|
else show (md5 (encodeBS base))
|
2015-11-13 18:49:30 +00:00
|
|
|
dir <- ifM (doesDirectoryExist "/dev/shm")
|
|
|
|
( return "/dev/shm"
|
|
|
|
, return "/tmp"
|
|
|
|
)
|
|
|
|
return $ dir </> md5sum ++ shortbase ++ ".lck"
|
2015-11-13 18:04:29 +00:00
|
|
|
|
2015-11-12 19:38:02 +00:00
|
|
|
-- | Tries to take a lock; does not block when the lock is already held.
|
|
|
|
--
|
|
|
|
-- Note that stale locks are automatically detected and broken.
|
|
|
|
-- However, if the lock file is on a networked file system, and was
|
|
|
|
-- created on a different host than the current host (determined by hostname),
|
|
|
|
-- this can't be done and stale locks may persist.
|
|
|
|
tryLock :: LockFile -> IO (Maybe LockHandle)
|
|
|
|
tryLock lockfile = trySideLock lockfile $ \sidelock -> do
|
2015-11-16 19:25:04 +00:00
|
|
|
lockfile' <- absPath lockfile
|
|
|
|
(tmp, h) <- openTempFile (takeDirectory lockfile') "locktmp"
|
2015-11-12 19:38:02 +00:00
|
|
|
setFileMode tmp (combineModes readModes)
|
|
|
|
hPutStr h . show =<< mkPidLock
|
2015-11-13 19:20:52 +00:00
|
|
|
hClose h
|
2017-06-06 18:21:43 +00:00
|
|
|
let failedlock st = do
|
2015-11-16 15:36:11 +00:00
|
|
|
dropLock $ LockHandle tmp st sidelock
|
2017-06-06 18:23:33 +00:00
|
|
|
nukeFile tmp
|
2015-11-12 19:38:02 +00:00
|
|
|
return Nothing
|
2017-06-06 18:21:43 +00:00
|
|
|
let tooklock st = return $ Just $ LockHandle lockfile' st sidelock
|
2015-11-16 19:25:04 +00:00
|
|
|
ifM (linkToLock sidelock tmp lockfile')
|
2015-11-13 19:43:09 +00:00
|
|
|
( do
|
2015-11-13 16:52:24 +00:00
|
|
|
nukeFile tmp
|
2017-06-06 18:21:43 +00:00
|
|
|
-- May not have made a hard link, so stat
|
|
|
|
-- the lockfile
|
|
|
|
lckst <- getFileStatus lockfile'
|
|
|
|
tooklock lckst
|
2015-11-13 19:43:09 +00:00
|
|
|
, do
|
2015-11-16 19:25:04 +00:00
|
|
|
v <- readPidLock lockfile'
|
2015-11-12 19:38:02 +00:00
|
|
|
hn <- getHostName
|
2017-06-06 18:21:43 +00:00
|
|
|
tmpst <- getFileStatus tmp
|
2015-11-12 19:38:02 +00:00
|
|
|
case v of
|
|
|
|
Just pl | isJust sidelock && hn == lockingHost pl -> do
|
|
|
|
-- Since we have the sidelock,
|
|
|
|
-- and are on the same host that
|
2015-11-12 21:12:54 +00:00
|
|
|
-- the pidlock was taken on,
|
2015-11-12 19:38:02 +00:00
|
|
|
-- we know that the pidlock is
|
|
|
|
-- stale, and can take it over.
|
2015-11-16 19:25:04 +00:00
|
|
|
rename tmp lockfile'
|
2017-06-06 18:21:43 +00:00
|
|
|
tooklock tmpst
|
|
|
|
_ -> failedlock tmpst
|
2015-11-13 19:43:09 +00:00
|
|
|
)
|
2015-11-12 19:38:02 +00:00
|
|
|
|
2015-11-13 18:44:53 +00:00
|
|
|
-- Linux's open(2) man page recommends linking a pid lock into place,
|
2015-11-13 17:22:45 +00:00
|
|
|
-- as the most portable atomic operation that will fail if
|
2015-11-13 18:44:53 +00:00
|
|
|
-- it already exists.
|
|
|
|
--
|
|
|
|
-- open(2) suggests that link can sometimes appear to fail
|
|
|
|
-- on NFS but have actually succeeded, and the way to find out is to stat
|
|
|
|
-- the file and check its link count etc.
|
2017-02-10 19:21:58 +00:00
|
|
|
--
|
|
|
|
-- However, not all filesystems support hard links. So, first probe
|
|
|
|
-- to see if they are supported. If not, use open with O_EXCL.
|
2015-11-13 19:43:09 +00:00
|
|
|
linkToLock :: SideLockHandle -> FilePath -> FilePath -> IO Bool
|
|
|
|
linkToLock Nothing _ _ = return False
|
2015-11-13 18:44:53 +00:00
|
|
|
linkToLock (Just _) src dest = do
|
2017-02-10 19:21:58 +00:00
|
|
|
let probe = src ++ ".lnk"
|
|
|
|
v <- tryIO $ createLink src probe
|
|
|
|
nukeFile probe
|
|
|
|
case v of
|
|
|
|
Right _ -> do
|
|
|
|
_ <- tryIO $ createLink src dest
|
|
|
|
ifM (catchBoolIO checklinked)
|
|
|
|
( catchBoolIO $ not <$> checkInsaneLustre dest
|
|
|
|
, return False
|
|
|
|
)
|
|
|
|
Left _ -> catchBoolIO $ do
|
|
|
|
fd <- openFd dest WriteOnly
|
|
|
|
(Just $ combineModes readModes)
|
|
|
|
(defaultFileFlags {exclusive = True})
|
|
|
|
h <- fdToHandle fd
|
|
|
|
readFile src >>= hPutStr h
|
|
|
|
hClose h
|
|
|
|
return True
|
2015-11-13 17:22:45 +00:00
|
|
|
where
|
2015-11-13 19:51:45 +00:00
|
|
|
checklinked = do
|
2015-11-13 17:22:45 +00:00
|
|
|
x <- getSymbolicLinkStatus src
|
|
|
|
y <- getSymbolicLinkStatus dest
|
|
|
|
return $ and
|
|
|
|
[ deviceID x == deviceID y
|
|
|
|
, fileID x == fileID y
|
|
|
|
, fileMode x == fileMode y
|
|
|
|
, fileOwner x == fileOwner y
|
|
|
|
, fileGroup x == fileGroup y
|
|
|
|
, fileSize x == fileSize y
|
|
|
|
, modificationTime x == modificationTime y
|
|
|
|
, isRegularFile x == isRegularFile y
|
2015-11-13 18:44:53 +00:00
|
|
|
, linkCount x == linkCount y
|
|
|
|
, linkCount x == 2
|
2015-11-13 17:22:45 +00:00
|
|
|
]
|
|
|
|
|
2015-11-13 20:13:43 +00:00
|
|
|
-- On a Lustre filesystem, link has been observed to incorrectly *succeed*,
|
|
|
|
-- despite the dest already existing. A subsequent stat of the dest
|
|
|
|
-- looked like it had been replaced with the src. The process proceeded to
|
|
|
|
-- run and then deleted the dest, and after the process was done, the
|
|
|
|
-- original file was observed to still be in place.
|
|
|
|
--
|
|
|
|
-- We can detect this insanity by getting the directory contents after
|
|
|
|
-- making the link, and checking to see if 2 copies of the dest file,
|
|
|
|
-- with the SAME FILENAME exist.
|
|
|
|
checkInsaneLustre :: FilePath -> IO Bool
|
|
|
|
checkInsaneLustre dest = do
|
|
|
|
fs <- dirContents (takeDirectory dest)
|
|
|
|
case length (filter (== dest) fs) of
|
|
|
|
1 -> return False -- whew!
|
|
|
|
0 -> return True -- wtf?
|
|
|
|
_ -> do
|
|
|
|
-- Try to clean up the extra copy we made
|
|
|
|
-- that has the same name. Egads.
|
2015-11-16 15:36:11 +00:00
|
|
|
_ <- tryIO $ removeFile dest
|
2015-11-13 20:13:43 +00:00
|
|
|
return True
|
|
|
|
|
2015-11-12 19:38:02 +00:00
|
|
|
-- | Waits as necessary to take a lock.
|
|
|
|
--
|
|
|
|
-- Uses a 1 second wait-loop.
|
|
|
|
--
|
2016-03-01 16:24:22 +00:00
|
|
|
-- May wait until timeout if the lock file is stale and is on a network file
|
2015-11-12 19:38:02 +00:00
|
|
|
-- system, or on a system where the side lock cannot be taken.
|
2015-11-12 21:12:54 +00:00
|
|
|
waitLock :: Seconds -> LockFile -> IO LockHandle
|
|
|
|
waitLock (Seconds timeout) lockfile = go timeout
|
2015-11-12 19:38:02 +00:00
|
|
|
where
|
2015-11-12 21:12:54 +00:00
|
|
|
go n
|
|
|
|
| n > 0 = maybe (threadDelaySeconds (Seconds 1) >> go (pred n)) return
|
|
|
|
=<< tryLock lockfile
|
|
|
|
| otherwise = do
|
|
|
|
hPutStrLn stderr $ show timeout ++ " second timeout exceeded while waiting for pid lock file " ++ lockfile
|
2016-11-16 01:29:54 +00:00
|
|
|
giveup $ "Gave up waiting for possibly stale pid lock file " ++ lockfile
|
2015-11-12 19:38:02 +00:00
|
|
|
|
|
|
|
dropLock :: LockHandle -> IO ()
|
2015-11-13 19:43:09 +00:00
|
|
|
dropLock (LockHandle lockfile _ sidelock) = do
|
2015-11-13 16:36:37 +00:00
|
|
|
-- Drop side lock first, at which point the pid lock will be
|
|
|
|
-- considered stale.
|
2015-11-16 15:36:11 +00:00
|
|
|
dropSideLock sidelock
|
2015-11-12 19:38:02 +00:00
|
|
|
nukeFile lockfile
|
2015-11-12 20:31:34 +00:00
|
|
|
|
|
|
|
getLockStatus :: LockFile -> IO LockStatus
|
|
|
|
getLockStatus = maybe StatusUnLocked (StatusLockedBy . lockingPid) <$$> readPidLock
|
|
|
|
|
|
|
|
checkLocked :: LockFile -> IO (Maybe Bool)
|
|
|
|
checkLocked lockfile = conv <$> getLockStatus lockfile
|
|
|
|
where
|
|
|
|
conv (StatusLockedBy _) = Just True
|
|
|
|
conv _ = Just False
|
|
|
|
|
|
|
|
-- Checks that the lock file still exists, and is the same file that was
|
|
|
|
-- locked to get the LockHandle.
|
|
|
|
checkSaneLock :: LockFile -> LockHandle -> IO Bool
|
2015-11-16 19:37:27 +00:00
|
|
|
checkSaneLock lockfile (LockHandle _ st _) =
|
2015-11-12 20:31:34 +00:00
|
|
|
go =<< catchMaybeIO (getFileStatus lockfile)
|
|
|
|
where
|
|
|
|
go Nothing = return False
|
2015-11-16 19:25:04 +00:00
|
|
|
go (Just st') = return $
|
|
|
|
deviceID st == deviceID st' && fileID st == fileID st'
|