fix pid file locking
Ok, that's odd.. opening it before fork breaks the locking. I don't understand why.
This commit is contained in:
parent
0b3e2bed78
commit
8539a7bde8
1 changed files with 13 additions and 17 deletions
|
@ -19,16 +19,15 @@ 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
|
||||||
pidfd <- lockpidfile
|
_ <- forkProcess child1
|
||||||
_ <- forkProcess $ child1 pidfd
|
|
||||||
out
|
out
|
||||||
where
|
where
|
||||||
child1 pidfd = do
|
child1 = do
|
||||||
_ <- createSession
|
_ <- createSession
|
||||||
_ <- forkProcess $ child2 pidfd
|
_ <- forkProcess child2
|
||||||
out
|
out
|
||||||
child2 pidfd = do
|
child2 = do
|
||||||
writepidfile pidfd
|
maybe noop lockPidFile pidfile
|
||||||
when changedirectory $
|
when changedirectory $
|
||||||
setCurrentDirectory "/"
|
setCurrentDirectory "/"
|
||||||
nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
|
nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
|
||||||
|
@ -37,18 +36,15 @@ daemonize logfd pidfile changedirectory a = do
|
||||||
closeFd logfd
|
closeFd logfd
|
||||||
a
|
a
|
||||||
out
|
out
|
||||||
lockpidfile = case pidfile of
|
|
||||||
Just file -> do
|
|
||||||
fd <- openFd file ReadWrite (Just stdFileMode) defaultFileFlags
|
|
||||||
setLock fd (WriteLock, AbsoluteSeek, 0, 0)
|
|
||||||
return $ Just fd
|
|
||||||
Nothing -> return Nothing
|
|
||||||
writepidfile pidfd =
|
|
||||||
case pidfd of
|
|
||||||
Just fd -> void $
|
|
||||||
fdWrite fd =<< show <$> getProcessID
|
|
||||||
Nothing -> return ()
|
|
||||||
redir newh h = do
|
redir newh h = do
|
||||||
closeFd h
|
closeFd h
|
||||||
dupTo newh h
|
dupTo newh h
|
||||||
out = exitImmediately ExitSuccess
|
out = exitImmediately ExitSuccess
|
||||||
|
|
||||||
|
lockPidFile :: FilePath -> IO ()
|
||||||
|
lockPidFile file = void $ do
|
||||||
|
fd <- openFd file ReadWrite (Just stdFileMode) defaultFileFlags
|
||||||
|
catchIO
|
||||||
|
(setLock fd (WriteLock, AbsoluteSeek, 0, 0))
|
||||||
|
(const $ error "Daemon is already running.")
|
||||||
|
fdWrite fd =<< show <$> getProcessID
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue