Mercurial > hg > toybox
annotate toys/other/inotifyd.c @ 1776:7bf68329eb3b draft default tip
Repository switched to git at https://github.com/landley/toybox
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 09 Apr 2015 02:28:32 -0500 |
parents | 2f9bc9495144 |
children |
rev | line source |
---|---|
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
1 /* inotifyd.c - inotify daemon. |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
2 * |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
3 * Copyright 2013 Ashwini Kumar <ak.ashwini1981@gmail.com> |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com> |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
5 * |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
6 * No Standard. |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
7 |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
8 USE_INOTIFYD(NEWTOY(inotifyd, "<2", TOYFLAG_USR|TOYFLAG_BIN)) |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
9 |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
10 config INOTIFYD |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
11 bool "inotifyd" |
1442 | 12 default y |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
13 help |
1440 | 14 usage: inotifyd PROG FILE[:MASK] ... |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
15 |
1440 | 16 When a filesystem event matching MASK occurs to a FILE, run PROG as: |
17 | |
18 PROG EVENTS FILE [DIRFILE] | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
19 |
1441
2efbfb435c77
Minor help text tweak for inotifyd.
Rob Landley <rob@landley.net>
parents:
1440
diff
changeset
|
20 If PROG is "-" events are sent to stdout. |
2efbfb435c77
Minor help text tweak for inotifyd.
Rob Landley <rob@landley.net>
parents:
1440
diff
changeset
|
21 |
1440 | 22 This file is: |
23 a accessed c modified e metadata change w closed (writable) | |
24 r opened D deleted M moved 0 closed (unwritable) | |
25 u unmounted o overflow x unwatchable | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
26 |
1440 | 27 A file in this directory is: |
28 m moved in y moved out n created d deleted | |
29 | |
30 When x event happens for all FILEs, inotifyd exits (after waiting for PROG). | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
31 */ |
1440 | 32 |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
33 #define FOR_inotifyd |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
34 #include "toys.h" |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
35 #include <sys/inotify.h> |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
36 |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
37 void inotifyd_main(void) |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
38 { |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
39 struct pollfd fds; |
1440 | 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 // Track number of watched files. First one was program to run. | |
50 toys.optc--; | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
51 |
1440 | 52 while (*++ss) { |
53 char *path = *ss, *masks = strchr(*ss, ':'); | |
54 int i, mask = 0; | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
55 |
1440 | 56 if (!masks) mask = 0xfff; // default to all |
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; | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
63 } |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
64 } |
1440 | 65 |
66 // This returns increasing numbers starting from 1, which coincidentally | |
67 // is the toys.optargs position of the file. (0 is program to run.) | |
68 if (inotify_add_watch(fds.fd, path, mask) < 0) perror_exit("%s", path); | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
69 } |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
70 |
1440 | 71 for (;;) { |
72 int ret = 0, len; | |
73 void *buf = 0; | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
74 struct inotify_event *event; |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
75 |
1440 | 76 // Read next event(s) |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
77 ret = poll(&fds, 1, -1); |
1440 | 78 if (ret < 0 && errno == EINTR) continue; |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
79 if (ret <= 0) break; |
1440 | 80 xioctl(fds.fd, FIONREAD, &len); |
81 event = buf = xmalloc(len); | |
82 len = readall(fds.fd, buf, len); | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
83 |
1440 | 84 // Loop through set of events. |
85 for (;;) { | |
86 int left = len - (((char *)event)-(char *)buf), | |
87 size = sizeof(struct inotify_event); | |
88 | |
89 // Don't dereference event if ->len is off end of bufer | |
90 if (left >= size) size += event->len; | |
91 if (left < size) break; | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
92 |
1440 | 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; | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
99 |
1440 | 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); | |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
103 } else { |
1440 | 104 prog_args[1] = toybuf; |
105 prog_args[2] = toys.optargs[event->wd]; | |
106 prog_args[3] = event->len ? event->name : 0; | |
1472
2f9bc9495144
Split xpopen() into xpopen_both(), xpopen(), and xrun() depending on whether we want to redirect both, one, or neither of stdin/stdout.
Rob Landley <rob@landley.net>
parents:
1442
diff
changeset
|
107 xrun(prog_args); |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
108 } |
1440 | 109 |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
110 if (event->mask & IN_IGNORED) { |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
111 if (--toys.optc <= 0) { |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
112 free(buf); |
1440 | 113 |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
114 goto done; |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
115 } |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
116 inotify_rm_watch(fds.fd, event->wd); |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
117 } |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
118 } |
1440 | 119 event = (void*)(size + (char*)event); |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
120 } |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
121 free(buf); |
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
122 } |
1440 | 123 |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
124 done: |
1440 | 125 toys.exitval = !!toys.signal; |
1306
2d4675617dff
inotifyd - watch for filesystem events.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
126 } |