2014-08-20 20:45:58 +00:00
|
|
|
{- Posix lock files
|
|
|
|
-
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- 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,
|
2014-08-20 22:56:25 +00:00
|
|
|
tryLockExclusive,
|
2014-08-20 20:45:58 +00:00
|
|
|
createLockFile,
|
2014-08-20 21:03:04 +00:00
|
|
|
openExistingLockFile,
|
2014-08-20 22:56:25 +00:00
|
|
|
isLocked,
|
|
|
|
checkLocked,
|
2014-08-20 23:09:54 +00:00
|
|
|
getLockStatus,
|
2014-08-20 22:56:25 +00:00
|
|
|
dropLock,
|
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
|
2014-08-20 21:03:04 +00:00
|
|
|
|
2014-08-20 20:45:58 +00:00
|
|
|
import System.IO
|
|
|
|
import System.Posix
|
2014-08-20 22:56:25 +00:00
|
|
|
import Data.Maybe
|
2014-08-20 20:45:58 +00:00
|
|
|
|
|
|
|
type LockFile = FilePath
|
|
|
|
|
|
|
|
newtype LockHandle = LockHandle Fd
|
|
|
|
|
|
|
|
-- Takes a shared lock, blocking until the lock is available.
|
|
|
|
lockShared :: Maybe FileMode -> LockFile -> IO LockHandle
|
|
|
|
lockShared = lock ReadLock
|
|
|
|
|
|
|
|
-- Takes an exclusive lock, blocking until the lock is available.
|
|
|
|
lockExclusive :: Maybe FileMode -> LockFile -> IO LockHandle
|
|
|
|
lockExclusive = lock WriteLock
|
|
|
|
|
2014-08-20 22:56:25 +00:00
|
|
|
-- Tries to take an exclusive lock, but does not block.
|
|
|
|
tryLockExclusive :: Maybe FileMode -> LockFile -> IO (Maybe LockHandle)
|
|
|
|
tryLockExclusive mode lockfile = do
|
|
|
|
l <- openLockFile mode lockfile
|
|
|
|
v <- tryIO $ setLock l (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
case v of
|
|
|
|
Left _ -> do
|
|
|
|
closeFd l
|
|
|
|
return Nothing
|
|
|
|
Right _ -> return $ Just $ LockHandle l
|
|
|
|
|
|
|
|
-- Setting the FileMode allows creation of a new lock file.
|
|
|
|
-- If it's Nothing then this only succeeds when the lock file already exists.
|
2014-08-20 20:45:58 +00:00
|
|
|
lock :: LockRequest -> Maybe FileMode -> LockFile -> IO LockHandle
|
|
|
|
lock lockreq mode lockfile = do
|
2014-08-20 22:56:25 +00:00
|
|
|
l <- openLockFile mode lockfile
|
2014-08-20 20:45:58 +00:00
|
|
|
waitToSetLock l (lockreq, AbsoluteSeek, 0, 0)
|
|
|
|
return (LockHandle l)
|
|
|
|
|
2014-08-20 21:03:04 +00:00
|
|
|
-- Create and opens lock file; does not lock it.
|
2014-08-20 22:56:25 +00:00
|
|
|
createLockFile :: FileMode -> LockFile -> IO Fd
|
|
|
|
createLockFile filemode = openLockFile (Just filemode)
|
2014-08-20 21:03:04 +00:00
|
|
|
|
2014-08-20 22:56:25 +00:00
|
|
|
-- Opens an existing lock file; does not lock it, and if it does not exist,
|
|
|
|
-- returns Nothing.
|
2014-08-20 21:03:04 +00:00
|
|
|
openExistingLockFile :: LockFile -> IO (Maybe Fd)
|
2014-08-20 22:56:25 +00:00
|
|
|
openExistingLockFile = catchMaybeIO . openLockFile Nothing
|
2014-08-20 21:03:04 +00:00
|
|
|
|
|
|
|
-- Close on exec flag is set so child processes do not inherit the lock.
|
2014-08-20 22:56:25 +00:00
|
|
|
openLockFile :: Maybe FileMode -> LockFile -> IO Fd
|
|
|
|
openLockFile filemode lockfile = do
|
|
|
|
l <- openFd lockfile ReadWrite filemode defaultFileFlags
|
2014-08-20 20:45:58 +00:00
|
|
|
setFdOption l CloseOnExec True
|
|
|
|
return l
|
|
|
|
|
2014-08-20 22:56:25 +00:00
|
|
|
-- Check if a file is locked, either exclusively, or with shared lock.
|
|
|
|
-- When the file doesn't exist, it's considered not locked.
|
|
|
|
isLocked :: LockFile -> IO Bool
|
|
|
|
isLocked = fromMaybe False <$$> checkLocked
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
getLockStatus :: LockFile -> IO (Maybe (ProcessID, FileLock))
|
|
|
|
getLockStatus = fromMaybe Nothing <$$> getLockStatus'
|
|
|
|
|
|
|
|
getLockStatus' :: LockFile -> IO (Maybe (Maybe (ProcessID, FileLock)))
|
|
|
|
getLockStatus' lockfile = go =<< catchMaybeIO open
|
2014-08-20 22:56:25 +00:00
|
|
|
where
|
|
|
|
open = openFd lockfile ReadOnly Nothing defaultFileFlags
|
|
|
|
go Nothing = return Nothing
|
|
|
|
go (Just h) = do
|
2014-08-20 23:09:54 +00:00
|
|
|
ret <- getLock h (ReadLock, AbsoluteSeek, 0, 0)
|
2014-08-20 22:56:25 +00:00
|
|
|
closeFd h
|
2014-08-20 23:09:54 +00:00
|
|
|
return (Just ret)
|
2014-08-20 22:56:25 +00:00
|
|
|
|
2014-08-20 20:45:58 +00:00
|
|
|
dropLock :: LockHandle -> IO ()
|
|
|
|
dropLock (LockHandle fd) = closeFd fd
|