Example #1
0
def open_queue(name, create=False, consume=False):
    """Open a message queue via a context manager

    The message queue will automatically be closed when the
    context ends. The message queue may also optionally be
    created and/or consumed. If the "consume" flag is True,
    the message queue will be unlinked as well when the context
    ends.

    Args:
        name (str): the name of the message queue
        create (bool): whether to create the message queue or try
                       to open a pre-existing one.
        consume (bool): whether or not to unlink the message queue
                        when the context ends.

    Yields:
        posix_ipc.MessageQueue: the message queue
    """
    flags = 0
    if create:
        flags = posix_ipc.O_CREX

    queue = MessageQueue(name, flags=flags)
    try:
        yield queue
    finally:
        queue.close()
        if consume:
            queue.unlink()
Example #2
0
    def read_window_list_from_queue(stop, window):
        mq = MessageQueue("/lcarswm-active-window-list")
        while True:
            try:
                s, _ = mq.receive(.4)
                GLib.idle_add(window.on_list_update, window, s.decode("utf-8"))
            except BusyError:
                pass

            if stop():
                break

        mq.close()
def do_exit(mq):
    """
    Executed when the program is finished.
    It is generally not useful to call on it's own as it's job is to clean up message queues and shut the application.
    Args:
        mq: A list of message queues to close.
    """
    try:
        logger.info('Cleaning up.')
        logger.info(' - closing & destructing MessageQueue')
        MessageQueue.close(mq)
        MessageQueue.unlink_message_queue(mq)
        logger.info('Finished.')
    except ExistentialError as e:
        pass
    finally:
        sys.exit(0)
Example #4
0
def pi(n):
    pids = []
    unit = n / 10
    q = Queue("/pi", flags=os.O_CREAT)
    for i in range(10):
        mink = unit * i
        maxk = mink + unit
        pid = os.fork()
        if pid > 0:
            pids.append(pid)
        else:
            s = slice(mink, maxk)
            q.send(str(s))
            q.close()
            sys.exit(0)
    sums = []
    for pid in pids:
        sums.append(float(q.receive()[0]))
        os.waitpid(pid, 0)
    q.close()
    q.unlink()
    return math.sqrt(sum(sums) * 8)
Example #5
0
class RemoteMemoryProtocol(object):
    """
    This class listens to memoryforward requests and lifts them to avatar
    messages. Likewise it can be directed to emit memoryforward-response
    messages

    :param rx_queue_name: Name of the queue for receiving
    :param tx_queue_name: Name of the queue for sending
    :param avatar_queue:  Queue to dispatch received requests to
    :param origin:        Reference to the Target utilizing this protocol

    """
    def __init__(self,
                 rx_queue_name,
                 tx_queue_name,
                 avatar_queue,
                 origin=None):
        self._rx_queue = None
        self._tx_queue = None
        self._rx_listener = None

        self.rx_queue_name = rx_queue_name
        self.tx_queue_name = tx_queue_name
        self._avatar_queue = avatar_queue
        self._origin = origin

        self.log = logging.getLogger('%s.%s' %
                                     (origin.log.name, self.__class__.__name__)
                                     ) if origin else \
            logging.getLogger(self.__class__.__name__)

    def connect(self):
        """
        Connect to the message queues for remote memory

        :return True on success, else False
        """
        try:
            self._rx_queue = MessageQueue(self.rx_queue_name,
                                          flags=O_RDONLY,
                                          read=True,
                                          write=False)
        except Exception as e:
            self.log.error("Unable to create rx_queue: %s" % e)
            return False

        try:
            self._tx_queue = MessageQueue(self.tx_queue_name,
                                          flags=O_WRONLY,
                                          read=False,
                                          write=True)
        except Exception as e:
            self.log.error("Unable to create tx_queue: %s" % e)
            self._rx_queue.close()
            return False
        self._rx_listener = RemoteMemoryRequestListener(
            self._rx_queue, self._avatar_queue, self._origin)
        self._rx_listener.daemon = True
        self._rx_listener.start()
        self.log.info("Successfully connected rmp")
        return True

    def __del__(self):
        self.shutdown()

    def shutdown(self):
        if self._rx_listener:
            self._rx_listener.stop()
            self._rx_listener = None
        if self._rx_queue:
            try:
                self._rx_queue.unlink()
                self._rx_queue.close()
                self._rx_queue = None
            except ExistentialError:
                self.log.warning("Tried to close/unlink non existent rx_queue")
        if self._tx_queue:
            try:
                self._tx_queue.unlink()
                self._tx_queue.close()
                self._tx_queue = None
            except ExistentialError:
                self.log.warning("Tried to close/unlink non existent tx_queue")

    def send_response(self, id, value, success):
        response = RemoteMemoryResp(id, value, success)
        try:
            self._tx_queue.send(response)
            self.log.debug("Send RemoteMemoryResponse with id %d, %x" %
                           (id, value))
            return True
        except Exception as e:
            self.log.error("Unable to send response: %s" % e)
            return False
Example #6
0
class ARMV7MInterruptProtocol(Thread):
    """
    This protocol has two purposes: 
        a) injecting interrupts into an analysis target
        b) extracting interrupt exits and putting them into the avatar queue
    (b) is necessary in cases where two targets need to be synched on the
        interrupts.
        The way a v7m-nvic implements interrupt return is to put a magic value
        into $pc, and the hardware does the actual magic of popping from the
        interrupt stack and restoring the context. 
        However, the magic value defines the type of the interrupt return,
        and is hence synchronized on interrupt exit, alongside with the
        interrupt number
    :param origin:        Reference to the Target utilizing this protocol
    :param rx_queue_name: Name of the queue for receiving
    :param tx_queue_name: Name of the queue for sending
    """
    def __init__(self, origin, rx_queue_name, tx_queue_name):
        super(self.__class__, self).__init__()
        self._rx_queue_name = rx_queue_name
        self._tx_queue_name = tx_queue_name
        self._rx_queue = None
        self._tx_queue = None
        self._avatar_queue = origin.avatar.queue
        self._origin = origin
        self._close = Event()
        self._closed = Event()
        self._close.clear()
        self._closed.clear()
        self.log = logging.getLogger('%s.%s' %
                                     (origin.log.name, self.__class__.__name__)
                                     ) if origin else \
            logging.getLogger(self.__class__.__name__)

    def run(self):
        while True:
            if self._close.is_set():
                break

            request = None
            try:
                request = self._rx_queue.receive(0.5)
            except:
                continue

            req_struct = V7MRemoteInterruptNotification.from_buffer_copy(
                request[0])

            if RINOperation(req_struct.operation) == RINOperation.ENTER:
                msg = RemoteInterruptEnterMessage(self._origin, req_struct.id,
                                                  req_struct.num_irq)
            elif RINOperation(req_struct.operation) == RINOperation.EXIT:
                msg = RemoteInterruptExitMessage(self._origin, req_struct.id,
                                                 req_struct.type,
                                                 req_struct.num_irq)
                self.log.debug(
                    "Received an InterruptExitRequest for irq %d (%x)" %
                    (req_struct.num_irq, req_struct.type))

            else:
                msg = None
                raise Exception(
                    ("Received V7MRemoteInterrupt Notification with"
                     "unknown operation type %d") % req_struct.operation)

            self._avatar_queue.put(msg)

        self._closed.set()

    def stop(self):
        self._close.set()
        self._closed.wait()

    def enable_interrupts(self):
        if isinstance(self._origin, QemuTarget):
            # TODO: Make this more clean, i.e., check for remote memory
            rmem_rx_qname = self._origin.protocols.remote_memory.rx_queue_name
            rmem_tx_qname = self._origin.protocols.remote_memory.tx_queue_name
            # the tx-queue for qemu is the rx-queue for avatar and vice versa
            self._origin.protocols.monitor.execute_command(
                'avatar-armv7m-enable-irq', {
                    'irq_rx_queue_name': self._tx_queue_name,
                    'irq_tx_queue_name': self._rx_queue_name,
                    'rmem_rx_queue_name': rmem_tx_qname,
                    'rmem_tx_queue_name': rmem_rx_qname
                })
        else:
            raise Exception("V7MInterruptProtocol is not implemented for %s" %
                            self._origin.__class__)

        try:
            self._rx_queue = MessageQueue(self._rx_queue_name,
                                          flags=O_RDONLY,
                                          read=True,
                                          write=False)
        except Exception as e:
            self.log.error("Unable to create rx_queue: %s" % e)
            return False

        try:
            self._tx_queue = MessageQueue(self._tx_queue_name,
                                          flags=O_WRONLY,
                                          read=False,
                                          write=True)
        except Exception as e:
            self.log.error("Unable to create tx_queue: %s" % e)
            self._rx_queue.close()
            return False

        self.daemon = True
        self.start()
        self.log.info("Enabled Interrupt Forwarding for %s" % self._origin)
        return True

    def ignore_interrupt_return(self, interrupt_number):
        if isinstance(self._origin, QemuTarget):
            self.log.info("Disable handling of irq return for %d" %
                          interrupt_number)
            self._origin.protocols.monitor.execute_command(
                'avatar-armv7m-ignore-irq-return',
                {'num_irq': interrupt_number})

    def unignore_interrupt_return(self, interrupt_number):
        if isinstance(self._origin, QemuTarget):
            self.log.info("Re-enable handling of irq return for %d" %
                          interrupt_number)
            self._origin.protocols.monitor.execute_command(
                'avatar-armv7m-unignore-irq-return',
                {'num_irq': interrupt_number})

    def inject_interrupt(self, interrupt_number, cpu_number=0):
        if isinstance(self._origin, QemuTarget):
            self.log.info("Injecting interrupt %d" % interrupt_number)
            self._origin.protocols.monitor.execute_command(
                'avatar-armv7m-inject-irq', {
                    'num_irq': interrupt_number,
                    'num_cpu': cpu_number
                })

    def set_vector_table_base(self, base, cpu_number=0):
        if isinstance(self._origin, QemuTarget):
            self.log.info("Setting vector table base to 0x%x" % base)
            self._origin.protocols.monitor.execute_command(
                'avatar-armv7m-set-vector-table-base', {
                    'base': base,
                    'num_cpu': cpu_number
                })

    def send_interrupt_exit_response(self, id, success):
        response = V7MInterruptNotificationAck(id, success,
                                               RINOperation.EXIT.value)

        try:
            self._tx_queue.send(response)
            self.log.debug("Send RemoteInterruptExitResponse with id %d" % id)
            return True
        except Exception as e:
            self.log.error("Unable to send response: %s" % e)
            return False

    def send_interrupt_enter_response(self, id, success):
        response = V7MInterruptNotificationAck(id, success,
                                               RINOperation.ENTER.value)
        try:
            self._tx_queue.send(response)
            self.log.debug("Send RemoteInterruptEnterResponse with id %d" % id)
            return True
        except Exception as e:
            self.log.error("Unable to send response: %s" % e)
            return False

    def __del__(self):
        self.shutdown()

    def shutdown(self):
        self.stop()
        if self._rx_queue:
            try:
                self._rx_queue.unlink()
                self._rx_queue.close()
                self._rx_queue = None
            except ExistentialError:
                self.log.warning("Tried to close/unlink non existent rx_queue")
        if self._tx_queue:
            try:
                self._tx_queue.unlink()
                self._tx_queue.close()
                self._tx_queue = None
            except ExistentialError:
                self.log.warning("Tried to close/unlink non existent tx_queue")
Example #7
0
class LcarsdeAppMenu(Gtk.Window):
    """
    Application menu main window
    """
    def __init__(self):
        Gtk.Window.__init__(self, title="lcarsde app menu")

        self.css_provider = Gtk.CssProvider()
        self.css_provider.load_from_data(css)

        scroll_container = Gtk.ScrolledWindow()
        scroll_container.set_policy(Gtk.PolicyType.NEVER,
                                    Gtk.PolicyType.AUTOMATIC)

        self.app_container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
                                     spacing=8)

        spacer = Gtk.Label(label="")
        spacer.get_style_context().add_class("spacer")
        spacer.get_style_context().add_provider(
            self.css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
        self.app_container.pack_end(spacer, True, True, 0)

        scroll_container.add(self.app_container)
        self.add(scroll_container)
        self.entries = {}

        self.set_decorated(False)
        self.get_style_context().add_class("window")
        self.get_style_context().add_provider(self.css_provider,
                                              Gtk.STYLE_PROVIDER_PRIORITY_USER)

        self.sendQueue = MessageQueue("/lcarswm-app-menu-messages")

        self.stop_threads = False
        self.thread = Thread(target=self.read_window_list_from_queue,
                             args=(lambda: self.stop_threads, self))
        self.thread.daemon = True

        self.connect("realize", self.on_create)
        self.connect("destroy", self.on_destroy)

    def on_create(self, window):
        # mark myself as the app menu
        self.get_property("window").set_utf8_property("LCARSDE_APP_MENU",
                                                      "LCARSDE_APP_MENU")
        self.thread.start()

    def on_destroy(self, window):
        self.stop_threads = True
        self.sendQueue.close()
        self.thread.join()

    @staticmethod
    def read_window_list_from_queue(stop, window):
        mq = MessageQueue("/lcarswm-active-window-list")
        while True:
            try:
                s, _ = mq.receive(.4)
                GLib.idle_add(window.on_list_update, window, s.decode("utf-8"))
            except BusyError:
                pass

            if stop():
                break

        mq.close()

    @staticmethod
    def on_list_update(self, list_string):
        data_list = list_string.splitlines()
        if data_list[0] != "list":
            return

        updated_window_elements = dict(
            (window_id, (class_name, is_active == "active"))
            for window_id, class_name, is_active in (
                window_element.split("\t")
                for window_element in data_list[1:]))

        known_windows = list(self.entries.keys())
        self.cleanup_windows(known_windows, updated_window_elements)

        self.handle_current_windows(known_windows, updated_window_elements)
        self.show_all()

    def cleanup_windows(self, known_windows, updated_window_elements):
        for known_window_id in known_windows:
            if known_window_id not in updated_window_elements.keys():
                entry = self.entries[known_window_id]
                self.app_container.remove(entry)
                del self.entries[known_window_id]

    def handle_current_windows(self, known_windows, updated_window_elements):
        for (window_id, (class_name,
                         is_active)) in updated_window_elements.items():
            if window_id in known_windows:
                self.update_window(window_id, class_name, is_active)
            else:
                self.add_window(window_id, class_name, is_active)

    def update_window(self, window_id, class_name, is_active):
        entry = self.entries[window_id]
        entry.update_label(class_name)
        entry.update_activity(is_active)

    def add_window(self, window_id, class_name, is_active):
        entry = WindowEntry(window_id, class_name, is_active,
                            self.css_provider, self.sendQueue)
        self.app_container.pack_start(entry, False, False, 0)
        self.entries[window_id] = entry
Example #8
0
from posix_ipc import MessageQueue as MsgQ
import sysv_ipc
import os

if __name__ == '__main__':
    q = MsgQ("/tmp", flags=os.O_CREAT, mode=0666)
    q.send("adfg")
    rec = q.receive(1)
    print rec
    q.close()
    q.unlink()

    q2 = sysv_ipc.MessageQueue(16842753, flags=sysv_ipc.IPC_CREAT)
    q2.send("sferogh", type=1)