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