def signal_handler(n_signal, frame): def strsignal(n_signal): libc = ctypes.CDLL(ctypes.util.find_library('c')) strsignal_proto = ctypes.CFUNCTYPE(ctypes.c_char_p, ctypes.c_int) strsignal_c = strsignal_proto(("strsignal", libc), ((1, ), )) return strsignal_c(n_signal) logging.info("Signal [{0}-{1}] received. Exit cleaning...".format( n_signal, strsignal(n_signal))) # received Cntrl+C if n_signal == SIGINT or n_signal == SIGTERM: # kill C daemon if it's running try: pid = int( check_output([ "pidof", "{0}/bin/wazuh-clusterd-internal".format(ossec_path) ])) kill(pid, SIGINT) except Exception: pass if child_pid != 0: # kill child kill(child_pid, SIGTERM) # remove pid files delete_pid("wazuh-clusterd", getpid()) exit(1)
def signal_handler(n_signal, frame): def strsignal(n_signal): libc = ctypes.CDLL(ctypes.util.find_library('c')) strsignal_proto = ctypes.CFUNCTYPE(ctypes.c_char_p, ctypes.c_int) strsignal_c = strsignal_proto(("strsignal", libc), ((1, ), )) return strsignal_c(n_signal) logging.info("Signal [{0}-{1}] received. Exit cleaning...".format( n_signal, strsignal(n_signal))) # received Cntrl+C if n_signal == SIGINT or n_signal == SIGTERM: # kill C daemon if it's running try: pid = int( check_output([ "pidof", "{0}/bin/wazuh-clusterd-internal".format(common.ossec_path) ])) kill(pid, SIGINT) except Exception: pass if child_pid != 0: try: # kill child kill(child_pid, SIGTERM) # remove pid files delete_pid("wazuh-clusterd", getpid()) except Exception as e: logging.error("Error killing child process: {0}".format( str(e))) if args.d: raise else: for connections in common.cluster_connections.values(): try: logging.debug("Closing socket {0}...".format( connections.socket.getpeername())) connections.socket.close() except socket.error as e: if e.errno == socket.errno.EBADF: logging.debug("Socket already closed: {0}".format( str(e))) else: logging.error("Could not close socket: {0}".format( str(e))) exit(1)
"{0}/bin/wazuh-clusterd-internal".format(common.ossec_path), "-t{0}".format(cluster_config['node_type']) ] if debug: call_list.append("-ddd") check_call(call_list) if __name__ == '__main__': global cluster_connections args = parser.parse_args() try: if args.V: check_output([ "{0}/bin/wazuh-clusterd-internal".format(common.ossec_path), '-V' ]) exit(0) # Capture Cntrl + C signal(SIGINT, signal_handler) signal(SIGTERM, signal_handler) if not args.f: res_code = pyDaemon() else: # define a Handler which writes INFO messages or higher to the sys.stderr console = logging.StreamHandler() console.setLevel(logging.DEBUG) # set a format which is simpler for console use formatter = logging.Formatter(