1 |
Hi. |
2 |
|
3 |
I am trying to use inotify but something is not correct. |
4 |
I am using gentoo 2006.0. |
5 |
kernel: 2.6.17-gentoo-r4 |
6 |
inotify is compiled in. |
7 |
glibc 2.3.6-r4 |
8 |
|
9 |
What happens: |
10 |
I initialize inotify, it returns the file descriptor (fd). OK |
11 |
I add a directory to watch with all flags. It returns the watch descriptor (wd). OK |
12 |
I read (blocking) the fd. It blocks. OK |
13 |
-> I touch a file inside the directory I am watching |
14 |
the read unblocks, but it really returns 0 bytes read and do not block anymore. |
15 |
|
16 |
Thanks in advance for any clue. |
17 |
|
18 |
Marco |
19 |
|
20 |
The code is as follows: |
21 |
|
22 |
#include <linux/inotify.h> |
23 |
#include <stdio.h> |
24 |
#include <asm/unistd.h> |
25 |
|
26 |
int main( int argc, char **argv ) |
27 |
{ |
28 |
int fd = syscall(__NR_inotify_init); |
29 |
int wd = syscall(__NR_inotify_add_watch, fd, "/invalid", IN_ALL_EVENTS ); |
30 |
struct inotify_event ev; |
31 |
char *filename; |
32 |
uint32_t maxsize = 1024; |
33 |
int br=0; |
34 |
filename = (char *) malloc( maxsize * sizeof(char) ); |
35 |
if ( ! filename ) |
36 |
{ |
37 |
puts("Could not allocate 1024 chars. Kill me!"); |
38 |
exit(1); |
39 |
} |
40 |
while ( 1 ) |
41 |
{ |
42 |
do { |
43 |
br = read( fd, (void *) &ev, sizeof(struct inotify_event) ); |
44 |
printf( "Bytes read: %i, sizeof: %i, wd: %i, mask: %X\n", br, sizeof(struct inotify_event), ev.wd, ev.mask ); |
45 |
} while (!br); // I know it is not correct, just to catch the case where br is 0 |
46 |
if ( ev.len > 0 ) |
47 |
{ |
48 |
if ( ev.len > maxsize ) |
49 |
{ |
50 |
maxsize = ev.len; |
51 |
filename = (char*) realloc( (void*)filename, maxsize * sizeof(char) ); |
52 |
if ( ! filename ) |
53 |
{ |
54 |
printf("Could not reallocate %u chars. Kill me!\n", ev.len); |
55 |
exit(1); |
56 |
} |
57 |
} |
58 |
read( fd, filename, ev.len ); |
59 |
printf( "%s\n", filename ); |
60 |
} |
61 |
} |
62 |
free( filename ); |
63 |
return (0); |
64 |
} |
65 |
|
66 |
|
67 |
-- |
68 |
gentoo-user@g.o mailing list |