aboutsummaryrefslogtreecommitdiff
path: root/Utility/Daemon.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-11-16 14:27:23 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-11-16 14:34:30 -0400
commit8055c761751e5c6a913f76293281389e29378545 (patch)
treecd49ae0d803cfe65dbea48144fc030df8e8fc7a8 /Utility/Daemon.hs
parent4e9fc72e2dc1bb4e5251b1e0eb7b4290e0aa2988 (diff)
avoid crashing in checkDaemon when fcntl locking is not supported
Instead, just assume the daemon isn't running. Since the pid file locking fails on such a filesystem, we know it's not running.
Diffstat (limited to 'Utility/Daemon.hs')
-rw-r--r--Utility/Daemon.hs20
1 files changed, 11 insertions, 9 deletions
diff --git a/Utility/Daemon.hs b/Utility/Daemon.hs
index d7f0407be..3cc2eb261 100644
--- a/Utility/Daemon.hs
+++ b/Utility/Daemon.hs
@@ -119,16 +119,18 @@ alreadyRunning = error "Daemon is already running."
- If it's running, returns its pid. -}
checkDaemon :: FilePath -> IO (Maybe PID)
#ifndef mingw32_HOST_OS
-checkDaemon pidfile = do
- v <- catchMaybeIO $
- openFd pidfile ReadOnly (Just stdFileMode) defaultFileFlags
- case v of
- Just fd -> do
- locked <- getLock fd (ReadLock, AbsoluteSeek, 0, 0)
- p <- readish <$> readFile pidfile
- closeFd fd `after` return (check locked p)
- Nothing -> return Nothing
+checkDaemon pidfile = bracket setup cleanup go
where
+ setup = catchMaybeIO $
+ openFd pidfile ReadOnly (Just stdFileMode) defaultFileFlags
+ cleanup (Just fd) = closeFd fd
+ cleanup Nothing = return ()
+ go (Just fd) = catchDefaultIO Nothing $ do
+ locked <- getLock fd (ReadLock, AbsoluteSeek, 0, 0)
+ p <- readish <$> readFile pidfile
+ return (check locked p)
+ go Nothing = return Nothing
+
check Nothing _ = Nothing
check _ Nothing = Nothing
check (Just (pid, _)) (Just pid')