]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Unix.hs
Add a bunch of new options allowing htsn to daemonize.
[dead/htsn.git] / src / Unix.hs
1 module Unix
2 where
3
4 import Control.Concurrent ( ThreadId, myThreadId )
5 import Control.Exception ( throwTo )
6 import System.Exit ( ExitCode( ExitSuccess ) )
7 import System.Posix (
8 GroupEntry ( groupID ),
9 GroupID,
10 Handler ( Catch ),
11 UserEntry ( userID ),
12 UserID,
13 getGroupEntryForName,
14 getProcessID,
15 getRealGroupID,
16 getRealUserID,
17 getUserEntryForName,
18 installHandler,
19 removeLink,
20 setGroupID,
21 setUserID,
22 sigTERM )
23 import System.Posix.Daemonize ( daemonize )
24
25 import Configuration (
26 Configuration( pidfile,
27 run_as_group,
28 run_as_user ))
29 import Logging ( log_info )
30
31 get_user_id :: Maybe String -> IO UserID
32 get_user_id Nothing = getRealUserID
33 get_user_id (Just s) = fmap userID (getUserEntryForName s)
34
35 get_group_id :: Maybe String -> IO GroupID
36 get_group_id Nothing = getRealGroupID
37 get_group_id (Just s) = fmap groupID (getGroupEntryForName s)
38
39 graceful_shutdown :: Configuration -> ThreadId -> IO ()
40 graceful_shutdown cfg main_thread_id = do
41 log_info "SIGTERM received, removing PID file and shutting down."
42 removeLink (pidfile cfg)
43 throwTo main_thread_id ExitSuccess
44
45 full_daemonize :: Configuration -> IO () -> IO ()
46 full_daemonize cfg program = do
47 -- This is the 'daemonize' from System.Posix.Daemonize.
48 daemonize program'
49 where
50 -- We need to do all this stuff *after* we daemonize.
51 program' = do
52 -- First write the PID file which probably requires root.
53 pid <- getProcessID
54 writeFile (pidfile cfg) (show pid)
55
56 -- We need to pass the thread ID to the signal handler so it
57 -- knows which process to "exit."
58 tid <- myThreadId
59 _ <- installHandler sigTERM (Catch (graceful_shutdown cfg tid)) Nothing
60
61 -- Then drop privileges.
62 get_user_id (run_as_user cfg) >>= setUserID
63 get_group_id (run_as_group cfg) >>= setGroupID
64
65 -- Finally run the program we were asked to.
66 program