1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define FOR_inotifyd
34#include "toys.h"
35#include <sys/inotify.h>
36
37void inotifyd_main(void)
38{
39 struct pollfd fds;
40 char *prog_args[5], **ss = toys.optargs;
41 char *masklist ="acew0rmyndDM uox";
42
43 fds.events = POLLIN;
44
45 *prog_args = *toys.optargs;
46 prog_args[4] = 0;
47 if ((fds.fd = inotify_init()) == -1) perror_exit(0);
48
49
50 toys.optc--;
51
52 while (*++ss) {
53 char *path = *ss, *masks = strchr(*ss, ':');
54 int i, mask = 0;
55
56 if (!masks) mask = 0xfff;
57 else{
58 *masks++ = 0;
59 for (*masks++ = 0; *masks; masks++) {
60 i = stridx(masklist, *masks);;
61 if (i == -1) error_exit("bad mask '%c'", *masks);
62 mask |= 1<<i;
63 }
64 }
65
66
67
68 if (inotify_add_watch(fds.fd, path, mask) < 0) perror_exit_raw(path);
69 }
70
71 for (;;) {
72 int ret = 0, len;
73 void *buf = 0;
74 struct inotify_event *event;
75
76
77 ret = poll(&fds, 1, -1);
78 if (ret < 0 && errno == EINTR) continue;
79 if (ret <= 0) break;
80 xioctl(fds.fd, FIONREAD, &len);
81 event = buf = xmalloc(len);
82 len = readall(fds.fd, buf, len);
83
84
85 for (;;) {
86 int left = len - (((char *)event)-(char *)buf),
87 size = sizeof(struct inotify_event);
88
89
90 if (left >= size) size += event->len;
91 if (left < size) break;
92
93 if (event->mask) {
94 char *s = toybuf, *m;
95
96 for (m = masklist; *m; m++)
97 if (event->mask & (1<<(m-masklist))) *s++ = *m;
98 *s = 0;
99
100 if (**prog_args == '-' && !prog_args[0][1]) {
101 xprintf("%s\t%s\t%s\n" + 3*!event->len, toybuf,
102 toys.optargs[event->wd], event->name);
103 } else {
104 prog_args[1] = toybuf;
105 prog_args[2] = toys.optargs[event->wd];
106 prog_args[3] = event->len ? event->name : 0;
107 xrun(prog_args);
108 }
109
110 if (event->mask & IN_IGNORED) {
111 if (--toys.optc <= 0) {
112 free(buf);
113
114 goto done;
115 }
116 inotify_rm_watch(fds.fd, event->wd);
117 }
118 }
119 event = (void*)(size + (char*)event);
120 }
121 free(buf);
122 }
123
124done:
125 toys.exitval = !!toys.signal;
126}
127