avoid crashing in checkDaemon when fcntl locking is not supported
authorJoey Hess <joeyh@joeyh.name>
Mon, 16 Nov 2015 18:27:23 +0000 (14:27 -0400)
committerJoey Hess <joeyh@joeyh.name>
Mon, 16 Nov 2015 18:34:30 +0000 (14:34 -0400)
Instead, just assume the daemon isn't running. Since the pid file locking
fails on such a filesystem, we know it's not running.

Utility/Daemon.hs

index d7f0407bef0b3c49e3d0be2b15479bd846b0399a..3cc2eb26131e9cf9b885c886ab11a2c39555b3e5 100644 (file)
@@ -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')