Skip to content
  • Hans de Goede's avatar
    rfkill: set the g_io_channel to unbuffered mode · d2200632
    Hans de Goede authored
    Access to a /dev/foo device should never use buffered mode.
    
    While debugging a gsd-rfkill issue I noticed in the g_debug output
    that the rfkill-glib.c code now seems to be receiving bogus events.
    
    Doing a strace I noticed some read(dev_rfkill_fd, buf, 8) calls,
    even though we call:
      g_io_channel_read_chars(..., sizeof(struct rfkill_event, ...)
    
    Which requests 9 bytes. The problem is the kernel expects us to
    read 1 event per read() system-call and it will throw away
    excess data. The idea is here that the rfkill_event struct can
    be extended by adding new fields at the end and then userspace code
    compiled against older kernel headers will still work since it
    will only read the fields it knows in a single call and the
    extra fields are thrown away.
    
    Since the rfkill-glib.c code was using buffered-io and asking
    g_io_channel_read_chars for 9 bytes when compiled against recent
    kernel headers, what would happen is that 2 events would be consumed
    in 2 read(fd, buf, 8) syscalls and then the first byte of the
    second event read would be appended to the previous event and
    the remaining 7 bytes would be used as the first 7 bytes for the
    next event (and eventually completed with the first 2 bytes of
    the next event, etc.). Leading to completely bogus events.
    
    Enabling unbuffered mode fixes this.
    
    Note this is a relatively new problem, caused by the kernel
    recently extending the rfkill_event struct with an extra byte-field:
    "rfkill: add a reason to the HW rfkill state"
    https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=14486c82612a177cb910980c70ba900827ca0894
    
    Before that kernel change the rfkill_event struct was 8 bytes large
    which allowed us to get away with using buffered io here.
    d2200632