summaryrefslogtreecommitdiff
path: root/Utility/Daemon.hs
blob: b09dce739920438d9f9712bd8a6d4673afc91361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{- daemon support
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Utility.Daemon where

import Common

import System.Posix

{- Run an action as a daemon, with all output sent to a file descriptor.
 -
 - 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
	_ <- forkProcess child1
	out
	where
		child1 = do
			_ <- createSession
			_ <- forkProcess child2
			out
		child2 = do
			maybe noop (lockPidFile True alreadyrunning) pidfile 
			when changedirectory $
				setCurrentDirectory "/"
			nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
			_ <- redir nullfd stdInput
			mapM_ (redir logfd) [stdOutput, stdError]
			closeFd logfd
			a
			out
		redir newh h = do
			closeFd h
			dupTo newh h
		alreadyrunning = error "Daemon is already running."
		out = exitImmediately ExitSuccess

lockPidFile :: Bool -> IO () -> FilePath -> IO ()
lockPidFile write onfailure file = do
	fd <- openFd file ReadWrite (Just stdFileMode) defaultFileFlags
	when (write) $ void $
		fdWrite fd =<< show <$> getProcessID
	catchIO (setLock fd (locktype, AbsoluteSeek, 0, 0)) (const onfailure)
	where
		locktype
			| write = WriteLock
			| otherwise = ReadLock

{- Stops the daemon.
 -
 - The pid file is used to get the daemon's pid.
 -
 - To guard against a stale pid, try to take a nonblocking shared lock
 - of the pid file. If this *fails*, the daemon must be running,
 - and have the exclusive lock, so the pid file is trustworthy.
 -}
stopDaemon :: FilePath -> IO ()
stopDaemon pidfile = lockPidFile False go pidfile
	where
		go = do
			pid <- readish <$> readFile pidfile
			maybe noop (signalProcess sigTERM) pid