示例#1
0
    def _handle_device_event(self, device):
        """
        :type device: Device
        """
        # log.debug('Event: %r for %r', device.action, device)

        #TODO: udev_device_get_ifindex
        ifindex = device.get('IFINDEX', None)
        if ifindex is None:
            log.debug('Skipping device without IFINDEX %r', device)
            return
        ifindex = int(ifindex)

        if device.parent is None:
            log.debug('Skipping device without parent (non-hardware), %r', device)
            return

        # devtype = device.device_type
        # if devtype is not None:
        #     log.debug('Device %r have type %r, so skipped', device, devtype)
        #     return

        # noinspection PyUnresolvedReferences
        action = device.action

        # TODO: add may appear AFTER coldplug, this is OK (races)
        if (action is None) or (action == u'add'):

            if self.udev_version >= 165:
                # TODO: handle udev's initializations errors instead of raise
                if not device.is_initialized:
                    device_path = device.device_path
                    log.debug('Device is not initialized. re-reading %s', device_path)
                    device = Device.from_path(self.context, device_path)
                    if not device.is_initialized:
                        raise Exception('Double-getting device still not initializd')
            else:
                device = Device.from_path(self.context, device.device_path)

            #TODO: udev_device_get_ifindex
            self.netdevices[ifindex] = PhysicalEthernet(device)
            return

        if action == u'remove':
            try:
                old_eth_device = self.netdevices.pop(ifindex)
            except KeyError:
                log.exception('Device removal error (no such device). Scheduling full rescan.', device, ifindex)
                IOLoop.instance().add_callback(self.rescan_devices)
                return
            old_eth_device.close()
示例#2
0
    def __iter__(self):
        """
        Iterate over all matching devices.

        Yield :class:`Device` objects.
        """
        libudev.udev_enumerate_scan_devices(self)
        entry = libudev.udev_enumerate_get_list_entry(self)
        for name, _ in udev_list_iterate(entry):
            yield Device.from_sys_path(self.context, name)
示例#3
0
    def new_func(context, device, recursive=True):
        """
        New function wraps yielded value in Device.

        :param `Context` context: udev context
        :param `Device` device: device to start from
        :param bool recursive: if False, only show immediate slaves
        """
        for directory in func(device.sys_path, recursive):
            yield Device.from_sys_path(context, directory)
示例#4
0
    def __iter__(self):
        """
        Iterate over all matching devices.

        Yield :class:`Device` objects.
        """
        self._libudev.udev_enumerate_scan_devices(self)
        entry = self._libudev.udev_enumerate_get_list_entry(self)
        for name, _ in udev_list_iterate(self._libudev, entry):
            yield Device.from_sys_path(self.context, name)
示例#5
0
    def __iter__(self):
        """
        Iterate over all matching devices.

        Yield :class:`Device` objects.
        """
        self._libudev.udev_enumerate_scan_devices(self)
        entry = self._libudev.udev_enumerate_get_list_entry(self)
        for name, _ in udev_list_iterate(self._libudev, entry):
            try:
                yield Device.from_sys_path(self.context, name)
            except DeviceNotFoundAtPathError:
                continue
示例#6
0
    def _receive_device(self):
        """Receive a single device from the monitor.

        Return the received :class:`Device`, or ``None`` if no device could be
        received.

        """
        while True:
            try:
                device_p = self._libudev.udev_monitor_receive_device(self)
                return Device(self.context, device_p) if device_p else None
            except EnvironmentError as error:
                if error.errno in (errno.EAGAIN, errno.EWOULDBLOCK):
                    # No data available
                    return None
                elif error.errno == errno.EINTR:
                    # Try again if our system call was interrupted
                    continue
                else:
                    raise