1

I try to read some data from one fd, but failed with error message"Invalid argument!".

struct inotify_event eventHdr;
int head_read_len = (int)read(ctx->fd, (void *)&eventHdr, sizeof(inotify_event));
    if(head_read_len == -1){
       _debug("read eventHdr failed!!!!\n");
       perror("read eventHdr!"); //Print "Invalid argument."
   }
   else{
       _debug("read eventHdr succeed!!!!, head_read_len:%d, name:%s\n", head_read_len, eventHdr.name);
       lseek(ctx->fd, SEEK_CUR, -head_read_len);

}

Notes:

  1. The struct inotify_event is used for inotify system call, man inotify for more details.

  2. fd is guaranteed to be a valid inotify file descriptor.

What seems to be the problem? Any valuable insights?

Jabberwocky
  • 40,411
  • 16
  • 50
  • 92
FaceBro
  • 709
  • 1
  • 10
  • 26
  • 3
    Make sure to not spoil the value of `errno` before printing/interpreting it. It is not clear whether the code of `_debug("read eventHdr failed!!!!\n");` wouldn't exactly do this. Place the call to `perror()` before it. – alk Apr 14 '14 at 11:59
  • Thanks. This is a problem but not the one causing this. – FaceBro Apr 16 '14 at 11:57

1 Answers1

4

The reason is that: The fd of inotify system call can not be partially read.Otherwise, it will return "Invalid argument"!

Use a buffer with large enough bytes!

FaceBro
  • 709
  • 1
  • 10
  • 26