From 2a3c20fe95f2b695547bf0e14246e6c05e36f360 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 23 Dec 2013 10:25:46 -0500 Subject: [PATCH] Retain umask after daemonizing. Report when we have successfully logged in. --- src/Main.hs | 1 + src/Unix.hs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index 02b42ff..2b0b5b9 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -198,6 +198,7 @@ log_in cfg h = do else do send_cred h (password cfg) _ <- recv_line h -- "The Sports Network" + report_info $ "Logged in as " ++ (username cfg) ++ "." return () where username_prompt = "Username: " diff --git a/src/Unix.hs b/src/Unix.hs index a4389be..9b0ddaa 100644 --- a/src/Unix.hs +++ b/src/Unix.hs @@ -19,6 +19,7 @@ import System.Posix ( getUserEntryForName, installHandler, removeLink, + setFileCreationMask, setGroupID, setUserID, sigTERM ) @@ -62,14 +63,23 @@ graceful_shutdown cfg main_thread_id = do -- finally do the daemonization dance. -- full_daemonize :: Configuration -> IO () -> IO () -full_daemonize cfg program = +full_daemonize cfg program = do + -- The call to 'daemonize' will set the umask to zero, but we want + -- to retain it. So, we set the umask to zero before 'daemonize' + -- can, so that we can record the previous umask value (returned by + -- setFileCreationMask). + orig_umask <- setFileCreationMask 0 -- This is the 'daemonize' from System.Posix.Daemonize. - daemonize program' + daemonize (program' orig_umask) where -- We need to do all this stuff *after* we daemonize. - program' = do + program' orig_umask = do -- First write the PID file which probably requires root. pid <- getProcessID + + -- The PID file needs to be read-only for anyone but its + -- owner. Hopefully the umask accomplishes this! + _ <- setFileCreationMask orig_umask writeFile (pidfile cfg) (show pid) -- We need to pass the thread ID to the signal handler so it -- 2.44.2