2014-08-20 20:45:58 +00:00
|
|
|
{- Posix lock files
|
|
|
|
-
|
2023-04-27 19:57:50 +00:00
|
|
|
- Copyright 2014-2023 Joey Hess <id@joeyh.name>
|
2014-08-20 20:45:58 +00:00
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Utility.LockFile.Posix (
|
2014-08-20 21:03:04 +00:00
|
|
|
LockHandle,
|
2014-08-20 20:45:58 +00:00
|
|
|
lockShared,
|
|
|
|
lockExclusive,
|
2015-10-08 17:40:23 +00:00
|
|
|
tryLockShared,
|
2014-08-20 22:56:25 +00:00
|
|
|
tryLockExclusive,
|
|
|
|
checkLocked,
|
2014-08-20 23:09:54 +00:00
|
|
|
getLockStatus,
|
2015-05-20 03:35:24 +00:00
|
|
|
LockStatus(..),
|
2014-08-20 22:56:25 +00:00
|
|
|
dropLock,
|
2015-05-12 23:39:28 +00:00
|
|
|
checkSaneLock,
|
2015-11-12 21:47:31 +00:00
|
|
|
LockRequest(..),
|
|
|
|
openLockFile,
|
2014-08-20 20:45:58 +00:00
|
|
|
) where
|
|
|
|
|
2014-08-20 21:03:04 +00:00
|
|
|
import Utility.Exception
|
2014-08-20 22:56:25 +00:00
|
|
|
import Utility.Applicative
|
2023-04-27 19:57:50 +00:00
|
|
|
import Utility.FileMode
|
2015-11-12 19:38:02 +00:00
|
|
|
import Utility.LockFile.LockStatus
|
2023-08-01 22:41:27 +00:00
|
|
|
import Utility.OpenFd
|
2014-08-20 21:03:04 +00:00
|
|
|
|
2014-08-20 20:45:58 +00:00
|
|
|
import System.IO
|
2020-10-29 14:33:12 +00:00
|
|
|
import System.Posix.Types
|
|
|
|
import System.Posix.IO.ByteString
|
|
|
|
import System.Posix.Files.ByteString
|
|
|
|
import System.FilePath.ByteString (RawFilePath)
|
2014-08-20 22:56:25 +00:00
|
|
|
import Data.Maybe
|
2014-08-20 20:45:58 +00:00
|
|
|
|
2020-10-29 14:33:12 +00:00
|
|
|
type LockFile = RawFilePath
|
2014-08-20 20:45:58 +00:00
|
|
|
|
|
|
|
newtype LockHandle = LockHandle Fd
|
|
|
|
|
|
|
|
-- Takes a shared lock, blocking until the lock is available.
|
2023-04-27 19:57:50 +00:00
|
|
|
lockShared :: Maybe ModeSetter -> LockFile -> IO LockHandle
|
2014-08-20 20:45:58 +00:00
|
|
|
lockShared = lock ReadLock
|
|
|
|
|
|
|
|
-- Takes an exclusive lock, blocking until the lock is available.
|
2023-04-27 19:57:50 +00:00
|
|
|
lockExclusive :: Maybe ModeSetter -> LockFile -> IO LockHandle
|
2014-08-20 20:45:58 +00:00
|
|
|
lockExclusive = lock WriteLock
|
|
|
|
|
2015-10-08 17:40:23 +00:00
|
|
|
-- Tries to take a shared lock, but does not block.
|
2023-04-27 19:57:50 +00:00
|
|
|
tryLockShared :: Maybe ModeSetter -> LockFile -> IO (Maybe LockHandle)
|
2015-10-08 17:40:23 +00:00
|
|
|
tryLockShared = tryLock ReadLock
|
|
|
|
|
2014-08-20 22:56:25 +00:00
|
|
|
-- Tries to take an exclusive lock, but does not block.
|
2023-04-27 19:57:50 +00:00
|
|
|
tryLockExclusive :: Maybe ModeSetter -> LockFile -> IO (Maybe LockHandle)
|
2015-10-08 17:40:23 +00:00
|
|
|
tryLockExclusive = tryLock WriteLock
|
2014-08-20 22:56:25 +00:00
|
|
|
|
|
|
|
-- Setting the FileMode allows creation of a new lock file.
|
|
|
|
-- If it's Nothing then this only succeeds when the lock file already exists.
|
2023-04-27 19:57:50 +00:00
|
|
|
lock :: LockRequest -> Maybe ModeSetter -> LockFile -> IO LockHandle
|
2014-08-20 20:45:58 +00:00
|
|
|
lock lockreq mode lockfile = do
|
2015-10-08 17:31:55 +00:00
|
|
|
l <- openLockFile lockreq mode lockfile
|
2014-08-20 20:45:58 +00:00
|
|
|
waitToSetLock l (lockreq, AbsoluteSeek, 0, 0)
|
|
|
|
return (LockHandle l)
|
|
|
|
|
2015-10-08 17:40:23 +00:00
|
|
|
-- Tries to take an lock, but does not block.
|
2023-04-27 19:57:50 +00:00
|
|
|
tryLock :: LockRequest -> Maybe ModeSetter -> LockFile -> IO (Maybe LockHandle)
|
2020-06-09 17:48:48 +00:00
|
|
|
tryLock lockreq mode lockfile = uninterruptibleMask_ $ do
|
2015-10-08 17:40:23 +00:00
|
|
|
l <- openLockFile lockreq mode lockfile
|
|
|
|
v <- tryIO $ setLock l (lockreq, AbsoluteSeek, 0, 0)
|
|
|
|
case v of
|
|
|
|
Left _ -> do
|
|
|
|
closeFd l
|
|
|
|
return Nothing
|
|
|
|
Right _ -> return $ Just $ LockHandle l
|
|
|
|
|
2014-08-20 21:03:04 +00:00
|
|
|
-- Close on exec flag is set so child processes do not inherit the lock.
|
2023-04-27 19:57:50 +00:00
|
|
|
openLockFile :: LockRequest -> Maybe ModeSetter -> LockFile -> IO Fd
|
2015-10-08 17:31:55 +00:00
|
|
|
openLockFile lockreq filemode lockfile = do
|
2023-04-27 19:57:50 +00:00
|
|
|
l <- applyModeSetter filemode lockfile $ \filemode' ->
|
2023-08-01 22:41:27 +00:00
|
|
|
openFdWithMode lockfile openfor filemode' defaultFileFlags
|
2014-08-20 20:45:58 +00:00
|
|
|
setFdOption l CloseOnExec True
|
|
|
|
return l
|
2015-10-08 17:31:55 +00:00
|
|
|
where
|
|
|
|
openfor = case lockreq of
|
|
|
|
ReadLock -> ReadOnly
|
|
|
|
_ -> ReadWrite
|
2014-08-20 20:45:58 +00:00
|
|
|
|
2014-08-20 23:09:54 +00:00
|
|
|
-- Returns Nothing when the file doesn't exist, for cases where
|
|
|
|
-- that is different from it not being locked.
|
2014-08-20 22:56:25 +00:00
|
|
|
checkLocked :: LockFile -> IO (Maybe Bool)
|
2014-08-20 23:09:54 +00:00
|
|
|
checkLocked = maybe Nothing (Just . isJust) <$$> getLockStatus'
|
|
|
|
|
2015-05-20 03:35:24 +00:00
|
|
|
getLockStatus :: LockFile -> IO LockStatus
|
|
|
|
getLockStatus lockfile = do
|
|
|
|
v <- getLockStatus' lockfile
|
|
|
|
return $ case v of
|
|
|
|
Nothing -> StatusNoLockFile
|
|
|
|
Just Nothing -> StatusUnLocked
|
|
|
|
Just (Just pid) -> StatusLockedBy pid
|
2014-08-20 23:09:54 +00:00
|
|
|
|
2015-05-18 18:16:49 +00:00
|
|
|
getLockStatus' :: LockFile -> IO (Maybe (Maybe ProcessID))
|
2020-06-05 19:46:01 +00:00
|
|
|
getLockStatus' lockfile = bracket open close go
|
2014-08-20 22:56:25 +00:00
|
|
|
where
|
2020-06-05 19:46:01 +00:00
|
|
|
open = catchMaybeIO $ openLockFile ReadLock Nothing lockfile
|
|
|
|
|
|
|
|
close (Just h) = closeFd h
|
|
|
|
close Nothing = return ()
|
|
|
|
|
2014-08-20 22:56:25 +00:00
|
|
|
go (Just h) = do
|
2015-05-18 18:16:49 +00:00
|
|
|
v <- getLock h (ReadLock, AbsoluteSeek, 0, 0)
|
|
|
|
return (Just (fmap fst v))
|
2020-06-05 19:46:01 +00:00
|
|
|
go Nothing = return Nothing
|
2014-08-20 22:56:25 +00:00
|
|
|
|
2014-08-20 20:45:58 +00:00
|
|
|
dropLock :: LockHandle -> IO ()
|
|
|
|
dropLock (LockHandle fd) = closeFd fd
|
2015-05-12 23:39:28 +00:00
|
|
|
|
|
|
|
-- Checks that the lock file still exists, and is the same file that was
|
|
|
|
-- locked to get the LockHandle.
|
|
|
|
--
|
|
|
|
-- This check is useful if the lock file might get deleted by something
|
|
|
|
-- else.
|
|
|
|
checkSaneLock :: LockFile -> LockHandle -> IO Bool
|
|
|
|
checkSaneLock lockfile (LockHandle fd) =
|
|
|
|
go =<< catchMaybeIO (getFileStatus lockfile)
|
|
|
|
where
|
|
|
|
go Nothing = return False
|
|
|
|
go (Just st) = do
|
|
|
|
fdst <- getFdStatus fd
|
|
|
|
return $ deviceID fdst == deviceID st && fileID fdst == fileID st
|