Пример #1
0
    def sigterm_handler(signum, frame):

        terminate_subprocesses(subprocesses, handler)

        log("killed by SIGTERM")

        sys.exit(0)
Пример #2
0
def terminate_subprocesses(subprocesses, handler):
    # check if any subprocesses were started
    if subprocesses == 0:
        return 0, 0, 0
    # kill all subprocesses

    for sub in subprocesses:
        try:
            #log("will terminate")
            #log(sub.pid+1)
            os.kill(sub.pid + 1, signal.SIGTERM)
            #pgid = os.getpgid(sub.pid)
            #os.killpg(pgid, signal.SIGTERM)
        except Exception as e:
            log("subprocess could not be Terminated")
            log(type(e))
            log(e)
    # kill message handler if it was started
    # if handler != 0:
    #     log("will terminate hadnler first")
    #     log(handler)
    #     os.kill(handler, signal.SIGTERM)

    log("Terminated subprocesses")

    return 0, 0, 0
Пример #3
0
def execute_process(process):
    try:
        gesamt = process.name
        for so in process.sources:
            gesamt = gesamt + " -i " + so
        for ta in process.targets:
            gesamt = gesamt + " -o " + ta
        gesamt = gesamt + " " + process.parameter
        errpipe = open(process.sterr, 'w')  #TODO: open directly with fd
        outpipe = open(process.stout, 'w')
        errfd = errpipe.fileno()
        outfd = outpipe.fileno()
        #print( 'will attempt to run', gesamt)
        return subprocess.Popen(gesamt,
                                shell=True,
                                preexec_fn=os.setsid,
                                stderr=errfd,
                                stdout=outfd,
                                cwd=process.path)
        #print('Done subprocess!')

    except Exception as e:
        log("procces could not be started")
        log(type(e))
        log(e)
Пример #4
0
def connect_NC(NCcon):
    try:
        gesamt = NCcon.ip + " " + str(NCcon.port) + " " + NCcon.pipe
        return subprocess.Popen(["python bin/netcatCon.py " + gesamt],
                                shell=True,
                                preexec_fn=os.setsid)
    except Exception as e:
        log("netcatCon.py could not be started")
        log(type(e))
        log(e)
Пример #5
0
def socket_send_msg(socket, message):
    try:
        #prefix message with its length (4 as byte unsigned integer in big-endian byte-order)
        message = struct.pack('>I', len(message)) + message
        #send complete message with sendall to avoid multiple calls to send()
        socket.sendall(message)
    except Exception as e:
        log("socket_send_msg method failed")
        log(type(e))
        log(e)
Пример #6
0
def kill_subprocesses(subprocesses, handler):
    #check if any subprocesses were started
    if subprocesses == 0:
        return 0, 0, 0
    #kill all subprocesses
    for sub in subprocesses:
        try:
            os.killpg(os.getpgid(sub.pid), signal.SIGKILL)
        except Exception as e:
            log("subprocess could not be killed")
            log(type(e))
            log(e)
    #kill message handler if it was started
    if handler != 0:
        os.kill(handler, signal.SIGKILL)

    log("*Killed subprocesses")

    return 0, 0, 0
Пример #7
0
def socket_receive_msg(socket):
    try:
        #read length of next message from socket
        length_packed = socket_receive_bytes(socket, 4)
        if not length_packed:
            return None
        #unpack length to integer
        length = struct.unpack('>I', length_packed)[0]
        #read message of expected length
        return socket_receive_bytes(socket, length)
    except Exception as e:
        log("socket_receive_msg method failed")
        log(type(e))
        log(e)
Пример #8
0
def start(server_cmd,server_msg,id):
    #no subprocesses running
    subprocesses = []
    
    #get objects from the server
    processes,connections_in,connections_out = get_objects(server_cmd)

    #initialize pipes
    init_pipes(processes)
    #initialize ingoing connections
    subprocesses = subprocesses + init_ingoing_connections(connections_in)
    #initialize outgoing connections
    subprocesses = subprocesses + init_outgoing_connections(connections_out)

    #open read pipes (non blocking!)
    processes = open_ingoing_pipes(processes)

    #start message handler
    handler,processes=start_handler(server_msg,processes,id)
    running = 1

    #wait for starting signal from server (synchronization)
    try:
        socket_send_msg(server_cmd,"ready")
        socket_receive_msg(server_cmd)
    except Exception as e:
        log("synchronization with server failed")
        log(type(e))
        log(e)

    #open write pipes
    processes = open_outgoing_pipes(processes)
    #run processes
    log("execute processes")
    subprocesses = subprocesses + execute_processes(processes)

    return subprocesses,running,handler
Пример #9
0
def get_objects(server_socket):
    processes = []
    connections_in = []
    connections_out = []
    dir_iterator = 1
    pipe_path = "/home/asn/asn_daemon/temp/pipes_system/fifo_"

    #send 'ack' to server to begin with transfer
    socket_send_msg(server_socket, "CMD ack")
    #receive data
    try:
        rootString = socket_receive_msg(server_socket)
    except Exception as e:
        log("Receiving data from server failed")
        log(type(e))
        log(e)

    #parse data
    root = objectify.fromstring(rootString)
    #get Pi-id
    pi_id = root.get("pi_id")

    #loop through executables
    for p in root.getchildren():
        input = []
        output = []
        parameter = ''
        #get name of executable
        name = p.get('executable')
        #get path
        slash = name.rfind('/')
        path = 'system/' + str(dir_iterator)
        if slash != -1:
            path = path + '/' + name[:slash]
        dir_iterator = dir_iterator + 1

        #loop through parameters (input/output pipes and additional params)
        for atom in p.getchildren():

            # collect input pipes
            if (atom.tag == 'input'):
                pipe_id = atom.get("pipe_id")
                pipe_name = pipe_path + str(pipe_id)
                input.append(pipe_name)

                #register ingoing nc connection if necessary
                if atom.get("source_pi_id") != pi_id:
                    try:
                        int(pi_id)
                        connections_in.append(
                            NCcon_c("10.1.1." + str(pi_id),
                                    (1000 + int(pipe_id)), pipe_name))
                    except Exception:
                        connections_in.append(
                            NCcon_c(str(pi_id), (1000 + int(pipe_id)),
                                    pipe_name))

            # collect output fifos
            if (atom.tag == 'output'):
                pipe_id = atom.get("pipe_id")
                pipe_name = pipe_path + str(pipe_id)
                output.append(pipe_name)

                #register outgoing nc connection if necessary
                target_id = atom.get("target_pi_id")
                if target_id != pi_id:
                    try:
                        int(target_id)
                        connections_out.append(
                            NCcon_c("10.1.1." + target_id,
                                    (1000 + int(pipe_id)), pipe_name))
                    except Exception:
                        connections_out.append(
                            NCcon_c(target_id, (1000 + int(pipe_id)),
                                    pipe_name))
            # collect parameter
            if (atom.tag == 'parameter'):
                param = atom.get("param")
                parameter = parameter + param + " "

        # append new process
        processes.append(process_c(input, output, path, name, parameter))

    return processes, connections_in, connections_out
Пример #10
0
def reconnect(ip):
    log("Waiting for server")
    try:
        server_cmd = get_server_connection(ip, 9999)
        log("Command port connected")

        server_msg = get_server_connection(ip, 9998)
        log("Message port connected")
    except Exception as e:
        log("Connection to server failed")
        log(type(e))
        log(e)

    return server_cmd, server_msg
Пример #11
0
        terminate_subprocesses(subprocesses, handler)

        log("killed by SIGTERM")

        sys.exit(0)

    #set handler for SIGTERM signal

    signal.signal(signal.SIGTERM, sigterm_handler)

    #connect to server

    try:

        log("Waiting for server")

        server_cmd = get_server_connection(ip, cmdPort)

        log("Command port connected")

        server_msg = get_server_connection(ip, msgPort)

        log("Message port connected")

    except Exception as e:

        log("Connection to server failed")

        log(type(e))