summaryrefslogtreecommitdiff
path: root/package/busybox/busybox-1.13.0-inotify.patch
blob: 006dea1a6358f5f5a72154bc2d64c6703431f08c (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
--- busybox-1.13.0/miscutils/inotifyd.c	Thu Oct 30 08:41:37 2008
+++ busybox-1.13.0-inotify/miscutils/inotifyd.c	Fri Nov 14 22:33:27 2008
@@ -51,6 +51,7 @@
 int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int inotifyd_main(int argc UNUSED_PARAM, char **argv)
 {
+	int n;
 	unsigned mask = IN_ALL_EVENTS; // assume we want all events
 	struct pollfd pfd;
 	char **watched = ++argv; // watched name list
@@ -69,7 +70,6 @@
 	while (*++argv) {
 		char *path = *argv;
 		char *masks = strchr(path, ':');
-		int wd; // watch descriptor
 		// if mask is specified ->
 		if (masks) {
 			*masks = '\0'; // split path and mask
@@ -83,32 +83,39 @@
 			}
 		}
 		// add watch
-		wd = inotify_add_watch(pfd.fd, path, mask);
-		if (wd < 0) {
+		n = inotify_add_watch(pfd.fd, path, mask);
+		if (n < 0)
 			bb_perror_msg_and_die("add watch (%s) failed", path);
-//		} else {
-//			bb_error_msg("added %d [%s]:%4X", wd, path, mask);
-		}
+		//bb_error_msg("added %d [%s]:%4X", n, path, mask);
 	}
 
 	// setup signals
-	bb_signals(0
-		+ (1 << SIGHUP)
-		+ (1 << SIGINT)
-		+ (1 << SIGTERM)
-		+ (1 << SIGPIPE)
-		, record_signo);
+	bb_signals(BB_FATAL_SIGS, record_signo);
 
 	// do watch
-
-//	pfd.fd = fd;
 	pfd.events = POLLIN;
-
-	while (!bb_got_signal && poll(&pfd, 1, -1) > 0) {
+	while (1) {
 		ssize_t len;
 		void *buf;
 		struct inotify_event *ie;
 
+ again:
+		if (bb_got_signal)
+			break;
+		n = poll(&pfd, 1, -1);
+		/* Signal interrupted us? */
+		if (n < 0 && errno == EINTR)
+			goto again;
+		// Under Linux, above if() is not necessary.
+		// Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL,
+		// are not interrupting poll().
+		// Thus we can just break if n <= 0 (see below),
+		// because EINTR will happen only on SIGTERM et al.
+		// But this might be not true under other Unixes,
+		// and is generally way too subtle to depend on.
+		if (n <= 0) // strange error?
+			break;
+
 		// read out all pending events
 		xioctl(pfd.fd, FIONREAD, &len);
 #define eventbuf bb_common_bufsiz1
@@ -117,21 +124,21 @@
 		// process events. N.B. events may vary in length
 		while (len > 0) {
 			int i;
-			char events[12];
+			char events[sizeof(mask_names)];
 			char *s = events;
 			unsigned m = ie->mask;
 
-			for (i = 0; i < 12; ++i, m >>= 1) {
-				if (m & 1) {
+			for (i = 0; i < sizeof(mask_names)-1; ++i, m >>= 1) {
+				if (m & 1)
 					*s++ = mask_names[i];
-				}
 			}
 			*s = '\0';
-//			bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent, ie->mask, events, watched[ie->wd], ie->len ? ie->name : "");
+			//bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent,
+			// ie->mask, events, watched[ie->wd], ie->len ? ie->name : "");
 			args[1] = events;
 			args[2] = watched[ie->wd];
 			args[3] = ie->len ? ie->name : NULL;
-			xspawn((char **)args);
+			wait4pid(xspawn((char **)args));
 			// next event
 			i = sizeof(struct inotify_event) + ie->len;
 			len -= i;