public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user]  inotify problem
@ 2006-08-18 13:28 Marco Costa
  2006-08-22  9:51 ` [gentoo-user] Re: inotify problem - SOLVED Marco Costa
  0 siblings, 1 reply; 2+ messages in thread
From: Marco Costa @ 2006-08-18 13:28 UTC (permalink / raw
  To: gentoo-user

Hi.

I am trying to use inotify but something is not correct.
I am using gentoo 2006.0.
kernel:  2.6.17-gentoo-r4
  inotify is compiled in.
glibc 2.3.6-r4

What happens:
I initialize inotify, it returns the file descriptor (fd). OK
I add a directory to watch with all flags. It returns the watch descriptor (wd). OK
I read (blocking) the fd. It blocks. OK
-> I touch a file inside the directory I am watching
the read unblocks, but it really returns 0 bytes read and do not block anymore.

Thanks in advance for any clue.

Marco

The code is as follows:

#include <linux/inotify.h>
#include <stdio.h>
#include <asm/unistd.h>

int main( int argc, char **argv )
{
  int fd = syscall(__NR_inotify_init); 
  int wd = syscall(__NR_inotify_add_watch, fd, "/invalid", IN_ALL_EVENTS );
  struct inotify_event ev;
  char *filename;
  uint32_t  maxsize = 1024;
  int br=0;
  filename = (char *) malloc( maxsize * sizeof(char) );
  if ( ! filename )
  {
    puts("Could not allocate 1024 chars. Kill me!");
    exit(1);
  }
  while ( 1 ) 
  {
    do {
      br = read( fd, (void *) &ev, sizeof(struct inotify_event) );
      printf( "Bytes read: %i, sizeof: %i, wd: %i, mask: %X\n", br, sizeof(struct inotify_event), ev.wd, ev.mask ); 
    } while (!br); // I know it is not correct, just to catch the case where br is 0
    if ( ev.len > 0 )
    {
      if ( ev.len > maxsize )
      {
        maxsize = ev.len;
        filename = (char*) realloc( (void*)filename, maxsize * sizeof(char) );
        if ( ! filename )
        {
          printf("Could not reallocate %u chars. Kill me!\n", ev.len);
          exit(1);
        }
     }
     read( fd, filename, ev.len );
     printf( "%s\n", filename );
    }
  }
  free( filename );
  return (0);
}


-- 
gentoo-user@gentoo.org mailing list



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [gentoo-user]  Re: inotify problem - SOLVED
  2006-08-18 13:28 [gentoo-user] inotify problem Marco Costa
@ 2006-08-22  9:51 ` Marco Costa
  0 siblings, 0 replies; 2+ messages in thread
From: Marco Costa @ 2006-08-22  9:51 UTC (permalink / raw
  To: gentoo-user

[-- Attachment #1: Type: text/plain, Size: 739 bytes --]

Marco Costa wrote:
> Hi.
> 
> I am trying to use inotify but something is not correct.
> I am using gentoo 2006.0.
> kernel:  2.6.17-gentoo-r4
>  inotify is compiled in.
> glibc 2.3.6-r4
> 
> What happens:
> I initialize inotify, it returns the file descriptor (fd). OK
> I add a directory to watch with all flags. It returns the watch
> descriptor (wd). OK
> I read (blocking) the fd. It blocks. OK
> -> I touch a file inside the directory I am watching
> the read unblocks, but it really returns 0 bytes read and do not block
> anymore.
> 

Problem solved.

One must read all the available data at once. Reading just chunks will not work.
The use of the ioctl FIONREAD is recommended.

Regards,

Marco Costa


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-08-22 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-18 13:28 [gentoo-user] inotify problem Marco Costa
2006-08-22  9:51 ` [gentoo-user] Re: inotify problem - SOLVED Marco Costa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox