summaryrefslogtreecommitdiff
path: root/Utility/Daemon.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-06-11 00:39:09 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-06-11 00:39:09 -0400
commitd5884388b09347835df599d8a0dcea77e6795c10 (patch)
tree2f98ad9d0013b97daa04278bc837ef64967db73a /Utility/Daemon.hs
parentca9ee21bd771e7f94ecd3916f55b10fb3cc8dcbe (diff)
daemonize git annex watch
Diffstat (limited to 'Utility/Daemon.hs')
-rw-r--r--Utility/Daemon.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/Utility/Daemon.hs b/Utility/Daemon.hs
new file mode 100644
index 000000000..be3df17b7
--- /dev/null
+++ b/Utility/Daemon.hs
@@ -0,0 +1,39 @@
+{- daemon functions
+ -
+ - Copyright 2012 Joey Hess <joey@kitenet.net>
+ -
+ - Licensed under the GNU GPL version 3 or higher.
+ -}
+
+module Utility.Daemon where
+
+import System.Posix
+import System.Directory
+import System.Exit
+import Control.Monad
+
+{- Run an action as a daemon, with all output sent to a file descriptor.
+ -
+ - Does not return. -}
+daemonize :: Fd -> Bool -> IO () -> IO ()
+daemonize logfd changedirectory a = do
+ _ <- forkProcess child1
+ end
+ where
+ child1 = do
+ _ <- createSession
+ _ <- forkProcess child2
+ end
+ child2 = do
+ when changedirectory $
+ setCurrentDirectory "/"
+ nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
+ _ <- redir nullfd stdInput
+ mapM_ (redir logfd) [stdOutput, stdError]
+ closeFd logfd
+ a
+ end
+ redir newh h = do
+ closeFd h
+ dupTo newh h
+ end = exitImmediately ExitSuccess