def run(self, once=False, **kwargs): """Run the daemon""" utils.validate_configuration() utils.drop_privileges(self.conf.get('user', 'chase')) utils.capture_stdio(self.logger, **kwargs) def kill_children(*args): signal.signal(signal.SIGTERM, signal.SIG_IGN) os.killpg(0, signal.SIGTERM) sys.exit() signal.signal(signal.SIGTERM, kill_children) if once: self.run_once(**kwargs) else: self.run_forever(**kwargs)
def test_drop_privileges(self): user = getuser() # over-ride os with mock required_func_calls = ('setgid', 'setuid', 'setsid', 'chdir', 'umask') utils.os = MockOs(called_funcs=required_func_calls) # exercise the code utils.drop_privileges(user) for func in required_func_calls: self.assert_(utils.os.called_funcs[func]) # reset; test same args, OSError trying to get session leader utils.os = MockOs(called_funcs=required_func_calls, raise_funcs=('setsid', )) for func in required_func_calls: self.assertFalse(utils.os.called_funcs.get(func, False)) utils.drop_privileges(user) for func in required_func_calls: self.assert_(utils.os.called_funcs[func])
def test_drop_privileges(self): user = getuser() # over-ride os with mock required_func_calls = ('setgid', 'setuid', 'setsid', 'chdir', 'umask') utils.os = MockOs(called_funcs=required_func_calls) # exercise the code utils.drop_privileges(user) for func in required_func_calls: self.assert_(utils.os.called_funcs[func]) # reset; test same args, OSError trying to get session leader utils.os = MockOs(called_funcs=required_func_calls, raise_funcs=('setsid',)) for func in required_func_calls: self.assertFalse(utils.os.called_funcs.get(func, False)) utils.drop_privileges(user) for func in required_func_calls: self.assert_(utils.os.called_funcs[func])
validate_configuration() # pre-configure logger log_name = conf.get('log_name', app_section) if 'logger' in kwargs: logger = kwargs.pop('logger') else: logger = get_logger(conf, log_name, log_to_console=kwargs.pop('verbose', False), log_route='wsgi') # bind to address and port sock = get_socket(conf, default_port=kwargs.get('default_port', 8080)) # remaining tasks should not require elevated privileges drop_privileges(conf.get('user', 'chase')) # Ensure the application can be loaded before proceeding. loadapp('config:%s' % conf_file, global_conf={'log_name': log_name}) # redirect errors to logger and close stdio capture_stdio(logger) def run_server(): wsgi.HttpProtocol.default_request_version = "HTTP/1.0" # Turn off logging requests by the underlying WSGI software. wsgi.HttpProtocol.log_request = lambda *a: None # Redirect logging other messages by the underlying WSGI software. wsgi.HttpProtocol.log_message = \ lambda s, f, *a: logger.error('ERROR WSGI: ' + f % a) wsgi.WRITE_TIMEOUT = int(conf.get('client_timeout') or 60)
except Exception, e: print "Error trying to load config %s: %s" % (conf_file, e) return validate_configuration() # pre-configure logger log_name = conf.get("log_name", app_section) if "logger" in kwargs: logger = kwargs.pop("logger") else: logger = get_logger(conf, log_name, log_to_console=kwargs.pop("verbose", False), log_route="wsgi") # bind to address and port sock = get_socket(conf, default_port=kwargs.get("default_port", 8080)) # remaining tasks should not require elevated privileges drop_privileges(conf.get("user", "chase")) # Ensure the application can be loaded before proceeding. loadapp("config:%s" % conf_file, global_conf={"log_name": log_name}) # redirect errors to logger and close stdio capture_stdio(logger) def run_server(): wsgi.HttpProtocol.default_request_version = "HTTP/1.0" # Turn off logging requests by the underlying WSGI software. wsgi.HttpProtocol.log_request = lambda *a: None # Redirect logging other messages by the underlying WSGI software. wsgi.HttpProtocol.log_message = lambda s, f, *a: logger.error("ERROR WSGI: " + f % a) wsgi.WRITE_TIMEOUT = int(conf.get("client_timeout") or 60) eventlet.hubs.use_hub("poll")