def clean_on_exit(fp_sock): """Method executed on process exit""" logging.info('clean up on process exit') try: os.remove(fp_sock) except: pass logging.info('shutdown logging system') logging.close()
def handle_sigusr1(self, signum, frame): """Handle signal SIGUSR1 to reload server configuration""" logging.info('reload server configuration (by signal)') # Catch exception in the I{main} method caused by received signal self.__by_signal_do = 1 config = get_conf(CFG_PATH) # PID file cannot be changed config['server']['pid'] = self._config['server']['pid'] # Log file path changed if self._config['server']['log'] != config['server']['log']: logging.close(0) logging.init(0, filename=config['server']['log']) # Local socket path changed init_loc = (config['server']['sock'] != self._config['server']['sock']) # SSL option changed and/or server ip/port, we need to # shutdown current socket and open new one init_srv = (config['ssl'] != self._config['ssl'] \ or config['server']['port'] != self._config['server']['port'] \ or config['server']['ip'] != self._config['server']['ip']) self._config = config.copy() if init_srv: try: self.socket.shutdown(socket.SHUT_RDWR) self._create_bind_activate(1) except Exception as e: # @todo: fix: if cannot bind new socket, do not reload config self.__by_signal_do = 0 logging.error(e) if init_loc: try: self._create_local_socket(1) except Exception as e: pass self.socket.settimeout(config['server']['timeout']) # Non-blocking mode self.__local_sock.settimeout(0.0) logging.info('server configuration reloaded') del config
server._aliases = aliases.copy() # Catch SIGUSR1 signal to reload server configuration signal.signal(signal.SIGUSR1, server.handle_sigusr1) # Catch SIGUSR2 signal to reload aliases configuration signal.signal(signal.SIGUSR2, server.handle_sigusr2) msg_stdout('Signal handlers registered') if server.activate(): msg_stdout('Server activated', flush_buf=1) # Send message to local socket in the first parent process r = server.daemonize() if r: logging.info('forked as daemon process') # Close logging, init new to file logging.close(0) logging.init(0, filename=config['server']['log']) # Delete local socket on FS when process exit atexit.register(clean_on_exit, config['server']['sock']) # Start main loop server.main() else: msg_stdout('Error: can\'t daemonize', 2) elif sys.argv[1].lower() == 'stop': msg_sock(config['server'], 'shutdown', 0) elif sys.argv[1].lower() == 'reload': signal_to_daemon(config['server']['pid'], signal.SIGUSR1) elif sys.argv[1].lower() == 'aliases':