X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;ds=sidebyside;f=src%2FUnix.hs;h=a4389bee0e844ea7cd7024d608e3e126033bc926;hb=ac3a81eb6d0f8ca4e212752d5b390a4fc220cceb;hp=893132624165566232ae1da28bd698faea2f6465;hpb=f6cb0ba712e06e52d080b86e9eba6c3585a7514b;p=dead%2Fhtsn.git diff --git a/src/Unix.hs b/src/Unix.hs index 8931326..a4389be 100644 --- a/src/Unix.hs +++ b/src/Unix.hs @@ -1,3 +1,5 @@ +-- | Non-portable code for daemonizing on unix. +-- module Unix where @@ -28,22 +30,39 @@ import Configuration ( run_as_user )) import Logging ( log_info ) +-- | Retrieve the uid associated with the given system user name. We +-- take a Maybe String as an argument so the user name can be passed +-- in directly from the config. +-- get_user_id :: Maybe String -> IO UserID get_user_id Nothing = getRealUserID get_user_id (Just s) = fmap userID (getUserEntryForName s) + +-- | Retrieve the gid associated with the given system group name. We +-- take a Maybe String as an argument so the group name can be +-- passed in directly from the config. +-- get_group_id :: Maybe String -> IO GroupID get_group_id Nothing = getRealGroupID get_group_id (Just s) = fmap groupID (getGroupEntryForName s) + +-- | This function will be called in response to a SIGTERM; i.e. when +-- someone tries to kill our process. We simply delete the PID file +-- and signal our parent thread to quit (successfully). graceful_shutdown :: Configuration -> ThreadId -> IO () graceful_shutdown cfg main_thread_id = do log_info "SIGTERM received, removing PID file and shutting down." removeLink (pidfile cfg) throwTo main_thread_id ExitSuccess + +-- | Write a PID file, install a SIGTERM handler, drop privileges, and +-- finally do the daemonization dance. +-- full_daemonize :: Configuration -> IO () -> IO () -full_daemonize cfg program = do +full_daemonize cfg program = -- This is the 'daemonize' from System.Posix.Daemonize. daemonize program' where