Skip to content
Advertisement

C read() : Invalid arguments

I am trying to read from a nbd device with function read()

Here is the code:

static uint64_t hash_log_free_list;
int fd = open(argv[2], O_RDWR|O_LARGEFILE);
assert(fd != -1);
int err = read(fd, &hash_log_free_list, sizeof(uint64_t));
if (err != sizeof(uint64_t))
{
    MSGDEBUG

    perror("read");
    printf("err: %dn", err);
    abort();
}

argv[2] is a nbd device: /dev/nbd0

MSGDEBUG is defined to print __FILE__, __LINE__ and __FUNCTION__

and I got the error msg:

Error!!! dedup.c: 554: main read: Invalid argument err: 0 Aborted

Am I doing something wrong?

Advertisement

Answer

read returns 0 which is not an error, it just tells you that there’s nothing to read.

You can use select/epoll before calling read to make sure there’s actual data to read.

Also, remove the MSGDEBUG line because it’s printing stuff and most probably changing errno at the same time, so your perror probably gives you information about what happend in MSGDEBUG, not in the read before it.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement