Why does ioctl FIONREAD from /dev/null return 0 on Mac OS X and a random number on Linux?

783 views Asked by At

I've come across something that seems strange while adding tests to a project I'm working on - I have been using /dev/null as a serial port and not expecting any data to be available for reading.

However, on LINUX there is always data available, and on Mac OS X after a call to srand() there is data available.

Can someone help explain this behaviour?

Here is a minimum viable test C++

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int open_serial(const char *device) {
    speed_t bd = B115200;
    int fd;
    int state;
    struct termios config;

    if ((fd = open(device, O_NDELAY | O_NOCTTY | O_NONBLOCK | O_RDWR)) == -1)
        return -1;

    fcntl(fd, F_SETFL, O_RDWR);
    tcgetattr(fd, &config);
    cfmakeraw(&config);
    cfsetispeed(&config, bd);
    cfsetospeed(&config, bd);

    config.c_cflag |= (CLOCAL | CREAD);
    config.c_cflag &= ~(CSTOPB | CSIZE | PARENB);
    config.c_cflag |= CS8;
    config.c_lflag &= ~(ECHO | ECHOE | ICANON | ISIG);
    config.c_oflag &= ~OPOST;
    config.c_cc[VMIN] = 0;
    config.c_cc[VTIME] = 50; // 5 seconds reception timeout

    tcsetattr(fd, TCSANOW, &config);
    ioctl(fd, TIOCMGET, &state);
    state |= (TIOCM_DTR | TIOCM_RTS);
    ioctl(fd, TIOCMSET, &state);

    usleep(10000);    // Sleep for 10 milliseconds
    return fd;
};

int serial_data_available(const int fd) {
    int result;
    ioctl(fd, FIONREAD, &result);
    return result;
};

int main() {
    int fd = open_serial("/dev/null");
    printf("Opened /dev/null - FD: %d\n", fd);
    printf("Serial data available : %d\n", serial_data_available(fd));
    printf("Serial data available : %d\n", serial_data_available(fd));
    printf("Calling srand()\n");
    srand(1234);
    printf("Serial data available : %d\n", serial_data_available(fd));
    printf("Serial data available : %d\n", serial_data_available(fd));
    return 0;
}

Under Mac OS X the output is as follows :-

Opened /dev/null - FD: 3
Serial data available : 0
Serial data available : 0
Calling srand()
Serial data available : 148561936
Serial data available : 0

On Linux I get the following :-

Opened /dev/null - FD: 3
Serial data available : 32720
Serial data available : 32720
Calling srand()
Serial data available : 32720
Serial data available : 32720

Two questions -

  1. Shouldn't /dev/null should always have 0 bytes available for reading?
  2. Why does calling srand() on Mac OS X cause the bytes available for reading from /dev/null to change?
1

There are 1 answers

0
xlfe On

The problem was obvious (in hindsight!) - the result int is not initialised, so when ioctl has an error, the function returns a non-zero integer, even though data may not be available.

int serial_data_available(const int fd) {
    int result;
    ioctl(fd, FIONREAD, &result);
    return result;
};

the correct code should be

int serial_data_available(const int fd) {
    int result = 0;
    ioctl(fd, FIONREAD, &result);
    return result;
};