fix "daemon is already running" message display

Display it before daemon forks, so it's not shown after the shell prompt
returns.
This commit is contained in:
Joey Hess 2012-11-29 15:44:46 -04:00
parent 45ba20e351
commit e6e7f45716

View file

@ -19,9 +19,12 @@ import System.Posix
- When successful, does not return. -} - When successful, does not return. -}
daemonize :: Fd -> Maybe FilePath -> Bool -> IO () -> IO () daemonize :: Fd -> Maybe FilePath -> Bool -> IO () -> IO ()
daemonize logfd pidfile changedirectory a = do daemonize logfd pidfile changedirectory a = do
maybe noop checkalreadyrunning pidfile
_ <- forkProcess child1 _ <- forkProcess child1
out out
where where
checkalreadyrunning f = maybe noop (const $ alreadyRunning)
=<< checkDaemon f
child1 = do child1 = do
_ <- createSession _ <- createSession
_ <- forkProcess child2 _ <- forkProcess child2
@ -53,15 +56,17 @@ lockPidFile file = do
{ trunc = True } { trunc = True }
locked' <- catchMaybeIO $ setLock fd' (WriteLock, AbsoluteSeek, 0, 0) locked' <- catchMaybeIO $ setLock fd' (WriteLock, AbsoluteSeek, 0, 0)
case (locked, locked') of case (locked, locked') of
(Nothing, _) -> alreadyrunning (Nothing, _) -> alreadyRunning
(_, Nothing) -> alreadyrunning (_, Nothing) -> alreadyRunning
_ -> do _ -> do
_ <- fdWrite fd' =<< show <$> getProcessID _ <- fdWrite fd' =<< show <$> getProcessID
renameFile newfile file renameFile newfile file
closeFd fd closeFd fd
where where
newfile = file ++ ".new" newfile = file ++ ".new"
alreadyrunning = error "Daemon is already running."
alreadyRunning :: IO ()
alreadyRunning = error "Daemon is already running."
{- Checks if the daemon is running, by checking that the pid file {- 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. - is locked by the same process that is listed in the pid file.