Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Daemon.hs @ 36691f08

History | View | Annotate | Download (12 kB)

1 6ec7a50e Iustin Pop
{-| Implementation of the generic daemon functionality.
2 6ec7a50e Iustin Pop
3 6ec7a50e Iustin Pop
-}
4 6ec7a50e Iustin Pop
5 6ec7a50e Iustin Pop
{-
6 6ec7a50e Iustin Pop
7 6ec7a50e Iustin Pop
Copyright (C) 2011, 2012 Google Inc.
8 6ec7a50e Iustin Pop
9 6ec7a50e Iustin Pop
This program is free software; you can redistribute it and/or modify
10 6ec7a50e Iustin Pop
it under the terms of the GNU General Public License as published by
11 6ec7a50e Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 6ec7a50e Iustin Pop
(at your option) any later version.
13 6ec7a50e Iustin Pop
14 6ec7a50e Iustin Pop
This program is distributed in the hope that it will be useful, but
15 6ec7a50e Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 6ec7a50e Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 6ec7a50e Iustin Pop
General Public License for more details.
18 6ec7a50e Iustin Pop
19 6ec7a50e Iustin Pop
You should have received a copy of the GNU General Public License
20 6ec7a50e Iustin Pop
along with this program; if not, write to the Free Software
21 6ec7a50e Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 6ec7a50e Iustin Pop
02110-1301, USA.
23 6ec7a50e Iustin Pop
24 6ec7a50e Iustin Pop
-}
25 6ec7a50e Iustin Pop
26 6ec7a50e Iustin Pop
module Ganeti.Daemon
27 6ec7a50e Iustin Pop
  ( DaemonOptions(..)
28 6ec7a50e Iustin Pop
  , OptType
29 6ec7a50e Iustin Pop
  , defaultOptions
30 6ec7a50e Iustin Pop
  , oShowHelp
31 6ec7a50e Iustin Pop
  , oShowVer
32 6ec7a50e Iustin Pop
  , oNoDaemonize
33 6ec7a50e Iustin Pop
  , oNoUserChecks
34 6ec7a50e Iustin Pop
  , oDebug
35 6ec7a50e Iustin Pop
  , oPort
36 152e05e1 Iustin Pop
  , oBindAddress
37 b714ff89 Iustin Pop
  , oSyslogUsage
38 6ec7a50e Iustin Pop
  , parseArgs
39 152e05e1 Iustin Pop
  , parseAddress
40 6ec7a50e Iustin Pop
  , writePidFile
41 6ec7a50e Iustin Pop
  , genericMain
42 6ec7a50e Iustin Pop
  ) where
43 6ec7a50e Iustin Pop
44 79ac58fa Iustin Pop
import Control.Exception
45 6ec7a50e Iustin Pop
import Control.Monad
46 0c28bee1 Iustin Pop
import Data.Maybe (fromMaybe)
47 6ec7a50e Iustin Pop
import qualified Data.Version
48 6ec7a50e Iustin Pop
import Data.Word
49 0c28bee1 Iustin Pop
import GHC.IO.Handle (hDuplicateTo)
50 152e05e1 Iustin Pop
import qualified Network.Socket as Socket
51 79ac58fa Iustin Pop
import Prelude hiding (catch)
52 6ec7a50e Iustin Pop
import System.Console.GetOpt
53 6ec7a50e Iustin Pop
import System.Exit
54 6ec7a50e Iustin Pop
import System.Environment
55 6ec7a50e Iustin Pop
import System.Info
56 6ec7a50e Iustin Pop
import System.IO
57 6ec7a50e Iustin Pop
import System.Posix.Directory
58 6ec7a50e Iustin Pop
import System.Posix.Files
59 6ec7a50e Iustin Pop
import System.Posix.IO
60 6ec7a50e Iustin Pop
import System.Posix.Process
61 6ec7a50e Iustin Pop
import System.Posix.Types
62 36691f08 Iustin Pop
import System.Posix.Signals
63 6ec7a50e Iustin Pop
import Text.Printf
64 6ec7a50e Iustin Pop
65 6ec7a50e Iustin Pop
import Ganeti.Logging
66 6ec7a50e Iustin Pop
import Ganeti.Runtime
67 6ec7a50e Iustin Pop
import Ganeti.BasicTypes
68 6ec7a50e Iustin Pop
import Ganeti.HTools.Utils
69 6ec7a50e Iustin Pop
import qualified Ganeti.HTools.Version as Version(version)
70 6ec7a50e Iustin Pop
import qualified Ganeti.Constants as C
71 152e05e1 Iustin Pop
import qualified Ganeti.Ssconf as Ssconf
72 6ec7a50e Iustin Pop
73 0c28bee1 Iustin Pop
-- * Constants
74 0c28bee1 Iustin Pop
75 0c28bee1 Iustin Pop
-- | \/dev\/null path.
76 0c28bee1 Iustin Pop
devNull :: FilePath
77 0c28bee1 Iustin Pop
devNull = "/dev/null"
78 0c28bee1 Iustin Pop
79 6ec7a50e Iustin Pop
-- * Data types
80 6ec7a50e Iustin Pop
81 6ec7a50e Iustin Pop
-- | Command line options structure.
82 6ec7a50e Iustin Pop
data DaemonOptions = DaemonOptions
83 6ec7a50e Iustin Pop
  { optShowHelp     :: Bool           -- ^ Just show the help
84 6ec7a50e Iustin Pop
  , optShowVer      :: Bool           -- ^ Just show the program version
85 6ec7a50e Iustin Pop
  , optDaemonize    :: Bool           -- ^ Whether to daemonize or not
86 6ec7a50e Iustin Pop
  , optPort         :: Maybe Word16   -- ^ Override for the network port
87 6ec7a50e Iustin Pop
  , optDebug        :: Bool           -- ^ Enable debug messages
88 6ec7a50e Iustin Pop
  , optNoUserChecks :: Bool           -- ^ Ignore user checks
89 152e05e1 Iustin Pop
  , optBindAddress  :: Maybe String   -- ^ Override for the bind address
90 b714ff89 Iustin Pop
  , optSyslogUsage  :: Maybe SyslogUsage -- ^ Override for Syslog usage
91 6ec7a50e Iustin Pop
  }
92 6ec7a50e Iustin Pop
93 6ec7a50e Iustin Pop
-- | Default values for the command line options.
94 6ec7a50e Iustin Pop
defaultOptions :: DaemonOptions
95 6ec7a50e Iustin Pop
defaultOptions  = DaemonOptions
96 6ec7a50e Iustin Pop
  { optShowHelp     = False
97 6ec7a50e Iustin Pop
  , optShowVer      = False
98 6ec7a50e Iustin Pop
  , optDaemonize    = True
99 6ec7a50e Iustin Pop
  , optPort         = Nothing
100 6ec7a50e Iustin Pop
  , optDebug        = False
101 6ec7a50e Iustin Pop
  , optNoUserChecks = False
102 152e05e1 Iustin Pop
  , optBindAddress  = Nothing
103 b714ff89 Iustin Pop
  , optSyslogUsage  = Nothing
104 6ec7a50e Iustin Pop
  }
105 6ec7a50e Iustin Pop
106 6ec7a50e Iustin Pop
-- | Abrreviation for the option type.
107 6ec7a50e Iustin Pop
type OptType = OptDescr (DaemonOptions -> Result DaemonOptions)
108 6ec7a50e Iustin Pop
109 6ec7a50e Iustin Pop
-- | Helper function for required arguments which need to be converted
110 6ec7a50e Iustin Pop
-- as opposed to stored just as string.
111 6ec7a50e Iustin Pop
reqWithConversion :: (String -> Result a)
112 6ec7a50e Iustin Pop
                  -> (a -> DaemonOptions -> Result DaemonOptions)
113 6ec7a50e Iustin Pop
                  -> String
114 6ec7a50e Iustin Pop
                  -> ArgDescr (DaemonOptions -> Result DaemonOptions)
115 6ec7a50e Iustin Pop
reqWithConversion conversion_fn updater_fn metavar =
116 6ec7a50e Iustin Pop
  ReqArg (\string_opt opts -> do
117 6ec7a50e Iustin Pop
            parsed_value <- conversion_fn string_opt
118 6ec7a50e Iustin Pop
            updater_fn parsed_value opts) metavar
119 6ec7a50e Iustin Pop
120 6ec7a50e Iustin Pop
-- * Command line options
121 6ec7a50e Iustin Pop
122 6ec7a50e Iustin Pop
oShowHelp :: OptType
123 6ec7a50e Iustin Pop
oShowHelp = Option "h" ["help"]
124 6ec7a50e Iustin Pop
            (NoArg (\ opts -> Ok opts { optShowHelp = True}))
125 6ec7a50e Iustin Pop
            "Show the help message and exit"
126 6ec7a50e Iustin Pop
127 6ec7a50e Iustin Pop
oShowVer :: OptType
128 6ec7a50e Iustin Pop
oShowVer = Option "V" ["version"]
129 6ec7a50e Iustin Pop
           (NoArg (\ opts -> Ok opts { optShowVer = True}))
130 6ec7a50e Iustin Pop
           "Show the version of the program and exit"
131 6ec7a50e Iustin Pop
132 6ec7a50e Iustin Pop
oNoDaemonize :: OptType
133 6ec7a50e Iustin Pop
oNoDaemonize = Option "f" ["foreground"]
134 6ec7a50e Iustin Pop
               (NoArg (\ opts -> Ok opts { optDaemonize = False}))
135 6ec7a50e Iustin Pop
               "Don't detach from the current terminal"
136 6ec7a50e Iustin Pop
137 6ec7a50e Iustin Pop
oDebug :: OptType
138 6ec7a50e Iustin Pop
oDebug = Option "d" ["debug"]
139 6ec7a50e Iustin Pop
         (NoArg (\ opts -> Ok opts { optDebug = True }))
140 6ec7a50e Iustin Pop
         "Enable debug messages"
141 6ec7a50e Iustin Pop
142 6ec7a50e Iustin Pop
oNoUserChecks :: OptType
143 6ec7a50e Iustin Pop
oNoUserChecks = Option "" ["no-user-checks"]
144 6ec7a50e Iustin Pop
         (NoArg (\ opts -> Ok opts { optNoUserChecks = True }))
145 6ec7a50e Iustin Pop
         "Ignore user checks"
146 6ec7a50e Iustin Pop
147 6ec7a50e Iustin Pop
oPort :: Int -> OptType
148 e6812a1a Iustin Pop
oPort def = Option "p" ["port"]
149 6ec7a50e Iustin Pop
            (reqWithConversion (tryRead "reading port")
150 6ec7a50e Iustin Pop
             (\port opts -> Ok opts { optPort = Just port }) "PORT")
151 6ec7a50e Iustin Pop
            ("Network port (default: " ++ show def ++ ")")
152 6ec7a50e Iustin Pop
153 152e05e1 Iustin Pop
oBindAddress :: OptType
154 152e05e1 Iustin Pop
oBindAddress = Option "b" ["bind"]
155 152e05e1 Iustin Pop
               (ReqArg (\addr opts -> Ok opts { optBindAddress = Just addr })
156 152e05e1 Iustin Pop
                "ADDR")
157 152e05e1 Iustin Pop
               "Bind address (default depends on cluster configuration)"
158 152e05e1 Iustin Pop
159 b714ff89 Iustin Pop
oSyslogUsage :: OptType
160 b714ff89 Iustin Pop
oSyslogUsage = Option "" ["syslog"]
161 b714ff89 Iustin Pop
               (reqWithConversion syslogUsageFromRaw
162 b714ff89 Iustin Pop
                (\su opts -> Ok opts { optSyslogUsage = Just su })
163 b714ff89 Iustin Pop
                "SYSLOG")
164 b714ff89 Iustin Pop
               ("Enable logging to syslog (except debug \
165 b714ff89 Iustin Pop
                \messages); one of 'no', 'yes' or 'only' [" ++ C.syslogUsage ++
166 b714ff89 Iustin Pop
                "]")
167 b714ff89 Iustin Pop
168 6ec7a50e Iustin Pop
-- | Usage info.
169 6ec7a50e Iustin Pop
usageHelp :: String -> [OptType] -> String
170 6ec7a50e Iustin Pop
usageHelp progname =
171 6ec7a50e Iustin Pop
  usageInfo (printf "%s %s\nUsage: %s [OPTION...]"
172 6ec7a50e Iustin Pop
             progname Version.version progname)
173 6ec7a50e Iustin Pop
174 6ec7a50e Iustin Pop
-- | Command line parser, using the 'Options' structure.
175 6ec7a50e Iustin Pop
parseOpts :: [String]               -- ^ The command line arguments
176 6ec7a50e Iustin Pop
          -> String                 -- ^ The program name
177 6ec7a50e Iustin Pop
          -> [OptType]              -- ^ The supported command line options
178 6ec7a50e Iustin Pop
          -> IO (DaemonOptions, [String]) -- ^ The resulting options
179 6ec7a50e Iustin Pop
                                          -- and leftover arguments
180 6ec7a50e Iustin Pop
parseOpts argv progname options =
181 6ec7a50e Iustin Pop
  case getOpt Permute options argv of
182 6ec7a50e Iustin Pop
    (opt_list, args, []) ->
183 6ec7a50e Iustin Pop
      do
184 6ec7a50e Iustin Pop
        parsed_opts <-
185 88a10df5 Iustin Pop
          exitIfBad "Error while parsing command line arguments" $
186 88a10df5 Iustin Pop
          foldM (flip id) defaultOptions opt_list
187 6ec7a50e Iustin Pop
        return (parsed_opts, args)
188 6ec7a50e Iustin Pop
    (_, _, errs) -> do
189 6ec7a50e Iustin Pop
      hPutStrLn stderr $ "Command line error: "  ++ concat errs
190 6ec7a50e Iustin Pop
      hPutStrLn stderr $ usageHelp progname options
191 6ec7a50e Iustin Pop
      exitWith $ ExitFailure 2
192 6ec7a50e Iustin Pop
193 6ec7a50e Iustin Pop
-- | Small wrapper over getArgs and 'parseOpts'.
194 6ec7a50e Iustin Pop
parseArgs :: String -> [OptType] -> IO (DaemonOptions, [String])
195 6ec7a50e Iustin Pop
parseArgs cmd options = do
196 6ec7a50e Iustin Pop
  cmd_args <- getArgs
197 6ec7a50e Iustin Pop
  parseOpts cmd_args cmd options
198 6ec7a50e Iustin Pop
199 6ec7a50e Iustin Pop
-- * Daemon-related functions
200 6ec7a50e Iustin Pop
-- | PID file mode.
201 6ec7a50e Iustin Pop
pidFileMode :: FileMode
202 6ec7a50e Iustin Pop
pidFileMode = unionFileModes ownerReadMode ownerWriteMode
203 6ec7a50e Iustin Pop
204 6ec7a50e Iustin Pop
-- | Writes a PID file and locks it.
205 6ec7a50e Iustin Pop
_writePidFile :: FilePath -> IO Fd
206 6ec7a50e Iustin Pop
_writePidFile path = do
207 6ec7a50e Iustin Pop
  fd <- createFile path pidFileMode
208 6ec7a50e Iustin Pop
  setLock fd (WriteLock, AbsoluteSeek, 0, 0)
209 6ec7a50e Iustin Pop
  my_pid <- getProcessID
210 6ec7a50e Iustin Pop
  _ <- fdWrite fd (show my_pid ++ "\n")
211 6ec7a50e Iustin Pop
  return fd
212 6ec7a50e Iustin Pop
213 79ac58fa Iustin Pop
-- | Helper to format an IOError.
214 79ac58fa Iustin Pop
formatIOError :: String -> IOError -> String
215 79ac58fa Iustin Pop
formatIOError msg err = msg ++ ": " ++  show err
216 79ac58fa Iustin Pop
217 6ec7a50e Iustin Pop
-- | Wrapper over '_writePidFile' that transforms IO exceptions into a
218 6ec7a50e Iustin Pop
-- 'Bad' value.
219 6ec7a50e Iustin Pop
writePidFile :: FilePath -> IO (Result Fd)
220 6ec7a50e Iustin Pop
writePidFile path = do
221 79ac58fa Iustin Pop
  catch (fmap Ok $ _writePidFile path)
222 79ac58fa Iustin Pop
    (return . Bad . formatIOError "Failure during writing of the pid file")
223 6ec7a50e Iustin Pop
224 6ec7a50e Iustin Pop
-- | Sets up a daemon's environment.
225 6ec7a50e Iustin Pop
setupDaemonEnv :: FilePath -> FileMode -> IO ()
226 6ec7a50e Iustin Pop
setupDaemonEnv cwd umask = do
227 6ec7a50e Iustin Pop
  changeWorkingDirectory cwd
228 6ec7a50e Iustin Pop
  _ <- setFileCreationMask umask
229 6ec7a50e Iustin Pop
  _ <- createSession
230 6ec7a50e Iustin Pop
  return ()
231 6ec7a50e Iustin Pop
232 36691f08 Iustin Pop
-- | Signal handler for reopening log files.
233 36691f08 Iustin Pop
handleSigHup :: FilePath -> IO ()
234 36691f08 Iustin Pop
handleSigHup path = do
235 36691f08 Iustin Pop
  setupDaemonFDs (Just path)
236 36691f08 Iustin Pop
  logInfo "Reopening log files after receiving SIGHUP"
237 36691f08 Iustin Pop
238 0c28bee1 Iustin Pop
-- | Sets up a daemon's standard file descriptors.
239 0c28bee1 Iustin Pop
setupDaemonFDs :: Maybe FilePath -> IO ()
240 0c28bee1 Iustin Pop
setupDaemonFDs logfile = do
241 0c28bee1 Iustin Pop
  null_in_handle <- openFile devNull ReadMode
242 0c28bee1 Iustin Pop
  null_out_handle <- openFile (fromMaybe devNull logfile) AppendMode
243 0c28bee1 Iustin Pop
  hDuplicateTo null_in_handle stdin
244 0c28bee1 Iustin Pop
  hDuplicateTo null_out_handle stdout
245 0c28bee1 Iustin Pop
  hDuplicateTo null_out_handle stderr
246 0c28bee1 Iustin Pop
  hClose null_in_handle
247 0c28bee1 Iustin Pop
  hClose null_out_handle
248 0c28bee1 Iustin Pop
249 152e05e1 Iustin Pop
-- | Computes the default bind address for a given family.
250 152e05e1 Iustin Pop
defaultBindAddr :: Int                  -- ^ The port we want
251 152e05e1 Iustin Pop
                -> Socket.Family        -- ^ The cluster IP family
252 152e05e1 Iustin Pop
                -> Result (Socket.Family, Socket.SockAddr)
253 152e05e1 Iustin Pop
defaultBindAddr port Socket.AF_INET =
254 152e05e1 Iustin Pop
  Ok $ (Socket.AF_INET,
255 152e05e1 Iustin Pop
        Socket.SockAddrInet (fromIntegral port) Socket.iNADDR_ANY)
256 152e05e1 Iustin Pop
defaultBindAddr port Socket.AF_INET6 =
257 152e05e1 Iustin Pop
  Ok $ (Socket.AF_INET6,
258 152e05e1 Iustin Pop
        Socket.SockAddrInet6 (fromIntegral port) 0 Socket.iN6ADDR_ANY 0)
259 152e05e1 Iustin Pop
defaultBindAddr _ fam = Bad $ "Unsupported address family: " ++ show fam
260 152e05e1 Iustin Pop
261 152e05e1 Iustin Pop
-- | Default hints for the resolver
262 152e05e1 Iustin Pop
resolveAddrHints :: Maybe Socket.AddrInfo
263 152e05e1 Iustin Pop
resolveAddrHints =
264 152e05e1 Iustin Pop
  Just Socket.defaultHints { Socket.addrFlags = [Socket.AI_NUMERICHOST,
265 152e05e1 Iustin Pop
                                                 Socket.AI_NUMERICSERV] }
266 152e05e1 Iustin Pop
267 152e05e1 Iustin Pop
-- | Resolves a numeric address.
268 152e05e1 Iustin Pop
resolveAddr :: Int -> String -> IO (Result (Socket.Family, Socket.SockAddr))
269 152e05e1 Iustin Pop
resolveAddr port str = do
270 152e05e1 Iustin Pop
  resolved <- Socket.getAddrInfo resolveAddrHints (Just str) (Just (show port))
271 152e05e1 Iustin Pop
  return $ case resolved of
272 152e05e1 Iustin Pop
             [] -> Bad "Invalid results from lookup?"
273 152e05e1 Iustin Pop
             best:_ -> Ok $ (Socket.addrFamily best, Socket.addrAddress best)
274 152e05e1 Iustin Pop
275 152e05e1 Iustin Pop
-- | Based on the options, compute the socket address to use for the
276 152e05e1 Iustin Pop
-- daemon.
277 152e05e1 Iustin Pop
parseAddress :: DaemonOptions      -- ^ Command line options
278 152e05e1 Iustin Pop
             -> Int                -- ^ Default port for this daemon
279 152e05e1 Iustin Pop
             -> IO (Result (Socket.Family, Socket.SockAddr))
280 152e05e1 Iustin Pop
parseAddress opts defport = do
281 152e05e1 Iustin Pop
  let port = maybe defport fromIntegral $ optPort opts
282 152e05e1 Iustin Pop
  def_family <- Ssconf.getPrimaryIPFamily Nothing
283 152e05e1 Iustin Pop
  ainfo <- case optBindAddress opts of
284 152e05e1 Iustin Pop
             Nothing -> return (def_family >>= defaultBindAddr port)
285 152e05e1 Iustin Pop
             Just saddr -> catch (resolveAddr port saddr)
286 152e05e1 Iustin Pop
                           (annotateIOError $ "Invalid address " ++ saddr)
287 152e05e1 Iustin Pop
  return ainfo
288 152e05e1 Iustin Pop
289 6ec7a50e Iustin Pop
-- | Run an I/O action as a daemon.
290 6ec7a50e Iustin Pop
--
291 6ec7a50e Iustin Pop
-- WARNING: this only works in single-threaded mode (either using the
292 6ec7a50e Iustin Pop
-- single-threaded runtime, or using the multi-threaded one but with
293 6ec7a50e Iustin Pop
-- only one OS thread, i.e. -N1).
294 6ec7a50e Iustin Pop
--
295 6ec7a50e Iustin Pop
-- FIXME: this doesn't support error reporting and the prepfn
296 6ec7a50e Iustin Pop
-- functionality.
297 0c28bee1 Iustin Pop
daemonize :: FilePath -> IO () -> IO ()
298 0c28bee1 Iustin Pop
daemonize logfile action = do
299 6ec7a50e Iustin Pop
  -- first fork
300 6ec7a50e Iustin Pop
  _ <- forkProcess $ do
301 6ec7a50e Iustin Pop
    -- in the child
302 6ec7a50e Iustin Pop
    setupDaemonEnv "/" (unionFileModes groupModes otherModes)
303 0c28bee1 Iustin Pop
    setupDaemonFDs $ Just logfile
304 36691f08 Iustin Pop
    _ <- installHandler lostConnection (Catch (handleSigHup logfile)) Nothing
305 6ec7a50e Iustin Pop
    _ <- forkProcess action
306 6ec7a50e Iustin Pop
    exitImmediately ExitSuccess
307 6ec7a50e Iustin Pop
  exitImmediately ExitSuccess
308 6ec7a50e Iustin Pop
309 6ec7a50e Iustin Pop
-- | Generic daemon startup.
310 6ec7a50e Iustin Pop
genericMain :: GanetiDaemon -> [OptType] -> (DaemonOptions -> IO ()) -> IO ()
311 6ec7a50e Iustin Pop
genericMain daemon options main = do
312 6ec7a50e Iustin Pop
  let progname = daemonName daemon
313 6ec7a50e Iustin Pop
  (opts, args) <- parseArgs progname options
314 6ec7a50e Iustin Pop
315 6ec7a50e Iustin Pop
  when (optShowHelp opts) $ do
316 6ec7a50e Iustin Pop
    putStr $ usageHelp progname options
317 6ec7a50e Iustin Pop
    exitWith ExitSuccess
318 6ec7a50e Iustin Pop
  when (optShowVer opts) $ do
319 6ec7a50e Iustin Pop
    printf "%s %s\ncompiled with %s %s\nrunning on %s %s\n"
320 6ec7a50e Iustin Pop
           progname Version.version
321 6ec7a50e Iustin Pop
           compilerName (Data.Version.showVersion compilerVersion)
322 6ec7a50e Iustin Pop
           os arch :: IO ()
323 6ec7a50e Iustin Pop
    exitWith ExitSuccess
324 88a10df5 Iustin Pop
325 88a10df5 Iustin Pop
  exitUnless (null args) "This program doesn't take any arguments"
326 6ec7a50e Iustin Pop
327 6ec7a50e Iustin Pop
  unless (optNoUserChecks opts) $ do
328 6ec7a50e Iustin Pop
    runtimeEnts <- getEnts
329 88a10df5 Iustin Pop
    ents <- exitIfBad "Can't find required user/groups" runtimeEnts
330 88a10df5 Iustin Pop
    verifyDaemonUser daemon ents
331 6ec7a50e Iustin Pop
332 b714ff89 Iustin Pop
  syslog <- case optSyslogUsage opts of
333 88a10df5 Iustin Pop
              Nothing -> exitIfBad "Invalid cluster syslog setting" $
334 b714ff89 Iustin Pop
                         syslogUsageFromRaw C.syslogUsage
335 b714ff89 Iustin Pop
              Just v -> return v
336 0c28bee1 Iustin Pop
  let processFn = if optDaemonize opts
337 0c28bee1 Iustin Pop
                    then daemonize (daemonLogFile daemon)
338 0c28bee1 Iustin Pop
                    else id
339 b714ff89 Iustin Pop
  processFn $ innerMain daemon opts syslog (main opts)
340 6ec7a50e Iustin Pop
341 6ec7a50e Iustin Pop
-- | Inner daemon function.
342 6ec7a50e Iustin Pop
--
343 6ec7a50e Iustin Pop
-- This is executed after daemonization.
344 b714ff89 Iustin Pop
innerMain :: GanetiDaemon -> DaemonOptions -> SyslogUsage -> IO () -> IO ()
345 b714ff89 Iustin Pop
innerMain daemon opts syslog main = do
346 0c28bee1 Iustin Pop
  let logfile = if optDaemonize opts
347 0c28bee1 Iustin Pop
                  then Nothing
348 0c28bee1 Iustin Pop
                  else Just $ daemonLogFile daemon
349 0c28bee1 Iustin Pop
  setupLogging logfile (daemonName daemon) (optDebug opts) True False syslog
350 6ec7a50e Iustin Pop
  pid_fd <- writePidFile (daemonPidFile daemon)
351 88a10df5 Iustin Pop
  _ <- exitIfBad "Cannot write PID file; already locked? Error" pid_fd
352 6ec7a50e Iustin Pop
  logNotice "starting"
353 6ec7a50e Iustin Pop
  main