+ """
+ optionparser.add_option("-f", "--foreground", dest="fork",
+ help="Don't detach from the current terminal",
+ default=True, action="store_false")
+ optionparser.add_option("-d", "--debug", dest="debug",
+ help="Enable some debug messages",
+ default=False, action="store_true")
+ optionparser.add_option("--syslog", dest="syslog",
+ help="Enable logging to syslog (except debug"
+ " messages); one of 'no', 'yes' or 'only' [%s]" %
+ constants.SYSLOG_USAGE,
+ default=constants.SYSLOG_USAGE,
+ choices=["no", "yes", "only"])
+
+ if daemon_name in constants.DAEMONS_PORTS:
+ default_bind_address = "0.0.0.0"
+ default_port = utils.GetDaemonPort(daemon_name)
+
+ # For networked daemons we allow choosing the port and bind address
+ optionparser.add_option("-p", "--port", dest="port",
+ help="Network port (default: %s)" % default_port,
+ default=default_port, type="int")
+ optionparser.add_option("-b", "--bind", dest="bind_address",
+ help=("Bind address (default: %s)" %
+ default_bind_address),
+ default=default_bind_address, metavar="ADDRESS")
+
+ if default_ssl_key is not None and default_ssl_cert is not None:
+ optionparser.add_option("--no-ssl", dest="ssl",
+ help="Do not secure HTTP protocol with SSL",
+ default=True, action="store_false")
+ optionparser.add_option("-K", "--ssl-key", dest="ssl_key",
+ help=("SSL key path (default: %s)" %
+ default_ssl_key),
+ default=default_ssl_key, type="string",
+ metavar="SSL_KEY_PATH")
+ optionparser.add_option("-C", "--ssl-cert", dest="ssl_cert",
+ help=("SSL certificate path (default: %s)" %
+ default_ssl_cert),
+ default=default_ssl_cert, type="string",
+ metavar="SSL_CERT_PATH")
+
+ # Disable the use of fork(2) if the daemon uses threads
+ utils.no_fork = multithreaded
+
+ options, args = optionparser.parse_args()
+
+ if getattr(options, "ssl", False):
+ ssl_paths = {
+ "certificate": options.ssl_cert,
+ "key": options.ssl_key,
+ }
+
+ for name, path in ssl_paths.iteritems():
+ if not os.path.isfile(path):
+ print >> sys.stderr, "SSL %s file '%s' was not found" % (name, path)
+ sys.exit(constants.EXIT_FAILURE)
+
+ # TODO: By initiating http.HttpSslParams here we would only read the files
+ # once and have a proper validation (isfile returns False on directories)
+ # at the same time.
+
+ if check_fn is not None:
+ check_fn(options, args)
+
+ utils.EnsureDirs(dirs)
+
+ if options.fork:
+ utils.CloseFDs()
+ utils.Daemonize(logfile=constants.DAEMONS_LOGFILES[daemon_name])
+
+ utils.WritePidFile(daemon_name)
+ try:
+ utils.SetupLogging(logfile=constants.DAEMONS_LOGFILES[daemon_name],
+ debug=options.debug,
+ stderr_logging=not options.fork,
+ multithreaded=multithreaded,
+ program=daemon_name,
+ syslog=options.syslog)
+ logging.info("%s daemon startup", daemon_name)
+ exec_fn(options, args)
+ finally:
+ utils.RemovePidFile(daemon_name)