added preload_usb hack
[tridge/junkcode.git] / inotify.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <linux/inotify.h>
5 #include <errno.h>
6 #include <asm/unistd.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <signal.h>
11
12 #if 0
13 _syscall0(int, inotify_init);
14 _syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask);
15 _syscall2(int, inotify_rm_watch, int, fd, int, wd);
16
17 #else
18
19 static int inotify_init(void)
20 {
21         return syscall(__NR_inotify_init);
22 }
23
24 static int inotify_add_watch(int fd, const char *path, __u32 mask)
25 {
26         static int xx;
27         return syscall(__NR_inotify_add_watch, fd, path, mask+(++xx));
28 }
29
30 static int inotify_rm_watch(int fd, int wd)
31 {
32         return syscall(__NR_inotify_rm_watch, fd, wd);
33 }
34 #endif
35
36 static void usage(void)
37 {
38         printf("usage: inotify <DIRECTORY>\n");
39 }
40
41 int main(int argc, const char *argv[])
42 {
43         const char *path;
44         int fd, wd1, wd2;
45         pid_t child;
46
47         if (argc < 2) {
48                 usage();
49                 exit(1);
50         }
51
52         path = argv[1];
53
54         if ((child = fork()) == 0) {
55                 char *testname;
56                 asprintf(&testname, "%s/inotify_test.dat", path);
57                 while (1) {
58                         close(open(testname, O_CREAT|O_RDWR, 0666));
59                         unlink(testname);
60                         sleep(1);
61                 }
62         }
63
64         fd = inotify_init();
65         wd1 = inotify_add_watch(fd, path, IN_CREATE|IN_DELETE);
66         wd2 = inotify_add_watch(fd, path, IN_CREATE|IN_DELETE);
67
68         while (1) {
69                 char buf[1024];
70                 int ret = read(fd, buf, sizeof(buf));
71                 char *p = buf;
72                 struct inotify_event *ev;
73                 while (ret > sizeof(ev)) {
74                         ev = (struct inotify_event *)p;
75                         printf("%d 0x%08x %d '%s'\n", 
76                                ev->wd, ev->mask, ev->len, ev->name);
77                         p += sizeof(*ev) + ev->len;
78                         ret -= sizeof(*ev) + ev->len;
79                 }
80         }
81
82         inotify_rm_watch(fd, wd1);
83         inotify_rm_watch(fd, wd2);
84         close(fd);
85
86         kill(child, SIGKILL);
87
88         return 0;
89 }