2012-06-11 05:20:19 +00:00
|
|
|
{- daemon support
|
2012-06-11 04:39:09 +00:00
|
|
|
-
|
2014-02-11 19:22:08 +00:00
|
|
|
- Copyright 2012-2014 Joey Hess <joey@kitenet.net>
|
2012-06-11 04:39:09 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-06-11 04:39:09 +00:00
|
|
|
-}
|
|
|
|
|
2013-05-10 21:29:59 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-06-11 04:39:09 +00:00
|
|
|
module Utility.Daemon where
|
|
|
|
|
2012-06-11 05:20:19 +00:00
|
|
|
import Common
|
2014-02-11 19:22:08 +00:00
|
|
|
import Utility.PID
|
2013-08-04 17:54:09 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-03-01 17:30:48 +00:00
|
|
|
import Utility.LogFile
|
2014-02-13 18:00:15 +00:00
|
|
|
#else
|
|
|
|
import Utility.WinProcess
|
2014-08-23 23:27:24 +00:00
|
|
|
import Utility.LockFile
|
2013-08-04 17:54:09 +00:00
|
|
|
#endif
|
2012-06-11 05:20:19 +00:00
|
|
|
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-06-11 04:39:09 +00:00
|
|
|
import System.Posix
|
2013-10-10 20:05:44 +00:00
|
|
|
import Control.Concurrent.Async
|
2013-05-11 22:23:41 +00:00
|
|
|
#endif
|
2012-06-11 04:39:09 +00:00
|
|
|
|
2014-02-11 17:18:59 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-06-11 04:39:09 +00:00
|
|
|
{- Run an action as a daemon, with all output sent to a file descriptor.
|
|
|
|
-
|
2012-06-11 05:20:19 +00:00
|
|
|
- Can write its pid to a file, to guard against multiple instances
|
|
|
|
- running and allow easy termination.
|
|
|
|
-
|
|
|
|
- When successful, does not return. -}
|
|
|
|
daemonize :: Fd -> Maybe FilePath -> Bool -> IO () -> IO ()
|
|
|
|
daemonize logfd pidfile changedirectory a = do
|
2012-11-29 19:44:46 +00:00
|
|
|
maybe noop checkalreadyrunning pidfile
|
2012-06-11 05:37:25 +00:00
|
|
|
_ <- forkProcess child1
|
2012-06-11 05:20:19 +00:00
|
|
|
out
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2014-04-26 23:25:05 +00:00
|
|
|
checkalreadyrunning f = maybe noop (const alreadyRunning)
|
2012-12-13 04:24:19 +00:00
|
|
|
=<< checkDaemon f
|
|
|
|
child1 = do
|
2014-06-09 18:44:18 +00:00
|
|
|
_ <- tryIO createSession
|
2012-12-13 04:24:19 +00:00
|
|
|
_ <- forkProcess child2
|
|
|
|
out
|
|
|
|
child2 = do
|
|
|
|
maybe noop lockPidFile pidfile
|
|
|
|
when changedirectory $
|
|
|
|
setCurrentDirectory "/"
|
|
|
|
nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
|
2013-01-15 17:34:59 +00:00
|
|
|
redir nullfd stdInput
|
|
|
|
redirLog logfd
|
2014-06-09 17:48:44 +00:00
|
|
|
{- In old versions of ghc, forkProcess masks async exceptions;
|
|
|
|
- unmask them inside the action. -}
|
2013-10-10 20:05:44 +00:00
|
|
|
wait =<< asyncWithUnmask (\unmask -> unmask a)
|
2012-12-13 04:24:19 +00:00
|
|
|
out
|
|
|
|
out = exitImmediately ExitSuccess
|
2014-07-14 19:44:44 +00:00
|
|
|
#endif
|
2012-06-11 05:37:25 +00:00
|
|
|
|
2014-05-14 18:26:28 +00:00
|
|
|
{- To run an action that is normally daemonized in the forground. -}
|
2014-07-14 19:44:44 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-05-14 18:26:28 +00:00
|
|
|
foreground :: Fd -> Maybe FilePath -> IO () -> IO ()
|
|
|
|
foreground logfd pidfile a = do
|
2014-07-14 19:44:44 +00:00
|
|
|
#else
|
|
|
|
foreground :: Maybe FilePath -> IO () -> IO ()
|
|
|
|
foreground pidfile a = do
|
|
|
|
#endif
|
2014-05-14 18:26:28 +00:00
|
|
|
maybe noop lockPidFile pidfile
|
2014-07-14 19:44:44 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-06-09 18:44:18 +00:00
|
|
|
_ <- tryIO createSession
|
2014-05-14 18:26:28 +00:00
|
|
|
redirLog logfd
|
2014-07-14 19:44:44 +00:00
|
|
|
#endif
|
2014-05-14 18:26:28 +00:00
|
|
|
a
|
2014-07-14 19:44:44 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-05-14 18:26:28 +00:00
|
|
|
exitImmediately ExitSuccess
|
2014-07-14 19:44:44 +00:00
|
|
|
#else
|
|
|
|
exitWith ExitSuccess
|
2014-05-14 19:50:58 +00:00
|
|
|
#endif
|
2014-05-14 18:26:28 +00:00
|
|
|
|
2014-02-13 20:03:49 +00:00
|
|
|
{- Locks the pid file, with an exclusive, non-blocking lock,
|
|
|
|
- and leaves it locked on return.
|
|
|
|
-
|
2012-08-01 20:29:38 +00:00
|
|
|
- Writes the pid to the file, fully atomically.
|
|
|
|
- Fails if the pid file is already locked by another process. -}
|
|
|
|
lockPidFile :: FilePath -> IO ()
|
2014-02-13 20:03:49 +00:00
|
|
|
lockPidFile pidfile = do
|
2015-01-09 17:11:56 +00:00
|
|
|
createDirectoryIfMissing True (parentDir pidfile)
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-02-13 20:03:49 +00:00
|
|
|
fd <- openFd pidfile ReadWrite (Just stdFileMode) defaultFileFlags
|
2012-07-02 17:47:32 +00:00
|
|
|
locked <- catchMaybeIO $ setLock fd (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
fd' <- openFd newfile ReadWrite (Just stdFileMode) defaultFileFlags
|
|
|
|
{ trunc = True }
|
|
|
|
locked' <- catchMaybeIO $ setLock fd' (WriteLock, AbsoluteSeek, 0, 0)
|
|
|
|
case (locked, locked') of
|
2012-11-29 19:44:46 +00:00
|
|
|
(Nothing, _) -> alreadyRunning
|
|
|
|
(_, Nothing) -> alreadyRunning
|
2012-07-02 17:47:32 +00:00
|
|
|
_ -> do
|
2014-02-11 19:22:08 +00:00
|
|
|
_ <- fdWrite fd' =<< show <$> getPID
|
2012-07-02 17:47:32 +00:00
|
|
|
closeFd fd
|
2014-02-13 20:03:49 +00:00
|
|
|
rename newfile pidfile
|
|
|
|
where
|
|
|
|
newfile = pidfile ++ ".new"
|
2013-05-11 20:03:00 +00:00
|
|
|
#else
|
2014-02-13 20:03:49 +00:00
|
|
|
{- Not atomic on Windows, oh well. -}
|
2014-02-13 21:37:49 +00:00
|
|
|
unlessM (isNothing <$> checkDaemon pidfile)
|
|
|
|
alreadyRunning
|
2014-02-13 20:03:49 +00:00
|
|
|
pid <- getPID
|
|
|
|
writeFile pidfile (show pid)
|
|
|
|
lckfile <- winLockFile pid pidfile
|
|
|
|
writeFile lckfile ""
|
|
|
|
void $ lockExclusive lckfile
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2012-11-29 19:44:46 +00:00
|
|
|
|
|
|
|
alreadyRunning :: IO ()
|
|
|
|
alreadyRunning = error "Daemon is already running."
|
2012-06-11 06:01:20 +00:00
|
|
|
|
2012-07-26 03:13:01 +00:00
|
|
|
{- Checks if the daemon is running, by checking that the pid file
|
|
|
|
- is locked by the same process that is listed in the pid file.
|
2012-06-11 06:01:20 +00:00
|
|
|
-
|
2012-07-26 03:13:01 +00:00
|
|
|
- If it's running, returns its pid. -}
|
2014-02-11 19:22:08 +00:00
|
|
|
checkDaemon :: FilePath -> IO (Maybe PID)
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-07-26 03:13:01 +00:00
|
|
|
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
|
2013-05-27 20:48:41 +00:00
|
|
|
closeFd fd `after` return (check locked p)
|
2012-07-26 03:13:01 +00:00
|
|
|
Nothing -> return Nothing
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
check Nothing _ = Nothing
|
|
|
|
check _ Nothing = Nothing
|
|
|
|
check (Just (pid, _)) (Just pid')
|
|
|
|
| pid == pid' = Just pid
|
|
|
|
| otherwise = error $
|
|
|
|
"stale pid in " ++ pidfile ++
|
|
|
|
" (got " ++ show pid' ++
|
|
|
|
"; expected " ++ show pid ++ " )"
|
2013-05-11 20:03:00 +00:00
|
|
|
#else
|
2014-02-13 20:03:49 +00:00
|
|
|
checkDaemon pidfile = maybe (return Nothing) (check . readish)
|
|
|
|
=<< catchMaybeIO (readFile pidfile)
|
|
|
|
where
|
|
|
|
check Nothing = return Nothing
|
|
|
|
check (Just pid) = do
|
|
|
|
v <- lockShared =<< winLockFile pid pidfile
|
|
|
|
case v of
|
|
|
|
Just h -> do
|
|
|
|
dropLock h
|
|
|
|
return Nothing
|
|
|
|
Nothing -> return (Just pid)
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2012-07-26 03:13:01 +00:00
|
|
|
|
|
|
|
{- Stops the daemon, safely. -}
|
|
|
|
stopDaemon :: FilePath -> IO ()
|
|
|
|
stopDaemon pidfile = go =<< checkDaemon pidfile
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go Nothing = noop
|
2014-02-11 19:22:08 +00:00
|
|
|
go (Just pid) =
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
signalProcess sigTERM pid
|
|
|
|
#else
|
2014-02-13 18:00:15 +00:00
|
|
|
terminatePID pid
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2014-02-13 20:03:49 +00:00
|
|
|
|
|
|
|
{- Windows locks a lock file that corresponds with the pid of the process.
|
|
|
|
- This allows changing the process in the pid file and taking a new lock
|
|
|
|
- when eg, restarting the daemon.
|
|
|
|
-}
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
winLockFile :: PID -> FilePath -> IO FilePath
|
|
|
|
winLockFile pid pidfile = do
|
|
|
|
cleanstale
|
|
|
|
return $ prefix ++ show pid ++ suffix
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
prefix = pidfile ++ "."
|
2014-02-13 20:03:49 +00:00
|
|
|
suffix = ".lck"
|
|
|
|
cleanstale = mapM_ (void . tryIO . removeFile) =<<
|
2015-01-09 17:11:56 +00:00
|
|
|
(filter iswinlockfile <$> dirContents (parentDir pidfile))
|
2014-02-13 20:03:49 +00:00
|
|
|
iswinlockfile f = suffix `isSuffixOf` f && prefix `isPrefixOf` f
|
|
|
|
#endif
|